altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / kernel / ao_telemetry.c
1 /*
2  * Copyright © 2011 Keth Packard <keithp@keithp.com>
3  *
4  * This program 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 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include "ao_log.h"
21 #include "ao_product.h"
22
23 static uint16_t ao_telemetry_interval;
24
25 #if HAS_RADIO_RATE
26 static uint16_t ao_telemetry_desired_interval;
27 #endif
28
29 /* TeleMetrum v1.0 just doesn't have enough space to
30  * manage the more complicated telemetry scheduling, so
31  * it loses the ability to disable telem/rdf separately
32  */
33
34 #if defined(TELEMETRUM_V_1_0)
35 #define SIMPLIFY
36 #endif
37
38 #ifdef SIMPLIFY
39 #define ao_telemetry_time time
40 #define RDF_SPACE       
41 #else
42 #define RDF_SPACE       
43 static AO_TICK_TYPE     ao_telemetry_time;
44 #endif
45
46 #if HAS_RDF
47 static RDF_SPACE uint8_t ao_rdf = 0;
48 static RDF_SPACE AO_TICK_TYPE   ao_rdf_time;
49 #endif
50
51 #if HAS_APRS
52 static AO_TICK_TYPE ao_aprs_time;
53
54 #include <ao_aprs.h>
55 #endif
56
57 #if defined(TELEMEGA)
58 #define AO_SEND_MEGA    1
59 #endif
60
61 #if defined (TELEMETRUM_V_2_0) || defined (TELEMETRUM_V_3_0) || defined (TELEMETRUM_V_4_0)
62 #define AO_SEND_METRUM  1
63 #endif
64
65 #if defined(TELEMETRUM_V_0_1) || defined(TELEMETRUM_V_0_2) || defined(TELEMETRUM_V_1_0) || defined(TELEMETRUM_V_1_1) || defined(TELEBALLOON_V_1_1) || defined(TELEMETRUM_V_1_2)
66 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMETRUM
67 #endif
68
69 #if defined(TELEMINI_V_1_0)
70 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMINI
71 #endif
72
73 #if defined(TELENANO_V_0_1)
74 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELENANO
75 #endif
76
77 static union ao_telemetry_all   telemetry;
78
79 static void
80 ao_telemetry_send(void)
81 {
82         ao_radio_send(&telemetry, sizeof (telemetry));
83         ao_delay(1);
84 }
85
86 #if defined AO_TELEMETRY_SENSOR
87 /* Send sensor packet */
88 static void
89 ao_send_sensor(void)
90 {
91                 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
92
93         telemetry.generic.tick = packet->tick;
94         telemetry.generic.type = AO_TELEMETRY_SENSOR;
95
96         telemetry.sensor.state = ao_flight_state;
97 #if HAS_ACCEL
98         telemetry.sensor.accel = packet->adc.accel;
99 #else
100         telemetry.sensor.accel = 0;
101 #endif
102         telemetry.sensor.pres = ao_data_pres(packet);
103         telemetry.sensor.temp = packet->adc.temp;
104         telemetry.sensor.v_batt = packet->adc.v_batt;
105 #if HAS_IGNITE
106         telemetry.sensor.sense_d = packet->adc.sense_d;
107         telemetry.sensor.sense_m = packet->adc.sense_m;
108 #else
109         telemetry.sensor.sense_d = 0;
110         telemetry.sensor.sense_m = 0;
111 #endif
112
113         telemetry.sensor.acceleration = ao_accel;
114         telemetry.sensor.speed = ao_speed;
115         telemetry.sensor.height = ao_height;
116
117         telemetry.sensor.ground_pres = ao_ground_pres;
118 #if HAS_ACCEL
119         telemetry.sensor.ground_accel = ao_ground_accel;
120         telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
121         telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
122 #else
123         telemetry.sensor.ground_accel = 0;
124         telemetry.sensor.accel_plus_g = 0;
125         telemetry.sensor.accel_minus_g = 0;
126 #endif
127
128         ao_telemetry_send();
129 }
130 #endif
131
132
133 #ifdef AO_SEND_MEGA
134
135 /* Send mega sensor packet */
136 static void
137 ao_send_mega_sensor(void)
138 {
139         struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
140
141         telemetry.generic.tick = (uint16_t) packet->tick;
142 #if AO_LOG_NORMALIZED
143 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA_5
144         telemetry.generic.type = AO_TELEMETRY_MEGA_NORM_MPU6000_MMC5983;
145 #elif AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA_6
146         telemetry.generic.type = AO_TELEMETRY_MEGA_NORM_BMI088_MMC5983;
147 #else
148 #error unknown normalized log type
149 #endif
150
151 #if HAS_GYRO
152         telemetry.mega_norm.orient = (uint8_t) ao_sample_orient;
153 #endif
154         telemetry.mega_norm.accel = ao_data_accel(packet);
155         telemetry.mega_norm.pres = ao_data_pres(packet);
156         telemetry.mega_norm.temp = ao_data_temp(packet);
157
158 #ifdef ao_data_along
159         telemetry.mega_norm.accel_along = ao_data_along(packet);
160         telemetry.mega_norm.accel_across = ao_data_across(packet);
161         telemetry.mega_norm.accel_through = ao_data_through(packet);
162
163         telemetry.mega_norm.gyro_roll = ao_data_roll(packet);
164         telemetry.mega_norm.gyro_pitch = ao_data_pitch(packet);
165         telemetry.mega_norm.gyro_yaw = ao_data_yaw(packet);
166 #endif
167
168 #ifdef ao_data_mag_along
169         telemetry.mega_norm.mag_along = ao_data_mag_along(packet);
170         telemetry.mega_norm.mag_across = ao_data_mag_across(packet);
171         telemetry.mega_norm.mag_through = ao_data_mag_through(packet);
172 #endif
173
174 #else
175
176 #if HAS_BMX160
177         telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR_BMX160;
178 #else
179 #if HAS_MPU6000 || HAS_MPU9250
180         telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR_MPU;
181 #else
182 #error unknown IMU
183 #endif
184 #endif
185
186 #if HAS_GYRO
187         telemetry.mega_sensor.orient = (uint8_t) ao_sample_orient;
188 #endif
189         telemetry.mega_sensor.accel = ao_data_accel(packet);
190         telemetry.mega_sensor.pres = ao_data_pres(packet);
191         telemetry.mega_sensor.temp = ao_data_temp(packet);
192
193 #if HAS_MPU6000
194         telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
195         telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
196         telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
197
198         telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
199         telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
200         telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
201 #endif
202
203 #if HAS_HMC5883
204         telemetry.mega_sensor.mag_x = packet->hmc5883.x;
205         telemetry.mega_sensor.mag_z = packet->hmc5883.z;
206         telemetry.mega_sensor.mag_y = packet->hmc5883.y;
207 #endif
208
209 #if HAS_MPU9250
210         telemetry.mega_sensor.accel_x = packet->mpu9250.accel_x;
211         telemetry.mega_sensor.accel_y = packet->mpu9250.accel_y;
212         telemetry.mega_sensor.accel_z = packet->mpu9250.accel_z;
213
214         telemetry.mega_sensor.gyro_x = packet->mpu9250.gyro_x;
215         telemetry.mega_sensor.gyro_y = packet->mpu9250.gyro_y;
216         telemetry.mega_sensor.gyro_z = packet->mpu9250.gyro_z;
217
218         telemetry.mega_sensor.mag_x = packet->mpu9250.mag_x;
219         telemetry.mega_sensor.mag_z = packet->mpu9250.mag_z;
220         telemetry.mega_sensor.mag_y = packet->mpu9250.mag_y;
221 #endif
222
223 #if HAS_BMX160
224         telemetry.mega_sensor.accel_x = packet->bmx160.acc_x;
225         telemetry.mega_sensor.accel_y = packet->bmx160.acc_y;
226         telemetry.mega_sensor.accel_z = packet->bmx160.acc_z;
227
228         telemetry.mega_sensor.gyro_x = packet->bmx160.gyr_x;
229         telemetry.mega_sensor.gyro_y = packet->bmx160.gyr_y;
230         telemetry.mega_sensor.gyro_z = packet->bmx160.gyr_z;
231
232         telemetry.mega_sensor.mag_x = packet->bmx160.mag_x;
233         telemetry.mega_sensor.mag_z = packet->bmx160.mag_z;
234         telemetry.mega_sensor.mag_y = packet->bmx160.mag_y;
235 #endif
236 #endif
237         ao_telemetry_send();
238 }
239
240 static int16_t ao_telemetry_mega_data_max;
241 static int16_t ao_telemetry_mega_data_cur;
242
243 /* Send mega data packet */
244 static void
245 ao_send_mega_data(void)
246 {
247         if (--ao_telemetry_mega_data_cur <= 0) {
248                 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
249                 uint8_t i;
250
251                 telemetry.generic.tick = (uint16_t) packet->tick;
252                 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
253
254                 telemetry.mega_data.state = ao_flight_state;
255                 telemetry.mega_data.v_batt = packet->adc.v_batt;
256                 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
257
258                 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
259                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
260                         telemetry.mega_data.sense[i] = (int8_t) (packet->adc.sense[i] >> 4);
261
262                 telemetry.mega_data.ground_pres = ao_ground_pres;
263                 telemetry.mega_data.ground_accel = ao_ground_accel;
264                 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
265                 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
266
267                 telemetry.mega_data.acceleration = (int16_t) ao_accel;
268                 telemetry.mega_data.speed = (int16_t) ao_speed;
269                 telemetry.mega_data.height = (int16_t) ao_height;
270
271                 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
272                 ao_telemetry_send();
273         }
274 }
275 #endif /* AO_SEND_MEGA */
276
277 #ifdef AO_SEND_METRUM
278 /* Send telemetrum sensor packet */
279 static void
280 ao_send_metrum_sensor(void)
281 {
282         struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
283
284         telemetry.generic.tick = (uint16_t) packet->tick;
285         telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
286
287         telemetry.metrum_sensor.state = ao_flight_state;
288 #if HAS_ACCEL
289         telemetry.metrum_sensor.accel = ao_data_accel(packet);
290 #endif
291         telemetry.metrum_sensor.pres = ao_data_pres(packet);
292         telemetry.metrum_sensor.temp = ao_data_temp(packet);
293
294         telemetry.metrum_sensor.acceleration = (int16_t) ao_accel;
295         telemetry.metrum_sensor.speed = (int16_t) ao_speed;
296         telemetry.metrum_sensor.height = (int16_t) ao_height;
297
298         telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
299         telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
300         telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
301
302         ao_telemetry_send();
303 }
304
305 static int16_t ao_telemetry_metrum_data_max;
306 static int16_t ao_telemetry_metrum_data_cur;
307
308 /* Send telemetrum data packet */
309 static void
310 ao_send_metrum_data(void)
311 {
312         if (--ao_telemetry_metrum_data_cur <= 0) {
313                 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
314
315                 telemetry.generic.tick = (uint16_t) packet->tick;
316                 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
317
318                 telemetry.metrum_data.ground_pres = ao_ground_pres;
319 #if HAS_ACCEL
320                 telemetry.metrum_data.ground_accel = ao_ground_accel;
321                 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
322                 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
323 #else
324                 telemetry.metrum_data.ground_accel = 1;
325                 telemetry.metrum_data.accel_plus_g = 0;
326                 telemetry.metrum_data.accel_minus_g = 2;
327 #endif
328
329                 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
330                 ao_telemetry_send();
331         }
332 }
333 #endif /* AO_SEND_METRUM */
334
335 #ifdef AO_SEND_MINI
336
337 static void
338 ao_send_mini(void)
339 {
340         struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
341
342         telemetry.generic.tick = (uint16_t) packet->tick;
343         telemetry.generic.type = AO_SEND_MINI;
344
345         telemetry.mini.state = ao_flight_state;
346
347         telemetry.mini.v_batt = packet->adc.v_batt;
348         telemetry.mini.sense_a = packet->adc.sense_a;
349         telemetry.mini.sense_m = packet->adc.sense_m;
350
351         telemetry.mini.pres = ao_data_pres(packet);
352         telemetry.mini.temp = ao_data_temp(packet);
353
354         telemetry.mini.acceleration = (int16_t) ao_accel;
355         telemetry.mini.speed = (int16_t) ao_speed;
356         telemetry.mini.height = (int16_t) ao_height;
357
358         telemetry.mini.ground_pres = ao_ground_pres;
359
360         ao_telemetry_send();
361 }
362
363 #endif /* AO_SEND_MINI */
364
365 static int16_t ao_telemetry_config_max;
366 static int16_t ao_telemetry_config_cur;
367 static uint16_t ao_telemetry_flight_number;
368
369 #ifndef ao_telemetry_battery_convert
370 #define ao_telemetry_battery_convert(a) (a)
371 #endif
372
373 static void
374 ao_send_configuration(void)
375 {
376         if (--ao_telemetry_config_cur <= 0)
377         {
378                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
379                 telemetry.configuration.device = AO_idProduct_NUMBER;
380                 telemetry.configuration.flight = ao_telemetry_flight_number;
381                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
382                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
383 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
384                 /* TeleGPS gets battery voltage instead of apogee delay */
385                 telemetry.configuration.apogee_delay = (uint16_t) ao_telemetry_battery_convert(ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt);
386 #else
387                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
388                 telemetry.configuration.main_deploy = ao_config.main_deploy;
389 #endif
390
391                 telemetry.configuration.flight_log_max = (uint16_t) (ao_config.flight_log_max >> 10);
392                 memcpy (telemetry.configuration.callsign,
393                         ao_config.callsign,
394                         AO_MAX_CALLSIGN);
395                 memcpy (telemetry.configuration.version,
396                         ao_version,
397                         AO_MAX_VERSION);
398                 ao_telemetry_config_cur = ao_telemetry_config_max;
399                 ao_telemetry_send();
400         }
401 }
402
403 #if HAS_GPS
404
405 static int16_t ao_telemetry_gps_max;
406 static int16_t ao_telemetry_loc_cur;
407 static int16_t ao_telemetry_sat_cur;
408
409 static inline void *
410 telemetry_bits(struct ao_telemetry_location *l)
411 {
412         return ((uint8_t *) l) + offsetof(struct ao_telemetry_location, flags);
413 }
414
415 static inline size_t
416 telemetry_size(void)
417 {
418         return sizeof(struct ao_telemetry_location) - offsetof(struct ao_telemetry_location, flags);
419 }
420
421 static void
422 ao_send_location(void)
423 {
424         if (--ao_telemetry_loc_cur <= 0)
425         {
426                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
427                 ao_mutex_get(&ao_gps_mutex);
428                 memcpy(telemetry_bits(&telemetry.location),
429                        telemetry_bits(&ao_gps_data),
430                        telemetry_size());
431                 telemetry.location.tick = (uint16_t) ao_gps_tick;
432                 ao_mutex_put(&ao_gps_mutex);
433                 ao_telemetry_loc_cur = ao_telemetry_gps_max;
434                 ao_telemetry_send();
435         }
436 }
437
438 static void
439 ao_send_satellite(void)
440 {
441         if (--ao_telemetry_sat_cur <= 0)
442         {
443                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
444                 ao_mutex_get(&ao_gps_mutex);
445                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
446                 memcpy(&telemetry.satellite.sats,
447                        &ao_gps_tracking_data.sats,
448                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
449                 ao_mutex_put(&ao_gps_mutex);
450                 ao_telemetry_sat_cur = ao_telemetry_gps_max;
451                 ao_telemetry_send();
452         }
453 }
454 #endif
455
456 #if HAS_COMPANION
457
458 static int16_t ao_telemetry_companion_max;
459 static int16_t ao_telemetry_companion_cur;
460
461 static void
462 ao_send_companion(void)
463 {
464         if (--ao_telemetry_companion_cur <= 0) {
465                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
466                 telemetry.companion.board_id = (uint8_t) ao_companion_setup.board_id;
467                 telemetry.companion.update_period = ao_companion_setup.update_period;
468                 telemetry.companion.channels = ao_companion_setup.channels;
469                 ao_mutex_get(&ao_companion_mutex);
470                 memcpy(&telemetry.companion.companion_data,
471                        ao_companion_data,
472                        ao_companion_setup.channels * 2);
473                 ao_mutex_put(&ao_companion_mutex);
474                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
475                 ao_telemetry_send();
476         }
477 }
478 #endif
479
480 #if HAS_APRS
481 static void
482 ao_set_aprs_time(void)
483 {
484         uint16_t interval = ao_config.aprs_interval;
485
486         if ((ao_gps_data.flags & AO_GPS_DATE_VALID) && interval != 0) {
487                 int second = (ao_gps_data.second / interval + 1) * interval + ao_config.aprs_offset;
488                 int delta;
489                 if (second >= 60) {
490                         second = ao_config.aprs_offset;
491                         delta = second + 60 - ao_gps_data.second;
492                 } else {
493                         delta = second - ao_gps_data.second;
494                 }
495                 if (delta < (interval >> 1))
496                         delta += interval;
497
498                 ao_aprs_time = ao_gps_utc_tick + AO_SEC_TO_TICKS(delta);
499         } else {
500                 ao_aprs_time += AO_SEC_TO_TICKS(ao_config.aprs_interval);
501         }
502 }
503 #endif
504
505 static void
506 ao_telemetry(void)
507 {
508         AO_TICK_TYPE    time;
509         AO_TICK_SIGNED  delay;
510
511         ao_config_get();
512         if (!ao_config.radio_enable)
513                 ao_exit();
514         while (!ao_flight_number)
515                 ao_sleep(&ao_flight_number);
516
517         ao_telemetry_flight_number = ao_flight_number;
518 #if HAS_LOG
519         if (ao_log_full())
520                 ao_telemetry_flight_number = 0;
521 #endif
522         telemetry.generic.serial = ao_serial_number;
523         for (;;) {
524                 while (ao_telemetry_interval == 0)
525                         ao_sleep(&telemetry);
526                 time = ao_time();
527                 ao_telemetry_time = time;
528 #if HAS_RDF
529                 ao_rdf_time = time;
530 #endif
531 #if HAS_APRS
532                 ao_aprs_time = time;
533                 ao_set_aprs_time();
534 #endif
535                 while (ao_telemetry_interval) {
536                         time = ao_time() + AO_SEC_TO_TICKS(100);
537 #ifndef SIMPLIFY
538                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
539 #endif
540                         {
541 #ifndef SIMPLIFY
542                                 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
543 #endif
544                                 {
545                                         ao_telemetry_time = ao_time() + ao_telemetry_interval;
546 # ifdef AO_SEND_MEGA
547                                         ao_send_mega_sensor();
548                                         ao_send_mega_data();
549 # endif
550 # ifdef AO_SEND_METRUM
551                                         ao_send_metrum_sensor();
552                                         ao_send_metrum_data();
553 # endif
554 # ifdef AO_SEND_MINI
555                                         ao_send_mini();
556 # endif
557 # ifdef AO_TELEMETRY_SENSOR
558                                         ao_send_sensor();
559 # endif
560 #if HAS_COMPANION
561                                         if (ao_companion_running)
562                                                 ao_send_companion();
563 #endif
564 #if HAS_GPS
565                                         ao_send_location();
566                                         ao_send_satellite();
567 #endif
568                                         ao_send_configuration();
569                                 }
570 #ifndef SIMPLIFY
571                                 time = ao_telemetry_time;
572 #endif
573                         }
574 #if HAS_RDF
575                         if (ao_rdf
576 #ifndef SIMPLIFY
577                             && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
578 #endif
579                                 )
580                         {
581                                 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
582 #if HAS_IGNITE_REPORT
583                                         uint8_t c;
584 #endif
585                                         ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
586 #if HAS_IGNITE_REPORT
587                                         if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
588                                                 ao_radio_continuity(c);
589                                         else
590 #endif
591                                                 ao_radio_rdf();
592                                 }
593 #ifndef SIMPLIFY
594                                 if ((int16_t) (time - ao_rdf_time) > 0)
595                                         time = ao_rdf_time;
596 #endif
597                         }
598 #endif /* HAS_RDF */
599 #if HAS_APRS
600                         if (ao_config.aprs_interval != 0) {
601                                 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
602                                         ao_set_aprs_time();
603                                         ao_aprs_send();
604                                 }
605                                 if ((int16_t) (time - ao_aprs_time) > 0)
606                                         time = ao_aprs_time;
607                         }
608 #endif /* HAS_APRS */
609                         delay = (AO_TICK_SIGNED) (time - ao_time());
610                         if (delay > 0) {
611                                 ao_sleep_for(&telemetry, (AO_TICK_TYPE) delay);
612                         }
613                 }
614         }
615 }
616
617 #if HAS_RADIO_RATE
618 void
619 ao_telemetry_reset_interval(void)
620 {
621         ao_telemetry_set_interval(ao_telemetry_desired_interval);
622 }
623 #endif
624
625 void
626 ao_telemetry_set_interval(uint16_t interval)
627 {
628         int16_t cur = 0;
629
630 #if HAS_RADIO_RATE
631         /* Limit max telemetry rate based on available radio bandwidth.
632          */
633         static const uint16_t min_interval[] = {
634                 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
635                 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
636                 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
637         };
638
639         ao_telemetry_desired_interval = interval;
640         if (interval && interval < min_interval[ao_config.radio_rate])
641                 interval = min_interval[ao_config.radio_rate];
642 #endif
643         ao_telemetry_interval = interval;
644         if (interval) {
645 #if AO_SEND_MEGA
646                 if (interval > 1)
647                         ao_telemetry_mega_data_max = 1;
648                 else
649                         ao_telemetry_mega_data_max = 2;
650                 if (ao_telemetry_mega_data_max > cur)
651                         cur++;
652                 ao_telemetry_mega_data_cur = cur;
653 #endif
654 #if AO_SEND_METRUM
655                 ao_telemetry_metrum_data_max = (int16_t) (AO_SEC_TO_TICKS(1) / interval);
656                 if (ao_telemetry_metrum_data_max > cur)
657                         cur++;
658                 ao_telemetry_metrum_data_cur = cur;
659 #endif
660
661 #if HAS_COMPANION
662                 if (!ao_companion_setup.update_period)
663                         ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
664                 ao_telemetry_companion_max = (int16_t) (ao_companion_setup.update_period / interval);
665                 if (ao_telemetry_companion_max > cur)
666                         cur++;
667                 ao_telemetry_companion_cur = cur;
668 #endif
669
670 #if HAS_GPS
671                 ao_telemetry_gps_max = (int16_t) (AO_SEC_TO_TICKS(1) / interval);
672                 if (ao_telemetry_gps_max > cur)
673                         cur++;
674                 ao_telemetry_loc_cur = cur;
675                 if (ao_telemetry_gps_max > cur)
676                         cur++;
677                 ao_telemetry_sat_cur = cur;
678 #endif
679
680                 ao_telemetry_config_max = (int16_t) (AO_SEC_TO_TICKS(5) / interval);
681                 if (ao_telemetry_config_max > cur)
682                         cur++;
683                 ao_telemetry_config_cur = cur;
684         }
685
686 #ifndef SIMPLIFY
687         ao_telemetry_time = 
688 #if HAS_RDF
689                 ao_rdf_time =
690 #endif
691 #if HAS_APRS
692                 ao_aprs_time =
693 #endif
694                 ao_time();
695 #endif
696         ao_wakeup(&telemetry);
697 }
698
699 #if HAS_RDF
700 void
701 ao_rdf_set(uint8_t rdf)
702 {
703         ao_rdf = rdf;
704         if (rdf == 0)
705                 ao_radio_rdf_abort();
706         else {
707                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
708         }
709 }
710 #endif
711
712 struct ao_task  ao_telemetry_task;
713
714 void
715 ao_telemetry_init()
716 {
717         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
718 }