Flair
Framework Libre Air
UavStateMachine.h
1 // %flair:license{
2 // This file is part of the Flair framework distributed under the
3 // CECILL-C License, Version 1.0.
4 // %flair:license}
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 {
28 namespace core {
29 class FrameworkManager;
30 class AhrsData;
31 class io_data;
32 }
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 }
54 
55 namespace flair {
56 namespace meta {
57 
66 class UavStateMachine : public core::Thread {
67 protected:
68  enum class AltitudeMode_t { Manual, Custom };
69  const AltitudeMode_t &GetAltitudeMode(void) const { return altitudeMode; }
70  bool SetAltitudeMode(const AltitudeMode_t &altitudeMode);
71 
72  // uses TrajectoryGenerator1D *altitudeTrajectory to go to desiredAltitude
73  // available in mode AltitudeMode_t::Manual
74  // return true if goto is possible
75  bool GotoAltitude(float desiredAltitude);
76 
77  enum class OrientationMode_t { Manual, Custom };
78  const OrientationMode_t &GetOrientationMode(void) const {
79  return orientationMode;
80  }
81  bool SetOrientationMode(const OrientationMode_t &orientationMode);
82 
83  enum class ThrustMode_t { Default, Custom };
84  const ThrustMode_t &GetThrustMode() const { return thrustMode; }
85  bool SetThrustMode(const ThrustMode_t &thrustMode);
86 
87  enum class TorqueMode_t { Default, Custom };
88  const TorqueMode_t &GetTorqueMode(void) const { return torqueMode; }
89  bool SetTorqueMode(const TorqueMode_t &torqueMode);
90 
91  enum class Event_t {
92  EnteringFailSafeMode,
93  EnteringControlLoop,
94  StartLanding,
95  FinishLanding,
96  Stopped,
97  TakingOff,
98  EmergencyStop,
99  Stabilized, // as soon as uav is 3cm far from the ground
100  ZTrajectoryFinished,
101  };
102 
104  ~UavStateMachine();
105 
106  const core::Quaternion &GetCurrentQuaternion(void) const;
107 
108  const core::Vector3D &GetCurrentAngularSpeed(void) const;
109 
110  const meta::Uav *GetUav(void) const;
111 
112  void Land(void);
113  void EmergencyLand(void);
114  void TakeOff(void);
115  void EmergencyStop(void);
117 
120  virtual void SignalEvent(Event_t event);
121 
122  virtual const core::AhrsData *GetOrientation(void) const;
123  const core::AhrsData *GetDefaultOrientation(void) const;
124 
125  virtual void AltitudeValues(float &z, float &dz) const; // in uav coordinate!
126  void EnterFailSafeMode(void);
127  bool ExitFailSafeMode(void);
128  void FailSafeAltitudeValues(float &z, float &dz) const; // in uav coordinate!
129 
130  gui::GridLayout *GetButtonsLayout(void) const;
131  virtual void ExtraSecurityCheck(void){};
132  virtual void ExtraCheckJoystick(void){};
133  virtual void ExtraCheckPushButton(void){};
134 
135  void GetDefaultReferenceAltitude(float &refAltitude,
136  float &refVerticalVelocity);
137  virtual void GetReferenceAltitude(float &refAltitude,
138  float &refVerticalVelocity);
139  // float GetDefaultThrustOffset(void);
140  const core::AhrsData *GetDefaultReferenceOrientation(void) const;
141  virtual const core::AhrsData *GetReferenceOrientation(void);
142 
153  virtual void ComputeCustomTorques(core::Euler &torques);
154 
165  void ComputeDefaultTorques(core::Euler &torques);
166 
172  // const core::Euler &GetTorques() const;
173 
184  virtual float ComputeCustomThrust(void);
185 
196  float ComputeDefaultThrust(void);
197 
203  // float GetThrust() const;
204 
215  void AddDeviceToControlLawLog(const core::IODevice *device);
216 
227  void AddDataToControlLawLog(const core::io_data *data);
228 
229  const sensor::TargetController *GetJoystick(void) const;
230 
231  gui::Tab *setupLawTab, *graphLawTab;
232 
233 private:
238  enum class AltitudeState_t {
239  Stopped,
240  TakingOff,
242  Stabilized,
243  StartLanding,
246  };
247  AltitudeState_t altitudeState;
248  void ProcessAltitudeFiniteStateMachine();
249  void ComputeReferenceAltitude(float &refAltitude, float &refVerticalVelocity);
250 
251  float groundAltitude; // effective altitude when the uav leaves the ground
252  float currentAltitude, currentVerticalSpeed;
253 
254  bool failSafeMode;
255  void SecurityCheck(void);
256  void MandatorySecurityCheck(void);
257  void CheckJoystick();
258  void GenericCheckJoystick();
259  void CheckPushButton(void);
260  void GenericCheckPushButton(void);
261  void Run(void);
262  void StopMotors(void);
263  bool IsValuePossible(float value,std::string desc);
264 
265  meta::Uav *uav;
266  sensor::TargetController *controller;
267 
268  core::Quaternion currentQuaternion;
269  core::Vector3D currentAngularSpeed;
270 
271  const core::AhrsData *ComputeReferenceOrientation(void);
272 
273  void ComputeOrientation(void);
274  void ComputeAltitude(void);
275 
276  void ComputeTorques(void);
277  core::Euler currentTorques, savedDefaultTorques;
278  bool needToComputeDefaultTorques;
279 
280  void ComputeThrust(void);
281  float currentThrust, savedDefaultThrust;
282  bool needToComputeDefaultThrust;
283 
284  gui::PushButton *button_kill, *button_take_off, *button_land,
285  *button_start_log, *button_stop_log;
286  gui::GridLayout *buttonslayout;
287  gui::DoubleSpinBox *desiredTakeoffAltitude, *desiredLandingAltitude;
288  AltitudeMode_t altitudeMode;
289  OrientationMode_t orientationMode;
290  ThrustMode_t thrustMode;
291  TorqueMode_t torqueMode;
292  bool flagBatteryLow;
293  bool flagConnectionLost;
294  bool flagCriticalSensorLost;
295  bool flagZTrajectoryFinished;
296  bool safeToFly;
297  filter::NestedSat *uRoll, *uPitch;
298  filter::Pid *uYaw;
299  filter::PidThrust *uZ;
300 
301  MetaDualShock3 *joy;
302  filter::TrajectoryGenerator1D *altitudeTrajectory;
303 };
304 };
305 };
306 #endif // UAVSTATEMACHINE_H
Abstract class for data types.
Definition: io_data.h:94
virtual void SignalEvent(Event_t event)
Used to signal an event.
Classe int├ęgrant la manette MetaDualShock3.
Definition: MetaDualShock3.h:38
Abstract class for input/ouput system.
Definition: IODevice.h:44
namespace of the flair Framework
Definition: Ahrs.h:19
Class defining a 3D vector.
Definition: Vector3D.h:28
Class defining euler angles.
Definition: Euler.h:27
float ComputeDefaultThrust(void)
Compute Default Thrust.
Class defining a 2D vector.
Class displaying a QGridLayout on the ground station.
Definition: GridLayout.h:27
Class defining a Pid for Thrust. This Pid as an extra offset for compensating gravity.
Definition: PidThrust.h:33
Class defining AHRS datas.
Definition: AhrsData.h:30
Class defining a 3D vector.
State machine for UAV.
Definition: UavStateMachine.h:66
Base Class for target side remote controls.
Definition: TargetController.h:49
Class generating a trajectory in 1D.
Definition: TrajectoryGenerator1D.h:47
virtual float ComputeCustomThrust(void)
Get Torques.
void AddDeviceToControlLawLog(const core::IODevice *device)
Get Thrust.
Class displaying a QPushButton on the ground station.
Definition: PushButton.h:28
virtual void ComputeCustomTorques(core::Euler &torques)
Compute Custom Torques.
Class defining a PID.
Definition: Pid.h:32
Abstract class for a thread.
Class defining a quaternion.
Class defining a PID with saturations.
Definition: NestedSat.h:43
Abstract class for a thread.
Definition: Thread.h:38
Class displaying a QDoubleSpinBox on the ground station.
Definition: DoubleSpinBox.h:28
Base class to construct sensors/actuators depending on uav type.
Definition: Uav.h:47
void AddDataToControlLawLog(const core::io_data *data)
Add an io_data to the control law logs.
void ComputeDefaultTorques(core::Euler &torques)
Compute Default Torques.
Class defining a quaternion.
Definition: Quaternion.h:25
Class displaying a QTab on the ground station.
Definition: Tab.h:29
Class defining euler angles.