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