2014-04-13 11:54:25 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
2014-04-13 17:42:57 +02:00
|
|
|
* @file notification.c
|
2014-04-13 11:54:25 +02:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
|
2014-04-13 17:42:57 +02:00
|
|
|
* @brief notification library.
|
2014-04-13 11:54:25 +02:00
|
|
|
* --
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2014-04-13 17:42:57 +02:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2014-04-13 11:54:25 +02:00
|
|
|
* (at your option) any later version.
|
2014-04-13 17:42:57 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2014-04-13 11:54:25 +02:00
|
|
|
* for more details.
|
2014-04-13 17:42:57 +02:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2014-04-13 11:54:25 +02:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
#include "inc/notification.h"
|
|
|
|
#include <openpilot.h>
|
|
|
|
#include <pios_struct_helper.h>
|
|
|
|
#include <systemalarms.h>
|
|
|
|
#include <flightstatus.h>
|
2014-04-13 17:42:57 +02:00
|
|
|
#include <pios_notify.h>
|
2014-04-13 11:54:25 +02:00
|
|
|
|
|
|
|
#ifdef PIOS_LED_ALARM
|
|
|
|
#define ALARM_LED_ON() PIOS_LED_On(PIOS_LED_ALARM)
|
|
|
|
#define ALARM_LED_OFF() PIOS_LED_Off(PIOS_LED_ALARM)
|
|
|
|
#else
|
|
|
|
#define ALARM_LED_ON()
|
|
|
|
#define ALARM_LED_OFF()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PIOS_LED_HEARTBEAT
|
|
|
|
#define HEARTBEAT_LED_ON() PIOS_LED_On(PIOS_LED_HEARTBEAT)
|
|
|
|
#define HEARTBEAT_LED_OFF() PIOS_LED_Off(PIOS_LED_HEARTBEAT)
|
|
|
|
#else
|
|
|
|
#define HEARTBEAT_LED_ON()
|
|
|
|
#define HEARTBEAT_LED_OFF()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ALARM_BLINK_COUNT(x) \
|
|
|
|
(x == SYSTEMALARMS_ALARM_OK ? 0 : \
|
|
|
|
x == SYSTEMALARMS_ALARM_WARNING ? 1 : \
|
|
|
|
x == SYSTEMALARMS_ALARM_ERROR ? 2 : \
|
|
|
|
x == SYSTEMALARMS_ALARM_CRITICAL ? 0 : 0)
|
|
|
|
|
|
|
|
|
|
|
|
#define BLINK_COUNT(x) \
|
2014-04-13 17:42:57 +02:00
|
|
|
(x == FLIGHTSTATUS_FLIGHTMODE_STABILIZED1 ? 1 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_STABILIZED2 ? 2 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_STABILIZED3 ? 3 : \
|
2014-04-13 11:54:25 +02:00
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD ? 2 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEVARIO ? 2 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_VELOCITYCONTROL ? 2 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD ? 3 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_RETURNTOBASE ? 4 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_LAND ? 4 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER ? 3 : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_POI ? 3 : 1)
|
|
|
|
|
|
|
|
#define BLINK_RED(x) \
|
|
|
|
(x == FLIGHTSTATUS_FLIGHTMODE_STABILIZED1 ? false : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_STABILIZED2 ? false : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_STABILIZED3 ? false : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEHOLD ? true : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_ALTITUDEVARIO ? true : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_VELOCITYCONTROL ? true : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD ? true : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_RETURNTOBASE ? true : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_LAND ? true : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER ? true : \
|
|
|
|
x == FLIGHTSTATUS_FLIGHTMODE_POI ? true : false)
|
|
|
|
|
|
|
|
// led notification handling
|
|
|
|
static volatile SystemAlarmsAlarmOptions currentAlarmLevel = SYSTEMALARMS_ALARM_OK;
|
|
|
|
static volatile FlightStatusData currentFlightStatus;
|
|
|
|
static volatile bool started = false;
|
2014-04-13 17:42:57 +02:00
|
|
|
static volatile pios_notify_notification nextNotification = NOTIFY_NONE;
|
2014-04-13 11:54:25 +02:00
|
|
|
|
2014-04-13 17:42:57 +02:00
|
|
|
#ifdef PIOS_LED_ALARM
|
|
|
|
static bool handleAlarms(uint8_t cycleCount, uint8_t blinkCount);
|
|
|
|
#endif // PIOS_LED_ALARM
|
|
|
|
static bool handleNotifications(uint8_t cycleCount, pios_notify_notification runningNotification);
|
|
|
|
static void handleStatus(uint8_t cycleCount, uint8_t blinkCount);
|
|
|
|
|
|
|
|
void NotificationUpdateStatus()
|
|
|
|
{
|
2014-04-13 11:54:25 +02:00
|
|
|
started = true;
|
|
|
|
// get values to be used for led handling
|
|
|
|
FlightStatusGet((FlightStatusData *)¤tFlightStatus);
|
|
|
|
currentAlarmLevel = AlarmsGetHighestSeverity();
|
2014-04-13 17:42:57 +02:00
|
|
|
if (nextNotification == NOTIFY_NONE) {
|
|
|
|
nextNotification = PIOS_NOTIFY_GetActiveNotification(true);
|
|
|
|
}
|
2014-04-13 11:54:25 +02:00
|
|
|
}
|
|
|
|
|
2014-04-13 17:42:57 +02:00
|
|
|
void NotificationOnboardLedsRun()
|
|
|
|
{
|
2014-04-13 11:54:25 +02:00
|
|
|
static portTickType lastRunTimestamp;
|
2014-04-13 17:42:57 +02:00
|
|
|
static uint8_t blinkCount; // number of blinks since phase start
|
|
|
|
static uint8_t cycleCount; // cound the number of cycles (half of a blink)
|
|
|
|
static pios_notify_notification runningNotification = NOTIFY_NONE;
|
|
|
|
|
|
|
|
static enum {
|
|
|
|
STATUS_NOTIFY,
|
|
|
|
STATUS_ALARM,
|
|
|
|
STATUS_FLIGHTMODE,
|
|
|
|
STATUS_LENGHT
|
|
|
|
} status;
|
|
|
|
|
2014-04-13 11:54:25 +02:00
|
|
|
if (!started || (xTaskGetTickCount() - lastRunTimestamp) < (LED_BLINK_PERIOD_MS * portTICK_RATE_MS / 2)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-13 17:42:57 +02:00
|
|
|
|
2014-04-13 11:54:25 +02:00
|
|
|
lastRunTimestamp = xTaskGetTickCount();
|
|
|
|
// the led will show various status information, subdivided in three phases
|
|
|
|
// - Notification
|
|
|
|
// - Alarm
|
|
|
|
// - Flight status
|
|
|
|
// they are shown using the above priority
|
|
|
|
// a phase last exactly 8 cycles (so bit 1<<4 is used to determine if a phase end
|
|
|
|
|
|
|
|
cycleCount++;
|
2014-04-13 17:42:57 +02:00
|
|
|
// Notifications are "modal" to other statuses so they takes precedence
|
|
|
|
if (status != STATUS_NOTIFY && nextNotification != NOTIFY_NONE) {
|
2014-04-13 17:56:20 +02:00
|
|
|
// read next notification to show
|
2014-04-13 17:42:57 +02:00
|
|
|
runningNotification = nextNotification;
|
|
|
|
nextNotification = NOTIFY_NONE;
|
2014-04-13 17:56:20 +02:00
|
|
|
// Force a notification status
|
2014-04-13 17:42:57 +02:00
|
|
|
status = STATUS_NOTIFY;
|
|
|
|
cycleCount = 0; // instantly start a notify cycle
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if a phase has just finished
|
2014-04-13 11:54:25 +02:00
|
|
|
if (cycleCount & 0x08) {
|
2014-04-13 17:56:20 +02:00
|
|
|
// add a pause between each phase
|
2014-04-13 17:42:57 +02:00
|
|
|
if (cycleCount > 0x8 + LED_PAUSE_BETWEEN_PHASES) {
|
|
|
|
// ready to start a new phase
|
|
|
|
cycleCount = 0x0;
|
|
|
|
|
2014-04-13 17:56:20 +02:00
|
|
|
// Notification has been just shown, cleanup
|
2014-04-13 17:42:57 +02:00
|
|
|
if (status == STATUS_NOTIFY) {
|
|
|
|
runningNotification = NOTIFY_NONE;
|
|
|
|
}
|
|
|
|
status = (status + 1) % STATUS_LENGHT;
|
|
|
|
} else {
|
|
|
|
HEARTBEAT_LED_OFF();
|
|
|
|
ALARM_LED_OFF();
|
|
|
|
return;
|
2014-04-13 11:54:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-13 17:56:20 +02:00
|
|
|
// a blink last 2 cycles(on and off cycle).
|
2014-04-13 17:42:57 +02:00
|
|
|
blinkCount = (cycleCount & 0xF) >> 1;
|
|
|
|
|
2014-04-13 11:54:25 +02:00
|
|
|
if (status == STATUS_NOTIFY) {
|
2014-04-13 17:42:57 +02:00
|
|
|
if (!handleNotifications(cycleCount, runningNotification)) {
|
2014-04-13 17:56:20 +02:00
|
|
|
// no notifications, advance
|
2014-04-13 17:42:57 +02:00
|
|
|
status++;
|
|
|
|
}
|
2014-04-13 11:54:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handles Alarm display
|
|
|
|
if (status == STATUS_ALARM) {
|
2014-04-13 17:42:57 +02:00
|
|
|
#ifdef PIOS_LED_ALARM
|
|
|
|
if (!handleAlarms(cycleCount, blinkCount)) {
|
2014-04-13 17:56:20 +02:00
|
|
|
// no alarms, advance
|
2014-04-13 11:54:25 +02:00
|
|
|
status++;
|
|
|
|
}
|
2014-04-13 17:42:57 +02:00
|
|
|
#else
|
2014-04-13 17:56:20 +02:00
|
|
|
// no alarms leds, advance
|
2014-04-13 11:54:25 +02:00
|
|
|
status++;
|
2014-04-13 17:42:57 +02:00
|
|
|
#endif // PIOS_LED_ALARM
|
2014-04-13 11:54:25 +02:00
|
|
|
}
|
2014-04-13 17:56:20 +02:00
|
|
|
|
2014-04-13 11:54:25 +02:00
|
|
|
// **** Handles flightmode display
|
|
|
|
if (status == STATUS_FLIGHTMODE) {
|
2014-04-13 17:42:57 +02:00
|
|
|
handleStatus(cycleCount, blinkCount);
|
|
|
|
}
|
|
|
|
}
|
2014-04-13 11:54:25 +02:00
|
|
|
|
2014-04-13 17:42:57 +02:00
|
|
|
#if defined(PIOS_LED_ALARM)
|
|
|
|
static bool handleAlarms(uint8_t cycleCount, uint8_t blinkCount)
|
|
|
|
{
|
2014-04-13 17:56:20 +02:00
|
|
|
if (currentAlarmLevel == SYSTEMALARMS_ALARM_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentAlarmLevel == SYSTEMALARMS_ALARM_CRITICAL) {
|
|
|
|
// Slow blink
|
|
|
|
ALARM_LED_OFF();
|
|
|
|
if (cycleCount & 0x4) {
|
2014-04-13 17:42:57 +02:00
|
|
|
ALARM_LED_OFF();
|
2014-04-13 11:54:25 +02:00
|
|
|
} else {
|
2014-04-13 17:56:20 +02:00
|
|
|
ALARM_LED_ON();
|
2014-04-13 11:54:25 +02:00
|
|
|
}
|
2014-04-13 17:42:57 +02:00
|
|
|
} else {
|
2014-04-13 17:56:20 +02:00
|
|
|
if ((blinkCount < (ALARM_BLINK_COUNT(currentAlarmLevel))) &&
|
|
|
|
(cycleCount & 0x1)) {
|
|
|
|
ALARM_LED_ON();
|
|
|
|
} else {
|
|
|
|
ALARM_LED_OFF();
|
|
|
|
}
|
2014-04-13 17:42:57 +02:00
|
|
|
}
|
2014-04-13 17:56:20 +02:00
|
|
|
return true;
|
2014-04-13 17:42:57 +02:00
|
|
|
}
|
|
|
|
#endif /* PIOS_LED_ALARM */
|
|
|
|
|
|
|
|
|
|
|
|
static bool handleNotifications(uint8_t cycleCount, pios_notify_notification runningNotification)
|
|
|
|
{
|
|
|
|
if (runningNotification == NOTIFY_NONE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (cycleCount & 0x1) {
|
|
|
|
ALARM_LED_OFF();
|
|
|
|
HEARTBEAT_LED_ON();
|
|
|
|
} else {
|
|
|
|
ALARM_LED_ON();
|
|
|
|
HEARTBEAT_LED_OFF();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleStatus(uint8_t cycleCount, uint8_t blinkCount)
|
|
|
|
{
|
|
|
|
// Flash the heartbeat LED
|
|
|
|
#if defined(PIOS_LED_HEARTBEAT)
|
|
|
|
uint8_t flightmode = currentFlightStatus.FlightMode;
|
|
|
|
if (currentFlightStatus.Armed == FLIGHTSTATUS_ARMED_DISARMED) {
|
|
|
|
// Slow blink
|
|
|
|
if (blinkCount < 3) {
|
|
|
|
HEARTBEAT_LED_ON();
|
|
|
|
} else {
|
|
|
|
HEARTBEAT_LED_OFF();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((blinkCount < BLINK_COUNT(flightmode)) &&
|
|
|
|
(cycleCount & 0x1)) {
|
2014-04-13 17:56:20 +02:00
|
|
|
// red led will be active active on last (1-3 blinks) or last two (4 blinks case) blinks
|
2014-04-13 17:42:57 +02:00
|
|
|
if (BLINK_RED(flightmode) &&
|
|
|
|
((blinkCount == BLINK_COUNT(flightmode) - 1) ||
|
|
|
|
blinkCount > 1)) {
|
|
|
|
ALARM_LED_ON();
|
|
|
|
}
|
|
|
|
HEARTBEAT_LED_ON();
|
|
|
|
} else {
|
|
|
|
HEARTBEAT_LED_OFF();
|
|
|
|
ALARM_LED_OFF();
|
|
|
|
}
|
2014-04-13 11:54:25 +02:00
|
|
|
}
|
|
|
|
#endif /* PIOS_LED_HEARTBEAT */
|
|
|
|
}
|