2 ******************************************************************************
4 * @file VtolVelocityController.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
6 * @brief Velocity roam controller for vtols
7 * @see The GNU Public License (GPL) Version 3
9 *****************************************************************************/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <openpilot.h>
32 #include <CoordinateConversions.h>
33 #include <sin_lookup.h>
34 #include <pathdesired.h>
37 #include <sanitycheck.h>
39 #include <vtolpathfollowersettings.h>
40 #include <flightmodesettings.h>
41 #include <pathstatus.h>
42 #include <positionstate.h>
43 #include <velocitystate.h>
44 #include <velocitydesired.h>
45 #include <stabilizationdesired.h>
46 #include <attitudestate.h>
47 #include <flightstatus.h>
48 #include <manualcontrolcommand.h>
49 #include <systemsettings.h>
50 #include <stabilizationbank.h>
51 #include <stabilizationdesired.h>
52 #include <pathsummary.h>
56 #include "vtolvelocitycontroller.h"
57 #include "pidcontrolne.h"
61 // pointer to a singleton instance
62 VtolVelocityController
*VtolVelocityController::p_inst
= 0;
64 VtolVelocityController::VtolVelocityController()
65 : vtolPathFollowerSettings(0), mActive(false)
68 // Called when mode first engaged
69 void VtolVelocityController::Activate(void)
78 uint8_t VtolVelocityController::IsActive(void)
83 uint8_t VtolVelocityController::Mode(void)
85 return PATHDESIRED_MODE_VELOCITY
;
88 void VtolVelocityController::ObjectiveUpdated(void)
90 controlNE
.UpdateVelocitySetpoint(pathDesired
->ModeParameters
[PATHDESIRED_MODEPARAMETER_VELOCITY_VELOCITYVECTOR_NORTH
],
91 pathDesired
->ModeParameters
[PATHDESIRED_MODEPARAMETER_VELOCITY_VELOCITYVECTOR_EAST
]);
94 void VtolVelocityController::Deactivate(void)
98 controlNE
.Deactivate();
102 void VtolVelocityController::SettingsUpdated(void)
104 const float dT
= vtolPathFollowerSettings
->UpdatePeriod
/ 1000.0f
;
106 controlNE
.UpdateParameters(vtolPathFollowerSettings
->VelocityRoamHorizontalVelPID
.Kp
,
107 vtolPathFollowerSettings
->VelocityRoamHorizontalVelPID
.Ki
,
108 vtolPathFollowerSettings
->VelocityRoamHorizontalVelPID
.Kd
,
109 vtolPathFollowerSettings
->VelocityRoamHorizontalVelPID
.Beta
,
111 vtolPathFollowerSettings
->HorizontalVelMax
);
114 controlNE
.UpdatePositionalParameters(vtolPathFollowerSettings
->HorizontalPosP
);
115 controlNE
.UpdateCommandParameters(-vtolPathFollowerSettings
->VelocityRoamMaxRollPitch
, vtolPathFollowerSettings
->VelocityRoamMaxRollPitch
, vtolPathFollowerSettings
->VelocityFeedforward
);
118 int32_t VtolVelocityController::Initialize(VtolPathFollowerSettingsData
*ptr_vtolPathFollowerSettings
)
120 PIOS_Assert(ptr_vtolPathFollowerSettings
);
122 vtolPathFollowerSettings
= ptr_vtolPathFollowerSettings
;
127 void VtolVelocityController::UpdateVelocityDesired()
129 VelocityStateData velocityState
;
131 VelocityStateGet(&velocityState
);
132 VelocityDesiredData velocityDesired
;
134 controlNE
.UpdateVelocityState(velocityState
.North
, velocityState
.East
);
136 velocityDesired
.Down
= 0.0f
;
138 controlNE
.GetVelocityDesired(&north
, &east
);
139 velocityDesired
.North
= north
;
140 velocityDesired
.East
= east
;
143 pathStatus
->error
= 0.0f
;
144 pathStatus
->fractional_progress
= 0.0f
;
145 pathStatus
->path_direction_north
= velocityDesired
.North
;
146 pathStatus
->path_direction_east
= velocityDesired
.East
;
147 pathStatus
->path_direction_down
= velocityDesired
.Down
;
149 pathStatus
->correction_direction_north
= velocityDesired
.North
- velocityState
.North
;
150 pathStatus
->correction_direction_east
= velocityDesired
.East
- velocityState
.East
;
151 pathStatus
->correction_direction_down
= 0.0f
;
153 VelocityDesiredSet(&velocityDesired
);
156 int8_t VtolVelocityController::UpdateStabilizationDesired(__attribute__((unused
)) bool yaw_attitude
, __attribute__((unused
)) float yaw_direction
)
159 StabilizationDesiredData stabDesired
;
160 AttitudeStateData attitudeState
;
161 StabilizationBankData stabSettings
;
165 StabilizationDesiredGet(&stabDesired
);
166 AttitudeStateGet(&attitudeState
);
167 StabilizationBankGet(&stabSettings
);
169 controlNE
.GetNECommand(&northCommand
, &eastCommand
);
171 float angle_radians
= DEG2RAD(attitudeState
.Yaw
);
172 float cos_angle
= cosf(angle_radians
);
173 float sine_angle
= sinf(angle_radians
);
174 float maxPitch
= vtolPathFollowerSettings
->VelocityRoamMaxRollPitch
;
175 stabDesired
.StabilizationMode
.Pitch
= STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE
;
176 stabDesired
.Pitch
= boundf(-northCommand
* cos_angle
- eastCommand
* sine_angle
, -maxPitch
, maxPitch
);
177 stabDesired
.StabilizationMode
.Roll
= STABILIZATIONDESIRED_STABILIZATIONMODE_ATTITUDE
;
178 stabDesired
.Roll
= boundf(-northCommand
* sine_angle
+ eastCommand
* cos_angle
, -maxPitch
, maxPitch
);
180 ManualControlCommandData manualControl
;
181 ManualControlCommandGet(&manualControl
);
183 stabDesired
.StabilizationMode
.Yaw
= STABILIZATIONDESIRED_STABILIZATIONMODE_AXISLOCK
;
184 stabDesired
.Yaw
= stabSettings
.MaximumRate
.Yaw
* manualControl
.Yaw
;
186 // default thrust mode to altvario
187 stabDesired
.StabilizationMode
.Thrust
= STABILIZATIONDESIRED_STABILIZATIONMODE_ALTITUDEVARIO
;
188 stabDesired
.Thrust
= manualControl
.Thrust
;
190 StabilizationDesiredSet(&stabDesired
);
195 void VtolVelocityController::UpdateAutoPilot()
197 UpdateVelocityDesired();
199 bool yaw_attitude
= false;
202 int8_t result
= UpdateStabilizationDesired(yaw_attitude
, yaw
);
204 AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE
, SYSTEMALARMS_ALARM_OK
);
206 pathStatus
->Status
= PATHSTATUS_STATUS_CRITICAL
;
207 AlarmsSet(SYSTEMALARMS_ALARM_GUIDANCE
, SYSTEMALARMS_ALARM_WARNING
);
210 PathStatusSet(pathStatus
);