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

Extended supervisor #1272

Merged
merged 30 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9a9337c
Move emergency stop to supervisor
krichardsson Apr 19, 2023
d9fc6f9
Moved setpoint timeout from commander to
krichardsson Apr 19, 2023
50636eb
Add default handling
krichardsson Apr 19, 2023
e374aa7
Added low level motor stop
krichardsson Apr 20, 2023
63bee47
Added supervisor state diagram
krichardsson Apr 20, 2023
5a9be21
Added supervisor state machine
krichardsson Apr 20, 2023
8597cf5
Refactoring
krichardsson Apr 26, 2023
3334fd5
Similar functionality to master
krichardsson Apr 26, 2023
efcce28
Free fall if disarmed when flying
krichardsson Apr 26, 2023
b1b7475
Updated arming system which is on by default for bolt platform.
tobbeanton Apr 26, 2023
65cebc2
Enabled param stabilizer.stop again
krichardsson Apr 26, 2023
b574b67
Use explicit time for isTumbledCheck()
krichardsson Apr 26, 2023
08fad13
Fixed param_logic tests after isConnected update
tobbeanton Apr 26, 2023
4519c8e
Run supervisor at 25Hz
krichardsson Apr 26, 2023
9596af4
Added missing files
krichardsson Apr 26, 2023
6b35775
Added timeout for setting isFlying to false
krichardsson Apr 27, 2023
f036e45
Add blocking of High level commander by supervisor
krichardsson Apr 27, 2023
2663157
Added more conditions for leaving ready to fly state
krichardsson Apr 27, 2023
a7835f7
Updated supervisor logging in console
krichardsson Apr 27, 2023
2351815
Refactoring
krichardsson Apr 27, 2023
71b8969
Supervisor documentation
krichardsson Apr 27, 2023
1fd1259
Corrected isFlying
krichardsson Apr 27, 2023
34b5375
Added canArm log variable and new not_passed transition
tobbeanton May 9, 2023
381eea7
renamed test
krichardsson May 11, 2023
40e0442
Added more conditions back from preflight
krichardsson May 11, 2023
e9f82ae
Removed arming again
krichardsson May 11, 2023
0f24391
Started restructure state machine
krichardsson May 11, 2023
f4d0ad2
"All" and "any" for triggers and blockers
krichardsson May 11, 2023
5f35cff
Added negated triggers and blockers
krichardsson May 15, 2023
183fe51
Updated transitions
krichardsson May 15, 2023
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
9 changes: 9 additions & 0 deletions docs/functional-areas/supervisor/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: The supervisor
page_id: supervisor_index
---

The purpose of the supervisor is to monitor the system and its state. Depending on the situation, the supervisor
can enable/disable functionality as well as take action to protect the system or humans close by.

{% sub_page_menu %}
53 changes: 53 additions & 0 deletions docs/functional-areas/supervisor/states.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Supervisor states
page_id: supervisor_states
---
{% ditaa --alt "Supervisor states" %}
Boot|
V
+-------------------+
+ Pre-flight checks +<------------+
+ not passed +<--+ |
+-------+-----------+ | |
| ^ | |
V | | |
+---------+---------+ | |
+ Pre-flight checks + | |
+ passed + | |
+-------+-----------+ | |
| ^ | +-------+---------+
Arming| | | + Reset +
V | | + +
+---------+---------+ | +-----------------+
+ Ready to fly +---+ ^ ^
+ + | |
+--------+----------+ | |
| | |
V | |
+-------------------+ | | +-------------------+
+ Flying +------------|-|------>+ Exception +
+ +------------|-|---+ + Free fall +--+
+--------+----------+ | | | +-------------------+ |
| | | | |
V | | | |
+-------------------+ | | | +-------------------+ |
+ Landed +------------+ | +-->+ Exception + |
+ | | + Soft fall + |
+--------+----------+ | +---------+---------+ |
| | | |
| | | +---------+
| | | |
| | V V
+---------------+ | +-------------------+
| +-------+ Platform not +
| + moving +
| +---------+---------+
| |
| +--------------+
| |
V V
+-------------------+
+ Lock +
+ +
+-------------------+
{% endditaa %}
3 changes: 0 additions & 3 deletions src/modules/interface/commander.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

