Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start timeout after landing before disarming #1349

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/modules/interface/supervisor_state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ typedef enum {
supervisorConditionCommanderWdtTimeout,
supervisorConditionEmergencyStop,
supervisorConditionIsCrashed,
supervisorConditionLandingTimeout,
supervisorCondition_NrOfConditions,
} supervisorConditions_t;

Expand All @@ -64,6 +65,7 @@ typedef uint32_t supervisorConditionBits_t;
#define SUPERVISOR_CB_COMMANDER_WDT_TIMEOUT (1 << supervisorConditionCommanderWdtTimeout)
#define SUPERVISOR_CB_EMERGENCY_STOP (1 << supervisorConditionEmergencyStop)
#define SUPERVISOR_CB_CRASHED (1 << supervisorConditionIsCrashed)
#define SUPERVISOR_CB_LANDING_TIMEOUT (1 << supervisorConditionLandingTimeout)


// Enum that is used to describe how to combine the bits in the required field
Expand Down
47 changes: 42 additions & 5 deletions src/modules/src/supervisor.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#define AUTO_ARMING 0
#endif

static uint16_t landingTimeoutDuration = LANDING_TIMEOUT_MS;

typedef struct {
bool canFly;
bool isFlying;
Expand All @@ -79,6 +81,9 @@ typedef struct {
// The time (in ticks) of the latest high thrust event. 0=no high thrust event yet
uint32_t latestThrustTick;

// The time (in ticks) of the latest landing event. 0=no landing event yet
uint32_t latestLandingTick;

supervisorState_t state;

// Copy of latest conditions, for logging
Expand Down Expand Up @@ -120,6 +125,19 @@ bool supervisorIsCrashed() {
return supervisorMem.isCrashed;
}

static void supervisorSetLatestLandingTime(SupervisorMem_t* this, const uint32_t currentTick) {
this->latestLandingTick = currentTick;
}

bool supervisorIsLandingTimeout(SupervisorMem_t* this, const uint32_t currentTick) {
if (0 == this->latestLandingTick) {
return false;
}

const uint32_t landingTime = currentTick - this->latestLandingTick;
return landingTime > M2T(landingTimeoutDuration);
}

bool supervisorRequestCrashRecovery(const bool doRecovery) {

if (doRecovery && !supervisorIsCrashed()) {
Expand Down Expand Up @@ -235,13 +253,18 @@ static bool checkEmergencyStopWatchdog(const uint32_t tick) {
return isOk;
}

static void postTransitionActions(SupervisorMem_t* this, const supervisorState_t previousState) {
static void postTransitionActions(SupervisorMem_t* this, const supervisorState_t previousState, const uint32_t currentTick) {
const supervisorState_t newState = this->state;

if (newState == supervisorStateReadyToFly) {
DEBUG_PRINT("Ready to fly\n");
}

if (newState == supervisorStateLanded) {
DEBUG_PRINT("Landed, starting disarm timer\n");
gemenerik marked this conversation as resolved.
Show resolved Hide resolved
supervisorSetLatestLandingTime(this, currentTick);
}

if (newState == supervisorStateLocked) {
DEBUG_PRINT("Locked, reboot required\n");
}
Expand All @@ -252,13 +275,14 @@ static void postTransitionActions(SupervisorMem_t* this, const supervisorState_t
}

if ((previousState == supervisorStateNotInitialized || previousState == supervisorStateReadyToFly || previousState == supervisorStateFlying) &&
newState != supervisorStateReadyToFly && newState != supervisorStateFlying) {
newState != supervisorStateReadyToFly && newState != supervisorStateFlying && newState != supervisorStateLanded) {
DEBUG_PRINT("Can not fly\n");
}

if (newState != supervisorStateReadyToFly &&
newState != supervisorStateFlying &&
newState != supervisorStateWarningLevelOut) {
newState != supervisorStateWarningLevelOut &&
newState != supervisorStateLanded) {
supervisorRequestArming(false);
}

Expand Down Expand Up @@ -311,6 +335,10 @@ static supervisorConditionBits_t updateAndPopulateConditions(SupervisorMem_t* th
conditions |= SUPERVISOR_CB_CRASHED;
}

if (supervisorIsLandingTimeout(this, currentTick)) {
conditions |= SUPERVISOR_CB_LANDING_TIMEOUT;
}

return conditions;
}

Expand Down Expand Up @@ -359,7 +387,7 @@ void supervisorUpdate(const sensorData_t *sensors, const setpoint_t* setpoint, s
if (this->state != newState) {
const supervisorState_t previousState = this->state;
this->state = newState;
postTransitionActions(this, previousState);
postTransitionActions(this, previousState, currentTick);
}

this->latestConditions = conditions;
Expand All @@ -375,6 +403,8 @@ void supervisorOverrideSetpoint(setpoint_t* setpoint) {
switch(this->state){
case supervisorStateReadyToFly:
// Fall through
case supervisorStateLanded:
// Fall through
case supervisorStateFlying:
// Do nothing
break;
Expand Down Expand Up @@ -402,7 +432,8 @@ bool supervisorAreMotorsAllowedToRun() {
SupervisorMem_t* this = &supervisorMem;
return (this->state == supervisorStateReadyToFly) ||
(this->state == supervisorStateFlying) ||
(this->state == supervisorStateWarningLevelOut);
(this->state == supervisorStateWarningLevelOut) ||
(this->state == supervisorStateLanded);
}

void infoDump(const SupervisorMem_t* this) {
Expand Down Expand Up @@ -494,4 +525,10 @@ PARAM_GROUP_START(supervisor)
* @brief Set to nonzero to dump information about the current supervisor state to the console log
*/
PARAM_ADD(PARAM_UINT8, infdmp, &supervisorMem.doinfodump)

/**
* @brief Landing timeout duration (ms)
*/
PARAM_ADD(PARAM_UINT16 | PARAM_PERSISTENT, landedTimeout, &landingTimeoutDuration)

PARAM_GROUP_STOP(supervisor)
14 changes: 13 additions & 1 deletion src/modules/src/supervisor_state_machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static const char* const conditionNames[] = {
"commanderWdtTimeout",
"emergencyStop",
"isCrashed",
"landingTimeout",
};
static_assert(sizeof(conditionNames) / sizeof(conditionNames[0]) == supervisorCondition_NrOfConditions);

Expand Down Expand Up @@ -203,10 +204,21 @@ static SupervisorStateTransition_t transitionsLanded[] = {
{
.newState = supervisorStateReset,

.triggerCombiner = supervisorAlways,
.triggers = SUPERVISOR_CB_LANDING_TIMEOUT,
.negatedTriggers = SUPERVISOR_CB_NONE,
.triggerCombiner = supervisorAll,

.blockerCombiner = supervisorNever,
},
{
.newState = supervisorStateFlying,

.triggers = SUPERVISOR_CB_IS_FLYING,
.negatedTriggers = SUPERVISOR_CB_NONE,
.triggerCombiner = supervisorAll,

.blockerCombiner = supervisorNever,
}
};

static SupervisorStateTransition_t transitionsReset[] = {
Expand Down
5 changes: 5 additions & 0 deletions src/platform/interface/platform_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
#define SUPERVISOR_TUMBLE_CHECK_ENABLE true
#endif

// Landing timeout before disarming
#ifndef LANDING_TIMEOUT_MS
#define LANDING_TIMEOUT_MS 3000
#endif


// Health test parameters
#ifndef HEALTH_BRUSHED_ON_PERIOD_MSEC
Expand Down
Loading