NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rx.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 <stdbool.h>
21 #include "../config/rc_controls.h"
22 #include "target.h"
23 #include "system_calls.h"
24 
25 #include "../config/rx.h"
26 
27 #ifndef DEFAULT_RX_FEATURE
28 #define DEFAULT_RX_FEATURE FEATURE_RX_PARALLEL_PWM
29 #endif
30 
31 #define STICK_CHANNEL_COUNT 4
32 
33 typedef enum {
38 
39 typedef enum {
49 } SerialRXType;
50 
51 #define SERIALRX_PROVIDER_COUNT (SERIALRX_PROVIDER_MAX + 1)
52 
53 #define RX_MAX_PPM_CHANNELS 12
54 #define MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT 8
55 
56 #define MAX_AUX_CHANNEL_COUNT (RX_MAX_SUPPORTED_RC_CHANNELS - RX_NON_AUX_CHANNEL_COUNT)
57 
58 #if MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT > RX_MAX_PPM_CHANNELS
59 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT
60 #else
61 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT RX_MAX_PPM_CHANNELS
62 #endif
63 #define RX_FAILSAFE_MODE_COUNT 3
64 
65 typedef enum {
69 
70 #define RX_FAILSAFE_TYPE_COUNT 2
71 typedef struct rxRuntimeConfig_s {
72  uint8_t channelCount; // number of rc channels as reported by current input driver
74 
75 #define RSSI_ADC_SAMPLE_COUNT 16
76 #define RX_CHANNEL_TIMEOUT 300
77 #define PPM_AND_PWM_SAMPLE_COUNT 3
78 
79 typedef uint16_t (*rcReadRawDataPtr)(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t chan); // used by receiver driver to return channel data
80 
81 typedef enum {
94 } rx_type_t;
95 
96 struct config;
97 struct rx {
99 
100  uint16_t rssi;
101 
104 
107  uint8_t skipRxSamples;
108 
112  uint8_t rcSampleIndex;
113 
118  uint32_t active_channels;
121  uint32_t used_channels;
122 
124  uint16_t (*rcReadRawFunc)(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t chan); // used by receiver driver to return channel data
125 
127  uint16_t rxRefreshRate;
128 
129  #if defined(USE_ADC)
130  uint8_t adcRssiSamples[RSSI_ADC_SAMPLE_COUNT];
131  uint8_t adcRssiSampleIndex;
132  uint32_t rssiUpdateAt;
133  #endif
134 
136 
137  const struct system_calls *system;
138  const struct config *config;
139 };
140 
141 void rx_update(struct rx *self);
142 
144 bool rx_has_signal(struct rx *self);
145 
147 bool rx_is_healthy(struct rx *self);
148 
150 bool rx_flight_channels_valid(struct rx *self);
151 
152 void rx_init(struct rx *self, const struct system_calls *system, const struct config *config);
153 
154 // TODO: make it possible to switch type at runtime (involves sorting out some init things)
155 // currently it is only possible to set the type once
157 void rx_set_type(struct rx *self, rx_type_t type);
158 
159 void rx_update_rssi(struct rx *self);
160 
161 void rx_suspend_signal(struct rx *self);
162 void rx_resume_signal(struct rx *self);
163 
164 uint16_t rx_get_refresh_rate(struct rx *self);
165 
166 uint16_t rx_get_rssi(struct rx *self);
167 
168 char rx_get_channel_letter(uint8_t ch);
169 uint8_t rx_get_channel_count(struct rx *self);
170 int16_t rx_get_channel(struct rx *self, uint8_t chan);
171 
173 //void rx_set_channel(struct rx *self, uint8_t chan, int16_t value);
174 
bool rx_has_signal(struct rx *self)
RX has signal if at least one channel is healthy.
Definition: rx.c:280
Definition: rx.h:45
SerialRXType
Definition: rx.h:39
uint8_t type
Definition: fat_standard.h:67
Definition: rx.h:92
Definition: rx.h:47
Definition: config.h:81
#define RSSI_ADC_SAMPLE_COUNT
Definition: rx.h:75
uint16_t rx_get_refresh_rate(struct rx *self)
Definition: rx.c:457
rx_type_t
Definition: rx.h:81
Definition: rx.h:48
Definition: rx.h:83
uint16_t(* rcReadRawDataPtr)(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t chan)
Definition: rx.h:79
Definition: rx.h:36
uint16_t rcSamples[MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT][PPM_AND_PWM_SAMPLE_COUNT]
samples collected from the receiver.
Definition: rx.h:110
Definition: rx.h:93
#define PPM_AND_PWM_SAMPLE_COUNT
Definition: rx.h:77
bool rxSamplesCollected
Definition: rx.h:111
Definition: rx.h:46
Definition: rx.h:84
int16_t rx_get_channel(struct rx *self, uint8_t chan)
Get channel value. Returns interval [rx_min_usec;rx_max_usec].
Definition: rx.c:470
uint16_t rxRefreshRate
currently set refresh rate in microseconds
Definition: rx.h:127
int16_t rcData[RX_MAX_SUPPORTED_RC_CHANNELS]
calculated output channels
Definition: rx.h:117
sys_micros_t suspendRxSignalUntil
suspends rx signal until specified deadline
Definition: rx.h:106
Definition: rx.h:71
Definition: rx.h:43
bool rx_is_healthy(struct rx *self)
RX is healthy if all channels are healthy.
Definition: rx.c:284
struct system_config system
Definition: config.h:99
Definition: rx.h:91
Definition: rx.h:97
Definition: rx.h:88
uint32_t active_channels
holds currently active channels (until they time out)
Definition: rx.h:119
Definition: rx.h:40
uint16_t rssi
rssi in range: [0;1023]
Definition: rx.h:100
sys_micros_t rxUpdateAt
time for next update (we only update as often as is necessary depending on what type of receiver we h...
Definition: rx.h:103
void rx_update_rssi(struct rx *self)
Definition: rx.c:446
uint8_t rx_get_channel_count(struct rx *self)
Definition: rx.c:465
uint8_t channelCount
Definition: rx.h:72
const struct config * config
Definition: rx.h:138
int32_t sys_micros_t
Definition: system_calls.h:7
rx_type_t rx_type
Definition: rx.h:98
Definition: rx.h:42
Definition: rx.h:86
uint32_t used_channels
holds mask of channels that have been active since receiver was connected
Definition: rx.h:121
Definition: rx.h:82
uint16_t(* rcReadRawFunc)(rxRuntimeConfig_t *rxRuntimeConfig, uint8_t chan)
function used for reading raw values. Returns 0 on failure to read receiver.
Definition: rx.h:124
char rx_get_channel_letter(uint8_t ch)
Definition: rx.h:41
rxRuntimeConfig_t rxRuntimeConfig
Definition: flight_tilt_unittest.cc:57
Definition: rx.h:67
bool rx_flight_channels_valid(struct rx *self)
Returns true if channels 0-3 are active.
Definition: rx.c:288
#define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT
Definition: rx.h:61
Definition: rx.h:34
void rx_resume_signal(struct rx *self)
Definition: rx.c:299
struct rxRuntimeConfig_s rxRuntimeConfig_t
void rx_set_type(struct rx *self, rx_type_t type)
changes the receiver type (determines what system calls the rx system will use to get receiver data) ...
Definition: rx.c:185
rxFailsafeChannelType_e
Definition: rx.h:65
Definition: rx.h:90
rxRuntimeConfig_t rxRuntimeConfig
Definition: rx.h:135
#define RX_MAX_SUPPORTED_RC_CHANNELS
maximum supported channels when calling rx_get_channel
Definition: rx.h:27
serialrxFrameState_t
Definition: rx.h:33
Definition: system_calls.h:200
void rx_suspend_signal(struct rx *self)
Definition: rx.c:292
const struct system_calls * system
Definition: rx.h:137
Definition: rx.h:85
sys_micros_t rcInvalidPulsPeriod[RX_MAX_SUPPORTED_RC_CHANNELS]
period under which we have not had valid rx signal
Definition: rx.h:115
Definition: rx.h:66
Definition: rx.h:87
Definition: rx.h:44
uint8_t rcSampleIndex
Definition: rx.h:112
Definition: rx.h:89
uint8_t skipRxSamples
Definition: rx.h:107
Definition: rx.h:35
uint16_t rx_get_rssi(struct rx *self)
Definition: rx.c:461
void rx_update(struct rx *self)
Definition: rx.c:391
void rx_init(struct rx *self, const struct system_calls *system, const struct config *config)
Definition: rx.c:143