2 ******************************************************************************
4 * @file grounddrivecontroller.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
6 * @brief Ground drive controller
7 * the required PathDesired LAND mode.
8 * @see The GNU Public License (GPL) Version 3
10 *****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <openpilot.h>
32 #include <CoordinateConversions.h>
33 #include <sin_lookup.h>
34 #include <pathdesired.h>
37 #include <sanitycheck.h>
39 #include <homelocation.h>
40 #include <accelstate.h>
41 #include <groundpathfollowersettings.h>
42 #include <flightstatus.h>
43 #include <flightmodesettings.h>
44 #include <pathstatus.h>
45 #include <positionstate.h>
46 #include <velocitystate.h>
47 #include <velocitydesired.h>
48 #include <stabilizationdesired.h>
49 #include <airspeedstate.h>
50 #include <attitudestate.h>
51 #include <takeofflocation.h>
52 #include <poilocation.h>
53 #include <manualcontrolcommand.h>
54 #include <systemsettings.h>
55 #include <stabilizationbank.h>
56 #include <stabilizationdesired.h>
57 #include <pathsummary.h>
58 #include <statusgrounddrive.h>
62 #include "grounddrivecontroller.h"
66 // pointer to a singleton instance
67 GroundDriveController
*GroundDriveController::p_inst
= 0;
69 GroundDriveController::GroundDriveController()
70 : groundSettings(0), mActive(false), mMode(0)
73 // Called when mode first engaged
74 void GroundDriveController::Activate(void)
80 mMode
= pathDesired
->Mode
;
84 uint8_t GroundDriveController::IsActive(void)
89 uint8_t GroundDriveController::Mode(void)
94 // Objective updated in pathdesired
95 void GroundDriveController::ObjectiveUpdated(void)
98 void GroundDriveController::Deactivate(void)
102 controlNE
.Deactivate();
107 void GroundDriveController::SettingsUpdated(void)
109 const float dT
= groundSettings
->UpdatePeriod
/ 1000.0f
;
111 controlNE
.UpdatePositionalParameters(groundSettings
->HorizontalPosP
);
112 controlNE
.UpdateParameters(groundSettings
->SpeedPI
.Kp
,
113 groundSettings
->SpeedPI
.Ki
,
114 groundSettings
->SpeedPI
.Kd
,
115 groundSettings
->SpeedPI
.Beta
,
117 groundSettings
->HorizontalVelMax
);
120 // max/min are NE command values equivalent to thrust but must be symmetrical as this is NE not forward/reverse.
121 controlNE
.UpdateCommandParameters(-groundSettings
->ThrustLimit
.Max
, groundSettings
->ThrustLimit
.Max
, groundSettings
->VelocityFeedForward
);
125 * Initialise the module, called on startup
126 * \returns 0 on success or -1 if initialisation failed
128 int32_t GroundDriveController::Initialize(GroundPathFollowerSettingsData
*ptr_groundSettings
)
130 PIOS_Assert(ptr_groundSettings
);
132 groundSettings
= ptr_groundSettings
;
137 void GroundDriveController::UpdateAutoPilot()
139 uint8_t result
= updateAutoPilotGround();
142 AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE
, SYSTEMALARMS_ALARM_OK
);
144 pathStatus
->Status
= PATHSTATUS_STATUS_CRITICAL
;
145 AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE
, SYSTEMALARMS_ALARM_WARNING
);
148 PathStatusSet(pathStatus
);
152 * fixed wing autopilot:
154 * 1. update path velocity for limited motion crafts
155 * 2. update attitude according to default fixed wing pathfollower algorithm
157 uint8_t GroundDriveController::updateAutoPilotGround()
159 updatePathVelocity(groundSettings
->CourseFeedForward
);
160 return updateGroundDesiredAttitude();
164 * Compute desired velocity from the current position and path
166 void GroundDriveController::updatePathVelocity(float kFF
)
168 PositionStateData positionState
;
170 PositionStateGet(&positionState
);
171 VelocityStateData velocityState
;
172 VelocityStateGet(&velocityState
);
173 VelocityDesiredData velocityDesired
;
174 controlNE
.UpdateVelocityState(velocityState
.North
, velocityState
.East
);
176 // look ahead kFF seconds
177 float cur
[3] = { positionState
.North
+ (velocityState
.North
* kFF
),
178 positionState
.East
+ (velocityState
.East
* kFF
),
179 positionState
.Down
+ (velocityState
.Down
* kFF
) };
180 struct path_status progress
;
181 path_progress(pathDesired
, cur
, &progress
, false);
183 // GOTOENDPOINT: correction_vector is distance array to endpoint, path_vector is velocity vector
184 // FOLLOWVECTOR: correct_vector is distance to vector path, path_vector is the desired velocity vector
186 // Calculate the desired velocity from the lateral vector path errors (correct_vector) and the desired velocity vector (path_vector)
187 controlNE
.ControlPositionWithPath(&progress
);
189 controlNE
.GetVelocityDesired(&north
, &east
);
190 velocityDesired
.North
= north
;
191 velocityDesired
.East
= east
;
192 velocityDesired
.Down
= 0.0f
;
195 pathStatus
->error
= progress
.error
;
196 pathStatus
->fractional_progress
= progress
.fractional_progress
;
197 // FOLLOWVECTOR: desired velocity vector
198 pathStatus
->path_direction_north
= progress
.path_vector
[0];
199 pathStatus
->path_direction_east
= progress
.path_vector
[1];
200 pathStatus
->path_direction_down
= progress
.path_vector
[2];
202 // FOLLOWVECTOR: correction distance to vector path
203 pathStatus
->correction_direction_north
= progress
.correction_vector
[0];
204 pathStatus
->correction_direction_east
= progress
.correction_vector
[1];
205 pathStatus
->correction_direction_down
= progress
.correction_vector
[2];
207 VelocityDesiredSet(&velocityDesired
);
211 * Compute desired attitude for ground vehicles
213 uint8_t GroundDriveController::updateGroundDesiredAttitude()
215 StatusGroundDriveData statusGround
;
216 VelocityStateData velocityState
;
218 VelocityStateGet(&velocityState
);
219 AttitudeStateData attitudeState
;
220 AttitudeStateGet(&attitudeState
);
221 statusGround
.State
.Yaw
= attitudeState
.Yaw
;
222 statusGround
.State
.Velocity
= sqrtf(velocityState
.North
* velocityState
.North
+ velocityState
.East
* velocityState
.East
);
224 StabilizationDesiredData stabDesired
;
225 StabilizationDesiredGet(&stabDesired
);
227 // estimate a north/east command value to control the velocity error.
228 // ThrustLimits.Max(+-) limits the range. Think of this as a command unit vector
229 // of the ultimate direction to reduce lateral error and achieve the target direction (desired angle).
230 float northCommand
, eastCommand
;
231 controlNE
.GetNECommand(&northCommand
, &eastCommand
);
233 // Get current vehicle orientation
234 float angle_radians
= DEG2RAD(attitudeState
.Yaw
); // (+-pi)
235 float cos_angle
= cosf(angle_radians
);
236 float sine_angle
= sinf(angle_radians
);
238 float courseCommand
= 0.0f
;
239 float speedCommand
= 0.0f
;
240 float lateralCommand
= boundf(-northCommand
* sine_angle
+ eastCommand
* cos_angle
, -groundSettings
->ThrustLimit
.Max
, groundSettings
->ThrustLimit
.Max
);
241 float forwardCommand
= boundf(northCommand
* cos_angle
+ eastCommand
* sine_angle
, -groundSettings
->ThrustLimit
.Max
, groundSettings
->ThrustLimit
.Max
);
242 // +ve facing correct direction, lateral command should just correct angle,
243 if (forwardCommand
> 0.0f
) {
244 // if +ve forward command, -+ lateralCommand drives steering to manage lateral error and angular error
246 courseCommand
= boundf(lateralCommand
, -1.0f
, 1.0f
);
247 speedCommand
= boundf(forwardCommand
, groundSettings
->ThrustLimit
.SlowForward
, groundSettings
->ThrustLimit
.Max
);
249 // reinstate max thrust
250 controlNE
.UpdateCommandParameters(-groundSettings
->ThrustLimit
.Max
, groundSettings
->ThrustLimit
.Max
, groundSettings
->VelocityFeedForward
);
252 statusGround
.ControlState
= STATUSGROUNDDRIVE_CONTROLSTATE_ONTRACK
;
254 // -ve facing opposite direction, lateral command irrelevant, need to turn to change direction and do so slowly.
256 // Reduce steering angle based on current velocity
257 float steeringReductionFactor
= 1.0f
;
258 if (stabDesired
.Thrust
> 0.3f
) {
259 steeringReductionFactor
= (groundSettings
->HorizontalVelMax
- statusGround
.State
.Velocity
) / groundSettings
->HorizontalVelMax
;
260 steeringReductionFactor
= boundf(steeringReductionFactor
, 0.05f
, 1.0f
);
263 // should we turn left or right?
264 if (lateralCommand
>= 0.1f
) {
265 courseCommand
= 1.0f
* steeringReductionFactor
;
266 statusGround
.ControlState
= STATUSGROUNDDRIVE_CONTROLSTATE_TURNAROUNDRIGHT
;
268 courseCommand
= -1.0f
* steeringReductionFactor
;
269 statusGround
.ControlState
= STATUSGROUNDDRIVE_CONTROLSTATE_TURNAROUNDLEFT
;
272 // Impose limits to slow down.
273 controlNE
.UpdateCommandParameters(-groundSettings
->ThrustLimit
.SlowForward
, groundSettings
->ThrustLimit
.SlowForward
, 0.0f
);
275 speedCommand
= groundSettings
->ThrustLimit
.SlowForward
;
278 stabDesired
.Roll
= 0.0f
;
279 stabDesired
.Pitch
= 0.0f
;
280 stabDesired
.Yaw
= courseCommand
;
282 // Mix yaw into thrust limit TODO
283 stabDesired
.Thrust
= boundf(speedCommand
, groundSettings
->ThrustLimit
.Min
, groundSettings
->ThrustLimit
.Max
);
285 stabDesired
.StabilizationMode
.Roll
= STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL
;
286 stabDesired
.StabilizationMode
.Pitch
= STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL
;
287 stabDesired
.StabilizationMode
.Yaw
= STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL
;
288 stabDesired
.StabilizationMode
.Thrust
= STABILIZATIONDESIRED_STABILIZATIONMODE_MANUAL
;
289 StabilizationDesiredSet(&stabDesired
);
291 statusGround
.NECommand
.North
= northCommand
;
292 statusGround
.NECommand
.East
= eastCommand
;
293 statusGround
.State
.Thrust
= stabDesired
.Thrust
;
294 statusGround
.BodyCommand
.Forward
= forwardCommand
;
295 statusGround
.BodyCommand
.Right
= lateralCommand
;
296 statusGround
.ControlCommand
.Course
= courseCommand
;
297 statusGround
.ControlCommand
.Speed
= speedCommand
;
298 StatusGroundDriveSet(&statusGround
);