altos: Call ao_telemetry_reset_interval when telemetry rate changes
[fw/altos] / src / kernel / ao_telemetry.c
1 /*
2  * Copyright © 2011 Keith 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include "ao_log.h"
20 #include "ao_product.h"
21
22 #ifndef HAS_RDF
23 #define HAS_RDF 1
24 #endif
25
26 static __pdata uint16_t ao_telemetry_interval;
27
28 #if HAS_RADIO_RATE
29 static __pdata uint16_t ao_telemetry_desired_interval;
30 #endif
31
32 #if HAS_RDF
33 static __pdata uint8_t ao_rdf = 0;
34 static __pdata uint16_t ao_rdf_time;
35 #endif
36
37 #if HAS_APRS
38 static __pdata uint16_t ao_aprs_time;
39
40 #include <ao_aprs.h>
41 #endif
42
43 #if defined(TELEMEGA)
44 #define AO_SEND_MEGA    1
45 #endif
46
47 #if defined (TELEMETRUM_V_2_0)
48 #define AO_SEND_METRUM  1
49 #endif
50
51 #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)
52 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMETRUM
53 #endif
54
55 #if defined(TELEMINI_V_1_0)
56 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMINI
57 #endif
58
59 #if defined(TELENANO_V_0_1)
60 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELENANO
61 #endif
62
63 static __xdata union ao_telemetry_all   telemetry;
64
65 #if defined AO_TELEMETRY_SENSOR
66 /* Send sensor packet */
67 static void
68 ao_send_sensor(void)
69 {
70         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
71
72         telemetry.generic.tick = packet->tick;
73         telemetry.generic.type = AO_TELEMETRY_SENSOR;
74
75         telemetry.sensor.state = ao_flight_state;
76 #if HAS_ACCEL
77         telemetry.sensor.accel = packet->adc.accel;
78 #else
79         telemetry.sensor.accel = 0;
80 #endif
81         telemetry.sensor.pres = ao_data_pres(packet);
82         telemetry.sensor.temp = packet->adc.temp;
83         telemetry.sensor.v_batt = packet->adc.v_batt;
84 #if HAS_IGNITE
85         telemetry.sensor.sense_d = packet->adc.sense_d;
86         telemetry.sensor.sense_m = packet->adc.sense_m;
87 #else
88         telemetry.sensor.sense_d = 0;
89         telemetry.sensor.sense_m = 0;
90 #endif
91
92         telemetry.sensor.acceleration = ao_accel;
93         telemetry.sensor.speed = ao_speed;
94         telemetry.sensor.height = ao_height;
95
96         telemetry.sensor.ground_pres = ao_ground_pres;
97 #if HAS_ACCEL
98         telemetry.sensor.ground_accel = ao_ground_accel;
99         telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
100         telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
101 #else
102         telemetry.sensor.ground_accel = 0;
103         telemetry.sensor.accel_plus_g = 0;
104         telemetry.sensor.accel_minus_g = 0;
105 #endif
106
107         ao_radio_send(&telemetry, sizeof (telemetry));
108 }
109 #endif
110
111
112 #ifdef AO_SEND_MEGA
113
114 /* Send mega sensor packet */
115 static void
116 ao_send_mega_sensor(void)
117 {
118         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
119
120         telemetry.generic.tick = packet->tick;
121         telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
122
123         telemetry.mega_sensor.orient = ao_sample_orient;
124         telemetry.mega_sensor.accel = ao_data_accel(packet);
125         telemetry.mega_sensor.pres = ao_data_pres(packet);
126         telemetry.mega_sensor.temp = ao_data_temp(packet);
127
128 #if HAS_MPU6000
129         telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
130         telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
131         telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
132
133         telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
134         telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
135         telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
136 #endif
137
138 #if HAS_HMC5883
139         telemetry.mega_sensor.mag_x = packet->hmc5883.x;
140         telemetry.mega_sensor.mag_y = packet->hmc5883.y;
141         telemetry.mega_sensor.mag_z = packet->hmc5883.z;
142 #endif
143
144         ao_radio_send(&telemetry, sizeof (telemetry));
145 }
146
147 static __pdata int8_t ao_telemetry_mega_data_max;
148 static __pdata int8_t ao_telemetry_mega_data_cur;
149
150 /* Send mega data packet */
151 static void
152 ao_send_mega_data(void)
153 {
154         if (--ao_telemetry_mega_data_cur <= 0) {
155                 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
156                 uint8_t i;
157
158                 telemetry.generic.tick = packet->tick;
159                 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
160
161                 telemetry.mega_data.state = ao_flight_state;
162                 telemetry.mega_data.v_batt = packet->adc.v_batt;
163                 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
164
165                 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
166                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
167                         telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
168
169                 telemetry.mega_data.ground_pres = ao_ground_pres;
170                 telemetry.mega_data.ground_accel = ao_ground_accel;
171                 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
172                 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
173
174                 telemetry.mega_data.acceleration = ao_accel;
175                 telemetry.mega_data.speed = ao_speed;
176                 telemetry.mega_data.height = ao_height;
177
178                 ao_radio_send(&telemetry, sizeof (telemetry));
179                 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
180         }
181 }
182 #endif /* AO_SEND_MEGA */
183
184 #ifdef AO_SEND_METRUM
185 /* Send telemetrum sensor packet */
186 static void
187 ao_send_metrum_sensor(void)
188 {
189         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
190
191         telemetry.generic.tick = packet->tick;
192         telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
193
194         telemetry.metrum_sensor.state = ao_flight_state;
195 #if HAS_ACCEL
196         telemetry.metrum_sensor.accel = ao_data_accel(packet);
197 #endif
198         telemetry.metrum_sensor.pres = ao_data_pres(packet);
199         telemetry.metrum_sensor.temp = ao_data_temp(packet);
200
201         telemetry.metrum_sensor.acceleration = ao_accel;
202         telemetry.metrum_sensor.speed = ao_speed;
203         telemetry.metrum_sensor.height = ao_height;
204
205         telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
206         telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
207         telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
208
209         ao_radio_send(&telemetry, sizeof (telemetry));
210 }
211
212 static __pdata int8_t ao_telemetry_metrum_data_max;
213 static __pdata int8_t ao_telemetry_metrum_data_cur;
214
215 /* Send telemetrum data packet */
216 static void
217 ao_send_metrum_data(void)
218 {
219         if (--ao_telemetry_metrum_data_cur <= 0) {
220                 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
221
222                 telemetry.generic.tick = packet->tick;
223                 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
224
225                 telemetry.metrum_data.ground_pres = ao_ground_pres;
226 #if HAS_ACCEL
227                 telemetry.metrum_data.ground_accel = ao_ground_accel;
228                 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
229                 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
230 #else
231                 telemetry.metrum_data.ground_accel = 1;
232                 telemetry.metrum_data.accel_plus_g = 0;
233                 telemetry.metrum_data.accel_minus_g = 2;
234 #endif
235
236                 ao_radio_send(&telemetry, sizeof (telemetry));
237                 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
238         }
239 }
240 #endif /* AO_SEND_METRUM */
241
242 #ifdef AO_SEND_MINI
243
244 static void
245 ao_send_mini(void)
246 {
247         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
248
249         telemetry.generic.tick = packet->tick;
250         telemetry.generic.type = AO_TELEMETRY_MINI;
251
252         telemetry.mini.state = ao_flight_state;
253
254         telemetry.mini.v_batt = packet->adc.v_batt;
255         telemetry.mini.sense_a = packet->adc.sense_a;
256         telemetry.mini.sense_m = packet->adc.sense_m;
257
258         telemetry.mini.pres = ao_data_pres(packet);
259         telemetry.mini.temp = ao_data_temp(packet);
260
261         telemetry.mini.acceleration = ao_accel;
262         telemetry.mini.speed = ao_speed;
263         telemetry.mini.height = ao_height;
264
265         telemetry.mini.ground_pres = ao_ground_pres;
266
267         ao_radio_send(&telemetry, sizeof (telemetry));
268 }
269
270 #endif /* AO_SEND_MINI */
271
272 #ifdef AO_SEND_ALL_BARO
273 static uint8_t          ao_baro_sample;
274
275 static void
276 ao_send_baro(void)
277 {
278         uint8_t         sample = ao_sample_data;
279         uint8_t         samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
280
281         if (samples > 12) {
282                 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
283                 samples = 12;
284         }
285         telemetry.generic.tick = ao_data_ring[sample].tick;
286         telemetry.generic.type = AO_TELEMETRY_BARO;
287         telemetry.baro.samples = samples;
288         for (sample = 0; sample < samples; sample++) {
289                 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
290                 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
291         }
292         ao_radio_send(&telemetry, sizeof (telemetry));
293 }
294 #endif
295
296 static __pdata int8_t ao_telemetry_config_max;
297 static __pdata int8_t ao_telemetry_config_cur;
298
299 static void
300 ao_send_configuration(void)
301 {
302         if (--ao_telemetry_config_cur <= 0)
303         {
304                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
305                 telemetry.configuration.device = AO_idProduct_NUMBER;
306 #if HAS_LOG
307                 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
308 #else
309                 telemetry.configuration.flight = ao_flight_number;
310 #endif
311                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
312                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
313 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
314                 /* TeleGPS gets battery voltage instead of apogee delay */
315                 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
316 #else
317                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
318                 telemetry.configuration.main_deploy = ao_config.main_deploy;
319 #endif
320
321                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
322                 ao_xmemcpy (telemetry.configuration.callsign,
323                             ao_config.callsign,
324                             AO_MAX_CALLSIGN);
325                 ao_xmemcpy (telemetry.configuration.version,
326                             CODE_TO_XDATA(ao_version),
327                             AO_MAX_VERSION);
328                 ao_radio_send(&telemetry, sizeof (telemetry));
329                 ao_telemetry_config_cur = ao_telemetry_config_max;
330         }
331 }
332
333 #if HAS_GPS
334
335 static __pdata int8_t ao_telemetry_loc_cur;
336 static __pdata int8_t ao_telemetry_sat_cur;
337
338 static void
339 ao_send_location(void)
340 {
341         if (--ao_telemetry_loc_cur <= 0)
342         {
343                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
344                 ao_mutex_get(&ao_gps_mutex);
345                 ao_xmemcpy(&telemetry.location.flags,
346                        &ao_gps_data.flags,
347                        26);
348                 telemetry.location.tick = ao_gps_tick;
349                 ao_mutex_put(&ao_gps_mutex);
350                 ao_radio_send(&telemetry, sizeof (telemetry));
351                 ao_telemetry_loc_cur = ao_telemetry_config_max;
352         }
353 }
354
355 static void
356 ao_send_satellite(void)
357 {
358         if (--ao_telemetry_sat_cur <= 0)
359         {
360                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
361                 ao_mutex_get(&ao_gps_mutex);
362                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
363                 ao_xmemcpy(&telemetry.satellite.sats,
364                        &ao_gps_tracking_data.sats,
365                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
366                 ao_mutex_put(&ao_gps_mutex);
367                 ao_radio_send(&telemetry, sizeof (telemetry));
368                 ao_telemetry_sat_cur = ao_telemetry_config_max;
369         }
370 }
371 #endif
372
373 #if HAS_COMPANION
374
375 static __pdata int8_t ao_telemetry_companion_max;
376 static __pdata int8_t ao_telemetry_companion_cur;
377
378 static void
379 ao_send_companion(void)
380 {
381         if (--ao_telemetry_companion_cur <= 0) {
382                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
383                 telemetry.companion.board_id = ao_companion_setup.board_id;
384                 telemetry.companion.update_period = ao_companion_setup.update_period;
385                 telemetry.companion.channels = ao_companion_setup.channels;
386                 ao_mutex_get(&ao_companion_mutex);
387                 ao_xmemcpy(&telemetry.companion.companion_data,
388                        ao_companion_data,
389                        ao_companion_setup.channels * 2);
390                 ao_mutex_put(&ao_companion_mutex);
391                 ao_radio_send(&telemetry, sizeof (telemetry));
392                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
393         }
394 }
395 #endif
396
397 void
398 ao_telemetry(void)
399 {
400         uint16_t        time;
401         int16_t         delay;
402
403         ao_config_get();
404         if (!ao_config.radio_enable)
405                 ao_exit();
406         while (!ao_flight_number)
407                 ao_sleep(&ao_flight_number);
408
409         telemetry.generic.serial = ao_serial_number;
410         for (;;) {
411                 while (ao_telemetry_interval == 0)
412                         ao_sleep(&telemetry);
413                 time = ao_time();
414 #if HAS_RDF
415                 ao_rdf_time = time;
416 #endif
417 #if HAS_APRS
418                 ao_aprs_time = time;
419 #endif
420                 while (ao_telemetry_interval) {
421 #if HAS_APRS
422                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
423 #endif
424                         {
425 #ifdef AO_SEND_ALL_BARO
426                                 ao_send_baro();
427 #endif
428
429 #if HAS_FLIGHT
430 # ifdef AO_SEND_MEGA
431                                 ao_send_mega_sensor();
432                                 ao_send_mega_data();
433 # endif
434 # ifdef AO_SEND_METRUM
435                                 ao_send_metrum_sensor();
436                                 ao_send_metrum_data();
437 # endif
438 # ifdef AO_SEND_MINI
439                                 ao_send_mini();
440 # endif
441 # ifdef AO_TELEMETRY_SENSOR
442                                 ao_send_sensor();
443 # endif
444 #endif /* HAS_FLIGHT */
445
446 #if HAS_COMPANION
447                                 if (ao_companion_running)
448                                         ao_send_companion();
449 #endif
450                                 ao_send_configuration();
451 #if HAS_GPS
452                                 ao_send_location();
453                                 ao_send_satellite();
454 #endif
455                         }
456 #ifndef AO_SEND_ALL_BARO
457 #if HAS_RDF
458                         if (ao_rdf &&
459 #if HAS_APRS
460                             !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
461 #endif /* HAS_APRS */
462                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
463                         {
464 #if HAS_IGNITE_REPORT
465                                 uint8_t c;
466 #endif /* HAS_IGNITE_REPORT */
467                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
468 #if HAS_IGNITE_REPORT
469                                 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
470                                         ao_radio_continuity(c);
471                                 else
472 #endif /* HAS_IGNITE_REPORT*/
473                                         ao_radio_rdf();
474                         }
475 #endif /* HAS_RDF */
476 #if HAS_APRS
477                         if (ao_config.aprs_interval != 0 &&
478                             (int16_t) (ao_time() - ao_aprs_time) >= 0)
479                         {
480                                 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
481                                 ao_aprs_send();
482                         }
483 #endif /* HAS_APRS */
484 #endif /* !AO_SEND_ALL_BARO */
485                         time += ao_telemetry_interval;
486                         delay = time - ao_time();
487                         if (delay > 0) {
488                                 ao_alarm(delay);
489                                 ao_sleep(&telemetry);
490                                 ao_clear_alarm();
491                         }
492                         else
493                                 time = ao_time();
494                 }
495         }
496 }
497
498 #if HAS_RADIO_RATE
499 void
500 ao_telemetry_reset_interval(void)
501 {
502         ao_telemetry_set_interval(ao_telemetry_desired_interval);
503 }
504 #endif
505
506 void
507 ao_telemetry_set_interval(uint16_t interval)
508 {
509         int8_t  cur = 0;
510
511 #if HAS_RADIO_RATE
512         /* Limit max telemetry rate based on available radio bandwidth.
513          */
514         static const uint16_t min_interval[] = {
515                 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
516                 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
517                 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
518         };
519
520         ao_telemetry_desired_interval = interval;
521         if (interval < min_interval[ao_config.radio_rate])
522                 interval = min_interval[ao_config.radio_rate];
523 #endif
524         ao_telemetry_interval = interval;
525 #if AO_SEND_MEGA
526         if (interval > 1)
527                 ao_telemetry_mega_data_max = 1;
528         else
529                 ao_telemetry_mega_data_max = 2;
530         if (ao_telemetry_mega_data_max > cur)
531                 cur++;
532         ao_telemetry_mega_data_cur = cur;
533 #endif
534 #if AO_SEND_METRUM
535         ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
536         if (ao_telemetry_metrum_data_max > cur)
537                 cur++;
538         ao_telemetry_metrum_data_cur = cur;
539 #endif
540
541 #if HAS_COMPANION
542         if (!ao_companion_setup.update_period)
543                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
544         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
545         if (ao_telemetry_companion_max > cur)
546                 cur++;
547         ao_telemetry_companion_cur = cur;
548 #endif
549
550         ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
551 #if HAS_COMPANION
552         if (ao_telemetry_config_max > cur)
553                 cur++;
554         ao_telemetry_config_cur = cur;
555 #endif
556
557 #if HAS_GPS
558         if (ao_telemetry_config_max > cur)
559                 cur++;
560         ao_telemetry_loc_cur = cur;
561         if (ao_telemetry_config_max > cur)
562                 cur++;
563         ao_telemetry_sat_cur = cur;
564 #endif
565         ao_wakeup(&telemetry);
566 }
567
568 #if HAS_RDF
569 void
570 ao_rdf_set(uint8_t rdf)
571 {
572         ao_rdf = rdf;
573         if (rdf == 0)
574                 ao_radio_rdf_abort();
575         else {
576                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
577         }
578 }
579 #endif
580
581 __xdata struct ao_task  ao_telemetry_task;
582
583 void
584 ao_telemetry_init()
585 {
586         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
587 }