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