dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
VTXConfig.c
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  *
29  * Additional note on redistribution: The copyright and license notices above
30  * must be maintained in each individual source file that is a derivative work
31  * of this source file; otherwise redistribution is prohibited.
32  */
33 
34 #include "openpilot.h"
35 #include <pios_hal.h>
36 #include <pios_thread.h>
37 
38 #include "tbs_smartaudio.h"
39 
40 #include "vtxsettings.h"
41 #include "vtxinfo.h"
42 
43 // ****************
44 // Private functions
45 
46 static void vtxConfigTask(void *parameters);
47 //static void updateSettings();
48 
49 // ****************
50 // Private types
51 enum VTXTYPE {
54 };
55 
56 enum STATE {
59 };
60 
61 // ****************
62 // Private constants
63 #define STACK_SIZE_BYTES 560
64 #define TASK_PRIORITY PIOS_THREAD_PRIO_LOW
65 
66 #define MAX_FAILS 5
67 #define TBS_MIN_BAUD 4600
68 #define TBS_MAX_BAUD 5100
69 
70 
71 
72 static const uint16_t BAND_5G8_A_FREQS[VTXSETTINGS_BAND_5G8_A_FREQUENCY_MAXOPTVAL + 1] = {
73  [VTXSETTINGS_BAND_5G8_A_FREQUENCY_CH15865] = 5865,
74  [VTXSETTINGS_BAND_5G8_A_FREQUENCY_CH25845] = 5845,
75  [VTXSETTINGS_BAND_5G8_A_FREQUENCY_CH35825] = 5825,
76  [VTXSETTINGS_BAND_5G8_A_FREQUENCY_CH45805] = 5805,
77  [VTXSETTINGS_BAND_5G8_A_FREQUENCY_CH55785] = 5785,
78  [VTXSETTINGS_BAND_5G8_A_FREQUENCY_CH65765] = 5765,
79  [VTXSETTINGS_BAND_5G8_A_FREQUENCY_CH75745] = 5745,
80  [VTXSETTINGS_BAND_5G8_A_FREQUENCY_CH85725] = 5725
81 };
82 
83 static const uint16_t BAND_5G8_B_FREQS[VTXSETTINGS_BAND_5G8_B_FREQUENCY_MAXOPTVAL + 1] = {
84  [VTXSETTINGS_BAND_5G8_B_FREQUENCY_CH15733] = 5733,
85  [VTXSETTINGS_BAND_5G8_B_FREQUENCY_CH25752] = 5752,
86  [VTXSETTINGS_BAND_5G8_B_FREQUENCY_CH35771] = 5771,
87  [VTXSETTINGS_BAND_5G8_B_FREQUENCY_CH45790] = 5790,
88  [VTXSETTINGS_BAND_5G8_B_FREQUENCY_CH55809] = 5809,
89  [VTXSETTINGS_BAND_5G8_B_FREQUENCY_CH65828] = 5828,
90  [VTXSETTINGS_BAND_5G8_B_FREQUENCY_CH75847] = 5847,
91  [VTXSETTINGS_BAND_5G8_B_FREQUENCY_CH85866] = 5866
92 };
93 
94 static const uint16_t BAND_5G8_E_FREQS[VTXSETTINGS_BAND_5G8_E_FREQUENCY_MAXOPTVAL + 1] = {
95  [VTXSETTINGS_BAND_5G8_E_FREQUENCY_CH15705] = 5705,
96  [VTXSETTINGS_BAND_5G8_E_FREQUENCY_CH25685] = 5685,
97  [VTXSETTINGS_BAND_5G8_E_FREQUENCY_CH35665] = 5665,
98  [VTXSETTINGS_BAND_5G8_E_FREQUENCY_CH45645] = 5645,
99  [VTXSETTINGS_BAND_5G8_E_FREQUENCY_CH55885] = 5885,
100  [VTXSETTINGS_BAND_5G8_E_FREQUENCY_CH65905] = 5905,
101  [VTXSETTINGS_BAND_5G8_E_FREQUENCY_CH75925] = 5925,
102  [VTXSETTINGS_BAND_5G8_E_FREQUENCY_CH85945] = 5945
103 };
104 
105 static const uint16_t AIRWAVE_FREQS[VTXSETTINGS_AIRWAVE_FREQUENCY_MAXOPTVAL + 1] = {
106  [VTXSETTINGS_AIRWAVE_FREQUENCY_CH15740] = 5740,
107  [VTXSETTINGS_AIRWAVE_FREQUENCY_CH25760] = 5760,
108  [VTXSETTINGS_AIRWAVE_FREQUENCY_CH35780] = 5780,
109  [VTXSETTINGS_AIRWAVE_FREQUENCY_CH45800] = 5800,
110  [VTXSETTINGS_AIRWAVE_FREQUENCY_CH55820] = 5820,
111  [VTXSETTINGS_AIRWAVE_FREQUENCY_CH65840] = 5840,
112  [VTXSETTINGS_AIRWAVE_FREQUENCY_CH75860] = 5860,
113  [VTXSETTINGS_AIRWAVE_FREQUENCY_CH85880] = 5880
114 };
115 
116 static const uint16_t RACEBAND_FREQS[VTXSETTINGS_RACEBAND_FREQUENCY_MAXOPTVAL + 1] = {
117  [VTXSETTINGS_RACEBAND_FREQUENCY_CH15658] = 5658,
118  [VTXSETTINGS_RACEBAND_FREQUENCY_CH25695] = 5695,
119  [VTXSETTINGS_RACEBAND_FREQUENCY_CH35732] = 5732,
120  [VTXSETTINGS_RACEBAND_FREQUENCY_CH45769] = 5769,
121  [VTXSETTINGS_RACEBAND_FREQUENCY_CH55806] = 5806,
122  [VTXSETTINGS_RACEBAND_FREQUENCY_CH65843] = 5843,
123  [VTXSETTINGS_RACEBAND_FREQUENCY_CH75880] = 5880,
124  [VTXSETTINGS_RACEBAND_FREQUENCY_CH85917] = 5917
125 };
126 
127 static const uint16_t VTX_POWER[VTXSETTINGS_POWER_GLOBAL_MAXOPTVAL + 1] = {
128  [VTXSETTINGS_POWER_25] = 25,
129  [VTXSETTINGS_POWER_200] = 200,
130  [VTXSETTINGS_POWER_500] = 500,
131  [VTXSETTINGS_POWER_800] = 800,
132 };
133 
134 // ****************
135 // Private variables
136 
137 static uint32_t vtxConfigPort;
138 static bool module_enabled = false;
139 static volatile bool settings_updated = false;
140 static struct pios_thread *vtxConfigTaskHandle;
141 
142 static enum VTXTYPE vtx_type = VTX_NONE;
143 
144 #if defined(PIOS_INCLUDE_TBSVTXCONFIG)
145 extern uintptr_t pios_com_tbsvtxconfig_id;
146 #endif
147 
148 
149 // ****************
156 int32_t VTXConfigStart(void)
157 {
158  if (module_enabled) {
159  if (vtxConfigPort) {
160  // Start task
162  TaskMonitorAdd(TASKINFO_RUNNING_VTXCONFIG, vtxConfigTaskHandle);
163  return 0;
164  }
165  }
166  return -1;
167 }
168 
174 int32_t VTXConfigInitialize(void)
175 {
176 #if defined(PIOS_INCLUDE_TBSVTXCONFIG)
180  }
181 #endif /* PIOS_INCLUDE_TBSVTXCONFIG */
182 
183  if (vtx_type != VTX_NONE) {
185  }
186  else {
187  module_enabled = false;
188  }
189 
190  VTXSettingsInitialize();
191 
192  if (module_enabled) {
193  VTXInfoInitialize();
194  VTXSettingsConnectCallbackCtx(UAVObjCbSetFlag, &settings_updated);
195  }
196 
197  return 0;
198 }
199 
201 
202 
207 static void vtxConfigTask(void *parameters)
208 {
209  VTXInfoData info;
210  VTXSettingsData settings;
211 
212  uint16_t baud_rate = TBS_MIN_BAUD;
213  uint8_t n_fails = 0;
214  enum STATE state = DISCONNECTED;
215  enum STATE state_prev = DISCONNECTED;
216  bool update_vtx = false;
217 
218  VTXSettingsGet(&settings);
219 
220  // Wait for power to stabilize before talking to external devices
221  PIOS_Thread_Sleep(1000);
222 
223  // Loop forever
224  while (1) {
225  // Try to connect faster when disconnected
226  if (state == CONNECTED) {
227  PIOS_Thread_Sleep(1000);
228  }
229  else {
230  PIOS_Thread_Sleep(500);
231  }
232 
233  if (settings_updated) {
234  VTXSettingsGet(&settings);
235  settings_updated = false;
236  update_vtx = true;
237  }
238 
239  if (tbsvtx_get_state(vtxConfigPort, &info) >= 0) {
240  state = CONNECTED;
241  n_fails = 0;
242  info.State = VTXINFO_STATE_CONNECTED;
243  VTXInfoSet(&info);
244  if (state_prev == DISCONNECTED) {
245  update_vtx = true;
246  }
247  }
248  else {
249  n_fails += 1;
250  }
251 
252  if (n_fails > MAX_FAILS) {
253  state = DISCONNECTED;
254  memset((void*)&info, 0, sizeof(info));
255  info.State = VTXINFO_STATE_DISCONNECTED;
256  VTXInfoSet(&info);
257  }
258 
259  if (state == CONNECTED) {
260  PIOS_Thread_Sleep(500);
261  //update_vtx = true;
262  if ((settings.Mode == VTXSETTINGS_MODE_ACTIVE) && update_vtx) {
263 
264  // Get the frequency
265  uint16_t frequency;
266  switch (settings.Band) {
267  case VTXSETTINGS_BAND_BAND5G8A:
268  frequency = BAND_5G8_A_FREQS[settings.Band_5G8_A_Frequency];
269  break;
270  case VTXSETTINGS_BAND_BAND5G8B:
271  frequency = BAND_5G8_B_FREQS[settings.Band_5G8_B_Frequency];
272  break;
273  case VTXSETTINGS_BAND_BAND5G8E:
274  frequency = BAND_5G8_E_FREQS[settings.Band_5G8_E_Frequency];
275  break;
276  case VTXSETTINGS_BAND_AIRWAVE:
277  frequency = AIRWAVE_FREQS[settings.Airwave_Frequency];
278  break;
279  case VTXSETTINGS_BAND_RACEBAND:
280  frequency = RACEBAND_FREQS[settings.Raceband_Frequency];
281  break;
282  default:
283  update_vtx = false;
284  }
285  if (update_vtx) {
286  update_vtx = false;
287  if (frequency != info.Frequency) {
288  if (tbsvtx_set_freq(vtxConfigPort, frequency) < 0) {
289  // something went wrong, we need to re-try again
290  update_vtx = true;
291  }
292  PIOS_Thread_Sleep(500);
293  }
294 
295  // Don't increase the power if the VTX reports 0mW. This is a special mode
296  // e.g. "pit mode" for TBS and the user requested to transmit at low power.
297  if ((info.Power != 0) && (VTX_POWER[settings.Power] != info.Power)) {
298  if (tbsvtx_set_power(vtxConfigPort, VTX_POWER[settings.Power]) < 0) {
299  // somethign went wrong, we need to re-try again
300  update_vtx = true;
301  }
302  }
303  }
304  }
305  } else {
306  // Cycle through different baud rates. The spec says 4.8kbps, but it can deviate
307  if (baud_rate < TBS_MAX_BAUD) {
308  baud_rate += 50;
309  }
310  else {
311  baud_rate = TBS_MIN_BAUD;
312  }
314  }
315  state_prev = state;
316  }
317 }
static const uint16_t BAND_5G8_A_FREQS[VTXSETTINGS_BAND_5G8_A_FREQUENCY_MAXOPTVAL+1]
Definition: VTXConfig.c:72
static uint32_t vtxConfigPort
Definition: VTXConfig.c:137
static struct pios_thread * vtxConfigTaskHandle
Definition: VTXConfig.c:140
int32_t VTXConfigInitialize(void)
Definition: VTXConfig.c:174
static void vtxConfigTask(void *parameters)
Definition: VTXConfig.c:207
#define TBS_MAX_BAUD
Definition: VTXConfig.c:68
bool PIOS_Modules_IsEnabled(enum pios_modules module)
Definition: pios_modules.c:41
#define TASK_PRIORITY
Definition: VTXConfig.c:64
uintptr_t pios_com_tbsvtxconfig_id
void UAVObjCbSetFlag(const UAVObjEvent *objEv, void *ctx, void *obj, int len)
STATE
Definition: VTXConfig.c:56
static const uint16_t RACEBAND_FREQS[VTXSETTINGS_RACEBAND_FREQUENCY_MAXOPTVAL+1]
Definition: VTXConfig.c:116
int32_t tbsvtx_set_freq(uintptr_t usart_id, uint16_t frequency)
#define MAX_FAILS
Definition: VTXConfig.c:66
static const uint16_t AIRWAVE_FREQS[VTXSETTINGS_AIRWAVE_FREQUENCY_MAXOPTVAL+1]
Definition: VTXConfig.c:105
static const uint16_t VTX_POWER[VTXSETTINGS_POWER_GLOBAL_MAXOPTVAL+1]
Definition: VTXConfig.c:127
static bool module_enabled
Definition: VTXConfig.c:138
#define STACK_SIZE_BYTES
Definition: VTXConfig.c:63
static enum VTXTYPE vtx_type
Definition: VTXConfig.c:142
static volatile FlightStatsSettingsData settings
int32_t tbsvtx_set_power(uintptr_t usart_id, uint16_t power)
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
Definition: pios_thread.c:89
This module configures the video transmitter.
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
Definition: taskmonitor.c:67
int32_t VTXConfigStart(void)
Definition: VTXConfig.c:156
void PIOS_Thread_Sleep(uint32_t time_ms)
Definition: pios_thread.c:229
VTXTYPE
Definition: VTXConfig.c:51
Includes PiOS and core architecture components.
MODULE_INITCALL(VTXConfigInitialize, VTXConfigStart)
static const uint16_t BAND_5G8_E_FREQS[VTXSETTINGS_BAND_5G8_E_FREQUENCY_MAXOPTVAL+1]
Definition: VTXConfig.c:94
static volatile bool settings_updated
Definition: VTXConfig.c:139
static const uint16_t BAND_5G8_B_FREQS[VTXSETTINGS_BAND_5G8_B_FREQUENCY_MAXOPTVAL+1]
Definition: VTXConfig.c:83
int32_t tbsvtx_get_state(uintptr_t usart_id, VTXInfoData *info)
#define TBS_MIN_BAUD
Definition: VTXConfig.c:67
int32_t PIOS_COM_ChangeBaud(uintptr_t com_id, uint32_t baud)
enum arena_state state