update serTcpOpen declaration to fix compile errors (#14113)
[betaflight.git] / src / main / flight / autopilot.c
blob8a41219b5f6b3f6537d5cd9ca0317298ca47ff55
1 /*
2 * This file is part of Betaflight.
4 * Betaflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Betaflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Betaflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <stdbool.h>
21 #include <math.h>
23 #include "platform.h"
24 #include "build/debug.h"
25 #include "common/axis.h"
26 #include "common/filter.h"
27 #include "common/maths.h"
28 #include "common/vector.h"
29 #include "fc/rc.h"
30 #include "fc/runtime_config.h"
32 #include "flight/imu.h"
33 #include "flight/position.h"
34 #include "rx/rx.h"
35 #include "sensors/gyro.h"
37 #include "pg/autopilot.h"
38 #include "autopilot.h"
40 #define ALTITUDE_P_SCALE 0.01f
41 #define ALTITUDE_I_SCALE 0.003f
42 #define ALTITUDE_D_SCALE 0.01f
43 #define ALTITUDE_F_SCALE 0.01f
44 #define POSITION_P_SCALE 0.0012f
45 #define POSITION_I_SCALE 0.0001f
46 #define POSITION_D_SCALE 0.0015f
47 #define POSITION_A_SCALE 0.0008f
48 #define UPSAMPLING_CUTOFF_HZ 5.0f
50 static pidCoefficient_t altitudePidCoeffs;
51 static pidCoefficient_t positionPidCoeffs;
53 static float altitudeI = 0.0f;
54 static float throttleOut = 0.0f;
56 typedef struct efPidAxis_s {
57 bool isStopping;
58 float previousDistance;
59 float previousVelocity;
60 float integral;
61 pt1Filter_t velocityLpf;
62 pt1Filter_t accelerationLpf;
63 } efPidAxis_t;
65 typedef enum {
66 // axes are for ENU system; it is different from current Betaflight code
67 LON = 0, // X, east
68 LAT // Y, north
69 } axisEF_e;
71 typedef struct autopilotState_s {
72 gpsLocation_t targetLocation; // active / current target
73 float sanityCheckDistance;
74 float upsampleLpfGain; // for the Body Frame upsample filter for pitch and roll
75 float vaLpfCutoff; // velocity + acceleration lowpass filter cutoff
76 bool sticksActive;
77 float maxAngle;
78 vector2_t pidSumBF; // pid output, updated on each GPS update, rotated to body frame
79 pt3Filter_t upsampleLpfBF[RP_AXIS_COUNT]; // upsampling filter
80 efPidAxis_t efAxis[EF_AXIS_COUNT];
81 } autopilotState_t;
83 static autopilotState_t ap = {
84 .sanityCheckDistance = 1000.0f,
85 .upsampleLpfGain = 1.0f,
86 .vaLpfCutoff = 1.0f,
87 .sticksActive = false,
90 float autopilotAngle[RP_AXIS_COUNT];
92 static void resetEFAxisFilters(efPidAxis_t* efAxis, const float vaGain)
94 pt1FilterInit(&efAxis->velocityLpf, vaGain);
95 pt1FilterInit(&efAxis->accelerationLpf, vaGain);
98 void resetEFAxisParams(efPidAxis_t *efAxis, const float vaGain)
100 // at start only
101 resetEFAxisFilters(efAxis, vaGain);
102 efAxis->isStopping = true; // Enter starting (deceleration) 'phase'
103 efAxis->integral = 0.0f;
106 static void resetUpsampleFilters(void)
108 for (unsigned i = 0; i < ARRAYLEN(ap.upsampleLpfBF); i++) {
109 pt3FilterInit(&ap.upsampleLpfBF[i], ap.upsampleLpfGain);
113 // get sanity distance based on speed
114 static inline float sanityCheckDistance(const float gpsGroundSpeedCmS)
116 return fmaxf(1000.0f, gpsGroundSpeedCmS * 2.0f);
117 // distance flown in 2s at current speed. with minimum of 10m
120 void resetPositionControl(const gpsLocation_t *initialTargetLocation, unsigned taskRateHz)
122 // from pos_hold.c (or other client) when initiating position hold at target location
123 ap.targetLocation = *initialTargetLocation;
124 ap.sticksActive = false;
125 // set sanity check distance according to groundspeed at start, minimum of 10m
126 ap.sanityCheckDistance = sanityCheckDistance(gpsSol.groundSpeed);
127 for (unsigned i = 0; i < ARRAYLEN(ap.efAxis); i++) {
128 // clear anything stored in the filter at first call
129 resetEFAxisParams(&ap.efAxis[i], 1.0f);
131 const float taskInterval = 1.0f / taskRateHz;
132 ap.upsampleLpfGain = pt3FilterGain(UPSAMPLING_CUTOFF_HZ, taskInterval); // 5Hz; normally at 100Hz task rate
133 resetUpsampleFilters(); // clear accumlator from previous iterations
136 void autopilotInit(void)
138 const apConfig_t *cfg = apConfig();
140 ap.sticksActive = false;
141 ap.maxAngle = cfg->max_angle;
142 altitudePidCoeffs.Kp = cfg->altitude_P * ALTITUDE_P_SCALE;
143 altitudePidCoeffs.Ki = cfg->altitude_I * ALTITUDE_I_SCALE;
144 altitudePidCoeffs.Kd = cfg->altitude_D * ALTITUDE_D_SCALE;
145 altitudePidCoeffs.Kf = cfg->altitude_F * ALTITUDE_F_SCALE;
146 positionPidCoeffs.Kp = cfg->position_P * POSITION_P_SCALE;
147 positionPidCoeffs.Ki = cfg->position_I * POSITION_I_SCALE;
148 positionPidCoeffs.Kd = cfg->position_D * POSITION_D_SCALE;
149 positionPidCoeffs.Kf = cfg->position_A * POSITION_A_SCALE; // Kf used for acceleration
150 // initialise filters with approximate filter gains; location isn't used at this point.
151 ap.upsampleLpfGain = pt3FilterGain(UPSAMPLING_CUTOFF_HZ, 0.01f); // 5Hz, assuming 100Hz task rate at init
152 resetUpsampleFilters();
153 // Initialise PT1 filters for velocity and acceleration in earth frame axes
154 ap.vaLpfCutoff = cfg->position_cutoff * 0.01f;
155 const float vaGain = pt1FilterGain(ap.vaLpfCutoff, 0.1f); // assume 10Hz GPS connection at start; value is overwritten before first filter use
156 for (unsigned i = 0; i < ARRAYLEN(ap.efAxis); i++) {
157 resetEFAxisFilters(&ap.efAxis[i], vaGain);
161 void resetAltitudeControl (void) {
162 altitudeI = 0.0f;
165 void altitudeControl(float targetAltitudeCm, float taskIntervalS, float targetAltitudeStep)
167 const float verticalVelocityCmS = getAltitudeDerivative();
168 const float altitudeErrorCm = targetAltitudeCm - getAltitudeCm();
169 const float altitudeP = altitudeErrorCm * altitudePidCoeffs.Kp;
171 // reduce the iTerm gain for errors greater than 200cm (2m), otherwise it winds up too much
172 const float itermRelax = (fabsf(altitudeErrorCm) < 200.0f) ? 1.0f : 0.1f;
173 altitudeI += altitudeErrorCm * altitudePidCoeffs.Ki * itermRelax * taskIntervalS;
174 // limit iTerm to not more than 200 throttle units
175 altitudeI = constrainf(altitudeI, -200.0f, 200.0f);
177 // increase D when velocity is high, typically when initiating hold at high vertical speeds
178 // 1.0 when less than 5 m/s, 2x at 10m/s, 2.5 at 20 m/s, 2.8 at 50 m/s, asymptotes towards max 3.0.
179 float dBoost = 1.0f;
181 const float startValue = 500.0f; // velocity at which D should start to increase
182 const float altDeriv = fabsf(verticalVelocityCmS);
183 if (altDeriv > startValue) {
184 const float ratio = altDeriv / startValue;
185 dBoost = (3.0f * ratio - 2.0f) / ratio;
189 const float altitudeD = verticalVelocityCmS * dBoost * altitudePidCoeffs.Kd;
191 const float altitudeF = targetAltitudeStep * altitudePidCoeffs.Kf;
193 const float hoverOffset = apConfig()->hover_throttle - PWM_RANGE_MIN;
194 float throttleOffset = altitudeP + altitudeI - altitudeD + altitudeF + hoverOffset;
196 const float tiltMultiplier = 1.0f / fmaxf(getCosTiltAngle(), 0.5f);
197 // 1 = flat, 1.3 at 40 degrees, 1.56 at 50 deg, max 2.0 at 60 degrees or higher
198 // note: the default limit of Angle Mode is 60 degrees
200 throttleOffset *= tiltMultiplier;
202 float newThrottle = PWM_RANGE_MIN + throttleOffset;
203 newThrottle = constrainf(newThrottle, apConfig()->throttle_min, apConfig()->throttle_max);
204 DEBUG_SET(DEBUG_AUTOPILOT_ALTITUDE, 0, lrintf(newThrottle)); // normal range 1000-2000 but is before constraint
206 newThrottle = scaleRangef(newThrottle, MAX(rxConfig()->mincheck, PWM_RANGE_MIN), PWM_RANGE_MAX, 0.0f, 1.0f);
208 throttleOut = constrainf(newThrottle, 0.0f, 1.0f);
210 DEBUG_SET(DEBUG_AUTOPILOT_ALTITUDE, 1, lrintf(tiltMultiplier * 100));
211 DEBUG_SET(DEBUG_AUTOPILOT_ALTITUDE, 3, lrintf(targetAltitudeCm));
212 DEBUG_SET(DEBUG_AUTOPILOT_ALTITUDE, 4, lrintf(altitudeP));
213 DEBUG_SET(DEBUG_AUTOPILOT_ALTITUDE, 5, lrintf(altitudeI));
214 DEBUG_SET(DEBUG_AUTOPILOT_ALTITUDE, 6, lrintf(-altitudeD));
215 DEBUG_SET(DEBUG_AUTOPILOT_ALTITUDE, 7, lrintf(altitudeF));
218 void setSticksActiveStatus(bool areSticksActive)
220 ap.sticksActive = areSticksActive;
223 void setTargetLocationByAxis(const gpsLocation_t* newTargetLocation, axisEF_e efAxisIdx)
224 // not used at present but needed by upcoming GPS code
226 if (efAxisIdx == LON) {
227 ap.targetLocation.lon = newTargetLocation->lon; // update East-West / / longitude position
228 } else {
229 ap.targetLocation.lat = newTargetLocation->lat; // update North-South / latitude position
233 bool positionControl(void)
235 unsigned debugAxis = gyroConfig()->gyro_filter_debug_axis;
236 static vector2_t debugGpsDistance = { 0 }; // keep last calculated distance for DEBUG
237 static vector2_t debugPidSumEF = { 0 }; // and last pidsum in EF
238 static uint16_t gpsStamp = 0;
239 if (gpsHasNewData(&gpsStamp)) {
240 const float gpsDataInterval = getGpsDataIntervalSeconds(); // interval for current GPS data value 0.05 - 2.5s
241 const float gpsDataFreq = getGpsDataFrequencyHz();
243 // get lat and long distances from current location (gpsSol.llh) to target location
244 vector2_t gpsDistance;
245 GPS_distance2d(&gpsSol.llh, &ap.targetLocation, &gpsDistance); // X is EW/lon, Y is NS/lat
246 debugGpsDistance = gpsDistance;
247 const float distanceNormCm = vector2Norm(&gpsDistance);
249 // ** Sanity check **
250 // primarily to detect flyaway from no Mag or badly oriented Mag
251 // must accept some overshoot at the start, especially if entering at high speed
252 if (distanceNormCm > ap.sanityCheckDistance) {
253 return false;
256 // update filters according to current GPS update rate
257 const float vaGain = pt1FilterGain(ap.vaLpfCutoff, gpsDataInterval);
258 const float iTermLeakGain = 1.0f - pt1FilterGainFromDelay(2.5f, gpsDataInterval); // 2.5s time constant
259 vector2_t pidSum = { 0 }; // P+I in loop, D+A added after the axis loop (after limiting it)
260 vector2_t pidDA; // D+A
262 for (axisEF_e efAxisIdx = LON; efAxisIdx <= LAT; efAxisIdx++) {
263 efPidAxis_t *efAxis = &ap.efAxis[efAxisIdx];
264 // separate PID controllers for longitude (EastWest or EW, X) and latitude (NorthSouth or NS, Y)
265 const float axisDistance = gpsDistance.v[efAxisIdx];
267 // ** P **
268 const float pidP = axisDistance * positionPidCoeffs.Kp;
269 pidSum.v[efAxisIdx] += pidP;
271 // ** I **
272 // only add to iTerm while in hold phase
273 efAxis->integral += efAxis->isStopping ? 0.0f : axisDistance * gpsDataInterval;
274 const float pidI = efAxis->integral * positionPidCoeffs.Ki;
275 pidSum.v[efAxisIdx] += pidI;
277 // ** D ** //
278 // Velocity derived from GPS position works better than module supplied GPS Speed and Heading information
280 const float velocity = (axisDistance - efAxis->previousDistance) * gpsDataFreq; // cm/s
281 efAxis->previousDistance = axisDistance;
282 pt1FilterUpdateCutoff(&efAxis->velocityLpf, vaGain);
283 const float velocityFiltered = pt1FilterApply(&efAxis->velocityLpf, velocity);
284 float pidD = velocityFiltered * positionPidCoeffs.Kd;
286 // differentiate velocity another time to get acceleration
287 float acceleration = (velocityFiltered - efAxis->previousVelocity) * gpsDataFreq;
288 efAxis->previousVelocity = velocityFiltered;
289 // apply second filter to acceleration (acc is filtered twice)
290 pt1FilterUpdateCutoff(&efAxis->accelerationLpf, vaGain);
291 const float accelerationFiltered = pt1FilterApply(&efAxis->accelerationLpf, acceleration);
292 const float pidA = accelerationFiltered * positionPidCoeffs.Kf;
294 if (ap.sticksActive) {
295 // sticks active 'phase', prepare to enter stopping
296 efAxis->isStopping = true;
297 // slowly leak iTerm away
298 efAxis->integral *= iTermLeakGain;
299 efAxis->previousDistance = 0.0f; // avoid D and A spikes
300 // rest is handled after axis loop
301 } else if (efAxis->isStopping) {
302 // 'phase' after sticks are centered, but before craft has stopped; in given Earth axis
303 pidD *= 1.6f; // aribitrary D boost to stop more quickly than usual
304 // detect when axis has nearly stopped by sign reversal of velocity (comparing sign of velocityFiltered, which is delayed, to velocity)
305 if (velocity * velocityFiltered < 0.0f) {
306 setTargetLocationByAxis(&gpsSol.llh, efAxisIdx); // reset target location for this axis, forcing P to zero
307 efAxis->previousDistance = 0.0f; // ensure minimal D jump from the updated location
308 efAxis->isStopping = false; // end the 'stopping' phase
309 if (ap.efAxis[LAT].isStopping == ap.efAxis[LON].isStopping) {
310 // when both axes have stopped moving, reset the sanity distance to 10m default
311 ap.sanityCheckDistance = sanityCheckDistance(1000);
315 pidDA.v[efAxisIdx] = pidD + pidA; // save DA here, processed after axis loop
316 if (debugAxis == efAxisIdx) {
317 DEBUG_SET(DEBUG_AUTOPILOT_POSITION, 0, lrintf(distanceNormCm)); // same for both axes
318 DEBUG_SET(DEBUG_AUTOPILOT_POSITION, 4, lrintf(pidP * 10));
319 DEBUG_SET(DEBUG_AUTOPILOT_POSITION, 5, lrintf(pidI * 10));
320 DEBUG_SET(DEBUG_AUTOPILOT_POSITION, 6, lrintf(pidD * 10));
321 DEBUG_SET(DEBUG_AUTOPILOT_POSITION, 7, lrintf(pidA * 10));
323 } // end for loop
326 // limit sum of D and A per axis based on total DA vector length, otherwise can be too aggressive when starting at speed
327 // limit is 35 degrees from D and A alone, arbitrary value. 20 is a bit too low, allows a lot of overshoot
328 // note: an angle of more than 35 degrees can still be achieved as P and I grow
329 const float maxDAAngle = 35.0f; // D+A limit in degrees; arbitrary angle
330 const float mag = vector2Norm(&pidDA);
331 if (mag > maxDAAngle) {
332 vector2Scale(&pidDA, &pidDA, maxDAAngle / mag);
336 // add constrained DA to sum
337 vector2Add(&pidSum, &pidSum, &pidDA);
338 debugPidSumEF = pidSum;
339 vector2_t anglesBF;
341 if (ap.sticksActive) {
342 // if a Position Hold deadband is set, and sticks are outside deadband, allow pilot control in angle mode
343 anglesBF = (vector2_t){{0, 0}}; // set output PIDS to 0; upsampling filter will smooth this
344 // reset target location each cycle (and set previousDistance to zero in for loop), to keep D current, and avoid a spike when stopping
345 ap.targetLocation = gpsSol.llh;
346 // keep updating sanity check distance while sticks are out because speed may get high
347 ap.sanityCheckDistance = sanityCheckDistance(gpsSol.groundSpeed);
348 } else {
349 // ** Rotate pid Sum to body frame, and convert it into pitch and roll **
350 // attitude.values.yaw increases clockwise from north
351 // PID is running in ENU, adapt angle (to 0deg = EAST);
352 // rotation is from EarthFrame to BodyFrame, no change of sign from heading
353 const float angle = DECIDEGREES_TO_RADIANS(attitude.values.yaw - 900);
354 vector2_t pidBodyFrame; // pid output in body frame; X is forward, Y is left
355 vector2Rotate(&pidBodyFrame, &pidSum, angle); // rotate by angle counterclockwise
356 anglesBF.v[AI_ROLL] = -pidBodyFrame.y; // negative roll to fly left
357 anglesBF.v[AI_PITCH] = pidBodyFrame.x; // positive pitch for forward
358 // limit angle vector to maxAngle
359 const float mag = vector2Norm(&anglesBF);
360 if (mag > ap.maxAngle && mag > 0.0f) {
361 vector2Scale(&anglesBF, &anglesBF, ap.maxAngle / mag);
364 ap.pidSumBF = anglesBF; // this value will be upsampled
367 // Final output to pid.c Angle Mode at 100Hz with PT3 upsampling
368 for (unsigned i = 0; i < RP_AXIS_COUNT; i++) {
369 // note: upsampling should really be done in earth frame, to avoid 10Hz wobbles if pilot yaws and the controller is applying significant pitch or roll
370 autopilotAngle[i] = pt3FilterApply(&ap.upsampleLpfBF[i], ap.pidSumBF.v[i]);
373 if (debugAxis < 2) {
374 // this is different from @ctzsnooze version
375 // debugAxis = 0: store longitude + roll
376 // debugAxis = 1: store latitude + pitch
377 DEBUG_SET(DEBUG_AUTOPILOT_POSITION, 1, lrintf(debugGpsDistance.v[debugAxis])); // cm
378 DEBUG_SET(DEBUG_AUTOPILOT_POSITION, 2, lrintf(debugPidSumEF.v[debugAxis] * 10)); // deg * 10
379 DEBUG_SET(DEBUG_AUTOPILOT_POSITION, 3, lrintf(autopilotAngle[debugAxis] * 10)); // deg * 10
381 return true;
384 bool isBelowLandingAltitude(void)
386 return getAltitudeCm() < 100.0f * apConfig()->landing_altitude_m;
389 float getAutopilotThrottle(void)
391 return throttleOut;
394 bool isAutopilotInControl(void)
396 return !ap.sticksActive;