source: flair-src/trunk/lib/FlairMeta/src/UavStateMachine.h@ 20

Last change on this file since 20 was 15, checked in by Bayard Gildas, 9 years ago

sources reformatted with flair-format-dir script

File size: 8.2 KB
Line 
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
27namespace flair {
28namespace core {
29class FrameworkManager;
30class AhrsData;
31class io_data;
32}
33namespace gui {
34class PushButton;
35class GridLayout;
36class Tab;
37class DoubleSpinBox;
38}
39namespace filter {
40class ControlLaw;
41class NestedSat;
42class Pid;
43class PidThrust;
44class TrajectoryGenerator1D;
45}
46namespace sensor {
47class TargetController;
48}
49namespace meta {
50class MetaDualShock3;
51class Uav;
52}
53}
54
55namespace flair {
56namespace meta {
57
58/*! \class UavStateMachine
59*
60* \brief State machine for UAV
61*
62* thread synchronized with ahrs, unless a period is set with SetPeriodUS or
63*SetPeriodMS
64*/
65
66class UavStateMachine : public core::Thread {
67protected:
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
103 UavStateMachine(meta::Uav *uav, uint16_t ds3Port = 20000);
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 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);
120
121 virtual const core::AhrsData *GetOrientation(void) const;
122 const core::AhrsData *GetDefaultOrientation(void) const;
123
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!
128
129 gui::GridLayout *GetButtonsLayout(void) const;
130 virtual void ExtraSecurityCheck(void){};
131 virtual void ExtraCheckJoystick(void){};
132 virtual void ExtraCheckPushButton(void){};
133
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);
141
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);
153
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);
165
166 /*!
167 * \brief Get Torques
168 *
169 * \return torques current torques
170 */
171 // const core::Euler &GetTorques() const;
172
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);
184
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);
196
197 /*!
198 * \brief Get Thrust
199 *
200 * \return current thrust
201 */
202 // float GetThrust() const;
203
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);
215
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);
227
228 const sensor::TargetController *GetJoystick(void) const;
229
230 gui::Tab *setupLawTab, *graphLawTab;
231
232private:
233 /*!
234 \enum AltitudeState_t
235 \brief States of the altitude state machine
236 */
237 enum class AltitudeState_t {
238 Stopped, /*!< the uav motors are stopped */
239 TakingOff, /*!< take off initiated. Motors accelerate progressively until
240 the UAV lift up */
241 Stabilized, /*!< the uav is actively maintaining its altitude */
242 StartLanding, /*!< landing initiated. Altitude is required to reach the
243 landing altitude (0 by default) */
244 FinishLanding /*!< motors are gradually stopped */
245 };
246 AltitudeState_t altitudeState;
247 void ProcessAltitudeFiniteStateMachine();
248 void ComputeReferenceAltitude(float &refAltitude, float &refVerticalVelocity);
249
250 float groundAltitude; // effective altitude when the uav leaves the ground
251 float currentAltitude, currentVerticalSpeed;
252
253 bool failSafeMode;
254 void SecurityCheck(void);
255 void MandatorySecurityCheck(void);
256 void CheckJoystick();
257 void GenericCheckJoystick();
258 void CheckPushButton(void);
259 void GenericCheckPushButton(void);
260 void Run(void);
261 void StopMotors(void);
262
263 meta::Uav *uav;
264
265 core::Quaternion currentQuaternion;
266 core::Vector3D currentAngularSpeed;
267
268 const core::AhrsData *ComputeReferenceOrientation(void);
269
270 void ComputeOrientation(void);
271 void ComputeAltitude(void);
272
273 void ComputeTorques(void);
274 core::Euler currentTorques, savedDefaultTorques;
275 bool needToComputeDefaultTorques;
276
277 void ComputeThrust(void);
278 float currentThrust, savedDefaultThrust;
279 bool needToComputeDefaultThrust;
280
281 gui::PushButton *button_kill, *button_take_off, *button_land,
282 *button_start_log, *button_stop_log;
283 gui::GridLayout *buttonslayout;
284 gui::DoubleSpinBox *desiredTakeoffAltitude, *desiredLandingAltitude;
285 AltitudeMode_t altitudeMode;
286 OrientationMode_t orientationMode;
287 ThrustMode_t thrustMode;
288 TorqueMode_t torqueMode;
289 bool flagBatteryLow;
290 bool flagConnectionLost;
291 bool flagZTrajectoryFinished;
292 filter::NestedSat *uRoll, *uPitch;
293 filter::Pid *uYaw;
294 filter::PidThrust *uZ;
295
296 MetaDualShock3 *joy;
297 filter::TrajectoryGenerator1D *altitudeTrajectory;
298};
299};
300};
301#endif // UAVSTATEMACHINE_H
Note: See TracBrowser for help on using the repository browser.