Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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
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
53float set_cov_div(bool cov_method, struct OFhistory *history, struct DesiredInputs *inputs)
54{
55 float cov_div = 0;
56 // histories and cov detection:
57
59
60 float normalized_thrust = (float)(100.0 * inputs->thrust / MAX_PPRZ);
62
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
72 } else if (cov_method == 1 && cov_array_filledZ > 1) {
73 // todo: delay steps should be invariant to the run frequency
75 }
76
79 }
81
82 return cov_div;
83}
84
85
90void 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
98 while (ind_past < 0) { ind_past += COV_WINDOW_SIZE; }
101 float normalized_phi = (float)(100.0 * inputs->phi / OFH_MAXBANK);
102 float normalized_theta = (float)(100.0 * inputs->theta / OFH_MAXBANK);
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
122 }
124}
125
126
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:
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
uint16_t foo
Definition main_demo5.c:58
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 divergence
Divergence estimate.
float flowX
Flow estimate in X direction.
float input[COV_WINDOW_SIZE]
float past_OF[COV_WINDOW_SIZE]
float flowY
Flow estimate in Y direction.
float OF[COV_WINDOW_SIZE]
#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.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.