NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
anglerate.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Ninjaflight is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <stdint.h>
21 #include <stdbool.h>
22 
23 #include "common/axis.h"
24 #include "common/filter.h"
25 
26 #include "sensors/acceleration.h"
27 #include "sensors/instruments.h"
28 
29 #include "rx/rx.h"
30 
31 #include "rate_profile.h"
32 
33 #include "../config/anglerate.h"
34 
36 struct rxConfig_s;
37 
39  int16_t axis[3];
40 
41  // TODO: remove these intermediate values. For now leaving them here because unit tests rely on them.
42  // unit tests need to be rewritten to not rely on any kinf of intermediate crap and just test expected behavior!
43  float axis_P[3];
44  float axis_I[3];
45  float axis_D[3];
46 };
47 
48 struct anglerate {
49  // PIDweight is a scale factor for PIDs which is derived from the throttle and TPA setting, and 100 = 100% scale means no PID reduction
50  uint8_t PIDweight[3];
51 
52  int32_t lastITerm[3], ITermLimit[3];
53  float lastITermf[3], ITermLimitf[3];
54 
56 
57  // update outputs based on current attitude information
58  void (*update)(struct anglerate *self, float dT);
59 
60  int16_t body_rates[3];
61  int16_t body_angles[3];
62  int16_t user[3];
63 
64  // used for luxfloat
65  float lastRateForDelta[3];
67 
68  // used for mwii23
69  int32_t ITermAngle[2];
70  uint8_t pidScale[3];
71 
72  // used for mwiirewrite
73  int32_t lastRateForDeltai[3];
75 
77 
80 
81  uint8_t level_percent[2];
82  uint8_t flags;
83 
84  struct instruments *ins;
85  const struct config * config;
86 };
87 
88 #define IS_PID_CONTROLLER_FP_BASED(pidController) (pidController == PID_CONTROLLER_LUX_FLOAT)
89 //float pidScaleITermToRcInput(int axis);
90 //void pidFilterIsSetCheck(const struct pid_config *pidProfile);
91 
92 void anglerate_init(struct anglerate *self,
93  struct instruments *ins,
94  const struct config * config);
96 void anglerate_reset_angle_i(struct anglerate *self);
97 void anglerate_reset_rate_i(struct anglerate *self);
98 const struct pid_controller_output *anglerate_get_output_ptr(struct anglerate *self);
99 
100 void anglerate_input_body_rates(struct anglerate *self, int16_t x, int16_t y, int16_t z);
101 void anglerate_input_body_angles(struct anglerate *self, int16_t roll, int16_t pitch, int16_t yaw);
102 void anglerate_input_user(struct anglerate *self, int16_t roll, int16_t pitch, int16_t yaw);
103 
104 static inline int16_t anglerate_get_roll(struct anglerate *self) { return self->output.axis[0]; }
105 static inline int16_t anglerate_get_pitch(struct anglerate *self) { return self->output.axis[1]; }
106 static inline int16_t anglerate_get_yaw(struct anglerate *self) { return self->output.axis[2]; }
107 
108 void anglerate_update(struct anglerate *self, float dT);
109 
110 void anglerate_enable_antiwindup(struct anglerate *self, bool on);
111 void anglerate_enable_plimit(struct anglerate *self, bool on);
112 
113 // TODO: unify pid scaling so we have just one call for all pid controllers (axis scale is only used in mw23)
114 void anglerate_set_pid_axis_scale(struct anglerate *self, uint8_t axis, int32_t scale);
115 void anglerate_set_pid_axis_weight(struct anglerate *self, uint8_t axis, int32_t weight);
116 void anglerate_set_level_percent(struct anglerate *self, uint8_t roll, uint8_t pitch);
void anglerate_set_pid_axis_scale(struct anglerate *self, uint8_t axis, int32_t scale)
Definition: anglerate.c:313
int16_t user[3]
user input command
Definition: anglerate.h:62
uint8_t type
Definition: fat_standard.h:67
void anglerate_enable_antiwindup(struct anglerate *self, bool on)
Definition: anglerate.c:308
Definition: config.h:81
biquad_t deltaFilterState[3]
Definition: anglerate.h:55
void(* update)(struct anglerate *self, float dT)
Definition: anglerate.h:58
float axis_I[3]
Definition: anglerate.h:44
uint8_t level_percent[2]
Definition: anglerate.h:81
void anglerate_update(struct anglerate *self, float dT)
Definition: anglerate.c:299
int16_t axis[3]
Definition: anglerate.h:39
float deltaStatef[3][DTERM_AVERAGE_COUNT]
Definition: anglerate.h:66
void anglerate_set_algo(struct anglerate *self, pid_controller_type_t type)
Definition: anglerate.c:270
int32_t ITermLimit[3]
Definition: anglerate.h:52
struct instruments * ins
Definition: anglerate.h:84
void anglerate_reset_rate_i(struct anglerate *self)
Definition: anglerate.c:290
uint8_t PIDweight[3]
Definition: anglerate.h:50
void anglerate_init(struct anglerate *self, struct instruments *ins, const struct config *config)
int32_t ITermAngle[2]
Definition: anglerate.h:69
uint8_t pidScale[3]
Definition: anglerate.h:70
void anglerate_enable_plimit(struct anglerate *self, bool on)
Definition: anglerate.c:303
float lastITermf[3]
Definition: anglerate.h:53
float ITermLimitf[3]
Definition: anglerate.h:53
#define DTERM_AVERAGE_COUNT
Definition: anglerate.h:43
int16_t body_angles[3]
Definition: anglerate.h:61
const struct pid_controller_output * anglerate_get_output_ptr(struct anglerate *self)
Definition: anglerate.c:295
int16_t body_rates[3]
Definition: anglerate.h:60
int16_t pitch
Definition: accelerometer.h:52
uint16_t max_angle_inclination
Definition: anglerate.h:79
void anglerate_set_level_percent(struct anglerate *self, uint8_t roll, uint8_t pitch)
Definition: anglerate.c:341
uint8_t z
set the acc deadband for z-Axis, this ignores small accelerations
Definition: accelerometer.h:52
float lastRateForDelta[3]
Definition: anglerate.h:65
pid_controller_type_t
Definition: anglerate.h:59
Definition: accelerometer.h:38
int16_t yaw
Definition: sensors.h:31
void anglerate_input_user(struct anglerate *self, int16_t roll, int16_t pitch, int16_t yaw)
Definition: anglerate.c:335
Definition: anglerate.h:38
int16_t scale
Definition: mixer.h:14
float axis_P[3]
Definition: anglerate.h:43
void anglerate_set_pid_axis_weight(struct anglerate *self, uint8_t axis, int32_t weight)
Definition: anglerate.c:318
bool _delta_state_set
Definition: anglerate.h:78
float axis_D[3]
Definition: anglerate.h:45
struct pid_controller_output output
Definition: anglerate.h:76
int32_t lastRateForDeltai[3]
Definition: anglerate.h:73
int32_t deltaStatei[3][DTERM_AVERAGE_COUNT]
Definition: anglerate.h:74
void anglerate_reset_angle_i(struct anglerate *self)
Definition: anglerate.c:285
void anglerate_input_body_rates(struct anglerate *self, int16_t x, int16_t y, int16_t z)
Definition: anglerate.c:323
uint8_t flags
Definition: anglerate.h:82
void anglerate_input_body_angles(struct anglerate *self, int16_t roll, int16_t pitch, int16_t yaw)
Definition: anglerate.c:329
int32_t lastITerm[3]
Definition: anglerate.h:52
const struct config * config
Definition: anglerate.h:85
Definition: filter.h:27
Definition: instruments.h:11
Definition: anglerate.h:48
int16_t roll
Definition: accelerometer.h:51