NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mixer.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 "system_calls.h"
21 #include "../config/mixer.h"
22 #include "../config/rc_controls.h"
23 #include "../config/rx.h"
24 
25 #include "../common/filter.h"
26 #include "../common/maths.h"
27 
28 struct config;
29 /*
30 #if USE_QUAD_MIXER_ONLY == 1
31 #define MAX_SUPPORTED_SERVOS 1
32 #else
33 #define MAX_SUPPORTED_SERVOS 8
34 #endif
35 #define MAX_SERVO_RULES (2 * MAX_SUPPORTED_SERVOS)
36 #define MAX_SERVO_SPEED UINT8_MAX
37 #define MAX_SERVO_BOXES 3
38 */
39 // TODO: revise usage of MAX_SUPPORTED_MOTORS to see if it is wrong to define it to 12
40 /*
41 #if USE_QUAD_MIXER_ONLY == 1
42 #define MAX_SUPPORTED_MOTORS 4
43 
44 #elif defined(TARGET_MOTOR_COUNT)
45 #define MAX_SUPPORTED_MOTORS TARGET_MOTOR_COUNT
46 
47 #else
48 #define MAX_SUPPORTED_MOTORS 12
49 #endif
50 */
51 
52 #define YAW_JUMP_PREVENTION_LIMIT_LOW 80
53 #define YAW_JUMP_PREVENTION_LIMIT_HIGH 500
54 
55 struct mixer {
57 
58  // TODO: gimbal stuff should be above mixer code not part of it
59  // move when we have refactored gimbal code
60  //int16_t gimbal_angles[3];
61 
62  uint8_t flags;
63 
65  uint8_t motorCount;
66  uint8_t servoCount;
67  uint8_t ruleCount;
68 
70 
72  uint8_t mode;
73 
75 
78 
80 
81  // TODO: mixer should not need so many configs. Need to factor out control logic out of the mixer!
82  const struct config *config;
83 
84  const struct system_calls_pwm *pwm;
85 };
86 
88 void mixer_init(struct mixer *self, const struct config *config, const struct system_calls_pwm *pwm);
89 
91 void mixer_input_command(struct mixer *self, mixer_input_t i, int16_t value);
92 
94 void mixer_update(struct mixer *self);
95 
97 void mixer_enable_armed(struct mixer *self, bool on);
98 
100 bool mixer_motor_limit_reached(struct mixer *self);
101 
103 void mixer_set_throttle_range(struct mixer *self, int16_t mid, int16_t min, int16_t max);
104 /*
106 uint16_t mixer_get_servo_value(struct mixer *self, uint8_t id);
107 
109 uint16_t mixer_get_motor_value(struct mixer *self, uint8_t id);
110 */
112 uint8_t mixer_get_motor_count(struct mixer *self);
113 
115 uint8_t mixer_get_servo_count(struct mixer *self);
116 
117 // TODO: remove these mixer loading/saving methods once user interface has been changed to the new mixer format
118 
120 //int mixer_save_motor_mixer(struct mixer *self, struct motor_mixer *output);
121 
123 //void mixer_load_motor_mixer(struct mixer *self, const struct motor_mixer *motors);
124 
126 //int mixer_save_servo_mixer(struct mixer *self, struct servo_mixer *output);
127 
129 //void mixer_load_servo_mixer(struct mixer *self, const struct servo_mixer *servos);
130 
132 void mixer_clear_rules(struct mixer *self);
133 
#define MAX_SUPPORTED_SERVOS
Definition: mixer.h:25
biquad_t servoFilterState[MAX_SUPPORTED_SERVOS]
Definition: mixer.h:79
Definition: mixer.h:134
#define MIXER_MAX_RULES
Definition: mixer.h:171
Definition: config.h:81
void mixer_input_command(struct mixer *self, mixer_input_t i, int16_t value)
inputs a command to one of the input channels of the mixer
Definition: mixer.c:750
const struct system_calls_pwm * pwm
Definition: mixer.h:84
int16_t minthrottle
Definition: mixer.h:77
pwm related system calls
Definition: system_calls.h:47
int16_t maxthrottle
Definition: mixer.h:77
int16_t midthrottle
output offset, min and max for motors
Definition: mixer.h:77
void mixer_set_throttle_range(struct mixer *self, int16_t mid, int16_t min, int16_t max)
sets throttle range of the mixer (can be used to set 3d throttle range too)
Definition: mixer.c:764
struct mixer_rule_def active_rules[MIXER_MAX_RULES]
Definition: mixer.h:74
void mixer_enable_armed(struct mixer *self, bool on)
puts mixer into armed state so that outputs are calculated (TODO: this should probably be placed outs...
Definition: mixer.c:883
bool motorLimitReached
Definition: mixer.h:69
uint8_t flags
Definition: mixer.h:62
uint8_t servoCount
Definition: mixer.h:66
const struct config * config
Definition: mixer.h:82
int16_t input[MIXER_INPUT_COUNT]
Definition: mixer.h:56
void mixer_clear_rules(struct mixer *self)
saves motor mixer into cleanflight motor mixer format
Definition: mixer.c:739
void mixer_update(struct mixer *self)
calculates outputs from all mixer inputs and mixing rules
Definition: mixer.c:815
mixer_input_t
Definition: mixer.h:97
void mixer_init(struct mixer *self, const struct config *config, const struct system_calls_pwm *pwm)
initializes a mixer struct
int16_t max
Maximum pwm value that is sent to the servo.
Definition: mixer.h:13
uint8_t ruleCount
Definition: mixer.h:67
uint8_t mixer_get_servo_count(struct mixer *self)
returns total number of servos that are being actively mxier by the mixer as part of current profile ...
Definition: mixer.c:916
int16_t min
Minimum pwm value that is sent to the servo.
Definition: mixer.h:12
uint8_t motorCount
output count in current configuration being used by the mixer
Definition: mixer.h:65
bool mixer_motor_limit_reached(struct mixer *self)
tests if any of the motors have reached their limit (usually maxthrottle)
Definition: mixer.c:906
uint8_t mode
currently used mixer mode
Definition: mixer.h:72
Definition: filter.h:27
mixer rule definition in new format
Definition: mixer.h:165
Definition: mixer.h:55
uint8_t mixer_get_motor_count(struct mixer *self)
returns total number of motors that are being actively mixed by the mixer as part of current profile ...
Definition: mixer.c:911