Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fvantienen authored and dewagter committed Sep 18, 2023
1 parent ac073ce commit ca7b5ad
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 44 deletions.
5 changes: 3 additions & 2 deletions sw/airborne/autopilot.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,16 @@ void autopilot_force_motors_on(bool motors_on)
/** turn motors on/off, eventually depending of the current mode
* set kill_throttle accordingly FIXME is it true for FW firmware ?
*/
void autopilot_set_motors_on(bool motors_on)
bool autopilot_set_motors_on(bool motors_on)
{
#if PREFLIGHT_CHECKS
// When we fail the preflight checks abort
if(motors_on && !preflight_check()) {
return;
return false;
}
#endif
autopilot_force_motors_on(motors_on);
return true;
}

/** get motors status
Expand Down
4 changes: 3 additions & 1 deletion sw/airborne/autopilot.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ extern void autopilot_force_motors_on(bool motors_on);
* or when the preflight checks are failing.
*
* @param[in] motors_on true to start motors, false to stop
* @return true The preflight checks are successful and motors are started/stopped
* @return false The preflight checks are failed
*/
extern void autopilot_set_motors_on(bool motors_on);
extern bool autopilot_set_motors_on(bool motors_on);

/** Get motor status
*
Expand Down
80 changes: 74 additions & 6 deletions sw/airborne/modules/checks/preflight_checks.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@
#define PREFLIGHT_MAX_MSG_BUF 512
#endif

#ifndef PREFLIGHT_CHECK_SEPERATOR
#define PREFLIGHT_CHECK_SEPERATOR ';'
#endif

static struct preflight_check_t *preflight_head = NULL;

/**
* @brief Register a preflight check and add it to the linked list
*
* @param check The check to add containing a linked list
* @param func The function to register for the check
*/
void preflight_check_register(struct preflight_check_t *check, preflight_check_f func) {
// Prepend the preflight check
struct preflight_check_t *next = preflight_head;
Expand All @@ -41,9 +51,15 @@ void preflight_check_register(struct preflight_check_t *check, preflight_check_f
}

#include "modules/datalink/telemetry.h"
/**
* @brief Perform all the preflight checks
*
* @return true When all preflight checks are successful
* @return false When one or more preflight checks fail
*/
bool preflight_check(void) {
char error_msg[PREFLIGHT_MAX_MSG_BUF];
struct preflight_error_t error = {
struct preflight_result_t result = {
.message = error_msg,
.max_len = PREFLIGHT_MAX_MSG_BUF,
.fail_cnt = 0,
Expand All @@ -54,18 +70,70 @@ bool preflight_check(void) {
struct preflight_check_t *check = preflight_head;
while(check != NULL) {
// Peform the check and register errors
check->func(&error);
check->func(&result);
check = check->next;
}

// We failed a check
if(error.fail_cnt > 0) {
printf("Preflight fail [%d/%d]:\n%s\n", error.fail_cnt, (error.fail_cnt+error.success_cnt), error_msg);
DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, PREFLIGHT_MAX_MSG_BUF-error.max_len, error_msg);
if(result.fail_cnt > 0) {
printf("Preflight fail [%d/%d]:\n%s\n", result.fail_cnt, (result.fail_cnt+result.success_cnt), error_msg);
DOWNLINK_SEND_INFO_MSG(DefaultChannel, DefaultDevice, PREFLIGHT_MAX_MSG_BUF-result.max_len, error_msg);
return false;
}

// Return success if we didn't fail a preflight check
printf("Preflight success [%d]\n", error.success_cnt);
printf("Preflight success [%d]\n", result.success_cnt);
return true;
}

/**
* @brief Register a preflight error used inside the preflight checking functions
*
* @param result Where the error gets registered
* @param fmt A formatted string describing the error used in a vsnprintf
* @param ... The arguments for the vsnprintf
*/
void preflight_error(struct preflight_result_t *result, const char *fmt, ...) {
// Record the error count
result->fail_cnt++;

// No more space in the message
if(result->max_len <= 0) {
return;
}

// Add the error
va_list args;
va_start(args, fmt);
int rc = vsnprintf(result->message, result->max_len, fmt, args);
va_end(args);

// Remove the length from the buffer if it was successfull
if(rc > 0) {
result->max_len -= rc;
result->message += rc;

// Add seperator if it fits
if(result->max_len > 0) {
result->message[0] = PREFLIGHT_CHECK_SEPERATOR;
result->max_len--;
result->message++;

// Add the '\0' character
if(result->max_len > 0)
result->message[0] = 0;
}
}
}

/**
* @brief Register a preflight success used inside the preflight checking functions
*
* @param result
* @param __attribute__
* @param ...
*/
void preflight_success(struct preflight_result_t *result, const char *fmt __attribute__((unused)), ...) {
// Record the success count
result->success_cnt++;
}
33 changes: 4 additions & 29 deletions sw/airborne/modules/checks/preflight_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,14 @@
#include "std.h"
#include <stdarg.h>

struct preflight_error_t {
struct preflight_result_t {
char *message;
uint16_t max_len;
uint16_t fail_cnt;
uint16_t success_cnt;
};

typedef void (*preflight_check_f)(struct preflight_error_t *error);

static inline void preflight_error(struct preflight_error_t *error, const char *fmt, ...) {
// Record the error count
error->fail_cnt++;

// No more space in the message
if(error->max_len <= 0) {
return;
}

// Add the error
va_list args;
va_start(args, fmt);
int rc = vsnprintf(error->message, error->max_len, fmt, args);
va_end(args);

// Remove the length (minus \0 character) from the buffer
if(rc > 0) {
error->max_len -= (rc - 1);
error->message += (rc - 1);
}
}

static inline void preflight_success(struct preflight_error_t *error, const char *fmt __attribute__((unused)), ...) {
// Record the success count
error->success_cnt++;
}
typedef void (*preflight_check_f)(struct preflight_result_t *result);

struct preflight_check_t {
preflight_check_f func;
Expand All @@ -73,5 +46,7 @@ struct preflight_check_t {

extern void preflight_check_register(struct preflight_check_t *check, preflight_check_f func);
extern bool preflight_check(void);
extern void preflight_error(struct preflight_result_t *result, const char *fmt, ...);
extern void preflight_success(struct preflight_result_t *result, const char *fmt, ...);

#endif /* PREFLIGHT_CHECKS_H */
6 changes: 3 additions & 3 deletions sw/airborne/modules/energy/electrical.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ static struct {
#include "modules/checks/preflight_checks.h"
static struct preflight_check_t electrical_pfc;

static void electrical_preflight(struct preflight_error_t *error) {
static void electrical_preflight(struct preflight_result_t *result) {
if(electrical.vsupply < TAKEOFF_BAT_LEVEL) {
preflight_error(error, "Battery level %.2fV below minimum takeoff level %.2fV\n", electrical.vsupply, TAKEOFF_BAT_LEVEL);
preflight_error(result, "Battery level %.2fV below minimum takeoff level %.2fV", electrical.vsupply, TAKEOFF_BAT_LEVEL);
} else {
preflight_success(error, "Battery level %.2fV above takeoff level %.2fV\n", electrical.vsupply, TAKEOFF_BAT_LEVEL);
preflight_success(result, "Battery level %.2fV above takeoff level %.2fV", electrical.vsupply, TAKEOFF_BAT_LEVEL);
}
}
#endif // PREFLIGHT_CHECKS
Expand Down
6 changes: 3 additions & 3 deletions sw/airborne/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ struct State state;
#include "modules/checks/preflight_checks.h"
static struct preflight_check_t state_pfc;

static void state_preflight(struct preflight_error_t *error) {
static void state_preflight(struct preflight_result_t *result) {
if(true || !stateIsAttitudeValid()) {
preflight_error(error, "State attitude is invalid\n");
preflight_error(result, "State attitude is invalid");
} else {
preflight_success(error, "State attitude is valid\n");
preflight_success(result, "State attitude is valid");
}
}
#endif // PREFLIGHT_CHECKS
Expand Down

0 comments on commit ca7b5ad

Please sign in to comment.