altos/kernel: Allow TeleGPS v2 to scale battery in telem
[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_SEND_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 #ifndef ao_telemetry_battery_convert
300 #define ao_telemetry_battery_convert(a) (a)
301 #endif
302
303 static void
304 ao_send_configuration(void)
305 {
306         if (--ao_telemetry_config_cur <= 0)
307         {
308                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
309                 telemetry.configuration.device = AO_idProduct_NUMBER;
310                 telemetry.configuration.flight = ao_telemetry_flight_number;
311                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
312                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
313 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
314                 /* TeleGPS gets battery voltage instead of apogee delay */
315                 telemetry.configuration.apogee_delay = ao_telemetry_battery_convert(ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt);
316 #else
317                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
318                 telemetry.configuration.main_deploy = ao_config.main_deploy;
319 #endif
320
321                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
322                 ao_xmemcpy (telemetry.configuration.callsign,
323                             ao_config.callsign,
324                             AO_MAX_CALLSIGN);
325                 ao_xmemcpy (telemetry.configuration.version,
326                             CODE_TO_XDATA(ao_version),
327                             AO_MAX_VERSION);
328                 ao_telemetry_config_cur = ao_telemetry_config_max;
329                 ao_telemetry_send();
330         }
331 }
332
333 #if HAS_GPS
334
335 static __pdata int8_t ao_telemetry_gps_max;
336 static __pdata int8_t ao_telemetry_loc_cur;
337 static __pdata int8_t ao_telemetry_sat_cur;
338
339 static void
340 ao_send_location(void)
341 {
342         if (--ao_telemetry_loc_cur <= 0)
343         {
344                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
345                 ao_mutex_get(&ao_gps_mutex);
346                 ao_xmemcpy(&telemetry.location.flags,
347                        &ao_gps_data.flags,
348                        27);
349                 telemetry.location.tick = ao_gps_tick;
350                 ao_mutex_put(&ao_gps_mutex);
351                 ao_telemetry_loc_cur = ao_telemetry_gps_max;
352                 ao_telemetry_send();
353         }
354 }
355
356 static void
357 ao_send_satellite(void)
358 {
359         if (--ao_telemetry_sat_cur <= 0)
360         {
361                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
362                 ao_mutex_get(&ao_gps_mutex);
363                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
364                 ao_xmemcpy(&telemetry.satellite.sats,
365                        &ao_gps_tracking_data.sats,
366                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
367                 ao_mutex_put(&ao_gps_mutex);
368                 ao_telemetry_sat_cur = ao_telemetry_gps_max;
369                 ao_telemetry_send();
370         }
371 }
372 #endif
373
374 #if HAS_COMPANION
375
376 static __pdata int8_t ao_telemetry_companion_max;
377 static __pdata int8_t ao_telemetry_companion_cur;
378
379 static void
380 ao_send_companion(void)
381 {
382         if (--ao_telemetry_companion_cur <= 0) {
383                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
384                 telemetry.companion.board_id = ao_companion_setup.board_id;
385                 telemetry.companion.update_period = ao_companion_setup.update_period;
386                 telemetry.companion.channels = ao_companion_setup.channels;
387                 ao_mutex_get(&ao_companion_mutex);
388                 ao_xmemcpy(&telemetry.companion.companion_data,
389                        ao_companion_data,
390                        ao_companion_setup.channels * 2);
391                 ao_mutex_put(&ao_companion_mutex);
392                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
393                 ao_telemetry_send();
394         }
395 }
396 #endif
397
398 void
399 ao_telemetry(void)
400 {
401         uint16_t        time;
402         int16_t         delay;
403
404         ao_config_get();
405         if (!ao_config.radio_enable)
406                 ao_exit();
407         while (!ao_flight_number)
408                 ao_sleep(&ao_flight_number);
409
410         ao_telemetry_flight_number = ao_flight_number;
411 #if HAS_LOG
412         if (ao_log_full())
413                 ao_telemetry_flight_number = 0;
414 #endif
415         telemetry.generic.serial = ao_serial_number;
416         for (;;) {
417                 while (ao_telemetry_interval == 0)
418                         ao_sleep(&telemetry);
419                 time = ao_time();
420                 ao_telemetry_time = time;
421 #if HAS_RDF
422                 ao_rdf_time = time;
423 #endif
424 #if HAS_APRS
425                 ao_aprs_time = time;
426 #endif
427                 while (ao_telemetry_interval) {
428                         time = ao_time() + AO_SEC_TO_TICKS(100);
429 #ifndef SIMPLIFY
430                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
431 #endif
432                         {
433 #ifndef SIMPLIFY
434                                 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
435 #endif
436                                 {
437                                         ao_telemetry_time = ao_time() + ao_telemetry_interval;
438 # ifdef AO_SEND_MEGA
439                                         ao_send_mega_sensor();
440                                         ao_send_mega_data();
441 # endif
442 # ifdef AO_SEND_METRUM
443                                         ao_send_metrum_sensor();
444                                         ao_send_metrum_data();
445 # endif
446 # ifdef AO_SEND_MINI
447                                         ao_send_mini();
448 # endif
449 # ifdef AO_TELEMETRY_SENSOR
450                                         ao_send_sensor();
451 # endif
452 #if HAS_COMPANION
453                                         if (ao_companion_running)
454                                                 ao_send_companion();
455 #endif
456 #if HAS_GPS
457                                         ao_send_location();
458                                         ao_send_satellite();
459 #endif
460                                         ao_send_configuration();
461                                 }
462 #ifndef SIMPLIFY
463                                 time = ao_telemetry_time;
464 #endif
465                         }
466 #if HAS_RDF
467                         if (ao_rdf
468 #ifndef SIMPLIFY
469                             && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
470 #endif
471                                 )
472                         {
473                                 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
474 #if HAS_IGNITE_REPORT
475                                         uint8_t c;
476 #endif
477                                         ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
478 #if HAS_IGNITE_REPORT
479                                         if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
480                                                 ao_radio_continuity(c);
481                                         else
482 #endif
483                                                 ao_radio_rdf();
484                                 }
485 #ifndef SIMPLIFY
486                                 if ((int16_t) (time - ao_rdf_time) > 0)
487                                         time = ao_rdf_time;
488 #endif
489                         }
490 #endif /* HAS_RDF */
491 #if HAS_APRS
492                         if (ao_config.aprs_interval != 0) {
493                                 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
494                                         ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
495                                         ao_aprs_send();
496                                 }
497                                 if ((int16_t) (time - ao_aprs_time) > 0)
498                                         time = ao_aprs_time;
499                         }
500 #endif /* HAS_APRS */
501                         delay = time - ao_time();
502                         if (delay > 0) {
503                                 ao_sleep_for(&telemetry, delay);
504                         }
505                 }
506         }
507 }
508
509 #if HAS_RADIO_RATE
510 void
511 ao_telemetry_reset_interval(void)
512 {
513         ao_telemetry_set_interval(ao_telemetry_desired_interval);
514 }
515 #endif
516
517 void
518 ao_telemetry_set_interval(uint16_t interval)
519 {
520         int8_t  cur = 0;
521
522 #if HAS_RADIO_RATE
523         /* Limit max telemetry rate based on available radio bandwidth.
524          */
525         static __xdata const uint16_t min_interval[] = {
526                 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
527                 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
528                 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
529         };
530
531         ao_telemetry_desired_interval = interval;
532         if (interval && interval < min_interval[ao_config.radio_rate])
533                 interval = min_interval[ao_config.radio_rate];
534 #endif
535         ao_telemetry_interval = interval;
536 #if AO_SEND_MEGA
537         if (interval > 1)
538                 ao_telemetry_mega_data_max = 1;
539         else
540                 ao_telemetry_mega_data_max = 2;
541         if (ao_telemetry_mega_data_max > cur)
542                 cur++;
543         ao_telemetry_mega_data_cur = cur;
544 #endif
545 #if AO_SEND_METRUM
546         ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
547         if (ao_telemetry_metrum_data_max > cur)
548                 cur++;
549         ao_telemetry_metrum_data_cur = cur;
550 #endif
551
552 #if HAS_COMPANION
553         if (!ao_companion_setup.update_period)
554                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
555         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
556         if (ao_telemetry_companion_max > cur)
557                 cur++;
558         ao_telemetry_companion_cur = cur;
559 #endif
560
561 #if HAS_GPS
562         ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
563         if (ao_telemetry_gps_max > cur)
564                 cur++;
565         ao_telemetry_loc_cur = cur;
566         if (ao_telemetry_gps_max > cur)
567                 cur++;
568         ao_telemetry_sat_cur = cur;
569 #endif
570
571         ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
572         if (ao_telemetry_config_max > cur)
573                 cur++;
574         ao_telemetry_config_cur = cur;
575
576 #ifndef SIMPLIFY
577         ao_telemetry_time = 
578 #if HAS_RDF
579                 ao_rdf_time =
580 #endif
581 #if HAS_APRS
582                 ao_aprs_time =
583 #endif
584                 ao_time();
585 #endif
586         ao_wakeup(&telemetry);
587 }
588
589 #if HAS_RDF
590 void
591 ao_rdf_set(uint8_t rdf)
592 {
593         ao_rdf = rdf;
594         if (rdf == 0)
595                 ao_radio_rdf_abort();
596         else {
597                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
598         }
599 }
600 #endif
601
602 __xdata struct ao_task  ao_telemetry_task;
603
604 void
605 ao_telemetry_init()
606 {
607         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
608 }