Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
optical_flow_functions.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "optical_flow_functions.h"
22 #include "paparazzi.h"
23 #include "math/pprz_stat.h"
24 #include "std.h"
25 
26 // The maximum bank angle the algorithm can steer
27 #ifndef OFH_MAXBANK
28 #define OFH_MAXBANK 10.f
29 #endif
30 
31 // The low pass filter constant for updating the vision measurements in the algorithm
32 #ifndef OF_LP_CONST
33 #define OF_LP_CONST 0.5 // Average between 0.4 Bebop value and 0.6 ARDrone value
34 #endif
35 
36 // In case the autocovariance is used, select half the window size as the delay
37 #ifndef OF_COV_DELAY_STEPS
38 #define OF_COV_DELAY_STEPS COV_WINDOW_SIZE/2
39 #endif
40 
45 
47 
53 float set_cov_div(bool cov_method, struct OFhistory *history, struct DesiredInputs *inputs)
54 {
55  float cov_div = 0;
56  // histories and cov detection:
57 
58  history->OF[ind_histZ] = of_hover.divergence;
59 
60  float normalized_thrust = (float)(100.0 * inputs->thrust / MAX_PPRZ);
61  history->input[ind_histZ] = normalized_thrust;
62 
63  int ind_past = ind_histZ - OF_COV_DELAY_STEPS;
64  while (ind_past < 0) { ind_past += COV_WINDOW_SIZE; }
65  history->past_OF[ind_histZ] = history->OF[ind_past];
66 
67  // determine the covariance for hover detection:
68  // only take covariance into account if there are enough samples in the histories:
69  if (cov_method == 0 && cov_array_filledZ > 0) {
70  // TODO: step in hover set point causes an incorrectly perceived covariance
71  cov_div = covariance_f(history->input, history->OF, COV_WINDOW_SIZE);
72  } else if (cov_method == 1 && cov_array_filledZ > 1) {
73  // todo: delay steps should be invariant to the run frequency
74  cov_div = covariance_f(history->past_OF, history->OF, COV_WINDOW_SIZE);
75  }
76 
77  if (cov_array_filledZ < 2 && ind_histZ + 1 == COV_WINDOW_SIZE) {
79  }
81 
82  return cov_div;
83 }
84 
85 
90 void set_cov_flow(bool cov_method, struct OFhistory *historyX, struct OFhistory *historyY, struct DesiredInputs *inputs,
91  struct FloatVect3 *covs)
92 {
93  // histories and cov detection:
96 
97  int ind_past = ind_histXY - OF_COV_DELAY_STEPS;
98  while (ind_past < 0) { ind_past += COV_WINDOW_SIZE; }
99  historyX->past_OF[ind_histXY] = historyX->OF[ind_past];
100  historyY->past_OF[ind_histXY] = historyY->OF[ind_past];
101  float normalized_phi = (float)(100.0 * inputs->phi / OFH_MAXBANK);
102  float normalized_theta = (float)(100.0 * inputs->theta / OFH_MAXBANK);
103  historyX->input[ind_histXY] = normalized_phi;
104  historyY->input[ind_histXY] = normalized_theta;
105 
106  // determine the covariance for hover detection:
107  // only take covariance into account if there are enough samples in the histories:
108  if (cov_method == 0 && cov_array_filledXY > 0) {
109  // // TODO: step in hover set point causes an incorrectly perceived covariance
112  } else if (cov_method == 1 && cov_array_filledXY > 1) {
113  if (cov_array_filledXY > 1) {
114  // todo: delay steps should be invariant to the run frequency
117  }
118  }
119 
120  if (cov_array_filledXY < 2 && ind_histXY + 1 == COV_WINDOW_SIZE) {
122  }
124 }
125 
126 
133 float PID_flow_control(float dt, struct OpticalFlowHoverControl *of_hover_ctrl)
134 {
135  // update the controller errors:
136  float lp_factor = dt / OF_LP_CONST;
137  Bound(lp_factor, 0.f, 1.f);
138 
139  // maintain the controller errors:
140  of_hover_ctrl->PID.sum_err += of_hover_ctrl->PID.err;
141  of_hover_ctrl->PID.d_err += (((of_hover_ctrl->PID.err - of_hover_ctrl->PID.previous_err) / dt) -
142  of_hover_ctrl->PID.d_err) * lp_factor;
143  of_hover_ctrl->PID.previous_err = of_hover_ctrl->PID.err;
144 
145  // compute the desired angle
146  float des_angle = of_hover_ctrl->PID.P * of_hover_ctrl->PID.err + of_hover_ctrl->PID.I *
147  of_hover_ctrl->PID.sum_err + of_hover_ctrl->PID.D * of_hover_ctrl->PID.d_err;
148 
149  // Bound angle:
150  Bound(des_angle, -OFH_MAXBANK, OFH_MAXBANK);
151 
152  return des_angle;
153 }
154 
162 {
163  // update the controller errors:
164  float lp_factor = dt / OF_LP_CONST;
165  Bound(lp_factor, 0.f, 1.f);
166 
167  // maintain the controller errors:
168  of_hover_ctrl->PID.sum_err += of_hover_ctrl->PID.err;
169  of_hover_ctrl->PID.d_err += (((of_hover_ctrl->PID.err - of_hover_ctrl->PID.previous_err) / dt) -
170  of_hover_ctrl->PID.d_err) * lp_factor;
171  of_hover_ctrl->PID.previous_err = of_hover_ctrl->PID.err;
172 
173  // PID control:
174  int32_t thrust = (of_hover_ctrl->nominal_value
175  + of_hover_ctrl->PID.P * of_hover_ctrl->PID.err
176  + of_hover_ctrl->PID.I * of_hover_ctrl->PID.sum_err
177  + of_hover_ctrl->PID.D * of_hover_ctrl->PID.d_err) * MAX_PPRZ;
178 
179  // bound thrust:
180  Bound(thrust, 0.25 * of_hover_ctrl->nominal_value * MAX_PPRZ, MAX_PPRZ);
181 
182  return thrust;
183 }
float lp_factor
Definition: ins_flow.c:182
uint8_t cov_array_filledXY
int32_t PID_divergence_control(float dt, struct OpticalFlowHoverControl *of_hover_ctrl)
Determine and set the thrust for constant divergence control.
struct OpticalFlowHover of_hover
float set_cov_div(bool cov_method, struct OFhistory *history, struct DesiredInputs *inputs)
Set the covariance of the divergence and the thrust / past divergence This funciton should only be ca...
float PID_flow_control(float dt, struct OpticalFlowHoverControl *of_hover_ctrl)
Determine and set the desired angle for constant flow control.
#define OF_COV_DELAY_STEPS
#define OFH_MAXBANK
uint32_t ind_histXY
uint32_t ind_histZ
uint8_t cov_array_filledZ
void set_cov_flow(bool cov_method, struct OFhistory *historyX, struct OFhistory *historyY, struct DesiredInputs *inputs, struct FloatVect3 *covs)
Set the covariance of the flow and past flow / desired angle This funciton should only be called once...
#define OF_LP_CONST
float d_err
difference of error for the D-gain
float P
P-gain for control.
float divergence
Divergence estimate.
struct GainsPID PID
The struct with the PID gains.
float flowX
Flow estimate in X direction.
float previous_err
Previous tracking error.
float I
I-gain for control.
float input[COV_WINDOW_SIZE]
float sum_err
integration of the error for I-gain
float past_OF[COV_WINDOW_SIZE]
float D
D-gain for control.
float flowY
Flow estimate in Y direction.
float nominal_value
The nominal value of thrust, phi or theta depending on Z, Y, X.
float OF[COV_WINDOW_SIZE]
float err
Current tracking error.
#define COV_WINDOW_SIZE
struct OFhistory historyX
struct OFhistory historyY
bool cov_method
method to calculate the covariance: between thrust and div / angle and flow (0) or div and div past /...
float normalized_thrust
float cov_div
#define MAX_PPRZ
Definition: paparazzi.h:8
float covariance_f(float *arr1, float *arr2, uint32_t n_elements)
Compute the covariance of two arrays V(X) = E[(X-E[X])(Y-E[Y])] = E[XY] - E[X]E[Y] where E[X] is the ...
Definition: pprz_stat.c:152
Statistics functions.
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98