#define DEFAULT_YAW_MODE XMODE

#define COMMANDER_WDT_TIMEOUT_STABILIZE M2T(500)
#define COMMANDER_WDT_TIMEOUT_SHUTDOWN M2T(2000)

#define COMMANDER_PRIORITY_DISABLE 0
// Keep a macro for lowest non-disabled priority, regardless of source, in case
// some day there is a priority lower than the high-level commander.
Expand Down
20 changes: 20 additions & 0 deletions src/modules/interface/crtp_localization_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,24 @@ void locSrvSendRangeFloat(uint8_t id, float range);
void locSrvSendLighthouseAngle(int baseStation, pulseProcessorResult_t* angles);
#endif

/**
* @brief Check if there is a request for emergency stop.
*
* @return true Emergency stop requested
* @return false Emergency stop not requested
*/
bool locSrvIsEmergencyStopRequested();

/**
* @brief Reset any emergency stop request
*/
void locSrvResetEmergencyStopRequest();

/**
* @brief Get the time for when the latest emergency stop notification was received.
*
* @return uint32_t The system tick when the latest notification was received. 0 if no notification has been received.
*/
uint32_t locSrvGetEmergencyStopWatchdogNotificationTick();

#endif /* _CRTP_LOCALIZATION_SERVICE_H_ */
20 changes: 0 additions & 20 deletions src/modules/interface/stabilizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include "estimator.h"

#define EMERGENCY_STOP_TIMEOUT_DISABLED (-1)
/**
* Initialize the stabilizer subsystem and launch the stabilizer loop task.
* The stabilizer loop task will wait on systemWaitStart() before running.
Expand All @@ -45,23 +44,4 @@ void stabilizerInit(StateEstimatorType estimator);
*/
bool stabilizerTest(void);

/**
* Enable emergency stop, will shut-off energy to the motors.
*/
void stabilizerSetEmergencyStop();

/**
* Disable emergency stop, will enable energy to the motors.
*/
void stabilizerResetEmergencyStop();

/**
* Restart the countdown until emergercy stop will be enabled.
*
* @param timeout Timeout in stabilizer loop tick. The stabilizer loop rate is
* RATE_MAIN_LOOP.
*/
void stabilizerSetEmergencyStopTimeout(int timeout);


#endif /* STABILIZER_H_ */
5 changes: 4 additions & 1 deletion src/modules/interface/supervisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@

#include "stabilizer_types.h"

void supervisorUpdate(const sensorData_t *data);
void supervisorUpdate(const sensorData_t *sensors, const setpoint_t* setpoint);

void supervisorOverrideSetpoint(setpoint_t* setpoint);
bool supervisorAreMotorsAllowedToRun();

