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