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