Switch from GPLv2 to GPLv2+
[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 __pdata uint16_t ao_telemetry_interval;
24
25 #if HAS_RADIO_RATE
26 static __xdata 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       __pdata
41 #else
42 #define RDF_SPACE       __xdata
43 static __pdata uint16_t ao_telemetry_time;
44 #endif
45
46 #if HAS_RDF
47 static RDF_SPACE uint8_t ao_rdf = 0;
48 static RDF_SPACE uint16_t ao_rdf_time;
49 #endif
50
51 #if HAS_APRS
52 static __pdata uint16_t 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)
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 __xdata 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         __xdata struct ao_data *packet = (__xdata 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         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
140
141         telemetry.generic.tick = packet->tick;
142         telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
143
144 #if HAS_MPU6000
145         telemetry.mega_sensor.orient = ao_sample_orient;
146 #endif
147         telemetry.mega_sensor.accel = ao_data_accel(packet);
148         telemetry.mega_sensor.pres = ao_data_pres(packet);
149         telemetry.mega_sensor.temp = ao_data_temp(packet);
150
151 #if HAS_MPU6000
152         telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
153         telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
154         telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
155
156         telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
157         telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
158         telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
159 #endif
160
161 #if HAS_HMC5883
162         telemetry.mega_sensor.mag_x = packet->hmc5883.x;
163         telemetry.mega_sensor.mag_y = packet->hmc5883.y;
164         telemetry.mega_sensor.mag_z = packet->hmc5883.z;
165 #endif
166
167         ao_telemetry_send();
168 }
169
170 static __pdata int8_t ao_telemetry_mega_data_max;
171 static __pdata int8_t ao_telemetry_mega_data_cur;
172
173 /* Send mega data packet */
174 static void
175 ao_send_mega_data(void)
176 {
177         if (--ao_telemetry_mega_data_cur <= 0) {
178                 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
179                 uint8_t i;
180
181                 telemetry.generic.tick = packet->tick;
182                 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
183
184                 telemetry.mega_data.state = ao_flight_state;
185                 telemetry.mega_data.v_batt = packet->adc.v_batt;
186                 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
187
188                 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
189                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
190                         telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
191
192                 telemetry.mega_data.ground_pres = ao_ground_pres;
193                 telemetry.mega_data.ground_accel = ao_ground_accel;
194                 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
195                 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
196
197                 telemetry.mega_data.acceleration = ao_accel;
198                 telemetry.mega_data.speed = ao_speed;
199                 telemetry.mega_data.height = ao_height;
200
201                 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
202                 ao_telemetry_send();
203         }
204 }
205 #endif /* AO_SEND_MEGA */
206
207 #ifdef AO_SEND_METRUM
208 /* Send telemetrum sensor packet */
209 static void
210 ao_send_metrum_sensor(void)
211 {
212         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
213
214         telemetry.generic.tick = packet->tick;
215         telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
216
217         telemetry.metrum_sensor.state = ao_flight_state;
218 #if HAS_ACCEL
219         telemetry.metrum_sensor.accel = ao_data_accel(packet);
220 #endif
221         telemetry.metrum_sensor.pres = ao_data_pres(packet);
222         telemetry.metrum_sensor.temp = ao_data_temp(packet);
223
224         telemetry.metrum_sensor.acceleration = ao_accel;
225         telemetry.metrum_sensor.speed = ao_speed;
226         telemetry.metrum_sensor.height = ao_height;
227
228         telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
229         telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
230         telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
231
232         ao_telemetry_send();
233 }
234
235 static __pdata int8_t ao_telemetry_metrum_data_max;
236 static __pdata int8_t ao_telemetry_metrum_data_cur;
237
238 /* Send telemetrum data packet */
239 static void
240 ao_send_metrum_data(void)
241 {
242         if (--ao_telemetry_metrum_data_cur <= 0) {
243                 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
244
245                 telemetry.generic.tick = packet->tick;
246                 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
247
248                 telemetry.metrum_data.ground_pres = ao_ground_pres;
249 #if HAS_ACCEL
250                 telemetry.metrum_data.ground_accel = ao_ground_accel;
251                 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
252                 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
253 #else
254                 telemetry.metrum_data.ground_accel = 1;
255                 telemetry.metrum_data.accel_plus_g = 0;
256                 telemetry.metrum_data.accel_minus_g = 2;
257 #endif
258
259                 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
260                 ao_telemetry_send();
261         }
262 }
263 #endif /* AO_SEND_METRUM */
264
265 #ifdef AO_SEND_MINI
266
267 static void
268 ao_send_mini(void)
269 {
270         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
271
272         telemetry.generic.tick = packet->tick;
273         telemetry.generic.type = AO_TELEMETRY_MINI;
274
275         telemetry.mini.state = ao_flight_state;
276
277         telemetry.mini.v_batt = packet->adc.v_batt;
278         telemetry.mini.sense_a = packet->adc.sense_a;
279         telemetry.mini.sense_m = packet->adc.sense_m;
280
281         telemetry.mini.pres = ao_data_pres(packet);
282         telemetry.mini.temp = ao_data_temp(packet);
283
284         telemetry.mini.acceleration = ao_accel;
285         telemetry.mini.speed = ao_speed;
286         telemetry.mini.height = ao_height;
287
288         telemetry.mini.ground_pres = ao_ground_pres;
289
290         ao_telemetry_send();
291 }
292
293 #endif /* AO_SEND_MINI */
294
295 static __pdata int8_t ao_telemetry_config_max;
296 static __pdata int8_t ao_telemetry_config_cur;
297
298 static void
299 ao_send_configuration(void)
300 {
301         if (--ao_telemetry_config_cur <= 0)
302         {
303                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
304                 telemetry.configuration.device = AO_idProduct_NUMBER;
305 #if HAS_LOG
306                 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
307 #else
308                 telemetry.configuration.flight = ao_flight_number;
309 #endif
310                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
311                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
312 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
313                 /* TeleGPS gets battery voltage instead of apogee delay */
314                 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
315 #else
316                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
317                 telemetry.configuration.main_deploy = ao_config.main_deploy;
318 #endif
319
320                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
321                 ao_xmemcpy (telemetry.configuration.callsign,
322                             ao_config.callsign,
323                             AO_MAX_CALLSIGN);
324                 ao_xmemcpy (telemetry.configuration.version,
325                             CODE_TO_XDATA(ao_version),
326                             AO_MAX_VERSION);
327                 ao_telemetry_config_cur = ao_telemetry_config_max;
328                 ao_telemetry_send();
329         }
330 }
331
332 #if HAS_GPS
333
334 static __pdata int8_t ao_telemetry_gps_max;
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                        27);
348                 telemetry.location.tick = ao_gps_tick;
349                 ao_mutex_put(&ao_gps_mutex);
350                 ao_telemetry_loc_cur = ao_telemetry_gps_max;
351                 ao_telemetry_send();
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_telemetry_sat_cur = ao_telemetry_gps_max;
368                 ao_telemetry_send();
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_telemetry_companion_cur = ao_telemetry_companion_max;
392                 ao_telemetry_send();
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                 ao_telemetry_time = time;
415 #if HAS_RDF
416                 ao_rdf_time = time;
417 #endif
418 #if HAS_APRS
419                 ao_aprs_time = time;
420 #endif
421                 while (ao_telemetry_interval) {
422                         time = ao_time() + AO_SEC_TO_TICKS(100);
423 #ifndef SIMPLIFY
424                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
425 #endif
426                         {
427 #ifndef SIMPLIFY
428                                 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
429 #endif
430                                 {
431                                         ao_telemetry_time = ao_time() + ao_telemetry_interval;
432 # ifdef AO_SEND_MEGA
433                                         ao_send_mega_sensor();
434                                         ao_send_mega_data();
435 # endif
436 # ifdef AO_SEND_METRUM
437                                         ao_send_metrum_sensor();
438                                         ao_send_metrum_data();
439 # endif
440 # ifdef AO_SEND_MINI
441                                         ao_send_mini();
442 # endif
443 # ifdef AO_TELEMETRY_SENSOR
444                                         ao_send_sensor();
445 # endif
446 #if HAS_COMPANION
447                                         if (ao_companion_running)
448                                                 ao_send_companion();
449 #endif
450 #if HAS_GPS
451                                         ao_send_location();
452                                         ao_send_satellite();
453 #endif
454                                         ao_send_configuration();
455                                 }
456 #ifndef SIMPLIFY
457                                 time = ao_telemetry_time;
458 #endif
459                         }
460 #if HAS_RDF
461                         if (ao_rdf
462 #ifndef SIMPLIFY
463                             && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
464 #endif
465                                 )
466                         {
467                                 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
468 #if HAS_IGNITE_REPORT
469                                         uint8_t c;
470 #endif
471                                         ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
472 #if HAS_IGNITE_REPORT
473                                         if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
474                                                 ao_radio_continuity(c);
475                                         else
476 #endif
477                                                 ao_radio_rdf();
478                                 }
479 #ifndef SIMPLIFY
480                                 if ((int16_t) (time - ao_rdf_time) > 0)
481                                         time = ao_rdf_time;
482 #endif
483                         }
484 #endif /* HAS_RDF */
485 #if HAS_APRS
486                         if (ao_config.aprs_interval != 0) {
487                                 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
488                                         ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
489                                         ao_aprs_send();
490                                 }
491                                 if ((int16_t) (time - ao_aprs_time) > 0)
492                                         time = ao_aprs_time;
493                         }
494 #endif /* HAS_APRS */
495                         delay = time - ao_time();
496                         if (delay > 0) {
497                                 ao_sleep_for(&telemetry, delay);
498                         }
499                 }
500         }
501 }
502
503 #if HAS_RADIO_RATE
504 void
505 ao_telemetry_reset_interval(void)
506 {
507         ao_telemetry_set_interval(ao_telemetry_desired_interval);
508 }
509 #endif
510
511 void
512 ao_telemetry_set_interval(uint16_t interval)
513 {
514         int8_t  cur = 0;
515
516 #if HAS_RADIO_RATE
517         /* Limit max telemetry rate based on available radio bandwidth.
518          */
519         static __xdata const uint16_t min_interval[] = {
520                 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
521                 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
522                 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
523         };
524
525         ao_telemetry_desired_interval = interval;
526         if (interval && interval < min_interval[ao_config.radio_rate])
527                 interval = min_interval[ao_config.radio_rate];
528 #endif
529         ao_telemetry_interval = interval;
530 #if AO_SEND_MEGA
531         if (interval > 1)
532                 ao_telemetry_mega_data_max = 1;
533         else
534                 ao_telemetry_mega_data_max = 2;
535         if (ao_telemetry_mega_data_max > cur)
536                 cur++;
537         ao_telemetry_mega_data_cur = cur;
538 #endif
539 #if AO_SEND_METRUM
540         ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
541         if (ao_telemetry_metrum_data_max > cur)
542                 cur++;
543         ao_telemetry_metrum_data_cur = cur;
544 #endif
545
546 #if HAS_COMPANION
547         if (!ao_companion_setup.update_period)
548                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
549         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
550         if (ao_telemetry_companion_max > cur)
551                 cur++;
552         ao_telemetry_companion_cur = cur;
553 #endif
554
555 #if HAS_GPS
556         ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
557         if (ao_telemetry_gps_max > cur)
558                 cur++;
559         ao_telemetry_loc_cur = cur;
560         if (ao_telemetry_gps_max > cur)
561                 cur++;
562         ao_telemetry_sat_cur = cur;
563 #endif
564
565         ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
566         if (ao_telemetry_config_max > cur)
567                 cur++;
568         ao_telemetry_config_cur = cur;
569
570 #ifndef SIMPLIFY
571         ao_telemetry_time = 
572 #if HAS_RDF
573                 ao_rdf_time =
574 #endif
575 #if HAS_APRS
576                 ao_aprs_time =
577 #endif
578                 ao_time();
579 #endif
580         ao_wakeup(&telemetry);
581 }
582
583 #if HAS_RDF
584 void
585 ao_rdf_set(uint8_t rdf)
586 {
587         ao_rdf = rdf;
588         if (rdf == 0)
589                 ao_radio_rdf_abort();
590         else {
591                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
592         }
593 }
594 #endif
595
596 __xdata struct ao_task  ao_telemetry_task;
597
598 void
599 ao_telemetry_init()
600 {
601         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
602 }