Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
guidance_indi_hybrid.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Ewoud Smeur <ewoud.smeur@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
30 #include "generated/airframe.h"
33 #include "state.h"
35 #include "mcu_periph/sys_time.h"
36 #include "autopilot.h"
38 #include "stdio.h"
40 #include "modules/core/abi.h"
42 
43 
44 // The acceleration reference is calculated with these gains. If you use GPS,
45 // they are probably limited by the update rate of your GPS. The default
46 // values are tuned for 4 Hz GPS updates. If you have high speed position updates, the
47 // gains can be higher, depending on the speed of the inner loop.
48 #ifndef GUIDANCE_INDI_SPEED_GAIN
49 #define GUIDANCE_INDI_SPEED_GAIN 1.8
50 #define GUIDANCE_INDI_SPEED_GAINZ 1.8
51 #endif
52 
53 #ifndef GUIDANCE_INDI_POS_GAIN
54 #define GUIDANCE_INDI_POS_GAIN 0.5
55 #define GUIDANCE_INDI_POS_GAINZ 0.5
56 #endif
57 
58 #ifndef GUIDANCE_INDI_LIFTD_ASQ
59 #define GUIDANCE_INDI_LIFTD_ASQ 0.20
60 #endif
61 
62 /* If lift effectiveness at low airspeed not defined,
63  * just make one interpolation segment that connects to
64  * the quadratic part from 12 m/s onward
65  */
66 #ifndef GUIDANCE_INDI_LIFTD_P50
67 #define GUIDANCE_INDI_LIFTD_P80 (GUIDANCE_INDI_LIFTD_ASQ*12*12)
68 #define GUIDANCE_INDI_LIFTD_P50 (GUIDANCE_INDI_LIFTD_P80/2)
69 #endif
70 
73  .pos_gainz = GUIDANCE_INDI_POS_GAINZ,
74 
75  .speed_gain = GUIDANCE_INDI_SPEED_GAIN,
76  .speed_gainz = GUIDANCE_INDI_SPEED_GAINZ,
77 
78  .heading_bank_gain = GUIDANCE_INDI_HEADING_BANK_GAIN,
79  .liftd_asq = GUIDANCE_INDI_LIFTD_ASQ, // coefficient of airspeed squared
80  .liftd_p80 = GUIDANCE_INDI_LIFTD_P80,
81  .liftd_p50 = GUIDANCE_INDI_LIFTD_P50,
82 };
83 
84 #ifndef GUIDANCE_INDI_MAX_AIRSPEED
85 #error "You must have an airspeed sensor to use this guidance"
86 #endif
87 float guidance_indi_max_airspeed = GUIDANCE_INDI_MAX_AIRSPEED;
88 
89 // Quadplanes can hover at various pref pitch
91 
92 
93 // If using WLS, check that the matrix size is sufficient
94 #if GUIDANCE_INDI_HYBRID_USE_WLS
95 #if GUIDANCE_INDI_HYBRID_U > WLS_N_U
96 #error Matrix-WLS_N_U too small: increase WLS_N_U in airframe file
97 #endif
98 
99 #if GUIDANCE_INDI_HYBRID_V > WLS_N_V
100 #error Matrix-WLS_N_V too small: increase WLS_N_V in airframe file
101 #endif
102 #endif
103 
104 
105 // Tell the guidance that the airspeed needs to be zeroed.
106 // Recomended to also put GUIDANCE_INDI_NAV_SPEED_MARGIN low in this case.
107 #ifndef GUIDANCE_INDI_ZERO_AIRSPEED
108 #define GUIDANCE_INDI_ZERO_AIRSPEED FALSE
109 #endif
110 
111 /*Airspeed threshold where making a turn is "worth it"*/
112 #ifndef TURN_AIRSPEED_TH
113 #define TURN_AIRSPEED_TH 10.0
114 #endif
115 
116 /*Boolean to force the heading to a static value (only use for specific experiments)*/
117 bool take_heading_control = false;
118 
119 bool force_forward = false;
120 
122 
123 
124 struct FloatVect3 sp_accel = {0.0,0.0,0.0};
125 #ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
126 float guidance_indi_specific_force_gain = GUIDANCE_INDI_SPECIFIC_FORCE_GAIN;
127 static void guidance_indi_filter_thrust(void);
128 
129 #ifdef GUIDANCE_INDI_THRUST_DYNAMICS
130 #warning GUIDANCE_INDI_THRUST_DYNAMICS is deprecated, use GUIDANCE_INDI_THRUST_DYNAMICS_FREQ instead.
131 #warning "The thrust dynamics are now specified in continuous time with the corner frequency of the first order model!"
132 #warning "define GUIDANCE_INDI_THRUST_DYNAMICS_FREQ in rad/s"
133 #warning "Use -ln(1 - old_number) * PERIODIC_FREQUENCY to compute it from the old value."
134 #endif
135 
136 #ifndef GUIDANCE_INDI_THRUST_DYNAMICS_FREQ
137 #ifndef STABILIZATION_INDI_ACT_FREQ_P
138 #error "You need to define GUIDANCE_INDI_THRUST_DYNAMICS_FREQ to be able to use indi vertical control"
139 #else // assume that the same actuators are used for thrust as for roll (e.g. quadrotor)
140 #define GUIDANCE_INDI_THRUST_DYNAMICS_FREQ STABILIZATION_INDI_ACT_FREQ_P
141 #endif
142 #endif //GUIDANCE_INDI_THRUST_DYNAMICS_FREQ
143 
144 #endif //GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
145 
146 #ifndef GUIDANCE_INDI_FILTER_CUTOFF
147 #ifdef STABILIZATION_INDI_FILT_CUTOFF
148 #define GUIDANCE_INDI_FILTER_CUTOFF STABILIZATION_INDI_FILT_CUTOFF
149 #else
150 #define GUIDANCE_INDI_FILTER_CUTOFF 3.0
151 #endif
152 #endif
153 
154 #ifndef GUIDANCE_INDI_AIRSPEED_FILT_CUTOFF
155 #define GUIDANCE_INDI_AIRSPEED_FILT_CUTOFF 0.5
156 #endif
157 
158 #ifndef GUIDANCE_INDI_CLIMB_SPEED_FWD
159 #define GUIDANCE_INDI_CLIMB_SPEED_FWD 4.0
160 #endif
161 
162 #ifndef GUIDANCE_INDI_DESCEND_SPEED_FWD
163 #define GUIDANCE_INDI_DESCEND_SPEED_FWD -4.0
164 #endif
165 
168 
169 float inv_eff[4];
170 
171 // Max bank angle in radians
174 
176 struct FloatEulers eulers_zxy;
177 
178 float thrust_dyn = 0.f;
179 float thrust_act = 0;
186 
188 
189 float Ga[GUIDANCE_INDI_HYBRID_V][GUIDANCE_INDI_HYBRID_U];
190 struct FloatVect3 euler_cmd;
191 
192 #if GUIDANCE_INDI_HYBRID_USE_WLS
193 #include "math/wls/wls_alloc.h"
194 float du_min_gih[GUIDANCE_INDI_HYBRID_U];
195 float du_max_gih[GUIDANCE_INDI_HYBRID_U];
196 float du_pref_gih[GUIDANCE_INDI_HYBRID_U];
197 float *Bwls_gih[GUIDANCE_INDI_HYBRID_V];
198 #ifdef GUIDANCE_INDI_HYBRID_WLS_PRIORITIES
199 float Wv_gih[GUIDANCE_INDI_HYBRID_V] = GUIDANCE_INDI_HYBRID_WLS_PRIORITIES;
200 #else
201 float Wv_gih[GUIDANCE_INDI_HYBRID_V] = { 100.f, 100.f, 1.f }; // X,Y accel, Z accel
202 #endif
203 #ifdef GUIDANCE_INDI_HYBRID_WLS_WU
204 float Wu_gih[GUIDANCE_INDI_HYBRID_U] = GUIDANCE_INDI_HYBRID_WLS_WU;
205 #else
206 float Wu_gih[GUIDANCE_INDI_HYBRID_U] = { 1.f, 1.f, 1.f };
207 #endif
208 #endif
209 
210 // The control objective
211 float v_gih[3];
212 
213 // Filters
216 
219 float thrust_in;
220 
221 struct FloatVect3 gi_speed_sp = {0.0, 0.0, 0.0};
222 
223 #ifndef GUIDANCE_INDI_VEL_SP_ID
224 #define GUIDANCE_INDI_VEL_SP_ID ABI_BROADCAST
225 #endif
227 static void vel_sp_cb(uint8_t sender_id, struct FloatVect3 *vel_sp);
228 struct FloatVect3 indi_vel_sp = {0.0, 0.0, 0.0};
229 float time_of_vel_sp = 0.0;
230 
232 
233 #if PERIODIC_TELEMETRY
235 static void send_guidance_indi_hybrid(struct transport_tx *trans, struct link_device *dev)
236 {
237  pprz_msg_send_GUIDANCE_INDI_HYBRID(trans, dev, AC_ID,
238  &sp_accel.x,
239  &sp_accel.y,
240  &sp_accel.z,
241  &euler_cmd.x,
242  &euler_cmd.y,
243  &euler_cmd.z,
244  &filt_accel_ned[0].o[0],
245  &filt_accel_ned[1].o[0],
246  &filt_accel_ned[2].o[0],
247  &gi_speed_sp.x,
248  &gi_speed_sp.y,
249  &gi_speed_sp.z);
250 }
251 
252 #if GUIDANCE_INDI_HYBRID_USE_WLS
253 static void debug(struct transport_tx *trans, struct link_device *dev, char* name, float* data, int datasize)
254 {
255  pprz_msg_send_DEBUG_VECT(trans, dev,AC_ID,
256  strlen(name), name,
257  datasize, data);
258 }
259 
260 static void send_guidance_indi_debug(struct transport_tx *trans, struct link_device *dev)
261 {
262  static int c = 0;
263  switch (c++)
264  {
265  case 0:
266  debug(trans, dev, "v_gih", v_gih, 3);
267  break;
268  case 1:
269  debug(trans, dev, "du_min_gih", du_min_gih, GUIDANCE_INDI_HYBRID_U);
270  break;
271  case 2:
272  debug(trans, dev, "du_max_gih", du_max_gih, GUIDANCE_INDI_HYBRID_U);
273  break;
274  case 3:
275  debug(trans, dev, "du_pref_gih", du_pref_gih, GUIDANCE_INDI_HYBRID_U);
276  break;
277  case 4:
278  debug(trans, dev, "Wu_gih", Wu_gih, GUIDANCE_INDI_HYBRID_U);
279  break;
280  case 5:
281  debug(trans, dev, "Wv_gih", Wv_gih, GUIDANCE_INDI_HYBRID_V);
282  break;
283  default:
284  debug(trans, dev, "Bwls_gih[0]", Bwls_gih[0], GUIDANCE_INDI_HYBRID_U);
285  c=0;
286  break;
287  }
288 }
289 #else
290 static void send_guidance_indi_debug(struct transport_tx *trans UNUSED, struct link_device *dev UNUSED)
291 {
292 }
293 #endif // GUIDANCE_INDI_HYBRID_USE_WLS
294 
295 #endif // PERIODIC_TELEMETRY
296 
301 {
302  /*AbiBindMsgACCEL_SP(GUIDANCE_INDI_ACCEL_SP_ID, &accel_sp_ev, accel_sp_cb);*/
303  AbiBindMsgVEL_SP(GUIDANCE_INDI_VEL_SP_ID, &vel_sp_ev, vel_sp_cb);
304 
305 #ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
306 #ifdef GUIDANCE_INDI_THRUST_DYNAMICS
307  thrust_dyn = GUIDANCE_INDI_THRUST_DYNAMICS;
308 #else
309  thrust_dyn = 1-exp(-GUIDANCE_INDI_THRUST_DYNAMICS_FREQ/PERIODIC_FREQUENCY);
310 #endif
311 #endif
312 
313  float tau = 1.0/(2.0*M_PI*filter_cutoff);
314  float sample_time = 1.0/PERIODIC_FREQUENCY;
315  for(int8_t i=0; i<3; i++) {
316  init_butterworth_2_low_pass(&filt_accel_ned[i], tau, sample_time, 0.0);
317  }
318  init_butterworth_2_low_pass(&roll_filt, tau, sample_time, 0.0);
319  init_butterworth_2_low_pass(&pitch_filt, tau, sample_time, 0.0);
320  init_butterworth_2_low_pass(&thrust_filt, tau, sample_time, 0.0);
321  init_butterworth_2_low_pass(&accely_filt, tau, sample_time, 0.0);
322 
323  float tau_guidance_indi_airspeed = 1.0/(2.0*M_PI*guidance_indi_airspeed_filt_cutoff);
324  init_butterworth_2_low_pass(&guidance_indi_airspeed_filt, tau_guidance_indi_airspeed, sample_time, 0.0);
325 
326 #if GUIDANCE_INDI_HYBRID_USE_WLS
327  for (int8_t i = 0; i < GUIDANCE_INDI_HYBRID_V; i++) {
328  Bwls_gih[i] = Ga[i];
329  }
330 #endif
331 
332 #if PERIODIC_TELEMETRY
333  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_GUIDANCE_INDI_HYBRID, send_guidance_indi_hybrid);
335 #endif
336 }
337 
343  /*Obtain eulers with zxy rotation order*/
346 
347  thrust_in = stabilization_cmd[COMMAND_THRUST];
350 
351  float tau = 1.0 / (2.0 * M_PI * filter_cutoff);
352  float sample_time = 1.0 / PERIODIC_FREQUENCY;
353  for (int8_t i = 0; i < 3; i++) {
354  init_butterworth_2_low_pass(&filt_accel_ned[i], tau, sample_time, 0.0);
355  }
356 
357  /*Obtain eulers with zxy rotation order*/
359 
363  init_butterworth_2_low_pass(&accely_filt, tau, sample_time, 0.0);
364 
365  float tau_guidance_indi_airspeed = 1.0/(2.0*M_PI*guidance_indi_airspeed_filt_cutoff);
366  init_butterworth_2_low_pass(&guidance_indi_airspeed_filt, tau_guidance_indi_airspeed, sample_time, 0.0);
367 }
368 
377 struct StabilizationSetpoint guidance_indi_run(struct FloatVect3 *accel_sp, float heading_sp)
378 {
379  // set global accel sp variable FIXME clean this
380  sp_accel = *accel_sp;
381 
382  /* Obtain eulers with zxy rotation order */
384 
385  /* Calculate the transition percentage so that the ctrl_effecitveness scheduling works */
388  const int32_t max_offset = ANGLE_BFP_OF_REAL(TRANSITION_MAX_OFFSET);
391 
392  // filter accel to get rid of noise and filter attitude to synchronize with accel
394 
395 #if GUIDANCE_INDI_RC_DEBUG
396 #warning "GUIDANCE_INDI_RC_DEBUG lets you control the accelerations via RC, but disables autonomous flight!"
397  // for rc control horizontal, rotate from body axes to NED
398  float psi = eulers_zxy.psi;
399  float rc_x = -(radio_control.values[RADIO_PITCH]/9600.0)*8.0;
400  float rc_y = (radio_control.values[RADIO_ROLL]/9600.0)*8.0;
401  sp_accel.x = cosf(psi) * rc_x - sinf(psi) * rc_y;
402  sp_accel.y = sinf(psi) * rc_x + cosf(psi) * rc_y;
403 
404  // for rc vertical control
405  sp_accel.z = -(radio_control.values[RADIO_THROTTLE]-4500)*8.0/9600.0;
406 #endif
407 
408  struct FloatVect3 accel_filt;
409  accel_filt.x = filt_accel_ned[0].o[0];
410  accel_filt.y = filt_accel_ned[1].o[0];
411  accel_filt.z = filt_accel_ned[2].o[0];
412 
413  struct FloatVect3 a_diff;
414  a_diff.x = sp_accel.x - accel_filt.x;
415  a_diff.y = sp_accel.y - accel_filt.y;
416  a_diff.z = sp_accel.z - accel_filt.z;
417 
418  // Bound the acceleration error so that the linearization still holds
419  Bound(a_diff.x, -6.0, 6.0);
420  Bound(a_diff.y, -6.0, 6.0);
421  Bound(a_diff.z, -9.0, 9.0);
422 
423  // If the thrust to specific force ratio has been defined, include vertical control
424  // else ignore the vertical acceleration error
425 #ifndef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
426 #ifndef STABILIZATION_ATTITUDE_INDI_FULL
427  a_diff.z = 0.0;
428 #endif
429 #endif
430 
431 
432  // Calculate matrix of partial derivatives and control objective
434 
435 #if GUIDANCE_INDI_HYBRID_USE_WLS
436 
437  // Calculate the maximum deflections
439 
440  float du_gih[GUIDANCE_INDI_HYBRID_U]; // = {0.0f, 0.0f, 0.0f};
441 
442  int num_iter UNUSED = wls_alloc(
443  du_gih, v_gih, du_min_gih, du_max_gih,
444  Bwls_gih, 0, 0, Wv_gih, Wu_gih, du_pref_gih, 100000, 10,
445  GUIDANCE_INDI_HYBRID_U, GUIDANCE_INDI_HYBRID_V);
446 
447  euler_cmd.x = du_gih[0];
448  euler_cmd.y = du_gih[1];
449  euler_cmd.z = du_gih[2];
450 
451 #else
452  // compute inverse matrix of Ga
453  float Ga_inv[3][3] = {};
455  // Calculate roll,pitch and thrust command
456  float_mat3_mult(&euler_cmd, Ga_inv, a_diff);
457 #endif
458 
459  struct FloatVect3 thrust_vect;
460 #if GUIDANCE_INDI_HYBRID_U > 3
461  thrust_vect.x = du_gih[3];
462 #else
463  thrust_vect.x = 0;
464 #endif
465  thrust_vect.y = 0;
466  thrust_vect.z = euler_cmd.z;
467  AbiSendMsgTHRUST(THRUST_INCREMENT_ID, thrust_vect);
468 
469  // Coordinated turn
470  // feedforward estimate angular rotation omega = g*tan(phi)/v
471  float omega;
472  const float max_phi = RadOfDeg(60.0f);
473 #if GUIDANCE_INDI_ZERO_AIRSPEED
474  float airspeed_turn = 0.f;
475 #else
476  float airspeed_turn = stateGetAirspeed_f();
477 #endif
478  // We are dividing by the airspeed, so a lower bound is important
479  Bound(airspeed_turn, 10.0f, 30.0f);
480 
483 
484  //Bound euler angles to prevent flipping
487 
488  // Use the current roll angle to determine the corresponding heading rate of change.
489  float coordinated_turn_roll = eulers_zxy.phi;
490 
491  // When tilting backwards (e.g. waypoint behind the drone), we have to yaw around to face the direction
492  // of flight even when the drone is not rolling much (yet). Determine the shortest direction in which to yaw by
493  // looking at the roll angle.
494  if( (eulers_zxy.theta > 0.0f) && ( fabs(eulers_zxy.phi) < eulers_zxy.theta)) {
495  if (eulers_zxy.phi > 0.0f) {
496  coordinated_turn_roll = eulers_zxy.theta;
497  } else {
498  coordinated_turn_roll = -eulers_zxy.theta;
499  }
500  }
501 
502  if (fabsf(coordinated_turn_roll) < max_phi) {
503  omega = 9.81f / airspeed_turn * tanf(coordinated_turn_roll);
504  } else { //max 60 degrees roll
505  omega = 9.81f / airspeed_turn * 1.72305f * ((coordinated_turn_roll > 0.0f) - (coordinated_turn_roll < 0.0f));
506  }
507 
508 #ifdef FWD_SIDESLIP_GAIN
509  // Add sideslip correction
510  omega -= accely_filt.o[0]*FWD_SIDESLIP_GAIN;
511 #endif
512 
513  // We can pre-compute the required rates to achieve this turn rate:
514  // NOTE: there *should* not be any problems possible with Euler singularities here
515  struct FloatEulers *euler_zyx = stateGetNedToBodyEulers_f();
516 
517  struct FloatRates ff_rates;
518 
519  ff_rates.p = -sinf(euler_zyx->theta) * omega;
520  ff_rates.q = cosf(euler_zyx->theta) * sinf(euler_zyx->phi) * omega;
521  ff_rates.r = cosf(euler_zyx->theta) * cosf(euler_zyx->phi) * omega;
522 
523  // For a hybrid it is important to reduce the sideslip, which is done by changing the heading.
524  // For experiments, it is possible to fix the heading to a different value.
525  if (take_heading_control) {
526  // heading is fixed by nav
527  guidance_euler_cmd.psi = heading_sp;
528  }
529  else {
530  // heading is free and controlled by guidance
531  guidance_indi_hybrid_heading_sp += omega / PERIODIC_FREQUENCY;
533  // limit heading setpoint to be within bounds of current heading
534 #ifdef STABILIZATION_ATTITUDE_SP_PSI_DELTA_LIMIT
535  float delta_limit = STABILIZATION_ATTITUDE_SP_PSI_DELTA_LIMIT;
537  float delta_psi = guidance_indi_hybrid_heading_sp - heading;
538  FLOAT_ANGLE_NORMALIZE(delta_psi);
539  if (delta_psi > delta_limit) {
541  } else if (delta_psi < -delta_limit) {
543  }
545 #endif
547  }
548 
549 #ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
550  guidance_indi_filter_thrust();
551 
552  // Add the increment in specific force * specific_force_to_thrust_gain to the filtered thrust
554  Bound(thrust_in, GUIDANCE_INDI_MIN_THROTTLE, 9600);
555 
556 #if GUIDANCE_INDI_RC_DEBUG
557  if (radio_control.values[RADIO_THROTTLE] < 300) {
558  thrust_in = 0;
559  }
560 #endif
561 
562  // Overwrite the thrust command from guidance_v
563  stabilization_cmd[COMMAND_THRUST] = thrust_in;
564 #endif
565 
566  // Set the quaternion setpoint from eulers_zxy
567  struct FloatQuat sp_quat;
569  float_quat_normalize(&sp_quat);
570 
571  return stab_sp_from_quat_ff_rates_f(&sp_quat, &ff_rates);
572 }
573 
574 // compute accel setpoint from speed setpoint (use global variables ! FIXME)
575 static struct FloatVect3 compute_accel_from_speed_sp(void)
576 {
577  struct FloatVect3 accel_sp = { 0.f, 0.f, 0.f };
578 
580 
581  //for rc control horizontal, rotate from body axes to NED
582  float psi = eulers_zxy.psi;
583  float cpsi = cosf(psi);
584  float spsi = sinf(psi);
585  float speed_sp_b_x = cpsi * gi_speed_sp.x + spsi * gi_speed_sp.y;
586  float speed_sp_b_y = -spsi * gi_speed_sp.x + cpsi * gi_speed_sp.y;
587 
588  // Get airspeed or zero it
589 #if GUIDANCE_INDI_ZERO_AIRSPEED
590  float airspeed = 0.f;
591 #else
592  float airspeed = stateGetAirspeed_f();
594  airspeed = guidance_indi_airspeed_filt.o[0];
595  }
596 #endif
597  struct NedCoor_f *groundspeed = stateGetSpeedNed_f();
598  struct FloatVect2 airspeed_v = { cpsi * airspeed, spsi * airspeed };
599  struct FloatVect2 windspeed;
600  VECT2_DIFF(windspeed, *groundspeed, airspeed_v);
601 
602  VECT2_DIFF(desired_airspeed, gi_speed_sp, windspeed); // Use 2d part of gi_speed_sp
603  float norm_des_as = FLOAT_VECT2_NORM(desired_airspeed);
604 
605  // Make turn instead of straight line
606  if ((airspeed > TURN_AIRSPEED_TH) && (norm_des_as > (TURN_AIRSPEED_TH+2.0f))) {
607 
608  // Give the wind cancellation priority.
609  if (norm_des_as > guidance_indi_max_airspeed) {
610  float groundspeed_factor = 0.0f;
611 
612  // if the wind is faster than we can fly, just fly in the wind direction
613  if (FLOAT_VECT2_NORM(windspeed) < guidance_indi_max_airspeed) {
614  float av = gi_speed_sp.x * gi_speed_sp.x + gi_speed_sp.y * gi_speed_sp.y;
615  float bv = -2.f * (windspeed.x * gi_speed_sp.x + windspeed.y * gi_speed_sp.y);
616  float cv = windspeed.x * windspeed.x + windspeed.y * windspeed.y - guidance_indi_max_airspeed * guidance_indi_max_airspeed;
617 
618  float dv = bv * bv - 4.0f * av * cv;
619 
620  // dv can only be positive, but just in case
621  if (dv < 0.0f) {
622  dv = fabsf(dv);
623  }
624  float d_sqrt = sqrtf(dv);
625 
626  groundspeed_factor = (-bv + d_sqrt) / (2.0f * av);
627  }
628 
629  desired_airspeed.x = groundspeed_factor * gi_speed_sp.x - windspeed.x;
630  desired_airspeed.y = groundspeed_factor * gi_speed_sp.y - windspeed.y;
631 
632  speed_sp_b_x = guidance_indi_max_airspeed;
633  }
634 
635  // desired airspeed can not be larger than max airspeed
636  speed_sp_b_x = Min(norm_des_as, guidance_indi_max_airspeed);
637 
638  if (force_forward) {
639  speed_sp_b_x = guidance_indi_max_airspeed;
640  }
641 
642  // Calculate accel sp in body axes, because we need to regulate airspeed
643  struct FloatVect2 sp_accel_b;
644  // In turn acceleration proportional to heading diff
645  sp_accel_b.y = atan2f(desired_airspeed.y, desired_airspeed.x) - psi;
646  FLOAT_ANGLE_NORMALIZE(sp_accel_b.y);
647  sp_accel_b.y *= gih_params.heading_bank_gain;
648 
649  // Control the airspeed
650  sp_accel_b.x = (speed_sp_b_x - airspeed) * gih_params.speed_gain;
651 
652  accel_sp.x = cpsi * sp_accel_b.x - spsi * sp_accel_b.y;
653  accel_sp.y = spsi * sp_accel_b.x + cpsi * sp_accel_b.y;
655  }
656  else { // Go somewhere in the shortest way
657 
658  if (airspeed > 10.f) {
659  // Groundspeed vector in body frame
660  float groundspeed_x = cpsi * stateGetSpeedNed_f()->x + spsi * stateGetSpeedNed_f()->y;
661  float speed_increment = speed_sp_b_x - groundspeed_x;
662 
663  // limit groundspeed setpoint to max_airspeed + (diff gs and airspeed)
664  if ((speed_increment + airspeed) > guidance_indi_max_airspeed) {
665  speed_sp_b_x = guidance_indi_max_airspeed + groundspeed_x - airspeed;
666  }
667  }
668 
669  gi_speed_sp.x = cpsi * speed_sp_b_x - spsi * speed_sp_b_y;
670  gi_speed_sp.y = spsi * speed_sp_b_x + cpsi * speed_sp_b_y;
671 
675  }
676 
677  // Bound the acceleration setpoint
678  float accelbound = 3.0f + airspeed / guidance_indi_max_airspeed * 5.0f; // FIXME remove hard coded values
679  float_vect3_bound_in_2d(&accel_sp, accelbound);
680  /*BoundAbs(sp_accel.x, 3.0 + airspeed/guidance_indi_max_airspeed*6.0);*/
681  /*BoundAbs(sp_accel.y, 3.0 + airspeed/guidance_indi_max_airspeed*6.0);*/
682  BoundAbs(accel_sp.z, 3.0);
683 
684  return accel_sp;
685 }
686 
687 static float bound_vz_sp(float vz_sp)
688 {
689  // Bound vertical speed setpoint
690  if (stateGetAirspeed_f() > 13.f) {
691  Bound(vz_sp, -climb_vspeed_fwd, -descend_vspeed_fwd);
692  } else {
693  Bound(vz_sp, -nav.climb_vspeed, -nav.descend_vspeed); // FIXME don't use nav settings
694  }
695  return vz_sp;
696 }
697 
699 {
700  struct FloatVect3 pos_err = { 0 };
701  struct FloatVect3 accel_sp = { 0 };
702 
703  // First check for velocity setpoint from module // FIXME should be called like this
704  float dt = get_sys_time_float() - time_of_vel_sp;
705  // If the input command is not updated after a timeout, switch back to flight plan control
706  if (dt < 0.5) {
710  accel_sp = compute_accel_from_speed_sp(); // compute accel sp
711  return guidance_indi_run(&accel_sp, gh->sp.heading);
712  }
713 
714  if (h_mode == GUIDANCE_INDI_HYBRID_H_POS) {
715  //Linear controller to find the acceleration setpoint from position and velocity
716  pos_err.x = POS_FLOAT_OF_BFP(gh->ref.pos.x) - stateGetPositionNed_f()->x;
717  pos_err.y = POS_FLOAT_OF_BFP(gh->ref.pos.y) - stateGetPositionNed_f()->y;
718  gi_speed_sp.x = pos_err.x * gih_params.pos_gain + SPEED_FLOAT_OF_BFP(gh->ref.speed.x);
719  gi_speed_sp.y = pos_err.y * gih_params.pos_gain + SPEED_FLOAT_OF_BFP(gh->ref.speed.y);
720  if (v_mode == GUIDANCE_INDI_HYBRID_V_POS) {
721  pos_err.z = POS_FLOAT_OF_BFP(gv->z_ref) - stateGetPositionNed_f()->z;
723  } else if (v_mode == GUIDANCE_INDI_HYBRID_V_SPEED) {
725  } else {
726  gi_speed_sp.z = 0.f;
727  }
728  accel_sp = compute_accel_from_speed_sp(); // compute accel sp
729  if (v_mode == GUIDANCE_INDI_HYBRID_V_ACCEL) {
730  accel_sp.z = (gi_speed_sp.z - stateGetSpeedNed_f()->z) * gih_params.speed_gainz + ACCEL_FLOAT_OF_BFP(gv->zdd_ref); // overwrite accel
731  }
732  return guidance_indi_run(&accel_sp, gh->sp.heading);
733  }
734  else if (h_mode == GUIDANCE_INDI_HYBRID_H_SPEED) {
735  gi_speed_sp.x = SPEED_FLOAT_OF_BFP(gh->ref.speed.x);
736  gi_speed_sp.y = SPEED_FLOAT_OF_BFP(gh->ref.speed.y);
737  if (v_mode == GUIDANCE_INDI_HYBRID_V_POS) {
738  pos_err.z = POS_FLOAT_OF_BFP(gv->z_ref) - stateGetPositionNed_f()->z;
740  } else if (v_mode == GUIDANCE_INDI_HYBRID_V_SPEED) {
742  } else {
743  gi_speed_sp.z = 0.f;
744  }
745  accel_sp = compute_accel_from_speed_sp(); // compute accel sp
746  if (v_mode == GUIDANCE_INDI_HYBRID_V_ACCEL) {
747  accel_sp.z = (gi_speed_sp.z - stateGetSpeedNed_f()->z) * gih_params.speed_gainz + ACCEL_FLOAT_OF_BFP(gv->zdd_ref); // overwrite accel
748  }
749  return guidance_indi_run(&accel_sp, gh->sp.heading);
750  }
751  else { // H_ACCEL
752  gi_speed_sp.x = 0.f;
753  gi_speed_sp.y = 0.f;
754  if (v_mode == GUIDANCE_INDI_HYBRID_V_POS) {
755  pos_err.z = POS_FLOAT_OF_BFP(gv->z_ref) - stateGetPositionNed_f()->z;
757  } else if (v_mode == GUIDANCE_INDI_HYBRID_V_SPEED) {
759  } else {
760  gi_speed_sp.z = 0.f;
761  }
762  accel_sp = compute_accel_from_speed_sp(); // compute accel sp in case z control is required
763  // overwrite accel X and Y
764  accel_sp.x = (gi_speed_sp.x - stateGetSpeedNed_f()->x) * gih_params.speed_gain + ACCEL_FLOAT_OF_BFP(gh->ref.accel.x);
765  accel_sp.y = (gi_speed_sp.y - stateGetSpeedNed_f()->y) * gih_params.speed_gain + ACCEL_FLOAT_OF_BFP(gh->ref.accel.y);
766  if (v_mode == GUIDANCE_INDI_HYBRID_V_ACCEL) {
767  accel_sp.z = (gi_speed_sp.z - stateGetSpeedNed_f()->z) * gih_params.speed_gainz + ACCEL_FLOAT_OF_BFP(gv->zdd_ref); // overwrite accel
768  }
769  return guidance_indi_run(&accel_sp, gh->sp.heading);
770  }
771 }
772 
773 #ifdef GUIDANCE_INDI_SPECIFIC_FORCE_GAIN
777 void guidance_indi_filter_thrust(void)
778 {
779  // Actuator dynamics
781 
782  // same filter as for the acceleration
784 }
785 
786 #endif
787 
795  struct NedCoor_f *accel = stateGetAccelNed_f();
799 
802 
803  // Propagate filter for sideslip correction
804  float accely = ACCEL_FLOAT_OF_BFP(stateGetAccelBody_i()->y);
806 
807  float airspeed = stateGetAirspeed_f();
809 }
810 
811 
819 float WEAK guidance_indi_get_liftd(float airspeed, float theta) {
820  float liftd = 0.0f;
821 
822  if (airspeed < 12.f) {
823  /* Assume the airspeed is too low to be measured accurately
824  * Use scheduling based on pitch angle instead.
825  * You can define two interpolation segments
826  */
827  float pitch_interp = DegOfRad(theta);
828  const float min_pitch = -80.0f;
829  const float middle_pitch = -50.0f;
830  const float max_pitch = -20.0f;
831 
832  Bound(pitch_interp, min_pitch, max_pitch);
833  if (pitch_interp > middle_pitch) {
834  float ratio = (pitch_interp - max_pitch)/(middle_pitch - max_pitch);
835  liftd = -gih_params.liftd_p50*ratio;
836  } else {
837  float ratio = (pitch_interp - middle_pitch)/(min_pitch - middle_pitch);
839  }
840  } else {
841  liftd = -gih_params.liftd_asq*airspeed*airspeed;
842  }
843 
844  //TODO: bound liftd
845  return liftd;
846 }
847 
851 static void vel_sp_cb(uint8_t sender_id __attribute__((unused)), struct FloatVect3 *vel_sp)
852 {
853  indi_vel_sp.x = vel_sp->x;
854  indi_vel_sp.y = vel_sp->y;
855  indi_vel_sp.z = vel_sp->z;
857 }
858 
859 
860 #if GUIDANCE_INDI_HYBRID_USE_AS_DEFAULT
861 // guidance indi control function is implementing the default functions of guidance
862 
863 void guidance_h_run_enter(void)
864 {
866 }
867 
868 void guidance_v_run_enter(void)
869 {
870  // nothing to do
871 }
872 
873 static struct VerticalGuidance *_gv = &guidance_v;
875 
876 struct StabilizationSetpoint guidance_h_run_pos(bool in_flight, struct HorizontalGuidance *gh)
877 {
879 }
880 
881 struct StabilizationSetpoint guidance_h_run_speed(bool in_flight, struct HorizontalGuidance *gh)
882 {
884 }
885 
886 struct StabilizationSetpoint guidance_h_run_accel(bool in_flight, struct HorizontalGuidance *gh)
887 {
889 }
890 
891 int32_t guidance_v_run_pos(bool in_flight UNUSED, struct VerticalGuidance *gv)
892 {
893  _gv = gv;
895  return (int32_t)stabilization_cmd[COMMAND_THRUST]; // nothing to do
896 }
897 
898 int32_t guidance_v_run_speed(bool in_flight UNUSED, struct VerticalGuidance *gv)
899 {
900  _gv = gv;
902  return (int32_t)stabilization_cmd[COMMAND_THRUST]; // nothing to do
903 }
904 
905 int32_t guidance_v_run_accel(bool in_flight UNUSED, struct VerticalGuidance *gv)
906 {
907  _gv = gv;
909  return (int32_t)stabilization_cmd[COMMAND_THRUST]; // nothing to do
910 }
911 
912 #endif
913 
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition: abi_common.h:67
#define THRUST_INCREMENT_ID
Core autopilot interface common to all firmwares.
uint8_t last_wp UNUSED
#define Min(x, y)
Definition: esc_dshot.c:109
float phi
in radians
float p
in rad/s
float theta
in radians
float psi
in radians
static void float_quat_normalize(struct FloatQuat *q)
#define FLOAT_ANGLE_NORMALIZE(_a)
void float_mat3_mult(struct FloatVect3 *vect_out, float mat[3][3], struct FloatVect3 vect_in)
Multiply 3D matrix with vector.
void float_vect3_bound_in_2d(struct FloatVect3 *vect3, float bound)
void float_quat_of_eulers_zxy(struct FloatQuat *q, struct FloatEulers *e)
quat from euler rotation 'ZXY' This rotation order is useful if you need 90 deg pitch
void float_eulers_of_quat_zxy(struct FloatEulers *e, struct FloatQuat *q)
euler rotation 'ZXY' This rotation order is useful if you need 90 deg pitch
bool float_mat_inv_3d(float inv_out[3][3], float mat_in[3][3])
3x3 matrix inverse
#define FLOAT_VECT2_NORM(_v)
euler angles
Roation quaternion.
angular rates
#define VECT2_DIFF(_c, _a, _b)
Definition: pprz_algebra.h:92
#define INT_MULT_RSHIFT(_a, _b, _r)
#define ANGLE_BFP_OF_REAL(_af)
#define INT32_PERCENTAGE_FRAC
#define POS_FLOAT_OF_BFP(_ai)
#define SPEED_FLOAT_OF_BFP(_ai)
#define INT32_ANGLE_FRAC
#define BFP_OF_REAL(_vr, _frac)
#define ACCEL_FLOAT_OF_BFP(_ai)
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition: state.h:1038
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1143
static struct FloatQuat * stateGetNedToBodyQuat_f(void)
Get vehicle body attitude quaternion (float).
Definition: state.h:1131
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition: state.h:710
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition: state.h:908
static struct Int32Vect3 * stateGetAccelBody_i(void)
Get acceleration in Body coordinates (int).
Definition: state.h:953
static float stateGetAirspeed_f(void)
Get airspeed (float).
Definition: state.h:1407
struct FloatMat33 Ga_inv
float guidance_indi_specific_force_gain
struct FloatVect3 indi_vel_sp
bool force_forward
forward flight for hybrid nav
Butterworth2LowPass accely_filt
static void vel_sp_cb(uint8_t sender_id, struct FloatVect3 *vel_sp)
ABI callback that obtains the velocity setpoint from a module.
float time_of_vel_sp
Butterworth2LowPass roll_filt
#define GUIDANCE_INDI_SPEED_GAINZ
#define GUIDANCE_INDI_CLIMB_SPEED_FWD
struct guidance_indi_hybrid_params gih_params
float thrust_in
struct FloatVect3 gi_speed_sp
float guidance_indi_min_pitch
float guidance_indi_max_bank
float climb_vspeed_fwd
struct StabilizationSetpoint guidance_indi_run_mode(bool in_flight UNUSED, struct HorizontalGuidance *gh, struct VerticalGuidance *gv, enum GuidanceIndiHybrid_HMode h_mode, enum GuidanceIndiHybrid_VMode v_mode)
float inv_eff[4]
float descend_vspeed_fwd
struct FloatVect3 sp_accel
static void send_guidance_indi_debug(struct transport_tx *trans UNUSED, struct link_device *dev UNUSED)
struct FloatEulers guidance_euler_cmd
float guidance_indi_pitch_pref_deg
#define GUIDANCE_INDI_LIFTD_ASQ
float guidance_indi_airspeed_filt_cutoff
float thrust_act
void guidance_indi_enter(void)
Call upon entering indi guidance.
Butterworth2LowPass guidance_indi_airspeed_filt
Butterworth2LowPass pitch_filt
#define GUIDANCE_INDI_POS_GAIN
bool take_heading_control
float guidance_indi_hybrid_heading_sp
float WEAK guidance_indi_get_liftd(float airspeed, float theta)
Get the derivative of lift w.r.t.
#define GUIDANCE_INDI_LIFTD_P50
static void send_guidance_indi_hybrid(struct transport_tx *trans, struct link_device *dev)
void guidance_indi_init(void)
Init function.
float filter_cutoff
#define GUIDANCE_INDI_FILTER_CUTOFF
#define GUIDANCE_INDI_DESCEND_SPEED_FWD
float Ga[GUIDANCE_INDI_HYBRID_V][GUIDANCE_INDI_HYBRID_U]
struct FloatVect3 euler_cmd
static float bound_vz_sp(float vz_sp)
struct FloatEulers eulers_zxy
state eulers in zxy order
Butterworth2LowPass filt_accel_ned[3]
#define GUIDANCE_INDI_AIRSPEED_FILT_CUTOFF
#define GUIDANCE_INDI_POS_GAINZ
#define TURN_AIRSPEED_TH
float guidance_indi_max_airspeed
float thrust_dyn
static struct FloatVect3 compute_accel_from_speed_sp(void)
#define GUIDANCE_INDI_VEL_SP_ID
struct StabilizationSetpoint guidance_indi_run(struct FloatVect3 *accel_sp, float heading_sp)
float v_gih[3]
void guidance_indi_propagate_filters(void)
Low pass the accelerometer measurements to remove noise from vibrations.
bool guidance_indi_airspeed_filtering
#define GUIDANCE_INDI_LIFTD_P80
abi_event vel_sp_ev
Butterworth2LowPass thrust_filt
#define GUIDANCE_INDI_SPEED_GAIN
struct FloatVect2 desired_airspeed
A guidance mode based on Incremental Nonlinear Dynamic Inversion Come to ICRA2016 to learn more!
void guidance_indi_calcg_wing(float Gmat[GUIDANCE_INDI_HYBRID_V][GUIDANCE_INDI_HYBRID_U], struct FloatVect3 a_diff, float v_body[GUIDANCE_INDI_HYBRID_V])
Perform WLS.
GuidanceIndiHybrid_VMode
@ GUIDANCE_INDI_HYBRID_V_POS
@ GUIDANCE_INDI_HYBRID_V_SPEED
@ GUIDANCE_INDI_HYBRID_V_ACCEL
GuidanceIndiHybrid_HMode
@ GUIDANCE_INDI_HYBRID_H_SPEED
@ GUIDANCE_INDI_HYBRID_H_ACCEL
@ GUIDANCE_INDI_HYBRID_H_POS
void WEAK guidance_indi_hybrid_set_wls_settings(float body_v[3], float roll_angle, float pitch_angle)
#define GUIDANCE_INDI_MAX_PITCH
#define GUIDANCE_INDI_MIN_PITCH
static enum GuidanceOneloop_VMode _v_mode
void guidance_v_run_enter(void)
static struct VerticalGuidance * _gv
int32_t guidance_v_run_speed(bool in_flight UNUSED, struct VerticalGuidance *gv)
int32_t guidance_v_run_pos(bool in_flight UNUSED, struct VerticalGuidance *gv)
int32_t guidance_v_run_accel(bool in_flight UNUSED, struct VerticalGuidance *gv)
Simple first order low pass filter with bilinear transform.
float o[2]
output history
static void init_butterworth_2_low_pass(Butterworth2LowPass *filter, float tau, float sample_time, float value)
Init a second order Butterworth filter.
static float update_butterworth_2_low_pass(Butterworth2LowPass *filter, float value)
Update second order Butterworth low pass filter state with a new value.
Second order low pass filter structure.
float z
in meters
float x
in meters
float y
in meters
vector in North East Down coordinates Units: meters
struct RadioControl radio_control
Definition: radio_control.c:33
Generic interface for radio control modules.
pprz_t values[RADIO_CONTROL_NB_CHANNEL]
Definition: radio_control.h:67
Some helper functions to check RC sticks.
int32_t transition_percentage
Definition: guidance_h.c:52
void guidance_h_run_enter(void)
struct StabilizationSetpoint guidance_h_run_pos(bool in_flight, struct HorizontalGuidance *gh)
struct StabilizationSetpoint guidance_h_run_speed(bool in_flight, struct HorizontalGuidance *gh)
struct StabilizationSetpoint guidance_h_run_accel(bool in_flight, struct HorizontalGuidance *gh)
#define GUIDANCE_H_MAX_BANK
Max bank controlled by guidance.
Definition: guidance_h.h:71
struct VerticalGuidance guidance_v
Definition: guidance_v.c:59
int32_t z_ref
altitude reference in meters.
Definition: guidance_v.h:63
int32_t zd_ref
vertical speed reference in meter/s.
Definition: guidance_v.h:69
int32_t zdd_ref
vertical acceleration reference in meter/s^2.
Definition: guidance_v.h:75
struct RotorcraftNavigation nav
Definition: navigation.c:51
Rotorcraft navigation functions.
float descend_vspeed
descend speed setting, mostly used in flight plans
Definition: navigation.h:146
float heading
heading setpoint (in radians)
Definition: navigation.h:133
float climb_vspeed
climb speed setting, mostly used in flight plans
Definition: navigation.h:145
struct StabilizationSetpoint stab_sp_from_quat_ff_rates_f(struct FloatQuat *quat, struct FloatRates *rates)
int32_t stabilization_cmd[COMMANDS_NB]
Stabilization commands.
Definition: stabilization.c:34
int32_t transition_theta_offset
float stabilization_attitude_get_heading_f(void)
Read an attitude setpoint from the RC.
Rotorcraft attitude reference generation.
int num_iter
API to get/set the generic vehicle states.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:74
Stabilization setpoint.
Definition: stabilization.h:42
union StabilizationSetpoint::@278 sp
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:51
Periodic telemetry system header (includes downlink utility and generated code).
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
signed char int8_t
Typedef defining 8 bit char type.
Definition: vl53l1_types.h:103
float heading
Definition: wedgebug.c:258
int wls_alloc(float *u, float *v, float *umin, float *umax, float **B, float *u_guess, float *W_init, float *Wv, float *Wu, float *up, float gamma_sq, int imax, int n_u, int n_v)
active set algorithm for control allocation
Definition: wls_alloc.c:108