dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vtolpathfollower.c
Go to the documentation of this file.
1 
19 /*
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful, but
26  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
27  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28  * for more details.
29  *
30  * You should have received a copy of the GNU General Public License along
31  * with this program; if not, see <http://www.gnu.org/licenses/>
32  *
33  * Additional note on redistribution: The copyright and license notices above
34  * must be maintained in each individual source file that is a derivative work
35  * of this source file; otherwise redistribution is prohibited.
36  */
37 
38 #include "openpilot.h"
39 #include "physical_constants.h"
40 #include "misc_math.h"
41 #include "paths.h"
42 #include "pid.h"
43 #include "pios_thread.h"
44 
45 #include "vtol_follower_priv.h"
46 
47 #include "acceldesired.h"
48 #include "altitudeholdsettings.h"
49 #include "altitudeholdstate.h"
50 #include "modulesettings.h"
51 #include "pathdesired.h" // object that will be updated by the module
52 #include "flightstatus.h"
53 #include "pathstatus.h"
54 #include "stabilizationdesired.h"
55 #include "systemsettings.h"
56 #include "velocitydesired.h"
57 #include "vtolpathfollowersettings.h"
58 #include "vtolpathfollowerstatus.h"
59 #include "coordinate_conversions.h"
60 
61 VtolPathFollowerSettingsData vtol_guidanceSettings;
62 float vtol_dT = 0.050f;
63 
64 // Private constants
65 #define MAX_QUEUE_SIZE 4
66 #define STACK_SIZE_BYTES 1548
67 #define TASK_PRIORITY PIOS_THREAD_PRIO_NORMAL
68 
69 // Private types
70 
71 // Private variables
72 static struct pios_thread *pathfollowerTaskHandle;
73 static struct pios_queue *queue;
74 
75 // Private functions
76 static void vtolPathFollowerTask(void *parameters);
77 static bool module_enabled = false;
78 
84 {
85  if (module_enabled) {
86  // Create object queue
88  FlightStatusConnectQueue(queue);
89 
90  // Start main task
92  TaskMonitorAdd(TASKINFO_RUNNING_PATHFOLLOWER, pathfollowerTaskHandle);
93  }
94 
95  return 0;
96 }
97 
103 {
104 #ifdef MODULE_VtolPathFollower_BUILTIN
105  module_enabled = true;
106 #else
107  uint8_t module_state[MODULESETTINGS_ADMINSTATE_NUMELEM];
108  ModuleSettingsAdminStateGet(module_state);
109  if (module_state[MODULESETTINGS_ADMINSTATE_VTOLPATHFOLLOWER] == MODULESETTINGS_ADMINSTATE_ENABLED) {
110  module_enabled = true;
111  } else {
112  module_enabled = false;
113  return -1;
114  }
115 #endif
116 
118  module_enabled = false;
119  return -1;
120  }
121 
123  module_enabled = false;
124  return -1;
125  }
126 
127  if (AltitudeHoldSettingsInitialize() == -1 || VtolPathFollowerSettingsInitialize() == -1) {
128  module_enabled = false;
129  return -1;
130  }
131 
132  if (AccelDesiredInitialize() == -1 \
133  || AltitudeHoldStateInitialize() == -1 \
134  || PathDesiredInitialize() == -1 \
135  || PathStatusInitialize() == -1 \
136  || VelocityDesiredInitialize() == -1 \
137  || VtolPathFollowerStatusInitialize() == -1 ) {
138 
139  module_enabled = false;
140  return -1;
141  }
142 
143  return 0;
144 }
145 
147 
148 extern struct pid vtol_pids[VTOL_PID_NUM];
149 
150 volatile bool settings_updated = true;
151 
155 static void vtolPathFollowerTask(void *parameters)
156 {
157  SystemSettingsData systemSettings;
158  FlightStatusData flightStatus;
159 
160  VtolPathFollowerSettingsConnectCallbackCtx(UAVObjCbSetFlag,
162  AltitudeHoldSettingsConnectCallbackCtx(UAVObjCbSetFlag,
164 
165  // Main task loop
166  while (1) {
167  if (settings_updated) {
169  }
170 
171  SystemSettingsGet(&systemSettings);
172  if ( (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_VTOL) &&
173  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_QUADP) &&
174  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_QUADX) &&
175  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_HEXA) &&
176  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_HEXAX) &&
177  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_HEXACOAX) &&
178  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_OCTO) &&
179  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_OCTOV) &&
180  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_OCTOCOAXP) &&
181  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_OCTOCOAXX) &&
182  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_TRI) &&
183  (systemSettings.AirframeType != SYSTEMSETTINGS_AIRFRAMETYPE_HELICP) )
184  {
185  // This should be a critical alarm since the system will not attempt to
186  // control in this situation.
187  AlarmsSet(SYSTEMALARMS_ALARM_PATHFOLLOWER,SYSTEMALARMS_ALARM_CRITICAL);
188  PIOS_Thread_Sleep(1000);
189  continue;
190  }
191 
192  // Make sure when flight mode toggles, to immediately update the path
193  UAVObjEvent ev;
194  PIOS_Queue_Receive(queue, &ev, vtol_guidanceSettings.UpdatePeriod);
195 
196  static uint8_t last_flight_mode;
197  FlightStatusGet(&flightStatus);
198 
199  static bool fsm_running = false;
200 
201  if (flightStatus.FlightMode != last_flight_mode) {
202  // The mode has changed
203 
204  last_flight_mode = flightStatus.FlightMode;
205 
206  switch(flightStatus.FlightMode) {
207  case FLIGHTSTATUS_FLIGHTMODE_RETURNTOHOME:
209  fsm_running = true;
210  break;
211  case FLIGHTSTATUS_FLIGHTMODE_POSITIONHOLD:
213  fsm_running = true;
214  break;
215  case FLIGHTSTATUS_FLIGHTMODE_PATHPLANNER:
216  case FLIGHTSTATUS_FLIGHTMODE_TABLETCONTROL:
217  // TODO: currently when in this mode the follower just
218  // attempts to fly the path segments blindly which means
219  // the FSM cannot be utilized in a meaningful way. It might
220  // be better when flying in path planner mode for the path
221  // planner to specify the goals in PathDesired so things like
222  // RTH can be used. However, for now this isn't critical.
224  fsm_running = true;
225  break;
226  default:
228  fsm_running = false;
229  break;
230  }
231  }
232 
233  if (fsm_running) {
235  } else {
236  for (uint32_t i = 0; i < VTOL_PID_NUM; i++)
237  pid_zero(&vtol_pids[i]);
238 
239  // Track throttle before engaging this mode. Cheap system ident
240  StabilizationDesiredThrustGet(&vtol_pids[DOWN_VELOCITY].iAccumulator);
241  // Note the negative sign because this is the accumulation for down.
243  }
244 
245  AlarmsClear(SYSTEMALARMS_ALARM_PATHFOLLOWER);
246 
247  }
248 }
249 
int32_t vtol_follower_fsm_update()
int32_t vtol_follower_fsm_activate_goal(enum vtol_goals new_goal)
float vtol_dT
struct pios_queue * PIOS_Queue_Create(size_t queue_length, size_t item_size)
Definition: pios_queue.c:47
int32_t VtolPathFollowerStart()
#define MAX_QUEUE_SIZE
VtolPathFollowerSettingsData vtol_guidanceSettings
float iAccumulator
Definition: pid.h:50
void vtol_follower_control_settings_updated()
void UAVObjCbSetFlag(const UAVObjEvent *objEv, void *ctx, void *obj, int len)
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
Definition: alarms.c:97
bool PIOS_Queue_Receive(struct pios_queue *queuep, void *itemp, uint32_t timeout_ms)
Definition: pios_queue.c:213
volatile bool settings_updated
struct pid vtol_pids[VTOL_PID_NUM]
MODULE_INITCALL(VtolPathFollowerInitialize, VtolPathFollowerStart)
static struct pios_queue * queue
static bool module_enabled
Path calculation library with common API.
bool PIOS_SENSORS_IsRegistered(enum pios_sensor_type type)
Checks if a sensor type is registered with the PIOS_SENSORS interface.
Definition: pios_sensors.c:88
static void vtolPathFollowerTask(void *parameters)
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
Definition: pios_thread.c:89
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
Definition: taskmonitor.c:67
uint8_t i
Definition: msp_messages.h:97
Includes for the internal methods.
void pid_zero(struct pid *pid)
Definition: pid.c:252
void PIOS_Thread_Sleep(uint32_t time_ms)
Definition: pios_thread.c:229
Header for Coordinate conversions library in coordinate_conversions.c.
#define TASK_PRIORITY
#define STACK_SIZE_BYTES
Includes PiOS and core architecture components.
int32_t AlarmsClear(SystemAlarmsAlarmElem alarm)
Definition: alarms.c:171
int32_t VtolPathFollowerInitialize()
static struct pios_thread * pathfollowerTaskHandle
Definition: pid.h:42
PID Control algorithms.