46 #include "annunciatorsettings.h"
47 #include "flightstatus.h"
48 #include "manualcontrolcommand.h"
49 #include "manualcontrolsettings.h"
50 #include "objectpersistence.h"
51 #include "stabilizationsettings.h"
52 #include "stateestimation.h"
53 #include "systemsettings.h"
54 #include "systemstats.h"
55 #include "watchdogstatus.h"
57 #ifdef SYSTEMMOD_RGBLED_SUPPORT
58 #include "rgbledsettings.h"
62 #ifdef PIOS_INCLUDE_DAC_ANNUNCIATOR
68 #if defined(PIOS_INCLUDE_DEBUG_CONSOLE) && defined(DEBUG_THIS_FILE)
69 #define DEBUG_MSG(format, ...) PIOS_COM_SendFormattedString(PIOS_COM_DEBUG, format, ## __VA_ARGS__)
71 #define DEBUG_MSG(format, ...)
74 #ifndef IDLE_COUNTS_PER_SEC_AT_NO_LOAD
75 #define IDLE_COUNTS_PER_SEC_AT_NO_LOAD 995998 // calibrated by running tests/test_cpuload.c
80 #if defined(PIOS_SYSTEM_STACK_SIZE)
81 #define STACK_SIZE_BYTES PIOS_SYSTEM_STACK_SIZE
83 #define STACK_SIZE_BYTES 1024
86 #define TASK_PRIORITY PIOS_THREAD_PRIO_NORMAL
92 #ifdef SYSTEMMOD_RGBLED_SUPPORT
93 #define SYSTEM_RAPID_UPDATES
95 #define SYSTEM_UPDATE_PERIOD_MS 29
96 #define SYSTEM_UPDATE_PERIOD_MS4TH (SYSTEM_UPDATE_PERIOD_MS * 4)
100 #define SYSTEM_UPDATE_PERIOD_MS 117
101 #define SYSTEM_UPDATE_PERIOD_MS4TH (SYSTEM_UPDATE_PERIOD_MS)
144 void *ctx,
void *obj_data,
int len);
146 void *ctx,
void *obj,
int len);
157 void *ctx,
void *obj,
int len);
164 #if defined(WDG_STATS_DIAGNOSTICS)
165 static inline void updateWDGstats();
195 if (SystemSettingsInitialize() == -1
196 || SystemStatsInitialize() == -1
197 || FlightStatusInitialize() == -1
198 || ObjectPersistenceInitialize() == -1
199 || AnnunciatorSettingsInitialize() == -1
201 || RGBLEDSettingsInitialize() == -1
206 #if defined(DIAG_TASKS)
207 if (TaskInfoInitialize() == -1)
210 #if defined(WDG_STATS_DIAGNOSTICS)
211 if (WatchdogStatusInitialize() == -1)
216 if (objectPersistenceQueue == NULL)
233 #if defined(PIOS_INCLUDE_IAP)
244 ObjectPersistenceConnectQueue(objectPersistenceQueue);
246 #if defined(PIOS_INCLUDE_WDG) && defined(PIOS_WDG_SYSTEM)
255 if (StabilizationSettingsHandle())
257 if (SystemSettingsHandle())
259 if (ManualControlSettingsHandle())
261 if (FlightStatusHandle())
267 #if defined(PIOS_INCLUDE_WDG) && defined(PIOS_WDG_SYSTEM)
282 #if defined(PIOS_INCLUDE_ANNUNC)
284 #define BLINK_STRING_RADIO "r "
289 static inline uint8_t indicate_error(
const char **sequence)
293 return SYSTEMALARMS_ALARM_WARNING;
296 SystemAlarmsData alarms;
297 SystemAlarmsGet(&alarms);
301 uint8_t worst_sev = 0;
303 bool generic =
false;
305 for (uint32_t
i = 0;
i < SYSTEMALARMS_ALARM_NUMELEM;
i++) {
307 if (alarms.Alarm[
i] < worst_sev) {
313 if ((!
generic) && (alarms.Alarm[
i] == worst_sev)) {
317 uint8_t thresh = SYSTEMALARMS_ALARM_WARNING;
319 if (
i == SYSTEMALARMS_ALARM_TELEMETRY) {
322 thresh = SYSTEMALARMS_ALARM_CRITICAL;
325 if (
i == SYSTEMALARMS_ALARM_EVENTSYSTEM) {
330 if (alarms.Alarm[
i] < thresh) {
336 worst_sev = alarms.Alarm[
i];
340 case SYSTEMALARMS_ALARM_BATTERY:
344 case SYSTEMALARMS_ALARM_SYSTEMCONFIGURATION:
348 case SYSTEMALARMS_ALARM_GPS:
352 case SYSTEMALARMS_ALARM_MANUALCONTROL:
354 *sequence = BLINK_STRING_RADIO;
370 DONT_BUILD_IF(ANNUNCIATORSETTINGS_ANNUNCIATEAFTERARMING_NUMELEM !=
371 ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_NUMELEM,
372 AnnuncSettingsMismatch1);
373 DONT_BUILD_IF(ANNUNCIATORSETTINGS_ANNUNCIATEAFTERARMING_MAXOPTVAL !=
374 ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_MAXOPTVAL,
375 AnnuncSettingsMismatch2);
377 #if defined(PIOS_INCLUDE_ANNUNC)
378 static inline bool should_annunc(AnnunciatorSettingsData *annunciatorSettings,
379 bool been_armed,
bool is_manual_control, uint8_t blink_prio,
382 ANNUNCIATORSETTINGS_ANNUNCIATEAFTERARMING_NUMELEM);
385 if (blink_prio >= annunciatorSettings->AnnunciateAfterArming[cfg_field]) {
387 }
else if (is_manual_control && annunciatorSettings->AnnunciateAfterArming[cfg_field] == ANNUNCIATORSETTINGS_ANNUNCIATEAFTERARMING_MANUALCONTROLONLY) {
392 if (blink_prio >= annunciatorSettings->AnnunciateAnytime[cfg_field]) {
394 }
else if (is_manual_control && annunciatorSettings->AnnunciateAnytime[cfg_field] == ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_MANUALCONTROLONLY) {
401 static inline void consider_annunc(AnnunciatorSettingsData *annunciatorSettings,
402 bool is_active,
bool been_armed,
bool is_manual_control,
403 uint8_t blink_prio, uint32_t annunc_id,
407 should_annunc(annunciatorSettings, been_armed,
408 is_manual_control, blink_prio, cfg_field)
423 void *ctx,
void *obj_data,
int len)
425 (void) ev; (void) ctx; (void) obj_data; (void) len;
427 static unsigned int counter=0;
438 #ifdef SYSTEM_RAPID_UPDATES
439 bool fourth = (counter & 3) == 0;
453 #if defined(WDG_STATS_DIAGNOSTICS)
457 #if defined(DIAG_TASKS)
463 #if defined(PIOS_INCLUDE_ANNUNC)
466 static const char *blink_string = NULL;
467 static uint32_t blink_state = 0;
468 static uint8_t blink_prio = 0;
469 static bool ever_armed =
false;
470 static uint8_t armed_status = FLIGHTSTATUS_ARMED_DISARMED;
471 static bool is_manual_control =
false;
474 #ifdef SYSTEMMOD_RGBLED_SUPPORT
475 static bool led_override =
false;
482 const char *candidate_blink;
483 uint8_t candidate_prio;
486 candidate_prio = ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_HAIRONFIRE;
490 candidate_prio = indicate_error(&candidate_blink);
493 if (candidate_prio > blink_prio) {
496 blink_string = candidate_blink;
497 blink_prio = candidate_prio;
499 if (blink_prio == ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_HAIRONFIRE) {
504 !strcmp(blink_string, BLINK_STRING_RADIO)) {
506 is_manual_control =
true;
508 is_manual_control =
false;
512 FlightStatusArmedGet(&armed_status);
514 if (armed_status == FLIGHTSTATUS_ARMED_ARMED) {
517 if ((counter & 15) == 0) {
530 if ((blink_prio == 0) && (blink_state == 0)) {
532 if (armed_status == FLIGHTSTATUS_ARMED_ARMED) {
538 blink_prio = SHAREDDEFS_ALARMLEVELS_OK;
541 morse =
morse_send(&blink_string, &blink_state);
543 AnnunciatorSettingsData annunciatorSettings;
544 AnnunciatorSettingsGet(&annunciatorSettings);
546 #ifdef PIOS_LED_HEARTBEAT
547 consider_annunc(&annunciatorSettings, morse > 0, ever_armed,
550 ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_LED_HEARTBEAT);
553 #ifdef PIOS_LED_ALARM
554 consider_annunc(&annunciatorSettings, morse > 0, ever_armed,
557 ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_LED_ALARM);
560 uint8_t buzzer_prio = blink_prio;
563 if (annunciatorSettings.ManualBuzzer !=
564 ANNUNCIATORSETTINGS_MANUALBUZZER_DISABLED) {
565 float acc[MANUALCONTROLCOMMAND_ACCESSORY_NUMELEM];
566 ManualControlCommandAccessoryGet(acc);
568 if (acc[annunciatorSettings.ManualBuzzer - 1] > 0.0f)
569 buzzer_prio = ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_HAIRONFIRE;
575 #ifdef PIOS_ANNUNCIATOR_BUZZER
576 consider_annunc(&annunciatorSettings, morse > 0, ever_armed,
579 ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_BUZZER);
582 #ifdef PIOS_INCLUDE_DAC_ANNUNCIATOR
583 if (pios_dac_annunciator_id) {
584 if (should_annunc(&annunciatorSettings, ever_armed,
585 is_manual_control, buzzer_prio,
586 ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_DAC)) {
596 #ifdef PIOS_INCLUDE_ACTUATOR_ANNUNCIATOR
597 if (should_annunc(&annunciatorSettings, ever_armed,
598 is_manual_control, buzzer_prio,
599 ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_ACTUATORS)) {
605 #ifdef SYSTEMMOD_RGBLED_SUPPORT
606 led_override = should_annunc(&annunciatorSettings,
607 ever_armed, is_manual_control, blink_prio,
608 ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_RGB_LEDS);
616 is_manual_control =
false;
620 #ifdef SYSTEMMOD_RGBLED_SUPPORT
622 FLIGHTSTATUS_ARMED_DISARMED != armed_status,
623 (FLIGHTSTATUS_ARMED_ARMING == armed_status) &&
624 ((counter & 3) < 2));
635 void *ctx,
void *obj_data,
int len)
637 (void) ctx; (void) obj_data; (void) len;
639 ObjectPersistenceData objper;
643 if (ev->
obj == ObjectPersistenceHandle()) {
645 ObjectPersistenceGet(&objper);
650 if (objper.Operation == OBJECTPERSISTENCE_OPERATION_ERROR ||
651 objper.Operation == OBJECTPERSISTENCE_OPERATION_COMPLETED) {
655 if (objper.Operation == OBJECTPERSISTENCE_OPERATION_LOAD) {
663 }
else if (objper.Operation == OBJECTPERSISTENCE_OPERATION_SAVE) {
672 }
else if (objper.Operation == OBJECTPERSISTENCE_OPERATION_DELETE) {
675 }
else if (objper.Operation == OBJECTPERSISTENCE_OPERATION_FULLERASE) {
677 #if defined(PIOS_INCLUDE_LOGFS_SETTINGS)
689 objper.Operation = OBJECTPERSISTENCE_OPERATION_COMPLETED;
690 ObjectPersistenceSet(&objper);
693 objper.Operation = OBJECTPERSISTENCE_OPERATION_ERROR;
694 ObjectPersistenceSet(&objper);
708 void *ctx,
void *obj,
int len)
710 (void) ev; (void) ctx; (void) obj; (void) len;
718 #if defined(WDG_STATS_DIAGNOSTICS)
719 static WatchdogStatusData watchdogStatus;
720 static void updateWDGstats()
727 WatchdogStatusSet(&watchdogStatus);
737 static uint32_t lastTickCount = 0;
738 SystemStatsData
stats;
741 SystemStatsGet(&stats);
756 if (now > lastTickCount) {
757 float dT = (now - lastTickCount) / 1000.0
f;
762 if (idleFraction > 1)
765 stats.CPULoad = 100 - roundf(100.0
f * idleFraction);
771 SystemStatsSet(&stats);
780 SystemStatsData
stats;
783 SystemStatsGet(&stats);
788 #
if !defined(ARCH_POSIX) && !defined(ARCH_WIN32) && defined(CHECK_IRQ_STACK)
792 AlarmsSet(SYSTEMALARMS_ALARM_OUTOFMEMORY, SYSTEMALARMS_ALARM_CRITICAL);
795 #
if !defined(ARCH_POSIX) && !defined(ARCH_WIN32) && defined(CHECK_IRQ_STACK)
799 AlarmsSet(SYSTEMALARMS_ALARM_OUTOFMEMORY, SYSTEMALARMS_ALARM_WARNING);
811 AlarmsSet(SYSTEMALARMS_ALARM_CPUOVERLOAD, SYSTEMALARMS_ALARM_CRITICAL);
813 AlarmsSet(SYSTEMALARMS_ALARM_CPUOVERLOAD, SYSTEMALARMS_ALARM_WARNING);
825 AlarmsSet(SYSTEMALARMS_ALARM_EVENTSYSTEM, SYSTEMALARMS_ALARM_WARNING);
831 SystemStatsData sysStats;
832 SystemStatsGet(&sysStats);
833 sysStats.EventSystemWarningID = evStats.
lastErrorID;
836 SystemStatsSet(&sysStats);
954 if (objEntry == NULL)
return -1;
1008 #define MAX_UPDATE_PERIOD_MS 350
1065 return timeToNextUpdate - now;
1069 MANUALCONTROLCOMMAND_ACCESSORY_NUMELEM, TooManyManualBuzzers);
#define LL_FOREACH(head, el)
#define SYSTEMMOD_RGBLED_SUPPORT
uint32_t PIOS_Thread_Systime(void)
static volatile uint32_t idleCounter
uint32_t lastCallbackErrorID
UAVObjHandle UAVObjGetByID(uint32_t id)
struct pios_queue * PIOS_Queue_Create(size_t queue_length, size_t item_size)
static int32_t eventPeriodicCreate(UAVObjEvent *ev, UAVObjEventCallback cb, struct pios_queue *queue, uint16_t periodMs)
int32_t EventPeriodicQueueCreate(UAVObjEvent *ev, struct pios_queue *queue, uint16_t periodMs)
size_t PIOS_fastheap_get_free_size(void)
#define HEAP_LIMIT_WARNING
bool PIOS_WDG_RegisterFlag(uint16_t flag_requested)
Register a module against the watchdog.
#define IRQSTACK_LIMIT_CRITICAL
void vApplicationIdleHook(void)
bool PIOS_WDG_UpdateFlag(uint16_t flag)
Function called by modules to indicate they are still running.
bool PIOS_Queue_Send(struct pios_queue *queuep, const void *itemp, uint32_t timeout_ms)
int32_t UAVObjSave(UAVObjHandle obj_handle, uint16_t instId)
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
uintptr_t pios_uavo_settings_fs_id
static volatile uint32_t idleCounterClear
static void systemPeriodicCb(const UAVObjEvent *ev, void *ctx, void *obj_data, int len)
bool PIOS_Queue_Receive(struct pios_queue *queuep, void *itemp, uint32_t timeout_ms)
void TaskMonitorUpdateAll(void)
#define HEAP_LIMIT_CRITICAL
uint32_t eventQueueErrors
void PIOS_ANNUNC_Off(uint32_t annunc_id)
#define IDLE_COUNTS_PER_SEC_AT_NO_LOAD
void * PIOS_malloc_no_dma(size_t size)
void EventGetStats(EventStats *statsOut)
uint32_t lastQueueErrorID
static PeriodicObjectList * objList
void UAVObjGetStats(UAVObjStats *statsOut)
int32_t timeToNextUpdateMs
Utilities to validate a flight configuration.
uint16_t PIOS_WDG_GetBootupFlags()
Returns the flags that were set at bootup.
int32_t EventPeriodicCallbackCreate(UAVObjEvent *ev, UAVObjEventCallback cb, uint16_t periodMs)
uint16_t PIOS_WDG_GetActiveFlags()
Returns the currently active flags.
void PIOS_Thread_ChangePriority(enum pios_thread_prio_e prio)
#define MAX_UPDATE_PERIOD_MS
struct pios_recursive_mutex * PIOS_Recursive_Mutex_Create(void)
#define MODULE_HIPRI_INITCALL(ifn, sfn)
void PIOS_ANNUNCDAC_SetValue(annuncdac_dev_t dev, bool active, bool value)
Set whether we should be beeping.
static void updateSystemAlarms()
uint32_t UAVObjGetID(UAVObjHandle obj)
#define SYSTEM_UPDATE_PERIOD_MS
int actuator_send_dshot_command(uint8_t cmd_id, uint8_t num_to_send, uint16_t channel_mask)
#define CPULOAD_LIMIT_WARNING
bool PIOS_Recursive_Mutex_Lock(struct pios_recursive_mutex *mtx, uint32_t timeout_ms)
#define LL_APPEND(head, add)
uint32_t randomize_int(uint32_t interval)
int32_t SystemModStart(void)
Event dispatcher, distributes object events as callbacks. Alternative to using tasks and queues...
#define CPULOAD_LIMIT_CRITICAL
#define IRQSTACK_LIMIT_WARNING
void PIOS_ANNUNC_On(uint32_t annunc_id)
int32_t configuration_check()
struct pios_queue * queue
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
int32_t PIOS_SYS_Reset(void)
int32_t EventPeriodicQueueUpdate(UAVObjEvent *ev, struct pios_queue *queue, uint16_t periodMs)
void systemmod_process_rgb_leds(bool led_override, bool led_override_active, uint8_t blink_prio, bool is_armed, bool force_dim)
int32_t EventPeriodicCallbackUpdate(UAVObjEvent *ev, UAVObjEventCallback cb, uint16_t periodMs)
size_t PIOS_SYS_OsStackUnused(void)
#define DSHOT_CMD_BEACON3
DONT_BUILD_IF(ANNUNCIATORSETTINGS_ANNUNCIATEAFTERARMING_NUMELEM!=ANNUNCIATORSETTINGS_ANNUNCIATEANYTIME_NUMELEM, AnnuncSettingsMismatch1)
static struct pios_thread * systemTaskHandle
static const char *volatile custom_blink_string
int32_t UAVObjDeleteById(uint32_t obj_id, uint16_t inst_id)
void(* UAVObjEventCallback)(const UAVObjEvent *ev, void *cb_ctx, void *uavo_data, int uavo_len)
void PIOS_Thread_Sleep(uint32_t time_ms)
int32_t SystemModInitialize(void)
struct PeriodicObjectListStruct * next
int morse_send(const char **c, uint32_t *state)
static void objectUpdatedCb(const UAVObjEvent *ev, void *ctx, void *obj, int len)
static void updateStats()
uint32_t eventCallbackErrors
static uint32_t processPeriodicUpdates()
bool PIOS_heap_malloc_failed_p(void)
Includes PiOS and core architecture components.
int32_t AlarmsClear(SystemAlarmsAlarmElem alarm)
size_t PIOS_SYS_IrqStackUnused(void)
int32_t UAVObjLoad(UAVObjHandle obj_handle, uint16_t instId)
void PIOS_IAP_WriteBootCount(uint16_t)
static struct pios_queue * queue
void system_annunc_custom_string(const char *string)
static void configurationUpdatedCb(const UAVObjEvent *ev, void *ctx, void *obj, int len)
int32_t PIOS_FLASHFS_Format(uintptr_t fs_id)
Erases all filesystem arenas and activate the first arena.
static struct pios_queue * objectPersistenceQueue
if(BaroAltitudeHandle()!=NULL)
struct annuncdac_dev_s * annuncdac_dev_t
struct ObjectEventEntryThrottled * throttle
#define PIOS_Assert(test)
bool PIOS_Recursive_Mutex_Unlock(struct pios_recursive_mutex *mtx)
static int32_t eventPeriodicUpdate(UAVObjEvent *ev, UAVObjEventCallback cb, struct pios_queue *queue, uint16_t periodMs)
#define PIOS_ANNUNCIATOR_BUZZER
size_t PIOS_heap_get_free_size(void)
struct pios_thread * PIOS_Thread_WrapCurrentThread(const char *namep)
Creates a handle for the current thread.
static struct pios_recursive_mutex * mutex
#define PIOS_MUTEX_TIMEOUT_MAX
static volatile bool config_check_needed
#define PIOS_LED_HEARTBEAT