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