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