dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
virtualflybar.c
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 #include "openpilot.h"
32 #include "physical_constants.h"
33 #include "pid.h"
34 #include "stabilization.h"
35 #include "vbarsettings.h"
36 
38 static float vbar_integral[MAX_AXES];
39 extern float vbar_decay;
40 
42 static float bound(float val, float range);
43 
44 int stabilization_virtual_flybar(float gyro, float command, float *output, float dT, bool reinit, uint32_t axis, struct pid *pid, VbarSettingsData *settings)
45 {
46  float gyro_gain = 1.0f;
47 
48  if(reinit)
49  vbar_integral[axis] = 0;
50 
51  // Track the angle of the virtual flybar which includes a slow decay
52  vbar_integral[axis] = vbar_integral[axis] * vbar_decay + gyro * dT;
53  vbar_integral[axis] = bound(vbar_integral[axis], settings->VbarMaxAngle);
54 
55  // Compute the normal PID controller output
56  float pid_out = pid_apply_setpoint(pid, NULL, 0, gyro);
57 
58  // Command signal can indicate how much to disregard the gyro feedback (fast flips)
59  if (settings->VbarGyroSuppress > 0.0f) {
60  gyro_gain = (1.0f - fabsf(command) * settings->VbarGyroSuppress / 100.0f);
61  gyro_gain = (gyro_gain < 0.0f) ? 0.0f : gyro_gain;
62  }
63 
64  // Command signal is composed of stick input added to the gyro and virtual flybar
65  // Note the PID output has a positive sign (consistent with its use in other places)
66  // but the integral is negative since it is the angle of the virtual flybar which is
67  // the negative of the accumulated error.
68  *output = command * settings->VbarSensitivity[axis] +
69  gyro_gain * (pid_out - vbar_integral[axis] * pid->i);
70 
71  return 0;
72 }
73 
79 int stabilization_virtual_flybar_pirocomp(float z_gyro, float dT)
80 {
81  float cy = cosf(z_gyro * DEG2RAD * dT);
82  float sy = sinf(z_gyro * DEG2RAD * dT);
83 
84  float vbar_pitch = cy * vbar_integral[1] - sy * vbar_integral[0];
85  float vbar_roll = sy * vbar_integral[1] + cy * vbar_integral[0];
86 
87  vbar_integral[1] = vbar_pitch;
88  vbar_integral[0] = vbar_roll;
89 
90  return 0;
91 }
92 
96 static float bound(float val, float range)
97 {
98  if(val < -range) {
99  val = -range;
100  } else if(val > range) {
101  val = range;
102  }
103  return val;
104 }
105 
float vbar_decay
int stabilization_virtual_flybar(float gyro, float command, float *output, float dT, bool reinit, uint32_t axis, struct pid *pid, VbarSettingsData *settings)
Definition: virtualflybar.c:44
static float bound(float val, float range)
Private methods.
Definition: virtualflybar.c:96
float i
Definition: pid.h:44
static volatile FlightStatsSettingsData settings
static float vbar_integral[MAX_AXES]
Private variables.
Definition: virtualflybar.c:38
int stabilization_virtual_flybar_pirocomp(float z_gyro, float dT)
Definition: virtualflybar.c:79
tuple f
Definition: px_mkfw.py:81
Attitude stabilization module.
Includes PiOS and core architecture components.
float pid_apply_setpoint(struct pid *pid, struct pid_deadband *deadband, const float setpoint, const float measured)
Definition: pid.c:141
Definition: pid.h:42
PID Control algorithms.