Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
guidance_OA.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Roland + Clint
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  */
22 
26 // Own Header
27 #include "guidance_OA.h"
28 
29 // Stabilization
30 #include <stdio.h>
33 #include "autopilot.h"
37 #include "std.h"
38 //#include "modules/read_matrix_serial/read_matrix_serial.h"
39 
40 //#ifndef CMD_OF_SAT
41 #define CMD_OF_SAT 1500 // 1500 = 40 deg = 2859.1851
42 //#endif
43 
44 #ifndef VISION_PHI_PGAIN
45 #define VISION_PHI_PGAIN 400
46 #endif
48 
49 #ifndef VISION_PHI_IGAIN
50 #define VISION_PHI_IGAIN 20
51 #endif
53 
54 #ifndef VISION_THETA_PGAIN
55 #define VISION_THETA_PGAIN 400
56 #endif
58 
59 #ifndef VISION_THETA_IGAIN
60 #define VISION_THETA_IGAIN 20
61 #endif
63 
64 #ifndef VISION_DESIRED_VX
65 #define VISION_DESIRED_VX 0
66 #endif
68 
69 #ifndef VISION_DESIRED_VY
70 #define VISION_DESIRED_VY 0
71 #endif
73 
74 /* Check the control gains */
75 #if (VISION_PHI_PGAIN < 0) || \
76  (VISION_PHI_IGAIN < 0) || \
77  (VISION_THETA_PGAIN < 0) || \
78  (VISION_THETA_IGAIN < 0)
79 #error "ALL control gains have to be positive!!!"
80 #endif
81 
82 /* Initialize the default gains and settings */
85  .phi_igain = VISION_PHI_IGAIN,
86  .theta_pgain = VISION_THETA_PGAIN,
87  .theta_igain = VISION_THETA_IGAIN,
88  .desired_vx = VISION_DESIRED_VX,
89  .desired_vy = VISION_DESIRED_VY
90 };
91 
93 int8_t filter_flag = 0; //0 =>no filter 1 =>Kalman filter 2 =>Butterworth filter
94 int8_t repulsionforce_filter_flag = 0; //0 =>no filter 1 =>Butterworth filter
95 
97  PINGPONG; //0 =>No OA only opticflow 1=pingpong 2=>pot_heading 3=>pot_vel 4=>vector 5=>safetyzone
99 float vref_max = 100;
101 
102 //variables set by OA
103 float ref_pitch = 0.0;
104 float ref_roll = 0.0;
105 
106 //initialize variables
107 float r_dot_new = 0.0;
108 float desired_vx = 0.0;
109 float desired_vy = 0.0;
110 float speed_pot = 0.0;
111 float yaw_diff = 0.0;
112 float alpha_fil = 1.0;
113 float heading_target = 0;
114 float new_heading = 0;
115 float v_desired = 0.0;
116 
118 float yaw_rate_write = 0;
119 float yaw_ref_write = 0;
122 
123 float err_vx = 0;
124 float err_vy = 0;
125 
126 float Total_Kan_x = 0;
127 float Total_Kan_y = 0;
128 
130 struct FloatVect3 Total_force = {0, 0, 0};
131 //
132 //static void send_INPUT_CONTROL(void)
133 //{
134 // //DOWNLINK_SEND_INPUT_CONTROL(DefaultChannel, DefaultDevice, &Total_Kan_x, &Total_Kan_y, &opticflow_stab.desired_vx,
135 // // &opticflow_stab.desired_vy, &opti_speed_read.x, &opti_speed_read.y);
136 //}
137 
143 {
144  /* Reset the integrated errors */
147 
148  /* Set rool/pitch to 0 degrees and psi to current heading */
149  opticflow_stab.cmd.phi = 0;
152 
153  new_heading = 0;
154 
155 // register_periodic_telemetry(DefaultPeriodic, "INPUT_CONTROL", send_INPUT_CONTROL);
156 
158 }
159 
164 void guidance_module_run(bool in_flight)
165 {
166  OA_update();
168  struct ThrustSetpoint th = guidance_v_run(in_flight);
169  /* Run the default attitude stabilization */
170  stabilization_attitude_run(in_flight, &sp, &th, stabilization.cmd);
171 }
172 
177 void OA_update()
178 {
179  float v_x = 0;
180  float v_y = 0;
181 
182  if (opti_speed_flag == 1) {
183  //rotation_vector.psi = stateGetNedToBodyEulers_f()->psi;
184  //opti_speed = stateGetSpeedNed_f();
185 
186  //Transform to body frame.
187  //float_rmat_of_eulers_312(&T, &rotation_vector)Attractforce_goal_send;
188  //MAT33_VECT3_MUL(*opti_speed, T, *opti_speed);
189 
190  // Calculate the speed in body frame
191  struct FloatVect2 speed_cur;
192  float psi = stateGetNedToBodyEulers_f()->psi;
193  float s_psi = sin(psi);
194  float c_psi = cos(psi);
195  speed_cur.x = c_psi * stateGetSpeedNed_f()->x + s_psi * stateGetSpeedNed_f()->y;
196  speed_cur.y = -s_psi * stateGetSpeedNed_f()->x + c_psi * stateGetSpeedNed_f()->y;
197 
198  opti_speed_read.x = speed_cur.x * 100;
199  opti_speed_read.y = speed_cur.y * 100;
200 
201  //set result_vel
202  v_x = speed_cur.y * 100;
203  v_y = speed_cur.x * 100;
204  } else {
205  }
206 
208  /* Calculate the error if we have enough flow */
211 
214 
215  /* Calculate the integrated errors (TODO: bound??) */
218 
219  /* Calculate the commands */
224 
225  /* Bound the roll and pitch commands */
226  BoundAbs(opticflow_stab.cmd.phi, CMD_OF_SAT);
227  BoundAbs(opticflow_stab.cmd.theta, CMD_OF_SAT);
228  }
229 
230  if (OA_method_flag == PINGPONG) {
233  }
234 
235  if (OA_method_flag == 2) {
238 
239  alpha_fil = 0.1;
240 
243 
245 
248 
249  /* Calculate the error if we have enough flow */
250 
253 
254  /* Calculate the integrated errors (TODO: bound??) */
257 
258  /* Calculate the commands */
261 
264 
265  /* Bound the roll and pitch commands */
266  BoundAbs(opticflow_stab.cmd.phi, CMD_OF_SAT);
267  BoundAbs(opticflow_stab.cmd.theta, CMD_OF_SAT);
268 
269  }
270  if (OA_method_flag == POT_HEADING) {
272 
275 
276  /* Calculate the error if we have enough flow */
279 
280 
281  /* Calculate the integrated errors (TODO: bound??) */
284 
285  /* Calculate the commands */
290 
291  /* Bound the roll and pitch commands */
292  BoundAbs(opticflow_stab.cmd.phi, CMD_OF_SAT);
294 
295  }
296 
298  //vector field method
299  float v_desired_total;
300 
303 
305  Total_Kan_y; //alpha_fil*(Repulsionforce_Kan.y+Attractforce_goal.y) + result->vel_x;
307  Total_Kan_x; //alpha_fil*(Repulsionforce_Kan.x+Attractforce_goal.x) + result->vel_y;
308 
311 
312  if (v_desired_total >= vref_max) {
315  }
316 
317  /* Calculate the error if we have enough flow */
318 
319  //alpha_fil needs to be tuned!
322 
323  /* Calculate the integrated errors (TODO: bound??) */
326 
327  /* Calculate the commands */
332 
333  /* Bound the roll and pitch commands */
334  BoundAbs(opticflow_stab.cmd.phi, CMD_OF_SAT);
335  BoundAbs(opticflow_stab.cmd.theta, CMD_OF_SAT);
336 
337  }
338 }
Core autopilot interface common to all firmwares.
Interface for electrical status: supply voltage, current, battery status, etc.
float psi
in radians
int32_t phi
in rad with INT32_ANGLE_FRAC
int32_t psi
in rad with INT32_ANGLE_FRAC
int32_t theta
in rad with INT32_ANGLE_FRAC
#define ANGLE_BFP_OF_REAL(_af)
#define INT32_ANGLE_NORMALIZE(_a)
static struct Int32Eulers * stateGetNedToBodyEulers_i(void)
Get vehicle body attitude euler angles (int).
Definition: state.h:1288
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1306
static struct NedCoor_f * stateGetSpeedNed_f(void)
Get ground speed in local NED coordinates (float).
Definition: state.h:1049
float err_vy
Definition: guidance_OA.c:124
struct NedCoor_f opti_speed_read
Definition: guidance_OA.c:129
int8_t filter_flag
Definition: guidance_OA.c:93
float alpha_fil
Definition: guidance_OA.c:112
struct opticflow_stab_t opticflow_stab
Definition: guidance_OA.c:83
float r_dot_new
Definition: guidance_OA.c:107
int32_t yaw_rate
Definition: guidance_OA.c:117
float new_heading
Definition: guidance_OA.c:114
struct FloatVect3 Total_force
Definition: guidance_OA.c:130
float vref_max
Definition: guidance_OA.c:99
float heading_target
Definition: guidance_OA.c:113
#define VISION_THETA_PGAIN
Definition: guidance_OA.c:55
float speed_pot
Definition: guidance_OA.c:110
#define VISION_DESIRED_VY
Definition: guidance_OA.c:70
float Total_Kan_x
Definition: guidance_OA.c:126
oa_method OA_method_flag
Definition: guidance_OA.c:96
float Total_Kan_y
Definition: guidance_OA.c:127
float ref_pitch
Definition: guidance_OA.c:103
float v_desired
Definition: guidance_OA.c:115
void guidance_module_enter(void)
Horizontal guidance mode enter resets the errors and starts the controller.
Definition: guidance_OA.c:142
int8_t repulsionforce_filter_flag
Definition: guidance_OA.c:94
float yaw_ref_write
Definition: guidance_OA.c:119
void guidance_module_run(bool in_flight)
Main guidance loop.
Definition: guidance_OA.c:164
int32_t keep_turning
Definition: guidance_OA.c:121
int8_t opti_speed_flag
Definition: guidance_OA.c:98
int32_t keep_yaw_rate
Definition: guidance_OA.c:120
float yaw_rate_write
Definition: guidance_OA.c:118
#define VISION_DESIRED_VX
Definition: guidance_OA.c:65
float desired_vx
Definition: guidance_OA.c:108
#define VISION_PHI_PGAIN
Definition: guidance_OA.c:45
#define CMD_OF_SAT
Definition: guidance_OA.c:41
#define VISION_PHI_IGAIN
Definition: guidance_OA.c:50
#define VISION_THETA_IGAIN
Definition: guidance_OA.c:60
float err_vx
Definition: guidance_OA.c:123
float yaw_diff
Definition: guidance_OA.c:111
float desired_vy
Definition: guidance_OA.c:109
void OA_update()
Update the controls based on a vision result.
Definition: guidance_OA.c:177
float ref_roll
Definition: guidance_OA.c:104
Guidance for the obstacle avoidance methods.
float err_vx_int
The integrated velocity error in x direction (m/s)
struct Int32Eulers cmd
The commands that are send to the hover loop.
float err_vy_int
The integrated velocity error in y direction (m/s)
int32_t theta_pgain
The pitch P gain on the err_vy.
int32_t theta_igain
The pitch I gain on the err_vy_int.
oa_method
Definition: guidance_OA.h:52
@ SAFETYZONE
Definition: guidance_OA.h:52
@ LOGICBASED
Definition: guidance_OA.h:52
@ PINGPONG
Definition: guidance_OA.h:52
@ POT_HEADING
Definition: guidance_OA.h:52
@ NO_OBSTACLE_AVOIDANCE
Definition: guidance_OA.h:52
float desired_vy
The desired velocity in the y direction (cm/s)
float desired_vx
The desired velocity in the x direction (cm/s)
int32_t phi_pgain
The roll P gain on the err_vx.
int32_t phi_igain
The roll I gain on the err_vx_int.
Definition: point.c:84
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
float x
in meters
float y
in meters
vector in North East Down coordinates Units: meters
void guidance_v_mode_changed(uint8_t new_mode)
Definition: guidance_v.c:132
struct ThrustSetpoint guidance_v_run(bool in_flight)
Guidance vertical run functions.
Definition: guidance_v.c:221
Vertical guidance for rotorcrafts.
#define GUIDANCE_V_MODE_HOVER
Definition: guidance_v.h:39
General attitude stabilization interface for rotorcrafts.
void stabilization_attitude_run(bool in_flight, struct StabilizationSetpoint *sp, struct ThrustSetpoint *thrust, int32_t *cmd)
Attitude control run function.
struct Stabilization stabilization
Definition: stabilization.c:41
struct StabilizationSetpoint stab_sp_from_eulers_i(struct Int32Eulers *eulers)
int32_t cmd[COMMANDS_NB]
output command vector, range from [-MAX_PPRZ:MAX_PPRZ] (store for messages)
Stabilization setpoint.
Definition: stabilization.h:53
union StabilizationSetpoint::@278 sp
Thrust setpoint // TODO to a setpoint header Structure to store the desired thrust vector with differ...
Definition: stabilization.h:82
union ThrustSetpoint::@284 sp
Periodic telemetry system header (includes downlink utility and generated code).
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
signed char int8_t
Typedef defining 8 bit char type.
Definition: vl53l1_types.h:103