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