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 9 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 %}
46 changes: 46 additions & 0 deletions docs/functional-areas/supervisor/states.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
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 +------------|-------->+ Warning +
+ +<-----------|---------+ Level out +
+--------+-------+--+ | +---------+---------+
| | | |
V +---------------|-----+ V
+-------------------+ | | +-------------------+
+ Landed +------------+ +-->+ Exception +
+ | + Free fall +
+--------+----------+ +---------+---------+
|
|
+--------------+
|
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
80 changes: 80 additions & 0 deletions src/modules/interface/supervisor_state_machine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* ,---------, ____ _ __
* | ,-^-, | / __ )(_) /_______________ _____ ___
* | ( O ) | / __ / / __/ ___/ ___/ __ `/_ / / _ \
* | / ,--´ | / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
* +------` /_____/_/\__/\___/_/ \__,_/ /___/\___/
*
* Crazyflie control firmware
*
* Copyright (C) 2023 Bitcraze AB
*
* 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, in version 3.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <stdint.h>

typedef enum {
supervisorStatePreFlChecksNotPassed = 0,
supervisorStatePreFlChecksPassed,
supervisorStateReadyToFly,
supervisorStateFlying,
supervisorStateLanded,
supervisorStateReset,
supervisorStateWarningLevelOut,
supervisorStateExceptFreeFall,
supervisorStateLocked,
supervisorState_NrOfStates,
} supervisorState_t;

// Conditions supported by the supervisor
enum {
supervisorConditionArmed = 0,
supervisorConditionChargerConnected,
supervisorConditionIsFlying,
supervisorConditionIsTumbled,
supervisorConditionCommanderWdtWarning,
supervisorConditionCommanderWdtTimeout,
supervisorConditionEmergencyStop,
};

typedef uint32_t supervisorConditionBits_t;

// Condition bit definitions
#define SUPERVISOR_CB_NONE (0)
#define SUPERVISOR_CB_ARMED (1 << supervisorConditionArmed)
#define SUPERVISOR_CB_CHARGER_CONNECTED (1 << supervisorConditionChargerConnected)
#define SUPERVISOR_CB_IS_FLYING (1 << supervisorConditionIsFlying)
#define SUPERVISOR_CB_IS_TUMBLED (1 << supervisorConditionIsTumbled)
#define SUPERVISOR_CB_COMMANDER_WDT_WARNING (1 << supervisorConditionCommanderWdtWarning)
#define SUPERVISOR_CB_COMMANDER_WDT_TIMEOUT (1 << supervisorConditionCommanderWdtTimeout)
#define SUPERVISOR_CB_EMERGENCY_STOP (1 << supervisorConditionEmergencyStop)


typedef struct {
supervisorState_t newState;
supervisorConditionBits_t mustBeSet;
supervisorConditionBits_t mustNotBeSet;
} SupervisorStateTransition_t;

typedef struct {
SupervisorStateTransition_t* transitionList;
int length;
} SupervisorStateTransitionList_t;

// Macro used when defining SupervisorStateTransitionLists
#define SUPERVISOR_TRANSITION_ENTRY(TRANSITION_DEF) .transitionList=TRANSITION_DEF, .length=(sizeof(TRANSITION_DEF) / sizeof(SupervisorStateTransition_t))

supervisorState_t supervisorStateUpdate(const supervisorState_t currentState, const supervisorConditionBits_t conditions);
1 change: 1 addition & 0 deletions src/modules/src/Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ obj-y += sound_cf2.o
obj-y += stabilizer.o
obj-y += static_mem.o
obj-y += supervisor.o
obj-y += supervisor_state_machine.o
obj-y += sysload.o
obj-y += system.o
obj-$(CONFIG_DECK_LOCO) += tdoaEngineInstance.o
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
Loading