CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AC_AttitudeControl/AC_PosControl.cpp
Views: 1798
1
#include <AP_HAL/AP_HAL.h>
2
#include "AC_PosControl.h"
3
#include <AP_Math/AP_Math.h>
4
#include <AP_Logger/AP_Logger.h>
5
#include <AP_Motors/AP_Motors.h> // motors library
6
#include <AP_Vehicle/AP_Vehicle_Type.h>
7
#include <AP_Scheduler/AP_Scheduler.h>
8
9
extern const AP_HAL::HAL& hal;
10
11
#if APM_BUILD_TYPE(APM_BUILD_ArduPlane)
12
// default gains for Plane
13
# define POSCONTROL_POS_Z_P 1.0f // vertical position controller P gain default
14
# define POSCONTROL_VEL_Z_P 5.0f // vertical velocity controller P gain default
15
# define POSCONTROL_VEL_Z_IMAX 1000.0f // vertical velocity controller IMAX gain default
16
# define POSCONTROL_VEL_Z_FILT_HZ 5.0f // vertical velocity controller input filter
17
# define POSCONTROL_VEL_Z_FILT_D_HZ 5.0f // vertical velocity controller input filter for D
18
# define POSCONTROL_ACC_Z_P 0.3f // vertical acceleration controller P gain default
19
# define POSCONTROL_ACC_Z_I 1.0f // vertical acceleration controller I gain default
20
# define POSCONTROL_ACC_Z_D 0.0f // vertical acceleration controller D gain default
21
# define POSCONTROL_ACC_Z_IMAX 800 // vertical acceleration controller IMAX gain default
22
# define POSCONTROL_ACC_Z_FILT_HZ 10.0f // vertical acceleration controller input filter default
23
# define POSCONTROL_ACC_Z_DT 0.02f // vertical acceleration controller dt default
24
# define POSCONTROL_POS_XY_P 0.5f // horizontal position controller P gain default
25
# define POSCONTROL_VEL_XY_P 0.7f // horizontal velocity controller P gain default
26
# define POSCONTROL_VEL_XY_I 0.35f // horizontal velocity controller I gain default
27
# define POSCONTROL_VEL_XY_D 0.17f // horizontal velocity controller D gain default
28
# define POSCONTROL_VEL_XY_IMAX 1000.0f // horizontal velocity controller IMAX gain default
29
# define POSCONTROL_VEL_XY_FILT_HZ 5.0f // horizontal velocity controller input filter
30
# define POSCONTROL_VEL_XY_FILT_D_HZ 5.0f // horizontal velocity controller input filter for D
31
#elif APM_BUILD_TYPE(APM_BUILD_ArduSub)
32
// default gains for Sub
33
# define POSCONTROL_POS_Z_P 3.0f // vertical position controller P gain default
34
# define POSCONTROL_VEL_Z_P 8.0f // vertical velocity controller P gain default
35
# define POSCONTROL_VEL_Z_IMAX 1000.0f // vertical velocity controller IMAX gain default
36
# define POSCONTROL_VEL_Z_FILT_HZ 5.0f // vertical velocity controller input filter
37
# define POSCONTROL_VEL_Z_FILT_D_HZ 5.0f // vertical velocity controller input filter for D
38
# define POSCONTROL_ACC_Z_P 0.5f // vertical acceleration controller P gain default
39
# define POSCONTROL_ACC_Z_I 0.1f // vertical acceleration controller I gain default
40
# define POSCONTROL_ACC_Z_D 0.0f // vertical acceleration controller D gain default
41
# define POSCONTROL_ACC_Z_IMAX 100 // vertical acceleration controller IMAX gain default
42
# define POSCONTROL_ACC_Z_FILT_HZ 20.0f // vertical acceleration controller input filter default
43
# define POSCONTROL_ACC_Z_DT 0.0025f // vertical acceleration controller dt default
44
# define POSCONTROL_POS_XY_P 1.0f // horizontal position controller P gain default
45
# define POSCONTROL_VEL_XY_P 1.0f // horizontal velocity controller P gain default
46
# define POSCONTROL_VEL_XY_I 0.5f // horizontal velocity controller I gain default
47
# define POSCONTROL_VEL_XY_D 0.0f // horizontal velocity controller D gain default
48
# define POSCONTROL_VEL_XY_IMAX 1000.0f // horizontal velocity controller IMAX gain default
49
# define POSCONTROL_VEL_XY_FILT_HZ 5.0f // horizontal velocity controller input filter
50
# define POSCONTROL_VEL_XY_FILT_D_HZ 5.0f // horizontal velocity controller input filter for D
51
#else
52
// default gains for Copter / TradHeli
53
# define POSCONTROL_POS_Z_P 1.0f // vertical position controller P gain default
54
# define POSCONTROL_VEL_Z_P 5.0f // vertical velocity controller P gain default
55
# define POSCONTROL_VEL_Z_IMAX 1000.0f // vertical velocity controller IMAX gain default
56
# define POSCONTROL_VEL_Z_FILT_HZ 5.0f // vertical velocity controller input filter
57
# define POSCONTROL_VEL_Z_FILT_D_HZ 5.0f // vertical velocity controller input filter for D
58
# define POSCONTROL_ACC_Z_P 0.5f // vertical acceleration controller P gain default
59
# define POSCONTROL_ACC_Z_I 1.0f // vertical acceleration controller I gain default
60
# define POSCONTROL_ACC_Z_D 0.0f // vertical acceleration controller D gain default
61
# define POSCONTROL_ACC_Z_IMAX 800 // vertical acceleration controller IMAX gain default
62
# define POSCONTROL_ACC_Z_FILT_HZ 20.0f // vertical acceleration controller input filter default
63
# define POSCONTROL_ACC_Z_DT 0.0025f // vertical acceleration controller dt default
64
# define POSCONTROL_POS_XY_P 1.0f // horizontal position controller P gain default
65
# define POSCONTROL_VEL_XY_P 2.0f // horizontal velocity controller P gain default
66
# define POSCONTROL_VEL_XY_I 1.0f // horizontal velocity controller I gain default
67
# define POSCONTROL_VEL_XY_D 0.5f // horizontal velocity controller D gain default
68
# define POSCONTROL_VEL_XY_IMAX 1000.0f // horizontal velocity controller IMAX gain default
69
# define POSCONTROL_VEL_XY_FILT_HZ 5.0f // horizontal velocity controller input filter
70
# define POSCONTROL_VEL_XY_FILT_D_HZ 5.0f // horizontal velocity controller input filter for D
71
#endif
72
73
// vibration compensation gains
74
#define POSCONTROL_VIBE_COMP_P_GAIN 0.250f
75
#define POSCONTROL_VIBE_COMP_I_GAIN 0.125f
76
77
// velocity offset targets timeout if not updated within 3 seconds
78
#define POSCONTROL_POSVELACCEL_OFFSET_TARGET_TIMEOUT_MS 3000
79
80
AC_PosControl *AC_PosControl::_singleton;
81
82
const AP_Param::GroupInfo AC_PosControl::var_info[] = {
83
// 0 was used for HOVER
84
85
// @Param: _ACC_XY_FILT
86
// @DisplayName: XY Acceleration filter cutoff frequency
87
// @Description: Lower values will slow the response of the navigation controller and reduce twitchiness
88
// @Units: Hz
89
// @Range: 0.5 5
90
// @Increment: 0.1
91
// @User: Advanced
92
93
// @Param: _POSZ_P
94
// @DisplayName: Position (vertical) controller P gain
95
// @Description: Position (vertical) controller P gain. Converts the difference between the desired altitude and actual altitude into a climb or descent rate which is passed to the throttle rate controller
96
// @Range: 1.000 3.000
97
// @User: Standard
98
AP_SUBGROUPINFO(_p_pos_z, "_POSZ_", 2, AC_PosControl, AC_P_1D),
99
100
// @Param: _VELZ_P
101
// @DisplayName: Velocity (vertical) controller P gain
102
// @Description: Velocity (vertical) controller P gain. Converts the difference between desired vertical speed and actual speed into a desired acceleration that is passed to the throttle acceleration controller
103
// @Range: 1.000 8.000
104
// @User: Standard
105
106
// @Param: _VELZ_I
107
// @DisplayName: Velocity (vertical) controller I gain
108
// @Description: Velocity (vertical) controller I gain. Corrects long-term difference in desired velocity to a target acceleration
109
// @Range: 0.02 1.00
110
// @Increment: 0.01
111
// @User: Advanced
112
113
// @Param: _VELZ_IMAX
114
// @DisplayName: Velocity (vertical) controller I gain maximum
115
// @Description: Velocity (vertical) controller I gain maximum. Constrains the target acceleration that the I gain will output
116
// @Range: 1.000 8.000
117
// @User: Standard
118
119
// @Param: _VELZ_D
120
// @DisplayName: Velocity (vertical) controller D gain
121
// @Description: Velocity (vertical) controller D gain. Corrects short-term changes in velocity
122
// @Range: 0.00 1.00
123
// @Increment: 0.001
124
// @User: Advanced
125
126
// @Param: _VELZ_FF
127
// @DisplayName: Velocity (vertical) controller Feed Forward gain
128
// @Description: Velocity (vertical) controller Feed Forward gain. Produces an output that is proportional to the magnitude of the target
129
// @Range: 0 1
130
// @Increment: 0.01
131
// @User: Advanced
132
133
// @Param: _VELZ_FLTE
134
// @DisplayName: Velocity (vertical) error filter
135
// @Description: Velocity (vertical) error filter. This filter (in Hz) is applied to the input for P and I terms
136
// @Range: 0 100
137
// @Units: Hz
138
// @User: Advanced
139
140
// @Param: _VELZ_FLTD
141
// @DisplayName: Velocity (vertical) input filter for D term
142
// @Description: Velocity (vertical) input filter for D term. This filter (in Hz) is applied to the input for D terms
143
// @Range: 0 100
144
// @Units: Hz
145
// @User: Advanced
146
AP_SUBGROUPINFO(_pid_vel_z, "_VELZ_", 3, AC_PosControl, AC_PID_Basic),
147
148
// @Param: _ACCZ_P
149
// @DisplayName: Acceleration (vertical) controller P gain
150
// @Description: Acceleration (vertical) controller P gain. Converts the difference between desired vertical acceleration and actual acceleration into a motor output
151
// @Range: 0.200 1.500
152
// @Increment: 0.05
153
// @User: Standard
154
155
// @Param: _ACCZ_I
156
// @DisplayName: Acceleration (vertical) controller I gain
157
// @Description: Acceleration (vertical) controller I gain. Corrects long-term difference in desired vertical acceleration and actual acceleration
158
// @Range: 0.000 3.000
159
// @User: Standard
160
161
// @Param: _ACCZ_IMAX
162
// @DisplayName: Acceleration (vertical) controller I gain maximum
163
// @Description: Acceleration (vertical) controller I gain maximum. Constrains the maximum pwm that the I term will generate
164
// @Range: 0 1000
165
// @Units: d%
166
// @User: Standard
167
168
// @Param: _ACCZ_D
169
// @DisplayName: Acceleration (vertical) controller D gain
170
// @Description: Acceleration (vertical) controller D gain. Compensates for short-term change in desired vertical acceleration vs actual acceleration
171
// @Range: 0.000 0.400
172
// @User: Standard
173
174
// @Param: _ACCZ_FF
175
// @DisplayName: Acceleration (vertical) controller feed forward
176
// @Description: Acceleration (vertical) controller feed forward
177
// @Range: 0 0.5
178
// @Increment: 0.001
179
// @User: Standard
180
181
// @Param: _ACCZ_FLTT
182
// @DisplayName: Acceleration (vertical) controller target frequency in Hz
183
// @Description: Acceleration (vertical) controller target frequency in Hz
184
// @Range: 1 50
185
// @Increment: 1
186
// @Units: Hz
187
// @User: Standard
188
189
// @Param: _ACCZ_FLTE
190
// @DisplayName: Acceleration (vertical) controller error frequency in Hz
191
// @Description: Acceleration (vertical) controller error frequency in Hz
192
// @Range: 1 100
193
// @Increment: 1
194
// @Units: Hz
195
// @User: Standard
196
197
// @Param: _ACCZ_FLTD
198
// @DisplayName: Acceleration (vertical) controller derivative frequency in Hz
199
// @Description: Acceleration (vertical) controller derivative frequency in Hz
200
// @Range: 1 100
201
// @Increment: 1
202
// @Units: Hz
203
// @User: Standard
204
205
// @Param: _ACCZ_SMAX
206
// @DisplayName: Accel (vertical) slew rate limit
207
// @Description: Sets an upper limit on the slew rate produced by the combined P and D gains. If the amplitude of the control action produced by the rate feedback exceeds this value, then the D+P gain is reduced to respect the limit. This limits the amplitude of high frequency oscillations caused by an excessive gain. The limit should be set to no more than 25% of the actuators maximum slew rate to allow for load effects. Note: The gain will not be reduced to less than 10% of the nominal value. A value of zero will disable this feature.
208
// @Range: 0 200
209
// @Increment: 0.5
210
// @User: Advanced
211
212
// @Param: _ACCZ_PDMX
213
// @DisplayName: Acceleration (vertical) controller PD sum maximum
214
// @Description: Acceleration (vertical) controller PD sum maximum. The maximum/minimum value that the sum of the P and D term can output
215
// @Range: 0 1000
216
// @Units: d%
217
218
// @Param: _ACCZ_D_FF
219
// @DisplayName: Accel (vertical) Derivative FeedForward Gain
220
// @Description: FF D Gain which produces an output that is proportional to the rate of change of the target
221
// @Range: 0 0.02
222
// @Increment: 0.0001
223
// @User: Advanced
224
225
// @Param: _ACCZ_NTF
226
// @DisplayName: Accel (vertical) Target notch filter index
227
// @Description: Accel (vertical) Target notch filter index
228
// @Range: 1 8
229
// @User: Advanced
230
231
// @Param: _ACCZ_NEF
232
// @DisplayName: Accel (vertical) Error notch filter index
233
// @Description: Accel (vertical) Error notch filter index
234
// @Range: 1 8
235
// @User: Advanced
236
237
AP_SUBGROUPINFO(_pid_accel_z, "_ACCZ_", 4, AC_PosControl, AC_PID),
238
239
// @Param: _POSXY_P
240
// @DisplayName: Position (horizontal) controller P gain
241
// @Description: Position controller P gain. Converts the distance (in the latitude direction) to the target location into a desired speed which is then passed to the loiter latitude rate controller
242
// @Range: 0.500 2.000
243
// @User: Standard
244
AP_SUBGROUPINFO(_p_pos_xy, "_POSXY_", 5, AC_PosControl, AC_P_2D),
245
246
// @Param: _VELXY_P
247
// @DisplayName: Velocity (horizontal) P gain
248
// @Description: Velocity (horizontal) P gain. Converts the difference between desired and actual velocity to a target acceleration
249
// @Range: 0.1 6.0
250
// @Increment: 0.1
251
// @User: Advanced
252
253
// @Param: _VELXY_I
254
// @DisplayName: Velocity (horizontal) I gain
255
// @Description: Velocity (horizontal) I gain. Corrects long-term difference between desired and actual velocity to a target acceleration
256
// @Range: 0.02 1.00
257
// @Increment: 0.01
258
// @User: Advanced
259
260
// @Param: _VELXY_D
261
// @DisplayName: Velocity (horizontal) D gain
262
// @Description: Velocity (horizontal) D gain. Corrects short-term changes in velocity
263
// @Range: 0.00 1.00
264
// @Increment: 0.001
265
// @User: Advanced
266
267
// @Param: _VELXY_IMAX
268
// @DisplayName: Velocity (horizontal) integrator maximum
269
// @Description: Velocity (horizontal) integrator maximum. Constrains the target acceleration that the I gain will output
270
// @Range: 0 4500
271
// @Increment: 10
272
// @Units: cm/s/s
273
// @User: Advanced
274
275
// @Param: _VELXY_FLTE
276
// @DisplayName: Velocity (horizontal) input filter
277
// @Description: Velocity (horizontal) input filter. This filter (in Hz) is applied to the input for P and I terms
278
// @Range: 0 100
279
// @Units: Hz
280
// @User: Advanced
281
282
// @Param: _VELXY_FLTD
283
// @DisplayName: Velocity (horizontal) input filter
284
// @Description: Velocity (horizontal) input filter. This filter (in Hz) is applied to the input for D term
285
// @Range: 0 100
286
// @Units: Hz
287
// @User: Advanced
288
289
// @Param: _VELXY_FF
290
// @DisplayName: Velocity (horizontal) feed forward gain
291
// @Description: Velocity (horizontal) feed forward gain. Converts the difference between desired velocity to a target acceleration
292
// @Range: 0 6
293
// @Increment: 0.01
294
// @User: Advanced
295
AP_SUBGROUPINFO(_pid_vel_xy, "_VELXY_", 6, AC_PosControl, AC_PID_2D),
296
297
// @Param: _ANGLE_MAX
298
// @DisplayName: Position Control Angle Max
299
// @Description: Maximum lean angle autopilot can request. Set to zero to use ANGLE_MAX parameter value
300
// @Units: deg
301
// @Range: 0 45
302
// @Increment: 1
303
// @User: Advanced
304
AP_GROUPINFO("_ANGLE_MAX", 7, AC_PosControl, _lean_angle_max, 0.0f),
305
306
// IDs 8,9 used for _TC_XY and _TC_Z in beta release candidate
307
308
// @Param: _JERK_XY
309
// @DisplayName: Jerk limit for the horizontal kinematic input shaping
310
// @Description: Jerk limit of the horizontal kinematic path generation used to determine how quickly the aircraft varies the acceleration target
311
// @Units: m/s/s/s
312
// @Range: 1 20
313
// @Increment: 1
314
// @User: Advanced
315
AP_GROUPINFO("_JERK_XY", 10, AC_PosControl, _shaping_jerk_xy, POSCONTROL_JERK_XY),
316
317
// @Param: _JERK_Z
318
// @DisplayName: Jerk limit for the vertical kinematic input shaping
319
// @Description: Jerk limit of the vertical kinematic path generation used to determine how quickly the aircraft varies the acceleration target
320
// @Units: m/s/s/s
321
// @Range: 5 50
322
// @Increment: 1
323
// @User: Advanced
324
AP_GROUPINFO("_JERK_Z", 11, AC_PosControl, _shaping_jerk_z, POSCONTROL_JERK_Z),
325
326
AP_GROUPEND
327
};
328
329
// Default constructor.
330
// Note that the Vector/Matrix constructors already implicitly zero
331
// their values.
332
//
333
AC_PosControl::AC_PosControl(AP_AHRS_View& ahrs, const AP_InertialNav& inav,
334
const AP_Motors& motors, AC_AttitudeControl& attitude_control) :
335
_ahrs(ahrs),
336
_inav(inav),
337
_motors(motors),
338
_attitude_control(attitude_control),
339
_p_pos_xy(POSCONTROL_POS_XY_P),
340
_p_pos_z(POSCONTROL_POS_Z_P),
341
_pid_vel_xy(POSCONTROL_VEL_XY_P, POSCONTROL_VEL_XY_I, POSCONTROL_VEL_XY_D, 0.0f, POSCONTROL_VEL_XY_IMAX, POSCONTROL_VEL_XY_FILT_HZ, POSCONTROL_VEL_XY_FILT_D_HZ),
342
_pid_vel_z(POSCONTROL_VEL_Z_P, 0.0f, 0.0f, 0.0f, POSCONTROL_VEL_Z_IMAX, POSCONTROL_VEL_Z_FILT_HZ, POSCONTROL_VEL_Z_FILT_D_HZ),
343
_pid_accel_z(POSCONTROL_ACC_Z_P, POSCONTROL_ACC_Z_I, POSCONTROL_ACC_Z_D, 0.0f, POSCONTROL_ACC_Z_IMAX, 0.0f, POSCONTROL_ACC_Z_FILT_HZ, 0.0f),
344
_vel_max_xy_cms(POSCONTROL_SPEED),
345
_vel_max_up_cms(POSCONTROL_SPEED_UP),
346
_vel_max_down_cms(POSCONTROL_SPEED_DOWN),
347
_accel_max_xy_cmss(POSCONTROL_ACCEL_XY),
348
_accel_max_z_cmss(POSCONTROL_ACCEL_Z),
349
_jerk_max_xy_cmsss(POSCONTROL_JERK_XY * 100.0),
350
_jerk_max_z_cmsss(POSCONTROL_JERK_Z * 100.0)
351
{
352
AP_Param::setup_object_defaults(this, var_info);
353
354
_singleton = this;
355
}
356
357
358
///
359
/// 3D position shaper
360
///
361
362
/// input_pos_xyz - calculate a jerk limited path from the current position, velocity and acceleration to an input position.
363
/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.
364
/// The kinematic path is constrained by the maximum jerk parameter and the velocity and acceleration limits set using the function set_max_speed_accel_xy.
365
/// The jerk limit defines the acceleration error decay in the kinematic path as the system approaches constant acceleration.
366
/// The jerk limit also defines the time taken to achieve the maximum acceleration.
367
/// The function alters the input velocity to be the velocity that the system could reach zero acceleration in the minimum time.
368
void AC_PosControl::input_pos_xyz(const Vector3p& pos, float pos_terrain_target, float terrain_buffer)
369
{
370
// Terrain following velocity scalar must be calculated before we remove the position offset
371
const float offset_z_scaler = pos_offset_z_scaler(pos_terrain_target, terrain_buffer);
372
set_pos_terrain_target_cm(pos_terrain_target);
373
374
// calculated increased maximum acceleration and jerk if over speed
375
const float overspeed_gain = calculate_overspeed_gain();
376
const float accel_max_z_cmss = _accel_max_z_cmss * overspeed_gain;
377
const float jerk_max_z_cmsss = _jerk_max_z_cmsss * overspeed_gain;
378
379
update_pos_vel_accel_xy(_pos_desired.xy(), _vel_desired.xy(), _accel_desired.xy(), _dt, _limit_vector.xy(), _p_pos_xy.get_error(), _pid_vel_xy.get_error());
380
381
// adjust desired altitude if motors have not hit their limits
382
update_pos_vel_accel(_pos_desired.z, _vel_desired.z, _accel_desired.z, _dt, _limit_vector.z, _p_pos_z.get_error(), _pid_vel_z.get_error());
383
384
// calculate the horizontal and vertical velocity limits to travel directly to the destination defined by pos
385
float vel_max_xy_cms = 0.0f;
386
float vel_max_z_cms = 0.0f;
387
Vector3f dest_vector = (pos - _pos_desired).tofloat();
388
if (is_positive(dest_vector.length_squared()) ) {
389
dest_vector.normalize();
390
float dest_vector_xy_length = dest_vector.xy().length();
391
392
float vel_max_cms = kinematic_limit(dest_vector, _vel_max_xy_cms, _vel_max_up_cms, _vel_max_down_cms);
393
vel_max_xy_cms = vel_max_cms * dest_vector_xy_length;
394
vel_max_z_cms = fabsf(vel_max_cms * dest_vector.z);
395
}
396
397
// reduce speed if we are reaching the edge of our vertical buffer
398
vel_max_xy_cms *= offset_z_scaler;
399
400
Vector2f vel;
401
Vector2f accel;
402
shape_pos_vel_accel_xy(pos.xy(), vel, accel, _pos_desired.xy(), _vel_desired.xy(), _accel_desired.xy(),
403
vel_max_xy_cms, _accel_max_xy_cmss, _jerk_max_xy_cmsss, _dt, false);
404
405
float posz = pos.z;
406
shape_pos_vel_accel(posz, 0, 0,
407
_pos_desired.z, _vel_desired.z, _accel_desired.z,
408
-vel_max_z_cms, vel_max_z_cms,
409
-constrain_float(accel_max_z_cmss, 0.0f, 750.0f), accel_max_z_cmss,
410
jerk_max_z_cmsss, _dt, false);
411
}
412
413
414
/// pos_offset_z_scaler - calculates a multiplier used to reduce the horizontal velocity to allow the z position controller to stay within the provided buffer range
415
float AC_PosControl::pos_offset_z_scaler(float pos_offset_z, float pos_offset_z_buffer) const
416
{
417
if (is_zero(pos_offset_z_buffer)) {
418
return 1.0;
419
}
420
float pos_offset_error_z = _inav.get_position_z_up_cm() - (_pos_target.z + (pos_offset_z - _pos_terrain));
421
return constrain_float((1.0 - (fabsf(pos_offset_error_z) - 0.5 * pos_offset_z_buffer) / (0.5 * pos_offset_z_buffer)), 0.01, 1.0);
422
}
423
424
///
425
/// Lateral position controller
426
///
427
428
/// set_max_speed_accel_xy - set the maximum horizontal speed in cm/s and acceleration in cm/s/s
429
/// This function only needs to be called if using the kinematic shaping.
430
/// This can be done at any time as changes in these parameters are handled smoothly
431
/// by the kinematic shaping.
432
void AC_PosControl::set_max_speed_accel_xy(float speed_cms, float accel_cmss)
433
{
434
_vel_max_xy_cms = speed_cms;
435
_accel_max_xy_cmss = accel_cmss;
436
437
// ensure the horizontal jerk is less than the vehicle is capable of
438
const float jerk_max_cmsss = MIN(_attitude_control.get_ang_vel_roll_max_rads(), _attitude_control.get_ang_vel_pitch_max_rads()) * GRAVITY_MSS * 100.0;
439
const float snap_max_cmssss = MIN(_attitude_control.get_accel_roll_max_radss(), _attitude_control.get_accel_pitch_max_radss()) * GRAVITY_MSS * 100.0;
440
441
// get specified jerk limit
442
_jerk_max_xy_cmsss = _shaping_jerk_xy * 100.0;
443
444
// limit maximum jerk based on maximum angular rate
445
if (is_positive(jerk_max_cmsss) && _attitude_control.get_bf_feedforward()) {
446
_jerk_max_xy_cmsss = MIN(_jerk_max_xy_cmsss, jerk_max_cmsss);
447
}
448
449
// limit maximum jerk to maximum possible average jerk based on angular acceleration
450
if (is_positive(snap_max_cmssss) && _attitude_control.get_bf_feedforward()) {
451
_jerk_max_xy_cmsss = MIN(0.5 * safe_sqrt(_accel_max_xy_cmss * snap_max_cmssss), _jerk_max_xy_cmsss);
452
}
453
}
454
455
/// set_max_speed_accel_xy - set the position controller correction velocity and acceleration limit
456
/// This should be done only during initialisation to avoid discontinuities
457
void AC_PosControl::set_correction_speed_accel_xy(float speed_cms, float accel_cmss)
458
{
459
_p_pos_xy.set_limits(speed_cms, accel_cmss, 0.0f);
460
}
461
462
/// init_xy_controller_stopping_point - initialise the position controller to the stopping point with zero velocity and acceleration.
463
/// This function should be used when the expected kinematic path assumes a stationary initial condition but does not specify a specific starting position.
464
/// The starting position can be retrieved by getting the position target using get_pos_desired_cm() after calling this function.
465
void AC_PosControl::init_xy_controller_stopping_point()
466
{
467
init_xy_controller();
468
469
get_stopping_point_xy_cm(_pos_desired.xy());
470
_pos_target.xy() = _pos_desired.xy() + _pos_offset.xy();
471
_vel_desired.xy().zero();
472
_accel_desired.xy().zero();
473
}
474
475
// relax_velocity_controller_xy - initialise the position controller to the current position and velocity with decaying acceleration.
476
/// This function decays the output acceleration by 95% every half second to achieve a smooth transition to zero requested acceleration.
477
void AC_PosControl::relax_velocity_controller_xy()
478
{
479
// decay acceleration and therefore current attitude target to zero
480
// this will be reset by init_xy_controller() if !is_active_xy()
481
if (is_positive(_dt)) {
482
float decay = 1.0 - _dt / (_dt + POSCONTROL_RELAX_TC);
483
_accel_target.xy() *= decay;
484
}
485
486
init_xy_controller();
487
}
488
489
/// reduce response for landing
490
void AC_PosControl::soften_for_landing_xy()
491
{
492
// decay position error to zero
493
if (is_positive(_dt)) {
494
_pos_target.xy() += (_inav.get_position_xy_cm().topostype() - _pos_target.xy()) * (_dt / (_dt + POSCONTROL_RELAX_TC));
495
_pos_desired.xy() = _pos_target.xy() - _pos_offset.xy();
496
}
497
498
// Prevent I term build up in xy velocity controller.
499
// Note that this flag is reset on each loop in update_xy_controller()
500
set_externally_limited_xy();
501
}
502
503
/// init_xy_controller - initialise the position controller to the current position, velocity, acceleration and attitude.
504
/// This function is the default initialisation for any position control that provides position, velocity and acceleration.
505
void AC_PosControl::init_xy_controller()
506
{
507
// initialise offsets to target offsets and ensure offset targets are zero if they have not been updated.
508
init_offsets_xy();
509
510
// set roll, pitch lean angle targets to current attitude
511
const Vector3f &att_target_euler_cd = _attitude_control.get_att_target_euler_cd();
512
_roll_target = att_target_euler_cd.x;
513
_pitch_target = att_target_euler_cd.y;
514
_yaw_target = att_target_euler_cd.z; // todo: this should be thrust vector heading, not yaw.
515
_yaw_rate_target = 0.0f;
516
_angle_max_override_cd = 0.0;
517
518
_pos_target.xy() = _inav.get_position_xy_cm().topostype();
519
_pos_desired.xy() = _pos_target.xy() - _pos_offset.xy();
520
521
_vel_target.xy() = _inav.get_velocity_xy_cms();
522
_vel_desired.xy() = _vel_target.xy() - _vel_offset.xy();
523
524
// Set desired accel to zero because raw acceleration is prone to noise
525
_accel_desired.xy().zero();
526
527
if (!is_active_xy()) {
528
lean_angles_to_accel_xy(_accel_target.x, _accel_target.y);
529
}
530
531
// limit acceleration using maximum lean angles
532
float angle_max = MIN(_attitude_control.get_althold_lean_angle_max_cd(), get_lean_angle_max_cd());
533
float accel_max = angle_to_accel(angle_max * 0.01) * 100.0;
534
_accel_target.xy().limit_length(accel_max);
535
536
// initialise I terms from lean angles
537
_pid_vel_xy.reset_filter();
538
// initialise the I term to _accel_target - _accel_desired
539
// _accel_desired is zero and can be removed from the equation
540
_pid_vel_xy.set_integrator(_accel_target.xy() - _vel_target.xy() * _pid_vel_xy.ff());
541
542
// initialise ekf xy reset handler
543
init_ekf_xy_reset();
544
545
// initialise z_controller time out
546
_last_update_xy_ticks = AP::scheduler().ticks32();
547
}
548
549
/// input_accel_xy - calculate a jerk limited path from the current position, velocity and acceleration to an input acceleration.
550
/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.
551
/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_xy.
552
/// The jerk limit defines the acceleration error decay in the kinematic path as the system approaches constant acceleration.
553
/// The jerk limit also defines the time taken to achieve the maximum acceleration.
554
void AC_PosControl::input_accel_xy(const Vector3f& accel)
555
{
556
update_pos_vel_accel_xy(_pos_desired.xy(), _vel_desired.xy(), _accel_desired.xy(), _dt, _limit_vector.xy(), _p_pos_xy.get_error(), _pid_vel_xy.get_error());
557
shape_accel_xy(accel.xy(), _accel_desired.xy(), _jerk_max_xy_cmsss, _dt);
558
}
559
560
/// input_vel_accel_xy - calculate a jerk limited path from the current position, velocity and acceleration to an input velocity and acceleration.
561
/// The vel is projected forwards in time based on a time step of dt and acceleration accel.
562
/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.
563
/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_xy.
564
/// The parameter limit_output specifies if the velocity and acceleration limits are applied to the sum of commanded and correction values or just correction.
565
void AC_PosControl::input_vel_accel_xy(Vector2f& vel, const Vector2f& accel, bool limit_output)
566
{
567
update_pos_vel_accel_xy(_pos_desired.xy(), _vel_desired.xy(), _accel_desired.xy(), _dt, _limit_vector.xy(), _p_pos_xy.get_error(), _pid_vel_xy.get_error());
568
569
shape_vel_accel_xy(vel, accel, _vel_desired.xy(), _accel_desired.xy(),
570
_accel_max_xy_cmss, _jerk_max_xy_cmsss, _dt, limit_output);
571
572
update_vel_accel_xy(vel, accel, _dt, Vector2f(), Vector2f());
573
}
574
575
/// input_pos_vel_accel_xy - calculate a jerk limited path from the current position, velocity and acceleration to an input position velocity and acceleration.
576
/// The pos and vel are projected forwards in time based on a time step of dt and acceleration accel.
577
/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.
578
/// The function alters the pos and vel to be the kinematic path based on accel
579
/// The parameter limit_output specifies if the velocity and acceleration limits are applied to the sum of commanded and correction values or just correction.
580
void AC_PosControl::input_pos_vel_accel_xy(Vector2p& pos, Vector2f& vel, const Vector2f& accel, bool limit_output)
581
{
582
update_pos_vel_accel_xy(_pos_desired.xy(), _vel_desired.xy(), _accel_desired.xy(), _dt, _limit_vector.xy(), _p_pos_xy.get_error(), _pid_vel_xy.get_error());
583
584
shape_pos_vel_accel_xy(pos, vel, accel, _pos_desired.xy(), _vel_desired.xy(), _accel_desired.xy(),
585
_vel_max_xy_cms, _accel_max_xy_cmss, _jerk_max_xy_cmsss, _dt, limit_output);
586
587
update_pos_vel_accel_xy(pos, vel, accel, _dt, Vector2f(), Vector2f(), Vector2f());
588
}
589
590
/// update the horizontal position and velocity offsets
591
/// this moves the offsets (e.g _pos_offset, _vel_offset, _accel_offset) towards the targets (e.g. _pos_offset_target, _vel_offset_target, _accel_offset_target)
592
void AC_PosControl::update_offsets_xy()
593
{
594
// check for offset target timeout
595
uint32_t now_ms = AP_HAL::millis();
596
if (now_ms - _posvelaccel_offset_target_xy_ms > POSCONTROL_POSVELACCEL_OFFSET_TARGET_TIMEOUT_MS) {
597
_pos_offset_target.xy().zero();
598
_vel_offset_target.xy().zero();
599
_accel_offset_target.xy().zero();
600
}
601
602
// update position, velocity, accel offsets for this iteration
603
update_pos_vel_accel_xy(_pos_offset_target.xy(), _vel_offset_target.xy(), _accel_offset_target.xy(), _dt, Vector2f(), Vector2f(), Vector2f());
604
update_pos_vel_accel_xy(_pos_offset.xy(), _vel_offset.xy(), _accel_offset.xy(), _dt, _limit_vector.xy(), _p_pos_xy.get_error(), _pid_vel_xy.get_error());
605
606
// input shape horizontal position, velocity and acceleration offsets
607
shape_pos_vel_accel_xy(_pos_offset_target.xy(), _vel_offset_target.xy(), _accel_offset_target.xy(),
608
_pos_offset.xy(), _vel_offset.xy(), _accel_offset.xy(),
609
_vel_max_xy_cms, _accel_max_xy_cmss, _jerk_max_xy_cmsss, _dt, false);
610
}
611
612
/// stop_pos_xy_stabilisation - sets the target to the current position to remove any position corrections from the system
613
void AC_PosControl::stop_pos_xy_stabilisation()
614
{
615
_pos_target.xy() = _inav.get_position_xy_cm().topostype();
616
_pos_desired.xy() = _pos_target.xy() - _pos_offset.xy();
617
}
618
619
/// stop_vel_xy_stabilisation - sets the target to the current position and velocity to the current velocity to remove any position and velocity corrections from the system
620
void AC_PosControl::stop_vel_xy_stabilisation()
621
{
622
_pos_target.xy() = _inav.get_position_xy_cm().topostype();
623
_pos_desired.xy() = _pos_target.xy() - _pos_offset.xy();
624
625
_vel_target.xy() = _inav.get_velocity_xy_cms();;
626
_vel_desired.xy() = _vel_target.xy() - _vel_offset.xy();
627
628
// initialise I terms from lean angles
629
_pid_vel_xy.reset_filter();
630
_pid_vel_xy.reset_I();
631
}
632
633
// is_active_xy - returns true if the xy position controller has been run in the previous loop
634
bool AC_PosControl::is_active_xy() const
635
{
636
const uint32_t dt_ticks = AP::scheduler().ticks32() - _last_update_xy_ticks;
637
return dt_ticks <= 1;
638
}
639
640
/// update_xy_controller - runs the horizontal position controller correcting position, velocity and acceleration errors.
641
/// Position and velocity errors are converted to velocity and acceleration targets using PID objects
642
/// Desired velocity and accelerations are added to these corrections as they are calculated
643
/// Kinematically consistent target position and desired velocity and accelerations should be provided before calling this function
644
void AC_PosControl::update_xy_controller()
645
{
646
// check for ekf xy position reset
647
handle_ekf_xy_reset();
648
649
// Check for position control time out
650
if (!is_active_xy()) {
651
init_xy_controller();
652
if (has_good_timing()) {
653
// call internal error because initialisation has not been done
654
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
655
}
656
}
657
_last_update_xy_ticks = AP::scheduler().ticks32();
658
659
float ahrsGndSpdLimit, ahrsControlScaleXY;
660
AP::ahrs().getControlLimits(ahrsGndSpdLimit, ahrsControlScaleXY);
661
662
// update the position, velocity and acceleration offsets
663
update_offsets_xy();
664
665
// Position Controller
666
667
_pos_target.xy() = _pos_desired.xy() + _pos_offset.xy();
668
669
// determine the combined position of the actual position and the disturbance from system ID mode
670
const Vector3f &curr_pos = _inav.get_position_neu_cm();
671
Vector3f comb_pos = curr_pos;
672
comb_pos.xy() += _disturb_pos;
673
674
Vector2f vel_target = _p_pos_xy.update_all(_pos_target.x, _pos_target.y, comb_pos);
675
_pos_desired.xy() = _pos_target.xy() - _pos_offset.xy();
676
677
// Velocity Controller
678
679
// add velocity feed-forward scaled to compensate for optical flow measurement induced EKF noise
680
vel_target *= ahrsControlScaleXY;
681
682
_vel_target.xy() = vel_target;
683
_vel_target.xy() += _vel_desired.xy() + _vel_offset.xy();
684
685
// determine the combined velocity of the actual velocity and the disturbance from system ID mode
686
const Vector2f &curr_vel = _inav.get_velocity_xy_cms();
687
Vector2f comb_vel = curr_vel;
688
comb_vel += _disturb_vel;
689
690
Vector2f accel_target = _pid_vel_xy.update_all(_vel_target.xy(), comb_vel, _dt, _limit_vector.xy());
691
692
// Acceleration Controller
693
694
// acceleration to correct for velocity error and scale PID output to compensate for optical flow measurement induced EKF noise
695
accel_target *= ahrsControlScaleXY;
696
697
// pass the correction acceleration to the target acceleration output
698
_accel_target.xy() = accel_target;
699
_accel_target.xy() += _accel_desired.xy() + _accel_offset.xy();
700
701
// limit acceleration using maximum lean angles
702
float angle_max = MIN(_attitude_control.get_althold_lean_angle_max_cd(), get_lean_angle_max_cd());
703
float accel_max = angle_to_accel(angle_max * 0.01) * 100;
704
// Define the limit vector before we constrain _accel_target
705
_limit_vector.xy() = _accel_target.xy();
706
if (!limit_accel_xy(_vel_desired.xy(), _accel_target.xy(), accel_max)) {
707
// _accel_target was not limited so we can zero the xy limit vector
708
_limit_vector.xy().zero();
709
} else {
710
// Check for pitch limiting in the forward direction
711
const float accel_fwd_unlimited = _limit_vector.x * _ahrs.cos_yaw() + _limit_vector.y * _ahrs.sin_yaw();
712
const float pitch_target_unlimited = accel_to_angle(- MIN(accel_fwd_unlimited, accel_max) * 0.01f) * 100;
713
const float accel_fwd_limited = _accel_target.x * _ahrs.cos_yaw() + _accel_target.y * _ahrs.sin_yaw();
714
const float pitch_target_limited = accel_to_angle(- accel_fwd_limited * 0.01f) * 100;
715
_fwd_pitch_is_limited = is_negative(pitch_target_unlimited) && pitch_target_unlimited < pitch_target_limited;
716
}
717
718
// update angle targets that will be passed to stabilize controller
719
accel_to_lean_angles(_accel_target.x, _accel_target.y, _roll_target, _pitch_target);
720
calculate_yaw_and_rate_yaw();
721
722
// reset the disturbance from system ID mode to zero
723
_disturb_pos.zero();
724
_disturb_vel.zero();
725
}
726
727
728
///
729
/// Vertical position controller
730
///
731
732
/// set_max_speed_accel_z - set the maximum vertical speed in cm/s and acceleration in cm/s/s
733
/// speed_down can be positive or negative but will always be interpreted as a descent speed.
734
/// This function only needs to be called if using the kinematic shaping.
735
/// This can be done at any time as changes in these parameters are handled smoothly
736
/// by the kinematic shaping.
737
void AC_PosControl::set_max_speed_accel_z(float speed_down, float speed_up, float accel_cmss)
738
{
739
// ensure speed_down is always negative
740
speed_down = -fabsf(speed_down);
741
742
// sanity check and update
743
if (is_negative(speed_down)) {
744
_vel_max_down_cms = speed_down;
745
}
746
if (is_positive(speed_up)) {
747
_vel_max_up_cms = speed_up;
748
}
749
if (is_positive(accel_cmss)) {
750
_accel_max_z_cmss = accel_cmss;
751
}
752
753
// ensure the vertical Jerk is not limited by the filters in the Z accel PID object
754
_jerk_max_z_cmsss = _shaping_jerk_z * 100.0;
755
if (is_positive(_pid_accel_z.filt_T_hz())) {
756
_jerk_max_z_cmsss = MIN(_jerk_max_z_cmsss, MIN(GRAVITY_MSS * 100.0, _accel_max_z_cmss) * (M_2PI * _pid_accel_z.filt_T_hz()) / 5.0);
757
}
758
if (is_positive(_pid_accel_z.filt_E_hz())) {
759
_jerk_max_z_cmsss = MIN(_jerk_max_z_cmsss, MIN(GRAVITY_MSS * 100.0, _accel_max_z_cmss) * (M_2PI * _pid_accel_z.filt_E_hz()) / 5.0);
760
}
761
}
762
763
/// set_correction_speed_accel_z - set the position controller correction velocity and acceleration limit
764
/// speed_down can be positive or negative but will always be interpreted as a descent speed.
765
/// This should be done only during initialisation to avoid discontinuities
766
void AC_PosControl::set_correction_speed_accel_z(float speed_down, float speed_up, float accel_cmss)
767
{
768
// define maximum position error and maximum first and second differential limits
769
_p_pos_z.set_limits(-fabsf(speed_down), speed_up, accel_cmss, 0.0f);
770
}
771
772
/// init_z_controller - initialise the position controller to the current position, velocity, acceleration and attitude.
773
/// This function is the default initialisation for any position control that provides position, velocity and acceleration.
774
/// This function does not allow any negative velocity or acceleration
775
void AC_PosControl::init_z_controller_no_descent()
776
{
777
// Initialise the position controller to the current throttle, position, velocity and acceleration.
778
init_z_controller();
779
780
// remove all descent if present
781
_vel_target.z = MAX(0.0, _vel_target.z);
782
_vel_desired.z = MAX(0.0, _vel_desired.z);
783
_vel_terrain = MAX(0.0, _vel_terrain);
784
_vel_offset.z = MAX(0.0, _vel_offset.z);
785
_accel_target.z = MAX(0.0, _accel_target.z);
786
_accel_desired.z = MAX(0.0, _accel_desired.z);
787
_accel_terrain = MAX(0.0, _accel_terrain);
788
_accel_offset.z = MAX(0.0, _accel_offset.z);
789
}
790
791
/// init_z_controller_stopping_point - initialise the position controller to the stopping point with zero velocity and acceleration.
792
/// This function should be used when the expected kinematic path assumes a stationary initial condition but does not specify a specific starting position.
793
/// The starting position can be retrieved by getting the position target using get_pos_target_cm() after calling this function.
794
void AC_PosControl::init_z_controller_stopping_point()
795
{
796
// Initialise the position controller to the current throttle, position, velocity and acceleration.
797
init_z_controller();
798
799
get_stopping_point_z_cm(_pos_desired.z);
800
_pos_target.z = _pos_desired.z + _pos_offset.z;
801
_vel_desired.z = 0.0f;
802
_accel_desired.z = 0.0f;
803
}
804
805
// relax_z_controller - initialise the position controller to the current position and velocity with decaying acceleration.
806
/// This function decays the output acceleration by 95% every half second to achieve a smooth transition to zero requested acceleration.
807
void AC_PosControl::relax_z_controller(float throttle_setting)
808
{
809
// Initialise the position controller to the current position, velocity and acceleration.
810
init_z_controller();
811
812
// init_z_controller has set the accel PID I term to generate the current throttle set point
813
// Use relax_integrator to decay the throttle set point to throttle_setting
814
_pid_accel_z.relax_integrator((throttle_setting - _motors.get_throttle_hover()) * 1000.0f, _dt, POSCONTROL_RELAX_TC);
815
}
816
817
/// init_z_controller - initialise the position controller to the current position, velocity, acceleration and attitude.
818
/// This function is the default initialisation for any position control that provides position, velocity and acceleration.
819
/// This function is private and contains all the shared z axis initialisation functions
820
void AC_PosControl::init_z_controller()
821
{
822
// initialise terrain targets and offsets to zero
823
init_terrain();
824
825
// initialise offsets to target offsets and ensure offset targets are zero if they have not been updated.
826
init_offsets_z();
827
828
_pos_target.z = _inav.get_position_z_up_cm();
829
_pos_desired.z = _pos_target.z - _pos_offset.z;
830
831
_vel_target.z = _inav.get_velocity_z_up_cms();
832
_vel_desired.z = _vel_target.z - _vel_offset.z;
833
834
// Reset I term of velocity PID
835
_pid_vel_z.reset_filter();
836
_pid_vel_z.set_integrator(0.0f);
837
838
_accel_target.z = constrain_float(get_z_accel_cmss(), -_accel_max_z_cmss, _accel_max_z_cmss);
839
_accel_desired.z = _accel_target.z - (_accel_offset.z + _accel_terrain);
840
_pid_accel_z.reset_filter();
841
842
// Set accel PID I term based on the current throttle
843
// Remove the expected P term due to _accel_desired.z being constrained to _accel_max_z_cmss
844
// Remove the expected FF term due to non-zero _accel_target.z
845
_pid_accel_z.set_integrator((_attitude_control.get_throttle_in() - _motors.get_throttle_hover()) * 1000.0f
846
- _pid_accel_z.kP() * (_accel_target.z - get_z_accel_cmss())
847
- _pid_accel_z.ff() * _accel_target.z);
848
849
// initialise ekf z reset handler
850
init_ekf_z_reset();
851
852
// initialise z_controller time out
853
_last_update_z_ticks = AP::scheduler().ticks32();
854
}
855
856
/// input_accel_z - calculate a jerk limited path from the current position, velocity and acceleration to an input acceleration.
857
/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.
858
void AC_PosControl::input_accel_z(float accel)
859
{
860
// calculated increased maximum jerk if over speed
861
float jerk_max_z_cmsss = _jerk_max_z_cmsss * calculate_overspeed_gain();
862
863
// adjust desired alt if motors have not hit their limits
864
update_pos_vel_accel(_pos_desired.z, _vel_desired.z, _accel_desired.z, _dt, _limit_vector.z, _p_pos_z.get_error(), _pid_vel_z.get_error());
865
866
shape_accel(accel, _accel_desired.z, jerk_max_z_cmsss, _dt);
867
}
868
869
/// input_accel_z - calculate a jerk limited path from the current position, velocity and acceleration to an input acceleration.
870
/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.
871
/// The kinematic path is constrained by the maximum acceleration and jerk set using the function set_max_speed_accel_z.
872
/// The parameter limit_output specifies if the velocity and acceleration limits are applied to the sum of commanded and correction values or just correction.
873
void AC_PosControl::input_vel_accel_z(float &vel, float accel, bool limit_output)
874
{
875
// calculated increased maximum acceleration and jerk if over speed
876
const float overspeed_gain = calculate_overspeed_gain();
877
const float accel_max_z_cmss = _accel_max_z_cmss * overspeed_gain;
878
const float jerk_max_z_cmsss = _jerk_max_z_cmsss * overspeed_gain;
879
880
// adjust desired alt if motors have not hit their limits
881
update_pos_vel_accel(_pos_desired.z, _vel_desired.z, _accel_desired.z, _dt, _limit_vector.z, _p_pos_z.get_error(), _pid_vel_z.get_error());
882
883
shape_vel_accel(vel, accel,
884
_vel_desired.z, _accel_desired.z,
885
-constrain_float(accel_max_z_cmss, 0.0f, 750.0f), accel_max_z_cmss,
886
jerk_max_z_cmsss, _dt, limit_output);
887
888
update_vel_accel(vel, accel, _dt, 0.0, 0.0);
889
}
890
891
/// set_pos_target_z_from_climb_rate_cm - adjusts target up or down using a commanded climb rate in cm/s
892
/// using the default position control kinematic path.
893
/// The zero target altitude is varied to follow pos_offset_z
894
void AC_PosControl::set_pos_target_z_from_climb_rate_cm(float vel)
895
{
896
input_vel_accel_z(vel, 0.0);
897
}
898
899
/// land_at_climb_rate_cm - adjusts target up or down using a commanded climb rate in cm/s
900
/// using the default position control kinematic path.
901
/// ignore_descent_limit turns off output saturation handling to aid in landing detection. ignore_descent_limit should be false unless landing.
902
void AC_PosControl::land_at_climb_rate_cm(float vel, bool ignore_descent_limit)
903
{
904
if (ignore_descent_limit) {
905
// turn off limits in the negative z direction
906
_limit_vector.z = MAX(_limit_vector.z, 0.0f);
907
}
908
909
input_vel_accel_z(vel, 0.0);
910
}
911
912
/// input_pos_vel_accel_z - calculate a jerk limited path from the current position, velocity and acceleration to an input position velocity and acceleration.
913
/// The pos and vel are projected forwards in time based on a time step of dt and acceleration accel.
914
/// The function takes the current position, velocity, and acceleration and calculates the required jerk limited adjustment to the acceleration for the next time dt.
915
/// The function alters the pos and vel to be the kinematic path based on accel
916
/// The parameter limit_output specifies if the velocity and acceleration limits are applied to the sum of commanded and correction values or just correction.
917
void AC_PosControl::input_pos_vel_accel_z(float &pos, float &vel, float accel, bool limit_output)
918
{
919
// calculated increased maximum acceleration and jerk if over speed
920
const float overspeed_gain = calculate_overspeed_gain();
921
const float accel_max_z_cmss = _accel_max_z_cmss * overspeed_gain;
922
const float jerk_max_z_cmsss = _jerk_max_z_cmsss * overspeed_gain;
923
924
// adjust desired altitude if motors have not hit their limits
925
update_pos_vel_accel(_pos_desired.z, _vel_desired.z, _accel_desired.z, _dt, _limit_vector.z, _p_pos_z.get_error(), _pid_vel_z.get_error());
926
927
shape_pos_vel_accel(pos, vel, accel,
928
_pos_desired.z, _vel_desired.z, _accel_desired.z,
929
_vel_max_down_cms, _vel_max_up_cms,
930
-constrain_float(accel_max_z_cmss, 0.0f, 750.0f), accel_max_z_cmss,
931
jerk_max_z_cmsss, _dt, limit_output);
932
933
postype_t posp = pos;
934
update_pos_vel_accel(posp, vel, accel, _dt, 0.0, 0.0, 0.0);
935
pos = posp;
936
}
937
938
/// set_alt_target_with_slew - adjusts target up or down using a commanded altitude in cm
939
/// using the default position control kinematic path.
940
void AC_PosControl::set_alt_target_with_slew(float pos)
941
{
942
float zero = 0;
943
input_pos_vel_accel_z(pos, zero, 0);
944
}
945
946
/// update_offsets_z - updates the vertical offsets used by terrain following
947
void AC_PosControl::update_offsets_z()
948
{
949
// check for offset target timeout
950
uint32_t now_ms = AP_HAL::millis();
951
if (now_ms - _posvelaccel_offset_target_z_ms > POSCONTROL_POSVELACCEL_OFFSET_TARGET_TIMEOUT_MS) {
952
_pos_offset_target.z = 0.0;
953
_vel_offset_target.z = 0.0;
954
_accel_offset_target.z = 0.0;
955
}
956
957
// update position, velocity, accel offsets for this iteration
958
postype_t p_offset_z = _pos_offset.z;
959
update_pos_vel_accel(p_offset_z, _vel_offset.z, _accel_offset.z, _dt, MIN(_limit_vector.z, 0.0f), _p_pos_z.get_error(), _pid_vel_z.get_error());
960
_pos_offset.z = p_offset_z;
961
962
// input shape vertical position, velocity and acceleration offsets
963
shape_pos_vel_accel(_pos_offset_target.z, _vel_offset_target.z, _accel_offset_target.z,
964
_pos_offset.z, _vel_offset.z, _accel_offset.z,
965
get_max_speed_down_cms(), get_max_speed_up_cms(),
966
-get_max_accel_z_cmss(), get_max_accel_z_cmss(),
967
_jerk_max_z_cmsss, _dt, false);
968
969
p_offset_z = _pos_offset_target.z;
970
update_pos_vel_accel(p_offset_z, _vel_offset_target.z, _accel_offset_target.z, _dt, 0.0, 0.0, 0.0);
971
_pos_offset_target.z = p_offset_z;
972
}
973
974
// is_active_z - returns true if the z position controller has been run in the previous loop
975
bool AC_PosControl::is_active_z() const
976
{
977
const uint32_t dt_ticks = AP::scheduler().ticks32() - _last_update_z_ticks;
978
return dt_ticks <= 1;
979
}
980
981
/// update_z_controller - runs the vertical position controller correcting position, velocity and acceleration errors.
982
/// Position and velocity errors are converted to velocity and acceleration targets using PID objects
983
/// Desired velocity and accelerations are added to these corrections as they are calculated
984
/// Kinematically consistent target position and desired velocity and accelerations should be provided before calling this function
985
void AC_PosControl::update_z_controller()
986
{
987
// check for ekf z-axis position reset
988
handle_ekf_z_reset();
989
990
// Check for z_controller time out
991
if (!is_active_z()) {
992
init_z_controller();
993
if (has_good_timing()) {
994
// call internal error because initialisation has not been done
995
INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control);
996
}
997
}
998
_last_update_z_ticks = AP::scheduler().ticks32();
999
1000
// update the position, velocity and acceleration offsets
1001
update_offsets_z();
1002
update_terrain();
1003
_pos_target.z = _pos_desired.z + _pos_offset.z + _pos_terrain;
1004
1005
// calculate the target velocity correction
1006
float pos_target_zf = _pos_target.z;
1007
1008
_vel_target.z = _p_pos_z.update_all(pos_target_zf, _inav.get_position_z_up_cm());
1009
_vel_target.z *= AP::ahrs().getControlScaleZ();
1010
1011
_pos_target.z = pos_target_zf;
1012
_pos_desired.z = _pos_target.z - (_pos_offset.z + _pos_terrain);
1013
1014
// add feed forward component
1015
_vel_target.z += _vel_desired.z + _vel_offset.z + _vel_terrain;
1016
1017
// Velocity Controller
1018
1019
const float curr_vel_z = _inav.get_velocity_z_up_cms();
1020
_accel_target.z = _pid_vel_z.update_all(_vel_target.z, curr_vel_z, _dt, _motors.limit.throttle_lower, _motors.limit.throttle_upper);
1021
_accel_target.z *= AP::ahrs().getControlScaleZ();
1022
1023
// add feed forward component
1024
_accel_target.z += _accel_desired.z + _accel_offset.z + _accel_terrain;
1025
1026
// Acceleration Controller
1027
1028
// Calculate vertical acceleration
1029
const float z_accel_meas = get_z_accel_cmss();
1030
1031
// ensure imax is always large enough to overpower hover throttle
1032
if (_motors.get_throttle_hover() * 1000.0f > _pid_accel_z.imax()) {
1033
_pid_accel_z.set_imax(_motors.get_throttle_hover() * 1000.0f);
1034
}
1035
float thr_out;
1036
if (_vibe_comp_enabled) {
1037
thr_out = get_throttle_with_vibration_override();
1038
} else {
1039
thr_out = _pid_accel_z.update_all(_accel_target.z, z_accel_meas, _dt, (_motors.limit.throttle_lower || _motors.limit.throttle_upper)) * 0.001f;
1040
thr_out += _pid_accel_z.get_ff() * 0.001f;
1041
}
1042
thr_out += _motors.get_throttle_hover();
1043
1044
// Actuator commands
1045
1046
// send throttle to attitude controller with angle boost
1047
_attitude_control.set_throttle_out(thr_out, true, POSCONTROL_THROTTLE_CUTOFF_FREQ_HZ);
1048
1049
// Check for vertical controller health
1050
1051
// _speed_down_cms is checked to be non-zero when set
1052
float error_ratio = _pid_vel_z.get_error() / _vel_max_down_cms;
1053
_vel_z_control_ratio += _dt * 0.1f * (0.5 - error_ratio);
1054
_vel_z_control_ratio = constrain_float(_vel_z_control_ratio, 0.0f, 2.0f);
1055
1056
// set vertical component of the limit vector
1057
if (_motors.limit.throttle_upper) {
1058
_limit_vector.z = 1.0f;
1059
} else if (_motors.limit.throttle_lower) {
1060
_limit_vector.z = -1.0f;
1061
} else {
1062
_limit_vector.z = 0.0f;
1063
}
1064
}
1065
1066
1067
///
1068
/// Accessors
1069
///
1070
1071
/// get_lean_angle_max_cd - returns the maximum lean angle the autopilot may request
1072
float AC_PosControl::get_lean_angle_max_cd() const
1073
{
1074
if (is_positive(_angle_max_override_cd)) {
1075
return _angle_max_override_cd;
1076
}
1077
if (!is_positive(_lean_angle_max)) {
1078
return _attitude_control.lean_angle_max_cd();
1079
}
1080
return _lean_angle_max * 100.0f;
1081
}
1082
1083
/// set the desired position, velocity and acceleration targets
1084
void AC_PosControl::set_pos_vel_accel(const Vector3p& pos, const Vector3f& vel, const Vector3f& accel)
1085
{
1086
_pos_desired = pos;
1087
_vel_desired = vel;
1088
_accel_desired = accel;
1089
}
1090
1091
/// set the desired position, velocity and acceleration targets
1092
void AC_PosControl::set_pos_vel_accel_xy(const Vector2p& pos, const Vector2f& vel, const Vector2f& accel)
1093
{
1094
_pos_desired.xy() = pos;
1095
_vel_desired.xy() = vel;
1096
_accel_desired.xy() = accel;
1097
}
1098
1099
// get_lean_angles_to_accel - convert roll, pitch lean target angles to lat/lon frame accelerations in cm/s/s
1100
Vector3f AC_PosControl::lean_angles_to_accel(const Vector3f& att_target_euler) const
1101
{
1102
// rotate our roll, pitch angles into lat/lon frame
1103
const float sin_roll = sinf(att_target_euler.x);
1104
const float cos_roll = cosf(att_target_euler.x);
1105
const float sin_pitch = sinf(att_target_euler.y);
1106
const float cos_pitch = cosf(att_target_euler.y);
1107
const float sin_yaw = sinf(att_target_euler.z);
1108
const float cos_yaw = cosf(att_target_euler.z);
1109
1110
return Vector3f{
1111
(GRAVITY_MSS * 100.0f) * (-cos_yaw * sin_pitch * cos_roll - sin_yaw * sin_roll) / MAX(cos_roll * cos_pitch, 0.1f),
1112
(GRAVITY_MSS * 100.0f) * (-sin_yaw * sin_pitch * cos_roll + cos_yaw * sin_roll) / MAX(cos_roll * cos_pitch, 0.1f),
1113
(GRAVITY_MSS * 100.0f)
1114
};
1115
}
1116
1117
/// Terrain
1118
1119
/// set the terrain position, velocity and acceleration in cm, cms and cm/s/s from EKF origin in NE frame
1120
/// this is used to initiate the offsets when initialise the position controller or do an offset reset
1121
void AC_PosControl::init_terrain()
1122
{
1123
// set terrain position and target to zero
1124
_pos_terrain_target = 0.0;
1125
_pos_terrain = 0.0;
1126
1127
// set velocity offset to zero
1128
_vel_terrain = 0.0;
1129
1130
// set acceleration offset to zero
1131
_accel_terrain = 0.0;
1132
}
1133
1134
// init_pos_terrain_cm - initialises the current terrain altitude and target altitude to pos_offset_terrain_cm
1135
void AC_PosControl::init_pos_terrain_cm(float pos_terrain_cm)
1136
{
1137
_pos_desired.z -= (pos_terrain_cm - _pos_terrain);
1138
_pos_terrain_target = pos_terrain_cm;
1139
_pos_terrain = pos_terrain_cm;
1140
}
1141
1142
1143
/// Offsets
1144
1145
/// set the horizontal position, velocity and acceleration offsets in cm, cms and cm/s/s from EKF origin in NE frame
1146
/// this is used to initiate the offsets when initialise the position controller or do an offset reset
1147
void AC_PosControl::init_offsets_xy()
1148
{
1149
// check for offset target timeout
1150
uint32_t now_ms = AP_HAL::millis();
1151
if (now_ms - _posvelaccel_offset_target_xy_ms > POSCONTROL_POSVELACCEL_OFFSET_TARGET_TIMEOUT_MS) {
1152
_pos_offset_target.xy().zero();
1153
_vel_offset_target.xy().zero();
1154
_accel_offset_target.xy().zero();
1155
}
1156
1157
// set position offset to target
1158
_pos_offset.xy() = _pos_offset_target.xy();
1159
1160
// set velocity offset to target
1161
_vel_offset.xy() = _vel_offset_target.xy();
1162
1163
// set acceleration offset to target
1164
_accel_offset.xy() = _accel_offset_target.xy();
1165
}
1166
1167
/// set the horizontal position, velocity and acceleration offsets in cm, cms and cm/s/s from EKF origin in NE frame
1168
/// this is used to initiate the offsets when initialise the position controller or do an offset reset
1169
void AC_PosControl::init_offsets_z()
1170
{
1171
// check for offset target timeout
1172
uint32_t now_ms = AP_HAL::millis();
1173
if (now_ms - _posvelaccel_offset_target_z_ms > POSCONTROL_POSVELACCEL_OFFSET_TARGET_TIMEOUT_MS) {
1174
_pos_offset_target.z = 0.0;
1175
_vel_offset_target.z = 0.0;
1176
_accel_offset_target.z = 0.0;
1177
}
1178
// set position offset to target
1179
_pos_offset.z = _pos_offset_target.z;
1180
1181
// set velocity offset to target
1182
_vel_offset.z = _vel_offset_target.z;
1183
1184
// set acceleration offset to target
1185
_accel_offset.z = _accel_offset_target.z;
1186
}
1187
1188
#if AP_SCRIPTING_ENABLED
1189
// add an additional offset to vehicle's target position, velocity and acceleration
1190
// units are m, m/s and m/s/s in NED frame
1191
// Z-axis is not currently supported and is ignored
1192
bool AC_PosControl::set_posvelaccel_offset(const Vector3f &pos_offset_NED, const Vector3f &vel_offset_NED, const Vector3f &accel_offset_NED)
1193
{
1194
set_posvelaccel_offset_target_xy_cm(pos_offset_NED.topostype().xy() * 100.0, vel_offset_NED.xy() * 100.0, accel_offset_NED.xy() * 100.0);
1195
set_posvelaccel_offset_target_z_cm(-pos_offset_NED.topostype().z * 100.0, -vel_offset_NED.z * 100, -accel_offset_NED.z * 100.0);
1196
return true;
1197
}
1198
1199
// get position and velocity offset to vehicle's target velocity and acceleration
1200
// units are m and m/s in NED frame
1201
bool AC_PosControl::get_posvelaccel_offset(Vector3f &pos_offset_NED, Vector3f &vel_offset_NED, Vector3f &accel_offset_NED)
1202
{
1203
pos_offset_NED.xy() = _pos_offset_target.xy().tofloat() * 0.01;
1204
pos_offset_NED.z = -_pos_offset_target.z * 0.01;
1205
vel_offset_NED.xy() = _vel_offset_target.xy() * 0.01;
1206
vel_offset_NED.z = -_vel_offset_target.z * 0.01;
1207
accel_offset_NED.xy() = _accel_offset_target.xy() * 0.01;
1208
accel_offset_NED.z = -_accel_offset_target.z * 0.01;
1209
return true;
1210
}
1211
1212
// get target velocity in m/s in NED frame
1213
bool AC_PosControl::get_vel_target(Vector3f &vel_target_NED)
1214
{
1215
if (!is_active_xy() || !is_active_z()) {
1216
return false;
1217
}
1218
vel_target_NED.xy() = _vel_target.xy() * 0.01;
1219
vel_target_NED.z = -_vel_target.z * 0.01;
1220
return true;
1221
}
1222
1223
// get target acceleration in m/s/s in NED frame
1224
bool AC_PosControl::get_accel_target(Vector3f &accel_target_NED)
1225
{
1226
if (!is_active_xy() || !is_active_z()) {
1227
return false;
1228
}
1229
accel_target_NED.xy() = _accel_target.xy() * 0.01;
1230
accel_target_NED.z = -_accel_target.z * 0.01;
1231
return true;
1232
}
1233
#endif
1234
1235
/// set the horizontal position, velocity and acceleration offset targets in cm, cms and cm/s/s from EKF origin in NE frame
1236
/// these must be set every 3 seconds (or less) or they will timeout and return to zero
1237
void AC_PosControl::set_posvelaccel_offset_target_xy_cm(const Vector2p& pos_offset_target_xy_cm, const Vector2f& vel_offset_target_xy_cms, const Vector2f& accel_offset_target_xy_cmss)
1238
{
1239
// set position offset target
1240
_pos_offset_target.xy() = pos_offset_target_xy_cm;
1241
1242
// set velocity offset target
1243
_vel_offset_target.xy() = vel_offset_target_xy_cms;
1244
1245
// set acceleration offset target
1246
_accel_offset_target.xy() = accel_offset_target_xy_cmss;
1247
1248
// record time of update so we can detect timeouts
1249
_posvelaccel_offset_target_xy_ms = AP_HAL::millis();
1250
}
1251
1252
/// set the vertical position, velocity and acceleration offset targets in cm, cms and cm/s/s from EKF origin in NE frame
1253
/// these must be set every 3 seconds (or less) or they will timeout and return to zero
1254
void AC_PosControl::set_posvelaccel_offset_target_z_cm(float pos_offset_target_z_cm, float vel_offset_target_z_cms, const float accel_offset_target_z_cmss)
1255
{
1256
// set position offset target
1257
_pos_offset_target.z = pos_offset_target_z_cm;
1258
1259
// set velocity offset target
1260
_vel_offset_target.z = vel_offset_target_z_cms;
1261
1262
// set acceleration offset target
1263
_accel_offset_target.z = accel_offset_target_z_cmss;
1264
1265
// record time of update so we can detect timeouts
1266
_posvelaccel_offset_target_z_ms = AP_HAL::millis();
1267
}
1268
1269
// returns the NED target acceleration vector for attitude control
1270
Vector3f AC_PosControl::get_thrust_vector() const
1271
{
1272
Vector3f accel_target = get_accel_target_cmss();
1273
accel_target.z = -GRAVITY_MSS * 100.0f;
1274
return accel_target;
1275
}
1276
1277
/// get_stopping_point_xy_cm - calculates stopping point in NEU cm based on current position, velocity, vehicle acceleration
1278
/// function does not change the z axis
1279
void AC_PosControl::get_stopping_point_xy_cm(Vector2p &stopping_point) const
1280
{
1281
// todo: we should use the current target position and velocity if we are currently running the position controller
1282
stopping_point = _inav.get_position_xy_cm().topostype();
1283
stopping_point -= _pos_offset.xy();
1284
1285
Vector2f curr_vel = _inav.get_velocity_xy_cms();
1286
curr_vel -= _vel_offset.xy();
1287
1288
// calculate current velocity
1289
float vel_total = curr_vel.length();
1290
1291
if (!is_positive(vel_total)) {
1292
return;
1293
}
1294
1295
float kP = _p_pos_xy.kP();
1296
const float stopping_dist = stopping_distance(constrain_float(vel_total, 0.0, _vel_max_xy_cms), kP, _accel_max_xy_cmss);
1297
if (!is_positive(stopping_dist)) {
1298
return;
1299
}
1300
1301
// convert the stopping distance into a stopping point using velocity vector
1302
const float t = stopping_dist / vel_total;
1303
stopping_point += (curr_vel * t).topostype();
1304
}
1305
1306
/// get_stopping_point_z_cm - calculates stopping point in NEU cm based on current position, velocity, vehicle acceleration
1307
void AC_PosControl::get_stopping_point_z_cm(postype_t &stopping_point) const
1308
{
1309
float curr_pos_z = _inav.get_position_z_up_cm();
1310
curr_pos_z -= _pos_offset.z;
1311
1312
float curr_vel_z = _inav.get_velocity_z_up_cms();
1313
curr_vel_z -= _vel_offset.z;
1314
1315
// avoid divide by zero by using current position if kP is very low or acceleration is zero
1316
if (!is_positive(_p_pos_z.kP()) || !is_positive(_accel_max_z_cmss)) {
1317
stopping_point = curr_pos_z;
1318
return;
1319
}
1320
1321
stopping_point = curr_pos_z + constrain_float(stopping_distance(curr_vel_z, _p_pos_z.kP(), _accel_max_z_cmss), - POSCONTROL_STOPPING_DIST_DOWN_MAX, POSCONTROL_STOPPING_DIST_UP_MAX);
1322
}
1323
1324
/// get_bearing_to_target_cd - get bearing to target position in centi-degrees
1325
int32_t AC_PosControl::get_bearing_to_target_cd() const
1326
{
1327
return get_bearing_cd(_inav.get_position_xy_cm(), _pos_target.tofloat().xy());
1328
}
1329
1330
1331
///
1332
/// System methods
1333
///
1334
1335
// get throttle using vibration-resistant calculation (uses feed forward with manually calculated gain)
1336
float AC_PosControl::get_throttle_with_vibration_override()
1337
{
1338
const float thr_per_accelz_cmss = _motors.get_throttle_hover() / (GRAVITY_MSS * 100.0f);
1339
// during vibration compensation use feed forward with manually calculated gain
1340
// ToDo: clear pid_info P, I and D terms for logging
1341
if (!(_motors.limit.throttle_lower || _motors.limit.throttle_upper) || ((is_positive(_pid_accel_z.get_i()) && is_negative(_pid_vel_z.get_error())) || (is_negative(_pid_accel_z.get_i()) && is_positive(_pid_vel_z.get_error())))) {
1342
_pid_accel_z.set_integrator(_pid_accel_z.get_i() + _dt * thr_per_accelz_cmss * 1000.0f * _pid_vel_z.get_error() * _pid_vel_z.kP() * POSCONTROL_VIBE_COMP_I_GAIN);
1343
}
1344
return POSCONTROL_VIBE_COMP_P_GAIN * thr_per_accelz_cmss * _accel_target.z + _pid_accel_z.get_i() * 0.001f;
1345
}
1346
1347
/// standby_xyz_reset - resets I terms and removes position error
1348
/// This function will let Loiter and Alt Hold continue to operate
1349
/// in the event that the flight controller is in control of the
1350
/// aircraft when in standby.
1351
void AC_PosControl::standby_xyz_reset()
1352
{
1353
// Set _pid_accel_z integrator to zero.
1354
_pid_accel_z.set_integrator(0.0f);
1355
1356
// Set the target position to the current pos.
1357
_pos_target = _inav.get_position_neu_cm().topostype();
1358
1359
// Set _pid_vel_xy integrator and derivative to zero.
1360
_pid_vel_xy.reset_filter();
1361
1362
// initialise ekf xy reset handler
1363
init_ekf_xy_reset();
1364
}
1365
1366
#if HAL_LOGGING_ENABLED
1367
// write PSC and/or PSCZ logs
1368
void AC_PosControl::write_log()
1369
{
1370
if (is_active_xy()) {
1371
float accel_x, accel_y;
1372
lean_angles_to_accel_xy(accel_x, accel_y);
1373
Write_PSCN(_pos_desired.x, _pos_target.x, _inav.get_position_neu_cm().x,
1374
_vel_desired.x, _vel_target.x, _inav.get_velocity_neu_cms().x,
1375
_accel_desired.x, _accel_target.x, accel_x);
1376
Write_PSCE(_pos_desired.y, _pos_target.y, _inav.get_position_neu_cm().y,
1377
_vel_desired.y, _vel_target.y, _inav.get_velocity_neu_cms().y,
1378
_accel_desired.y, _accel_target.y, accel_y);
1379
1380
// log offsets if they are being used
1381
if (!_pos_offset.xy().is_zero()) {
1382
Write_PSON(_pos_offset_target.x, _pos_offset.x, _vel_offset_target.x, _vel_offset.x, _accel_offset_target.x, _accel_offset.x);
1383
Write_PSOE(_pos_offset_target.y, _pos_offset.y, _vel_offset_target.y, _vel_offset.y, _accel_offset_target.y, _accel_offset.y);
1384
}
1385
}
1386
1387
if (is_active_z()) {
1388
Write_PSCD(-_pos_desired.z, -_pos_target.z, -_inav.get_position_z_up_cm(),
1389
-_vel_desired.z, -_vel_target.z, -_inav.get_velocity_z_up_cms(),
1390
-_accel_desired.z, -_accel_target.z, -get_z_accel_cmss());
1391
1392
// log down and terrain offsets if they are being used
1393
if (!is_zero(_pos_offset.z)) {
1394
Write_PSOD(-_pos_offset_target.z, -_pos_offset.z, -_vel_offset_target.z, -_vel_offset.z, -_accel_offset_target.z, -_accel_offset.z);
1395
}
1396
if (!is_zero(_pos_terrain)) {
1397
Write_PSOT(-_pos_terrain_target, -_pos_terrain, 0, -_vel_terrain, 0, -_accel_terrain);
1398
}
1399
}
1400
}
1401
#endif // HAL_LOGGING_ENABLED
1402
1403
/// crosstrack_error - returns horizontal error to the closest point to the current track
1404
float AC_PosControl::crosstrack_error() const
1405
{
1406
const Vector2f pos_error = _inav.get_position_xy_cm() - (_pos_target.xy()).tofloat();
1407
if (is_zero(_vel_desired.xy().length_squared())) {
1408
// crosstrack is the horizontal distance to target when stationary
1409
return pos_error.length();
1410
} else {
1411
// crosstrack is the horizontal distance to the closest point to the current track
1412
const Vector2f vel_unit = _vel_desired.xy().normalized();
1413
const float dot_error = pos_error * vel_unit;
1414
1415
// todo: remove MAX of zero when safe_sqrt fixed
1416
return safe_sqrt(MAX(pos_error.length_squared() - sq(dot_error), 0.0));
1417
}
1418
}
1419
1420
///
1421
/// private methods
1422
///
1423
1424
/// Terrain
1425
1426
/// update_z_offsets - updates the vertical offsets used by terrain following
1427
void AC_PosControl::update_terrain()
1428
{
1429
// update position, velocity, accel offsets for this iteration
1430
postype_t pos_terrain = _pos_terrain;
1431
update_pos_vel_accel(pos_terrain, _vel_terrain, _accel_terrain, _dt, MIN(_limit_vector.z, 0.0f), _p_pos_z.get_error(), _pid_vel_z.get_error());
1432
_pos_terrain = pos_terrain;
1433
1434
// input shape horizontal position, velocity and acceleration offsets
1435
shape_pos_vel_accel(_pos_terrain_target, 0.0, 0.0,
1436
_pos_terrain, _vel_terrain, _accel_terrain,
1437
get_max_speed_down_cms(), get_max_speed_up_cms(),
1438
-get_max_accel_z_cmss(), get_max_accel_z_cmss(),
1439
_jerk_max_z_cmsss, _dt, false);
1440
1441
// we do not have to update _pos_terrain_target because we assume the target velocity and acceleration are zero
1442
// if we know how fast the terain altitude is changing we would add update_pos_vel_accel for _pos_terrain_target here
1443
}
1444
1445
// get_lean_angles_to_accel - convert roll, pitch lean angles to NE frame accelerations in cm/s/s
1446
void AC_PosControl::accel_to_lean_angles(float accel_x_cmss, float accel_y_cmss, float& roll_target, float& pitch_target) const
1447
{
1448
// rotate accelerations into body forward-right frame
1449
const float accel_forward = accel_x_cmss * _ahrs.cos_yaw() + accel_y_cmss * _ahrs.sin_yaw();
1450
const float accel_right = -accel_x_cmss * _ahrs.sin_yaw() + accel_y_cmss * _ahrs.cos_yaw();
1451
1452
// update angle targets that will be passed to stabilize controller
1453
pitch_target = accel_to_angle(-accel_forward * 0.01) * 100;
1454
float cos_pitch_target = cosf(pitch_target * M_PI / 18000.0f);
1455
roll_target = accel_to_angle((accel_right * cos_pitch_target)*0.01) * 100;
1456
}
1457
1458
// lean_angles_to_accel_xy - convert roll, pitch lean target angles to NE frame accelerations in cm/s/s
1459
// todo: this should be based on thrust vector attitude control
1460
void AC_PosControl::lean_angles_to_accel_xy(float& accel_x_cmss, float& accel_y_cmss) const
1461
{
1462
// rotate our roll, pitch angles into lat/lon frame
1463
Vector3f att_target_euler = _attitude_control.get_att_target_euler_rad();
1464
att_target_euler.z = _ahrs.yaw;
1465
Vector3f accel_cmss = lean_angles_to_accel(att_target_euler);
1466
1467
accel_x_cmss = accel_cmss.x;
1468
accel_y_cmss = accel_cmss.y;
1469
}
1470
1471
// calculate_yaw_and_rate_yaw - update the calculated the vehicle yaw and rate of yaw.
1472
void AC_PosControl::calculate_yaw_and_rate_yaw()
1473
{
1474
// Calculate the turn rate
1475
float turn_rate = 0.0f;
1476
const float vel_desired_xy_len = _vel_desired.xy().length();
1477
if (is_positive(vel_desired_xy_len)) {
1478
const float accel_forward = (_accel_desired.x * _vel_desired.x + _accel_desired.y * _vel_desired.y) / vel_desired_xy_len;
1479
const Vector2f accel_turn = _accel_desired.xy() - _vel_desired.xy() * accel_forward / vel_desired_xy_len;
1480
const float accel_turn_xy_len = accel_turn.length();
1481
turn_rate = accel_turn_xy_len / vel_desired_xy_len;
1482
if ((accel_turn.y * _vel_desired.x - accel_turn.x * _vel_desired.y) < 0.0) {
1483
turn_rate = -turn_rate;
1484
}
1485
}
1486
1487
// update the target yaw if velocity is greater than 5% _vel_max_xy_cms
1488
if (vel_desired_xy_len > _vel_max_xy_cms * 0.05f) {
1489
_yaw_target = degrees(_vel_desired.xy().angle()) * 100.0f;
1490
_yaw_rate_target = turn_rate * degrees(100.0f);
1491
return;
1492
}
1493
1494
// use the current attitude controller yaw target
1495
_yaw_target = _attitude_control.get_att_target_euler_cd().z;
1496
_yaw_rate_target = 0;
1497
}
1498
1499
// calculate_overspeed_gain - calculated increased maximum acceleration and jerk if over speed condition is detected
1500
float AC_PosControl::calculate_overspeed_gain()
1501
{
1502
if (_vel_desired.z < _vel_max_down_cms && !is_zero(_vel_max_down_cms)) {
1503
return POSCONTROL_OVERSPEED_GAIN_Z * _vel_desired.z / _vel_max_down_cms;
1504
}
1505
if (_vel_desired.z > _vel_max_up_cms && !is_zero(_vel_max_up_cms)) {
1506
return POSCONTROL_OVERSPEED_GAIN_Z * _vel_desired.z / _vel_max_up_cms;
1507
}
1508
return 1.0;
1509
}
1510
1511
/// initialise ekf xy position reset check
1512
void AC_PosControl::init_ekf_xy_reset()
1513
{
1514
Vector2f pos_shift;
1515
_ekf_xy_reset_ms = _ahrs.getLastPosNorthEastReset(pos_shift);
1516
}
1517
1518
/// handle_ekf_xy_reset - check for ekf position reset and adjust loiter or brake target position
1519
void AC_PosControl::handle_ekf_xy_reset()
1520
{
1521
// check for position shift
1522
Vector2f pos_shift;
1523
uint32_t reset_ms = _ahrs.getLastPosNorthEastReset(pos_shift);
1524
if (reset_ms != _ekf_xy_reset_ms) {
1525
1526
// ToDo: move EKF steps into the offsets for modes setting absolute position and velocity
1527
// for this we need some sort of switch to select what type of EKF handling we want to use
1528
1529
// To zero real position shift during relative position modes like Loiter, PosHold, Guided velocity and accleration control.
1530
_pos_target.xy() = (_inav.get_position_xy_cm() + _p_pos_xy.get_error()).topostype();
1531
_pos_desired.xy() = _pos_target.xy() - _pos_offset.xy();
1532
_vel_target.xy() = _inav.get_velocity_xy_cms() + _pid_vel_xy.get_error();
1533
_vel_desired.xy() = _vel_target.xy() - _vel_offset.xy();
1534
1535
_ekf_xy_reset_ms = reset_ms;
1536
}
1537
}
1538
1539
/// initialise ekf z axis reset check
1540
void AC_PosControl::init_ekf_z_reset()
1541
{
1542
float alt_shift;
1543
_ekf_z_reset_ms = _ahrs.getLastPosDownReset(alt_shift);
1544
}
1545
1546
/// handle_ekf_z_reset - check for ekf position reset and adjust loiter or brake target position
1547
void AC_PosControl::handle_ekf_z_reset()
1548
{
1549
// check for position shift
1550
float alt_shift;
1551
uint32_t reset_ms = _ahrs.getLastPosDownReset(alt_shift);
1552
if (reset_ms != 0 && reset_ms != _ekf_z_reset_ms) {
1553
1554
// ToDo: move EKF steps into the offsets for modes setting absolute position and velocity
1555
// for this we need some sort of switch to select what type of EKF handling we want to use
1556
1557
// To zero real position shift during relative position modes like Loiter, PosHold, Guided velocity and accleration control.
1558
_pos_target.z = _inav.get_position_z_up_cm() + _p_pos_z.get_error();
1559
_pos_desired.z = _pos_target.z - (_pos_offset.z + _pos_terrain);
1560
_vel_target.z = _inav.get_velocity_z_up_cms() + _pid_vel_z.get_error();
1561
_vel_desired.z = _vel_target.z - (_vel_offset.z + _vel_terrain);
1562
1563
_ekf_z_reset_ms = reset_ms;
1564
}
1565
}
1566
1567
bool AC_PosControl::pre_arm_checks(const char *param_prefix,
1568
char *failure_msg,
1569
const uint8_t failure_msg_len)
1570
{
1571
if (!is_positive(get_pos_xy_p().kP())) {
1572
hal.util->snprintf(failure_msg, failure_msg_len, "%s_POSXY_P must be > 0", param_prefix);
1573
return false;
1574
}
1575
if (!is_positive(get_pos_z_p().kP())) {
1576
hal.util->snprintf(failure_msg, failure_msg_len, "%s_POSZ_P must be > 0", param_prefix);
1577
return false;
1578
}
1579
if (!is_positive(get_vel_z_pid().kP())) {
1580
hal.util->snprintf(failure_msg, failure_msg_len, "%s_VELZ_P must be > 0", param_prefix);
1581
return false;
1582
}
1583
if (!is_positive(get_accel_z_pid().kP())) {
1584
hal.util->snprintf(failure_msg, failure_msg_len, "%s_ACCZ_P must be > 0", param_prefix);
1585
return false;
1586
}
1587
if (!is_positive(get_accel_z_pid().kI())) {
1588
hal.util->snprintf(failure_msg, failure_msg_len, "%s_ACCZ_I must be > 0", param_prefix);
1589
return false;
1590
}
1591
1592
return true;
1593
}
1594
1595
// return true if on a real vehicle or SITL with lock-step scheduling
1596
bool AC_PosControl::has_good_timing(void) const
1597
{
1598
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
1599
auto *sitl = AP::sitl();
1600
if (sitl) {
1601
return sitl->state.is_lock_step_scheduled;
1602
}
1603
#endif
1604
// real boards are assumed to have good timing
1605
return true;
1606
}
1607
1608