bool supervisorCanFly(void);
bool supervisorIsFlying(void);
Expand Down
18 changes: 1 addition & 17 deletions src/modules/src/commander.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,7 @@ void commanderGetSetpoint(setpoint_t *setpoint, const state_t *state)
{
xQueuePeek(setpointQueue, setpoint, 0);
lastUpdate = setpoint->timestamp;
uint32_t currentTime = xTaskGetTickCount();

if ((currentTime - setpoint->timestamp) > COMMANDER_WDT_TIMEOUT_SHUTDOWN) {
memcpy(setpoint, &nullSetpoint, sizeof(nullSetpoint));
} else if ((currentTime - setpoint->timestamp) > COMMANDER_WDT_TIMEOUT_STABILIZE) {
xQueueOverwrite(priorityQueue, &priorityDisable);
// Leveling ...
setpoint->mode.x = modeDisable;
setpoint->mode.y = modeDisable;
setpoint->mode.roll = modeAbs;
setpoint->mode.pitch = modeAbs;
setpoint->mode.yaw = modeVelocity;
setpoint->attitude.roll = 0;
setpoint->attitude.pitch = 0;
setpoint->attitudeRate.yaw = 0;
// Keep Z as it is
}

// This copying is not strictly necessary because stabilizer.c already keeps
// a static state_t containing the most recent state estimate. However, it is
// not accessible by the public interface.
Expand Down
20 changes: 17 additions & 3 deletions src/modules/src/crtp_localization_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
#define NBR_OF_SENSOR_DIFFS_IN_PACKET 3
#define NBR_OF_BASESTATIONS 2
#define NBR_OF_
#define DEFAULT_EMERGENCY_STOP_TIMEOUT (1 * RATE_MAIN_LOOP)

typedef enum
{
Expand Down Expand Up @@ -130,6 +129,9 @@ static void extPositionHandler(CRTPPacket* pk);
static void genericLocHandle(CRTPPacket* pk);
static void extPositionPackedHandler(CRTPPacket* pk);

static bool isEmergencyStopRequested = false;
static uint32_t emergencyStopWatchdogNotificationTick = 0;

void locSrvInit()
{
if (isInit) {
Expand Down Expand Up @@ -294,10 +296,10 @@ static void genericLocHandle(CRTPPacket* pk)
lpsShortLppPacketHandler(pk);
break;
case EMERGENCY_STOP:
stabilizerSetEmergencyStop();
isEmergencyStopRequested = true;
break;
case EMERGENCY_STOP_WATCHDOG:
stabilizerSetEmergencyStopTimeout(DEFAULT_EMERGENCY_STOP_TIMEOUT);
emergencyStopWatchdogNotificationTick = xTaskGetTickCount();
break;
case EXT_POSE:
extPoseHandler(pk);
Expand Down Expand Up @@ -390,6 +392,18 @@ void locSrvSendLighthouseAngle(int baseStation, pulseProcessorResult_t* angles)
}
#endif

bool locSrvIsEmergencyStopRequested() {
return isEmergencyStopRequested;
}

void locSrvResetEmergencyStopRequest() {
isEmergencyStopRequested = false;
}

uint32_t locSrvGetEmergencyStopWatchdogNotificationTick() {
return emergencyStopWatchdogNotificationTick;
}

// This logging group is deprecated (removed after August 2023)
LOG_GROUP_START(ext_pos)
LOG_ADD(LOG_FLOAT, X, &ext_pos.x)
Expand Down
100 changes: 39 additions & 61 deletions src/modules/src/stabilizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@
#include "rateSupervisor.h"

static bool isInit;
static bool emergencyStop = false;
static int emergencyStopTimeout = EMERGENCY_STOP_TIMEOUT_DISABLED;

static uint32_t inToOutLatency;

Expand Down Expand Up @@ -204,17 +202,6 @@ bool stabilizerTest(void)
return pass;
}

static void checkEmergencyStopTimeout()
{
if (emergencyStopTimeout >= 0) {
emergencyStopTimeout -= 1;

if (emergencyStopTimeout == 0) {
emergencyStop = true;
}
}
}

static void batteryCompensation(const motors_thrust_uncapped_t* motorThrustUncapped, motors_thrust_uncapped_t* motorThrustBatCompUncapped)
{
float supplyVoltage = pmGetBatteryVoltage();
Expand All @@ -233,6 +220,25 @@ static void setMotorRatios(const motors_thrust_pwm_t* motorPwm)
motorsSetRatio(MOTOR_M4, motorPwm->motors.m4);
}

static void updateStateEstimatorAndControllerTypes() {
if (stateEstimatorGetType() != estimatorType) {
stateEstimatorSwitchTo(estimatorType);
estimatorType = stateEstimatorGetType();
}

if (controllerGetType() != controllerType) {
controllerInit(controllerType);
controllerType = controllerGetType();
}
}

static void controlMotors(const control_t* control) {
powerDistribution(control, &motorThrustUncapped);
batteryCompensation(&motorThrustUncapped, &motorThrustBatCompUncapped);
powerDistributionCap(&motorThrustBatCompUncapped, &motorPwm);
setMotorRatios(&motorPwm);
}

/* The stabilizer loop runs at 1kHz. It is the
* responsibility of the different functions to run slower by skipping call
* (ie. returning without modifying the output structure).
Expand Down Expand Up @@ -270,48 +276,40 @@ static void stabilizerTask(void* param)
if (healthShallWeRunTest()) {
healthRunTests(&sensorData);
} else {
// allow to update estimator dynamically
if (stateEstimatorGetType() != estimatorType) {
stateEstimatorSwitchTo(estimatorType);
estimatorType = stateEstimatorGetType();
}
// allow to update controller dynamically
if (controllerGetType() != controllerType) {
controllerInit(controllerType);
controllerType = controllerGetType();
}
updateStateEstimatorAndControllerTypes();

stateEstimator(&state, stabilizerStep);
compressState();

if (crtpCommanderHighLevelGetSetpoint(&tempSetpoint, &state, stabilizerStep)) {
commanderSetSetpoint(&tempSetpoint, COMMANDER_PRIORITY_HIGHLEVEL);
}

commanderGetSetpoint(&setpoint, &state);
compressSetpoint();

collisionAvoidanceUpdateSetpoint(&setpoint, &sensorData, &state, stabilizerStep);
// Critical for safety, be careful if you modify this code!
// Let the supervisor update it's view of the current situation
supervisorUpdate(&sensorData, &setpoint);

controller(&control, &setpoint, &sensorData, &state, stabilizerStep);
// Let the collision avoidance module modify the setpoint, if needed
collisionAvoidanceUpdateSetpoint(&setpoint, &sensorData, &state, stabilizerStep);

checkEmergencyStopTimeout();
// Critical for safety, be careful if you modify this code!
// Let the supervisor modify the setpoint to handle exceptional conditions
supervisorOverrideSetpoint(&setpoint);

//
// The supervisor module keeps track of Crazyflie state such as if
// we are ok to fly, or if the Crazyflie is in flight.
//
supervisorUpdate(&sensorData);
controller(&control, &setpoint, &sensorData, &state, stabilizerStep);

if (emergencyStop || (systemIsArmed() == false)) {
motorsStop();
// Critical for safety, be careful if you modify this code!
// The supervisor will already set thrust to 0 in the setpoint if needed, but to be extra sure prevent motors from running.
if (supervisorAreMotorsAllowedToRun()) {
controlMotors(&control);
} else {
powerDistribution(&control, &motorThrustUncapped);
batteryCompensation(&motorThrustUncapped, &motorThrustBatCompUncapped);
powerDistributionCap(&motorThrustBatCompUncapped, &motorPwm);
setMotorRatios(&motorPwm);
motorsStop();
}

// Compute compressed log formats
compressState();
compressSetpoint();

#ifdef CONFIG_DECK_USD
// Log data to uSD card if configured
if (usddeckLoggingEnabled()
Expand All @@ -337,25 +335,9 @@ static void stabilizerTask(void* param)
}
}

void stabilizerSetEmergencyStop()
{
emergencyStop = true;
}

void stabilizerResetEmergencyStop()
{
emergencyStop = false;
}

void stabilizerSetEmergencyStopTimeout(int timeout)
{
emergencyStop = false;
emergencyStopTimeout = timeout;
}

/**
* Parameters to set the estimator and controller type
* for the stabilizer module, or to do an emergency stop
* for the stabilizer module
*/
PARAM_GROUP_START(stabilizer)
/**
Expand All @@ -368,10 +350,6 @@ PARAM_ADD_CORE(PARAM_UINT8, estimator, &estimatorType)
* @brief Controller type Auto select(0), PID(1), Mellinger(2), INDI(3), Brescianini(4) (Default: 0)
*/
PARAM_ADD_CORE(PARAM_UINT8, controller, &controllerType)
/**
* @brief If set to nonzero will turn off power
*/
PARAM_ADD_CORE(PARAM_UINT8, stop, &emergencyStop)
knmcguire marked this conversation as resolved.
Show resolved Hide resolved
PARAM_GROUP_STOP(stabilizer)


Expand Down
Loading