Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
guidance_indi.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"
32 #include "subsystems/ins/ins_int.h"
34 #include "state.h"
35 #include "subsystems/imu.h"
40 #include "mcu_periph/sys_time.h"
41 #include "autopilot.h"
43 
46 struct FloatVect3 sp_accel = {0.0,0.0,0.0};
47 
51 float filt_accelzbody = 0;
52 float filt_accelzbodyd = 0;
54 float roll_filt = 0;
55 float roll_filtd = 0;
56 float roll_filtdd = 0;
57 float pitch_filt = 0;
58 float pitch_filtd = 0;
59 float pitch_filtdd = 0;
60 
61 struct FloatMat33 Ga;
64 
65 float filter_omega = 20.0;
66 float filter_zeta = 0.65;
67 
69 
70 void guidance_indi_enter(void) {
71  filt_accelzbody = 0;
72  filt_accelzbodyd = 0;
74  roll_filt = 0;
75  roll_filtd = 0;
76  roll_filtdd = 0;
77  pitch_filt = 0;
78  pitch_filtd = 0;
79  pitch_filtdd = 0;
83 }
84 
85 void guidance_indi_run(bool_t in_flight, int32_t heading) {
86 
87  //filter accel to get rid of noise
88  //filter attitude to synchronize with accel
91 
92  float pos_x_err = POS_FLOAT_OF_BFP(guidance_h.ref.pos.x) - stateGetPositionNed_f()->x; //+-ov.y/ (STABILIZATION_ATTITUDE_SP_MAX_THETA)*3.0;
93  float pos_y_err = POS_FLOAT_OF_BFP(guidance_h.ref.pos.y) - stateGetPositionNed_f()->y; //+ ov.x/ (STABILIZATION_ATTITUDE_SP_MAX_PHI)*3.0;
94 
95  float speed_sp_x = pos_x_err*guidance_indi_pos_gain;
96  float speed_sp_y = pos_y_err*guidance_indi_pos_gain;
97 
98  sp_accel.x = (speed_sp_x - stateGetSpeedNed_f()->x)*guidance_indi_speed_gain;
99  sp_accel.y = (speed_sp_y - stateGetSpeedNed_f()->y)*guidance_indi_speed_gain;
100 // sp_accel.x = (radio_control.values[RADIO_PITCH]/9600.0)*8.0;
101 // sp_accel.y = -(radio_control.values[RADIO_ROLL]/9600.0)*8.0;
102 
103  // struct FloatMat33 Ga;
105  MAT33_INV(Ga_inv, Ga);
106 
107  float altitude_sp = POS_FLOAT_OF_BFP(guidance_v_z_sp);
108  float vertical_velocity_sp = guidance_indi_pos_gain*(altitude_sp - stateGetPositionNed_f()->z);
109 // float vertical_velocity_rc_euler = -(stabilization_cmd[COMMAND_THRUST]-4500.0)/4500.0*2.0;
110  float vertical_velocity_err_euler = vertical_velocity_sp - stateGetSpeedNed_f()->z;
111  sp_accel.z = vertical_velocity_err_euler*guidance_indi_speed_gain;
112 
113  struct FloatVect3 a_diff = { sp_accel.x - filt_accel_ned.x, sp_accel.y -filt_accel_ned.y, 0.0};
114 
115  Bound(a_diff.x, -6.0, 6.0);
116  Bound(a_diff.y, -6.0, 6.0);
117  Bound(a_diff.z, -9.0, 9.0);
118 
119  MAT33_VECT3_MUL(euler_cmd, Ga_inv, a_diff);
120 
123  guidance_euler_cmd.psi = 0;//stateGetNedToBodyEulers_f()->psi;
124 
125  //Bound euler angles to prevent flipping and keep upright
128 
130 
131 }
132 
133 //low pass the accelerometer measurements with a second order filter to remove noise from vibrations
135 {
137 // filt_accelzbody = filt_accelzbody + filt_accelzbodyd / PERIODIC_FREQUENCY; //also do body z accel
138 
140 // filt_accelzbodyd = filt_accelzbodyd + filt_accelzbodydd / PERIODIC_FREQUENCY; //also do body z accel
141 
145 // filt_accelzbodydd= -filt_accelzbodyd * 2 * filter_zeta * filter_omega + (accel_meas_body_f.z - filt_accelzbody) * filter_omega*filter_omega;
146 }
147 
149 {
152 
155 // float cospsi = cosf(stateGetNedToBodyEulers_f()->psi);
156 // float sinpsi = sinf(stateGetNedToBodyEulers_f()->psi);
157 
160 }
161 
162 void guidance_indi_calcG(struct FloatMat33 *Gmat) {
163 
164  struct FloatEulers *euler = stateGetNedToBodyEulers_f();
165 
166  float sphi = sinf(euler->phi);
167  float cphi = cosf(euler->phi);
168  float stheta = sinf(euler->theta);
169  float ctheta = cosf(euler->theta);
170  float spsi = sinf(euler->psi);
171  float cpsi = cosf(euler->psi);
172  float T = -9.81; //minus gravity is a good guesstimate of the thrust force
173 // float T = (filt_accelzn-9.81)/(cphi*ctheta); //calculate specific force in body z axis by using the accelerometer
174 // float T = filt_accelzbody; //if body acceleration is available, use that!
175 
176  RMAT_ELMT(*Gmat, 0, 0) = (cphi*spsi - sphi*cpsi*stheta)*T;
177  RMAT_ELMT(*Gmat, 1, 0) = (-sphi*spsi*stheta - cpsi*cphi)*T;
178  RMAT_ELMT(*Gmat, 2, 0) = -ctheta*sphi*T;
179  RMAT_ELMT(*Gmat, 0, 1) = (cphi*cpsi*ctheta)*T;
180  RMAT_ELMT(*Gmat, 1, 1) = (cphi*spsi*ctheta)*T;
181  RMAT_ELMT(*Gmat, 2, 1) = -stheta*cphi*T;
182  RMAT_ELMT(*Gmat, 0, 2) = sphi*spsi + cphi*cpsi*stheta;
183  RMAT_ELMT(*Gmat, 1, 2) = cphi*spsi*stheta - cpsi*sphi;
184  RMAT_ELMT(*Gmat, 2, 2) = cphi*ctheta;
185 }
186 
188 {
189  struct FloatQuat q_rp_cmd;
190  float_quat_of_eulers(&q_rp_cmd, &guidance_euler_cmd); //TODO this is a quaternion without yaw! add the desired yaw before you use it!
191 
192  /* get current heading */
193  const struct FloatVect3 zaxis = {0., 0., 1.};
194  struct FloatQuat q_yaw;
195 
196  float_quat_of_axis_angle(&q_yaw, &zaxis, stateGetNedToBodyEulers_f()->psi);
197 
198  /* roll/pitch commands applied to to current heading */
199  struct FloatQuat q_rp_sp;
200  float_quat_comp(&q_rp_sp, &q_yaw, &q_rp_cmd);
201  float_quat_normalize(&q_rp_sp);
202 
203  struct FloatQuat q_sp;
204 
205  if (in_flight) {
206  /* get current heading setpoint */
207  struct FloatQuat q_yaw_sp;
208  float_quat_of_axis_angle(&q_yaw_sp, &zaxis, ANGLE_FLOAT_OF_BFP(heading));
209 
210 
211  /* rotation between current yaw and yaw setpoint */
212  struct FloatQuat q_yaw_diff;
213  float_quat_comp_inv(&q_yaw_diff, &q_yaw_sp, &q_yaw);
214 
215  /* compute final setpoint with yaw */
216  float_quat_comp_norm_shortest(&q_sp, &q_rp_sp, &q_yaw_diff);
217  } else {
218  QUAT_COPY(q_sp, q_rp_sp);
219  }
220 
222 }
int32_t guidance_v_z_sp
altitude setpoint in meters (input).
Definition: guidance_v.c:118
float filter_zeta
Definition: guidance_indi.c:66
float filt_accelzbody
Definition: guidance_indi.c:51
void guidance_indi_enter(void)
Definition: guidance_indi.c:70
void float_quat_comp_inv(struct FloatQuat *a2b, struct FloatQuat *a2c, struct FloatQuat *b2c)
Composition (multiplication) of two quaternions.
void float_quat_of_eulers(struct FloatQuat *q, struct FloatEulers *e)
Quaternion from Euler angles.
float y
in meters
float phi
in radians
struct FloatMat33 Ga
Definition: guidance_indi.c:61
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1114
void float_quat_comp(struct FloatQuat *a2c, struct FloatQuat *a2b, struct FloatQuat *b2c)
Composition (multiplication) of two quaternions.
General attitude stabilization interface for rotorcrafts.
struct FloatVect3 sp_accel
Definition: guidance_indi.c:46
void guidance_indi_run(bool_t in_flight, int32_t heading)
Definition: guidance_indi.c:85
float pitch_filtdd
Definition: guidance_indi.c:59
float psi
in radians
Vertical guidance for rotorcrafts.
struct HorizontalGuidanceReference ref
reference calculated from setpoints
Definition: guidance_h.h:98
struct HorizontalGuidance guidance_h
Definition: guidance_h.c:79
float roll_filtdd
Definition: guidance_indi.c:56
#define ANGLE_FLOAT_OF_BFP(_ai)
float filter_omega
Definition: guidance_indi.c:65
euler angles
float z
in meters
struct FloatVect3 filt_accel_ned_d
Definition: guidance_indi.c:49
Roation quaternion.
void guidance_indi_filter_accel(void)
#define QUAT_COPY(_qo, _qi)
Definition: pprz_algebra.h:532
float theta
in radians
#define FLOAT_VECT3_ZERO(_v)
static float heading
Definition: ahrs_infrared.c:45
struct FloatVect3 filt_accel_ned_dd
Definition: guidance_indi.c:50
void guidance_indi_calcG(struct FloatMat33 *Gmat)
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition: state.h:885
Architecture independent timing functions.
float guidance_indi_pos_gain
Definition: guidance_indi.c:44
static struct NedCoor_f * stateGetAccelNed_f(void)
Get acceleration in NED coordinates (float).
Definition: state.h:1009
struct FloatMat33 Ga_inv
Definition: guidance_indi.c:62
#define QUAT_BFP_OF_REAL(_qi, _qf)
Definition: pprz_algebra.h:685
float filt_accelzbodyd
Definition: guidance_indi.c:52
void stabilization_attitude_set_setpoint_rp_quat_f(bool_t in_flight, int32_t heading)
static void float_quat_normalize(struct FloatQuat *q)
struct FloatEulers guidance_euler_cmd
Definition: guidance_indi.c:68
struct FloatVect3 euler_cmd
Definition: guidance_indi.c:63
struct FloatQuat stab_att_sp_quat
with INT32_QUAT_FRAC
#define GUIDANCE_H_MAX_BANK
Definition: guidance_h.c:65
Inertial Measurement Unit interface.
float roll_filtd
Definition: guidance_indi.c:55
float guidance_indi_speed_gain
Definition: guidance_indi.c:45
signed long int32_t
Definition: types.h:19
Some helper functions to check RC sticks.
#define MAT33_VECT3_MUL(_vout, _mat, _vin)
Definition: pprz_algebra.h:451
API to get/set the generic vehicle states.
#define RMAT_ELMT(_rm, _row, _col)
Definition: pprz_algebra.h:593
void float_quat_of_axis_angle(struct FloatQuat *q, const struct FloatVect3 *uv, float angle)
Quaternion from unit vector and angle.
void float_quat_comp_norm_shortest(struct FloatQuat *a2c, struct FloatQuat *a2b, struct FloatQuat *b2c)
Composition (multiplication) of two quaternions with normalization.
float filt_accelzbodydd
Definition: guidance_indi.c:53
Horizontal guidance for rotorcrafts.
float roll_filt
Definition: guidance_indi.c:54
struct Int32Vect2 pos
with INT32_POS_FRAC
Definition: guidance_h.h:76
struct FloatVect3 filt_accel_ned
Definition: guidance_indi.c:48
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:47
#define VECT3_ADD_SCALED(_a, _b, _s)
Definition: pprz_algebra.h:167
float pitch_filtd
Definition: guidance_indi.c:58
#define POS_FLOAT_OF_BFP(_ai)
void guidance_indi_filter_attitude(void)
#define MAT33_INV(_minv, _m)
Definition: pprz_algebra.h:477
float pitch_filt
Definition: guidance_indi.c:57
static struct NedCoor_f * stateGetPositionNed_f(void)
Get position in local NED coordinates (float).
Definition: state.h:693
Rotorcraft attitude reference generation.
A guidance mode based on Incremental Nonlinear Dynamic Inversion Come to ICRA2016 to learn more! ...
float x
in meters
INS for rotorcrafts combining vertical and horizontal filters.