[10] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[10] | 4 | // %flair:license}
|
---|
[7] | 5 | // created: 2014/04/29
|
---|
| 6 | // filename: UavStateMachine.h
|
---|
| 7 | //
|
---|
| 8 | // author: Gildas Bayard, Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: state machine for UAV
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 |
|
---|
| 18 | #ifndef UAVSTATEMACHINE_H
|
---|
| 19 | #define UAVSTATEMACHINE_H
|
---|
| 20 |
|
---|
| 21 | #include <Thread.h>
|
---|
| 22 | #include <Vector2D.h>
|
---|
| 23 | #include <Vector3D.h>
|
---|
| 24 | #include <Euler.h>
|
---|
| 25 | #include <Quaternion.h>
|
---|
| 26 |
|
---|
| 27 | namespace flair {
|
---|
[15] | 28 | namespace core {
|
---|
| 29 | class FrameworkManager;
|
---|
| 30 | class AhrsData;
|
---|
| 31 | class io_data;
|
---|
[7] | 32 | }
|
---|
[15] | 33 | namespace gui {
|
---|
| 34 | class PushButton;
|
---|
| 35 | class GridLayout;
|
---|
| 36 | class Tab;
|
---|
| 37 | class DoubleSpinBox;
|
---|
| 38 | }
|
---|
| 39 | namespace filter {
|
---|
| 40 | class ControlLaw;
|
---|
| 41 | class NestedSat;
|
---|
| 42 | class Pid;
|
---|
| 43 | class PidThrust;
|
---|
| 44 | class TrajectoryGenerator1D;
|
---|
| 45 | }
|
---|
| 46 | namespace sensor {
|
---|
| 47 | class TargetController;
|
---|
| 48 | }
|
---|
| 49 | namespace meta {
|
---|
| 50 | class MetaDualShock3;
|
---|
| 51 | class Uav;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
[7] | 54 |
|
---|
[15] | 55 | namespace flair {
|
---|
| 56 | namespace meta {
|
---|
[7] | 57 |
|
---|
[15] | 58 | /*! \class UavStateMachine
|
---|
[7] | 59 | *
|
---|
| 60 | * \brief State machine for UAV
|
---|
[122] | 61 | * The Thread is created with
|
---|
| 62 | * the FrameworkManager as parent. FrameworkManager must be created before.
|
---|
| 63 | * The Thread is synchronized with Ahrs, unless a period is set with SetPeriodUS or
|
---|
| 64 | * SetPeriodMS
|
---|
[7] | 65 | */
|
---|
| 66 |
|
---|
[15] | 67 | class UavStateMachine : public core::Thread {
|
---|
| 68 | protected:
|
---|
| 69 | enum class AltitudeMode_t { Manual, Custom };
|
---|
| 70 | const AltitudeMode_t &GetAltitudeMode(void) const { return altitudeMode; }
|
---|
| 71 | bool SetAltitudeMode(const AltitudeMode_t &altitudeMode);
|
---|
[7] | 72 |
|
---|
[15] | 73 | // uses TrajectoryGenerator1D *altitudeTrajectory to go to desiredAltitude
|
---|
| 74 | // available in mode AltitudeMode_t::Manual
|
---|
| 75 | // return true if goto is possible
|
---|
| 76 | bool GotoAltitude(float desiredAltitude);
|
---|
[7] | 77 |
|
---|
[15] | 78 | enum class OrientationMode_t { Manual, Custom };
|
---|
| 79 | const OrientationMode_t &GetOrientationMode(void) const {
|
---|
| 80 | return orientationMode;
|
---|
| 81 | }
|
---|
| 82 | bool SetOrientationMode(const OrientationMode_t &orientationMode);
|
---|
[7] | 83 |
|
---|
[15] | 84 | enum class ThrustMode_t { Default, Custom };
|
---|
| 85 | const ThrustMode_t &GetThrustMode() const { return thrustMode; }
|
---|
| 86 | bool SetThrustMode(const ThrustMode_t &thrustMode);
|
---|
[7] | 87 |
|
---|
[15] | 88 | enum class TorqueMode_t { Default, Custom };
|
---|
| 89 | const TorqueMode_t &GetTorqueMode(void) const { return torqueMode; }
|
---|
| 90 | bool SetTorqueMode(const TorqueMode_t &torqueMode);
|
---|
[7] | 91 |
|
---|
[15] | 92 | enum class Event_t {
|
---|
| 93 | EnteringFailSafeMode,
|
---|
| 94 | EnteringControlLoop,
|
---|
| 95 | StartLanding,
|
---|
| 96 | FinishLanding,
|
---|
| 97 | Stopped,
|
---|
| 98 | TakingOff,
|
---|
| 99 | EmergencyStop,
|
---|
| 100 | Stabilized, // as soon as uav is 3cm far from the ground
|
---|
| 101 | ZTrajectoryFinished,
|
---|
| 102 | };
|
---|
[7] | 103 |
|
---|
[122] | 104 | UavStateMachine(sensor::TargetController* controller);
|
---|
[38] | 105 | ~UavStateMachine();
|
---|
[7] | 106 |
|
---|
[15] | 107 | const core::Quaternion &GetCurrentQuaternion(void) const;
|
---|
[7] | 108 |
|
---|
[167] | 109 | const core::Vector3Df &GetCurrentAngularSpeed(void) const;
|
---|
[7] | 110 |
|
---|
[38] | 111 | void Land(void);
|
---|
| 112 | void EmergencyLand(void);
|
---|
| 113 | void TakeOff(void);
|
---|
| 114 | void EmergencyStop(void);
|
---|
| 115 | //! Used to signal an event
|
---|
| 116 | /*!
|
---|
| 117 | \param event the event which occured
|
---|
| 118 | */
|
---|
| 119 | virtual void SignalEvent(Event_t event);
|
---|
[7] | 120 |
|
---|
[15] | 121 | virtual const core::AhrsData *GetOrientation(void) const;
|
---|
| 122 | const core::AhrsData *GetDefaultOrientation(void) const;
|
---|
[7] | 123 |
|
---|
[15] | 124 | virtual void AltitudeValues(float &z, float &dz) const; // in uav coordinate!
|
---|
| 125 | void EnterFailSafeMode(void);
|
---|
| 126 | bool ExitFailSafeMode(void);
|
---|
| 127 | void FailSafeAltitudeValues(float &z, float &dz) const; // in uav coordinate!
|
---|
[7] | 128 |
|
---|
[15] | 129 | gui::GridLayout *GetButtonsLayout(void) const;
|
---|
| 130 | virtual void ExtraSecurityCheck(void){};
|
---|
| 131 | virtual void ExtraCheckJoystick(void){};
|
---|
| 132 | virtual void ExtraCheckPushButton(void){};
|
---|
[7] | 133 |
|
---|
[15] | 134 | void GetDefaultReferenceAltitude(float &refAltitude,
|
---|
| 135 | float &refVerticalVelocity);
|
---|
| 136 | virtual void GetReferenceAltitude(float &refAltitude,
|
---|
| 137 | float &refVerticalVelocity);
|
---|
| 138 | // float GetDefaultThrustOffset(void);
|
---|
| 139 | const core::AhrsData *GetDefaultReferenceOrientation(void) const;
|
---|
| 140 | virtual const core::AhrsData *GetReferenceOrientation(void);
|
---|
[7] | 141 |
|
---|
[15] | 142 | /*!
|
---|
| 143 | * \brief Compute Custom Torques
|
---|
| 144 | *
|
---|
| 145 | * Reimplement this method to use TorqueMode_t::Custom. \n
|
---|
| 146 | * This method is called internally by UavStateMachine, do not call it by
|
---|
| 147 | *yourself. \n
|
---|
| 148 | * See GetTorques if you need torques values.
|
---|
| 149 | *
|
---|
| 150 | * \param torques custom torques
|
---|
| 151 | */
|
---|
| 152 | virtual void ComputeCustomTorques(core::Euler &torques);
|
---|
[7] | 153 |
|
---|
[15] | 154 | /*!
|
---|
| 155 | * \brief Compute Default Torques
|
---|
| 156 | *
|
---|
| 157 | * This method is called internally by UavStateMachine when using
|
---|
| 158 | *TorqueMode_t::Default. \n
|
---|
| 159 | * Torques are only computed once by loop, other calls to this method will use
|
---|
| 160 | *previously computed torques.
|
---|
| 161 | *
|
---|
| 162 | * \param torques default torques
|
---|
| 163 | */
|
---|
| 164 | void ComputeDefaultTorques(core::Euler &torques);
|
---|
[7] | 165 |
|
---|
[15] | 166 | /*!
|
---|
| 167 | * \brief Get Torques
|
---|
| 168 | *
|
---|
| 169 | * \return torques current torques
|
---|
| 170 | */
|
---|
| 171 | // const core::Euler &GetTorques() const;
|
---|
[7] | 172 |
|
---|
[15] | 173 | /*!
|
---|
| 174 | * \brief Compute Custom Thrust
|
---|
| 175 | *
|
---|
| 176 | * Reimplement this method to use ThrustMode_t::Custom. \n
|
---|
| 177 | * This method is called internally by UavStateMachine, do not call it by
|
---|
| 178 | *yourself. \n
|
---|
| 179 | * See GetThrust if you need thrust value.
|
---|
| 180 | *
|
---|
| 181 | * \return custom Thrust
|
---|
| 182 | */
|
---|
| 183 | virtual float ComputeCustomThrust(void);
|
---|
[7] | 184 |
|
---|
[15] | 185 | /*!
|
---|
| 186 | * \brief Compute Default Thrust
|
---|
| 187 | *
|
---|
| 188 | * This method is called internally by UavStateMachine when using
|
---|
| 189 | *ThrustMode_t::Default. \n
|
---|
| 190 | * Thrust is only computed once by loop, other calls to this method will use
|
---|
| 191 | *previously computed thrust.
|
---|
| 192 | *
|
---|
| 193 | * \return default thrust
|
---|
| 194 | */
|
---|
| 195 | float ComputeDefaultThrust(void);
|
---|
[7] | 196 |
|
---|
[15] | 197 | /*!
|
---|
| 198 | * \brief Get Thrust
|
---|
| 199 | *
|
---|
| 200 | * \return current thrust
|
---|
| 201 | */
|
---|
| 202 | // float GetThrust() const;
|
---|
[7] | 203 |
|
---|
[15] | 204 | /*!
|
---|
| 205 | * \brief Add an IODevice to the control law logs
|
---|
| 206 | *
|
---|
| 207 | * The IODevice will be automatically logged among the Uz logs,
|
---|
| 208 | * if logging is enabled (see IODevice::SetDataToLog,
|
---|
| 209 | *FrameworkManager::StartLog
|
---|
| 210 | * and FrameworkManager::AddDeviceToLog). \n
|
---|
| 211 | *
|
---|
| 212 | * \param device IODevice to log
|
---|
| 213 | */
|
---|
| 214 | void AddDeviceToControlLawLog(const core::IODevice *device);
|
---|
[7] | 215 |
|
---|
[15] | 216 | /*!
|
---|
| 217 | * \brief Add an io_data to the control law logs
|
---|
| 218 | *
|
---|
| 219 | * The io_data will be automatically logged among the Uz logs,
|
---|
| 220 | * if logging is enabled (see IODevice::SetDataToLog,
|
---|
| 221 | *FrameworkManager::StartLog
|
---|
| 222 | * and FrameworkManager::AddDeviceToLog). \n
|
---|
| 223 | *
|
---|
| 224 | * \param data io_data to log
|
---|
| 225 | */
|
---|
| 226 | void AddDataToControlLawLog(const core::io_data *data);
|
---|
[7] | 227 |
|
---|
[313] | 228 | const sensor::TargetController *GetTargetController(void) const;
|
---|
| 229 | MetaDualShock3 *GetJoystick(void) const;
|
---|
[206] | 230 |
|
---|
| 231 | filter::NestedSat *GetURoll(void);
|
---|
| 232 | filter::NestedSat *GetUPitch(void);
|
---|
| 233 | filter::Pid *GetUYaw(void);
|
---|
| 234 | filter::PidThrust *GetUZ(void);
|
---|
| 235 | filter::TrajectoryGenerator1D *GetAltitudeTrajectory(void);
|
---|
[7] | 236 |
|
---|
[15] | 237 | gui::Tab *setupLawTab, *graphLawTab;
|
---|
[7] | 238 |
|
---|
[15] | 239 | private:
|
---|
| 240 | /*!
|
---|
| 241 | \enum AltitudeState_t
|
---|
| 242 | \brief States of the altitude state machine
|
---|
| 243 | */
|
---|
| 244 | enum class AltitudeState_t {
|
---|
| 245 | Stopped, /*!< the uav motors are stopped */
|
---|
| 246 | TakingOff, /*!< take off initiated. Motors accelerate progressively until
|
---|
| 247 | the UAV lift up */
|
---|
| 248 | Stabilized, /*!< the uav is actively maintaining its altitude */
|
---|
| 249 | StartLanding, /*!< landing initiated. Altitude is required to reach the
|
---|
| 250 | landing altitude (0 by default) */
|
---|
| 251 | FinishLanding /*!< motors are gradually stopped */
|
---|
| 252 | };
|
---|
| 253 | AltitudeState_t altitudeState;
|
---|
| 254 | void ProcessAltitudeFiniteStateMachine();
|
---|
| 255 | void ComputeReferenceAltitude(float &refAltitude, float &refVerticalVelocity);
|
---|
[7] | 256 |
|
---|
[15] | 257 | float groundAltitude; // effective altitude when the uav leaves the ground
|
---|
| 258 | float currentAltitude, currentVerticalSpeed;
|
---|
[7] | 259 |
|
---|
[15] | 260 | bool failSafeMode;
|
---|
| 261 | void SecurityCheck(void);
|
---|
| 262 | void MandatorySecurityCheck(void);
|
---|
| 263 | void CheckJoystick();
|
---|
| 264 | void GenericCheckJoystick();
|
---|
| 265 | void CheckPushButton(void);
|
---|
| 266 | void GenericCheckPushButton(void);
|
---|
| 267 | void Run(void);
|
---|
| 268 | void StopMotors(void);
|
---|
[45] | 269 | bool IsValuePossible(float value,std::string desc);
|
---|
[7] | 270 |
|
---|
[313] | 271 | Uav *uav;
|
---|
| 272 | MetaDualShock3 *joy;
|
---|
[38] | 273 | sensor::TargetController *controller;
|
---|
[7] | 274 |
|
---|
[15] | 275 | core::Quaternion currentQuaternion;
|
---|
[167] | 276 | core::Vector3Df currentAngularSpeed;
|
---|
[7] | 277 |
|
---|
[15] | 278 | const core::AhrsData *ComputeReferenceOrientation(void);
|
---|
[7] | 279 |
|
---|
[15] | 280 | void ComputeOrientation(void);
|
---|
| 281 | void ComputeAltitude(void);
|
---|
[7] | 282 |
|
---|
[15] | 283 | void ComputeTorques(void);
|
---|
| 284 | core::Euler currentTorques, savedDefaultTorques;
|
---|
| 285 | bool needToComputeDefaultTorques;
|
---|
[7] | 286 |
|
---|
[15] | 287 | void ComputeThrust(void);
|
---|
| 288 | float currentThrust, savedDefaultThrust;
|
---|
| 289 | bool needToComputeDefaultThrust;
|
---|
[7] | 290 |
|
---|
[15] | 291 | gui::PushButton *button_kill, *button_take_off, *button_land,
|
---|
| 292 | *button_start_log, *button_stop_log;
|
---|
| 293 | gui::GridLayout *buttonslayout;
|
---|
| 294 | gui::DoubleSpinBox *desiredTakeoffAltitude, *desiredLandingAltitude;
|
---|
| 295 | AltitudeMode_t altitudeMode;
|
---|
| 296 | OrientationMode_t orientationMode;
|
---|
| 297 | ThrustMode_t thrustMode;
|
---|
| 298 | TorqueMode_t torqueMode;
|
---|
| 299 | bool flagBatteryLow;
|
---|
| 300 | bool flagConnectionLost;
|
---|
[38] | 301 | bool flagCriticalSensorLost;
|
---|
[15] | 302 | bool flagZTrajectoryFinished;
|
---|
[38] | 303 | bool safeToFly;
|
---|
[15] | 304 | filter::NestedSat *uRoll, *uPitch;
|
---|
| 305 | filter::Pid *uYaw;
|
---|
| 306 | filter::PidThrust *uZ;
|
---|
| 307 | filter::TrajectoryGenerator1D *altitudeTrajectory;
|
---|
[7] | 308 | };
|
---|
| 309 | };
|
---|
| 310 | };
|
---|
| 311 | #endif // UAVSTATEMACHINE_H
|
---|