Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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 uint16_t ao_telemetry_interval;
24
25 #if HAS_RADIO_RATE
26 static 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       
41 #else
42 #define RDF_SPACE       
43 static 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 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 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                 struct ao_data *packet = (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                 struct ao_data *packet = (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 || HAS_MPU9250
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_z = packet->hmc5883.z;
164         telemetry.mega_sensor.mag_y = packet->hmc5883.y;
165 #endif
166
167 #if HAS_MPU9250
168         telemetry.mega_sensor.accel_x = packet->mpu9250.accel_x;
169         telemetry.mega_sensor.accel_y = packet->mpu9250.accel_y;
170         telemetry.mega_sensor.accel_z = packet->mpu9250.accel_z;
171
172         telemetry.mega_sensor.gyro_x = packet->mpu9250.gyro_x;
173         telemetry.mega_sensor.gyro_y = packet->mpu9250.gyro_y;
174         telemetry.mega_sensor.gyro_z = packet->mpu9250.gyro_z;
175
176         telemetry.mega_sensor.mag_x = packet->mpu9250.mag_x;
177         telemetry.mega_sensor.mag_z = packet->mpu9250.mag_z;
178         telemetry.mega_sensor.mag_y = packet->mpu9250.mag_y;
179 #endif
180
181         ao_telemetry_send();
182 }
183
184 static int8_t ao_telemetry_mega_data_max;
185 static int8_t ao_telemetry_mega_data_cur;
186
187 /* Send mega data packet */
188 static void
189 ao_send_mega_data(void)
190 {
191         if (--ao_telemetry_mega_data_cur <= 0) {
192                         struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
193                 uint8_t i;
194
195                 telemetry.generic.tick = packet->tick;
196                 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
197
198                 telemetry.mega_data.state = ao_flight_state;
199                 telemetry.mega_data.v_batt = packet->adc.v_batt;
200                 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
201
202                 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
203                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
204                         telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
205
206                 telemetry.mega_data.ground_pres = ao_ground_pres;
207                 telemetry.mega_data.ground_accel = ao_ground_accel;
208                 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
209                 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
210
211                 telemetry.mega_data.acceleration = ao_accel;
212                 telemetry.mega_data.speed = ao_speed;
213                 telemetry.mega_data.height = ao_height;
214
215                 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
216                 ao_telemetry_send();
217         }
218 }
219 #endif /* AO_SEND_MEGA */
220
221 #ifdef AO_SEND_METRUM
222 /* Send telemetrum sensor packet */
223 static void
224 ao_send_metrum_sensor(void)
225 {
226                 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
227
228         telemetry.generic.tick = packet->tick;
229         telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
230
231         telemetry.metrum_sensor.state = ao_flight_state;
232 #if HAS_ACCEL
233         telemetry.metrum_sensor.accel = ao_data_accel(packet);
234 #endif
235         telemetry.metrum_sensor.pres = ao_data_pres(packet);
236         telemetry.metrum_sensor.temp = ao_data_temp(packet);
237
238         telemetry.metrum_sensor.acceleration = ao_accel;
239         telemetry.metrum_sensor.speed = ao_speed;
240         telemetry.metrum_sensor.height = ao_height;
241
242         telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
243         telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
244         telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
245
246         ao_telemetry_send();
247 }
248
249 static int8_t ao_telemetry_metrum_data_max;
250 static int8_t ao_telemetry_metrum_data_cur;
251
252 /* Send telemetrum data packet */
253 static void
254 ao_send_metrum_data(void)
255 {
256         if (--ao_telemetry_metrum_data_cur <= 0) {
257                         struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
258
259                 telemetry.generic.tick = packet->tick;
260                 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
261
262                 telemetry.metrum_data.ground_pres = ao_ground_pres;
263 #if HAS_ACCEL
264                 telemetry.metrum_data.ground_accel = ao_ground_accel;
265                 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
266                 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
267 #else
268                 telemetry.metrum_data.ground_accel = 1;
269                 telemetry.metrum_data.accel_plus_g = 0;
270                 telemetry.metrum_data.accel_minus_g = 2;
271 #endif
272
273                 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
274                 ao_telemetry_send();
275         }
276 }
277 #endif /* AO_SEND_METRUM */
278
279 #ifdef AO_SEND_MINI
280
281 static void
282 ao_send_mini(void)
283 {
284                 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
285
286         telemetry.generic.tick = packet->tick;
287         telemetry.generic.type = AO_SEND_MINI;
288
289         telemetry.mini.state = ao_flight_state;
290
291         telemetry.mini.v_batt = packet->adc.v_batt;
292         telemetry.mini.sense_a = packet->adc.sense_a;
293         telemetry.mini.sense_m = packet->adc.sense_m;
294
295         telemetry.mini.pres = ao_data_pres(packet);
296         telemetry.mini.temp = ao_data_temp(packet);
297
298         telemetry.mini.acceleration = ao_accel;
299         telemetry.mini.speed = ao_speed;
300         telemetry.mini.height = ao_height;
301
302         telemetry.mini.ground_pres = ao_ground_pres;
303
304         ao_telemetry_send();
305 }
306
307 #endif /* AO_SEND_MINI */
308
309 static int8_t ao_telemetry_config_max;
310 static int8_t ao_telemetry_config_cur;
311 static uint16_t ao_telemetry_flight_number;
312
313 #ifndef ao_telemetry_battery_convert
314 #define ao_telemetry_battery_convert(a) (a)
315 #endif
316
317 static void
318 ao_send_configuration(void)
319 {
320         if (--ao_telemetry_config_cur <= 0)
321         {
322                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
323                 telemetry.configuration.device = AO_idProduct_NUMBER;
324                 telemetry.configuration.flight = ao_telemetry_flight_number;
325                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
326                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
327 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
328                 /* TeleGPS gets battery voltage instead of apogee delay */
329                 telemetry.configuration.apogee_delay = ao_telemetry_battery_convert(ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt);
330 #else
331                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
332                 telemetry.configuration.main_deploy = ao_config.main_deploy;
333 #endif
334
335                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
336                 ao_xmemcpy (telemetry.configuration.callsign,
337                             ao_config.callsign,
338                             AO_MAX_CALLSIGN);
339                 ao_xmemcpy (telemetry.configuration.version,
340                             ao_version,
341                             AO_MAX_VERSION);
342                 ao_telemetry_config_cur = ao_telemetry_config_max;
343                 ao_telemetry_send();
344         }
345 }
346
347 #if HAS_GPS
348
349 static int8_t ao_telemetry_gps_max;
350 static int8_t ao_telemetry_loc_cur;
351 static int8_t ao_telemetry_sat_cur;
352
353 static void
354 ao_send_location(void)
355 {
356         if (--ao_telemetry_loc_cur <= 0)
357         {
358                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
359                 ao_mutex_get(&ao_gps_mutex);
360                 ao_xmemcpy(&telemetry.location.flags,
361                        &ao_gps_data.flags,
362                        27);
363                 telemetry.location.tick = ao_gps_tick;
364                 ao_mutex_put(&ao_gps_mutex);
365                 ao_telemetry_loc_cur = ao_telemetry_gps_max;
366                 ao_telemetry_send();
367         }
368 }
369
370 static void
371 ao_send_satellite(void)
372 {
373         if (--ao_telemetry_sat_cur <= 0)
374         {
375                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
376                 ao_mutex_get(&ao_gps_mutex);
377                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
378                 ao_xmemcpy(&telemetry.satellite.sats,
379                        &ao_gps_tracking_data.sats,
380                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
381                 ao_mutex_put(&ao_gps_mutex);
382                 ao_telemetry_sat_cur = ao_telemetry_gps_max;
383                 ao_telemetry_send();
384         }
385 }
386 #endif
387
388 #if HAS_COMPANION
389
390 static int8_t ao_telemetry_companion_max;
391 static int8_t ao_telemetry_companion_cur;
392
393 static void
394 ao_send_companion(void)
395 {
396         if (--ao_telemetry_companion_cur <= 0) {
397                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
398                 telemetry.companion.board_id = ao_companion_setup.board_id;
399                 telemetry.companion.update_period = ao_companion_setup.update_period;
400                 telemetry.companion.channels = ao_companion_setup.channels;
401                 ao_mutex_get(&ao_companion_mutex);
402                 ao_xmemcpy(&telemetry.companion.companion_data,
403                        ao_companion_data,
404                        ao_companion_setup.channels * 2);
405                 ao_mutex_put(&ao_companion_mutex);
406                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
407                 ao_telemetry_send();
408         }
409 }
410 #endif
411
412 void
413 ao_telemetry(void)
414 {
415         uint16_t        time;
416         int16_t         delay;
417
418         ao_config_get();
419         if (!ao_config.radio_enable)
420                 ao_exit();
421         while (!ao_flight_number)
422                 ao_sleep(&ao_flight_number);
423
424         ao_telemetry_flight_number = ao_flight_number;
425 #if HAS_LOG
426         if (ao_log_full())
427                 ao_telemetry_flight_number = 0;
428 #endif
429         telemetry.generic.serial = ao_serial_number;
430         for (;;) {
431                 while (ao_telemetry_interval == 0)
432                         ao_sleep(&telemetry);
433                 time = ao_time();
434                 ao_telemetry_time = time;
435 #if HAS_RDF
436                 ao_rdf_time = time;
437 #endif
438 #if HAS_APRS
439                 ao_aprs_time = time;
440 #endif
441                 while (ao_telemetry_interval) {
442                         time = ao_time() + AO_SEC_TO_TICKS(100);
443 #ifndef SIMPLIFY
444                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
445 #endif
446                         {
447 #ifndef SIMPLIFY
448                                 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
449 #endif
450                                 {
451                                         ao_telemetry_time = ao_time() + ao_telemetry_interval;
452 # ifdef AO_SEND_MEGA
453                                         ao_send_mega_sensor();
454                                         ao_send_mega_data();
455 # endif
456 # ifdef AO_SEND_METRUM
457                                         ao_send_metrum_sensor();
458                                         ao_send_metrum_data();
459 # endif
460 # ifdef AO_SEND_MINI
461                                         ao_send_mini();
462 # endif
463 # ifdef AO_TELEMETRY_SENSOR
464                                         ao_send_sensor();
465 # endif
466 #if HAS_COMPANION
467                                         if (ao_companion_running)
468                                                 ao_send_companion();
469 #endif
470 #if HAS_GPS
471                                         ao_send_location();
472                                         ao_send_satellite();
473 #endif
474                                         ao_send_configuration();
475                                 }
476 #ifndef SIMPLIFY
477                                 time = ao_telemetry_time;
478 #endif
479                         }
480 #if HAS_RDF
481                         if (ao_rdf
482 #ifndef SIMPLIFY
483                             && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
484 #endif
485                                 )
486                         {
487                                 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
488 #if HAS_IGNITE_REPORT
489                                         uint8_t c;
490 #endif
491                                         ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
492 #if HAS_IGNITE_REPORT
493                                         if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
494                                                 ao_radio_continuity(c);
495                                         else
496 #endif
497                                                 ao_radio_rdf();
498                                 }
499 #ifndef SIMPLIFY
500                                 if ((int16_t) (time - ao_rdf_time) > 0)
501                                         time = ao_rdf_time;
502 #endif
503                         }
504 #endif /* HAS_RDF */
505 #if HAS_APRS
506                         if (ao_config.aprs_interval != 0) {
507                                 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
508                                         ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
509                                         ao_aprs_send();
510                                 }
511                                 if ((int16_t) (time - ao_aprs_time) > 0)
512                                         time = ao_aprs_time;
513                         }
514 #endif /* HAS_APRS */
515                         delay = time - ao_time();
516                         if (delay > 0) {
517                                 ao_sleep_for(&telemetry, delay);
518                         }
519                 }
520         }
521 }
522
523 #if HAS_RADIO_RATE
524 void
525 ao_telemetry_reset_interval(void)
526 {
527         ao_telemetry_set_interval(ao_telemetry_desired_interval);
528 }
529 #endif
530
531 void
532 ao_telemetry_set_interval(uint16_t interval)
533 {
534         int8_t  cur = 0;
535
536 #if HAS_RADIO_RATE
537         /* Limit max telemetry rate based on available radio bandwidth.
538          */
539         static const uint16_t min_interval[] = {
540                 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
541                 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
542                 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
543         };
544
545         ao_telemetry_desired_interval = interval;
546         if (interval && interval < min_interval[ao_config.radio_rate])
547                 interval = min_interval[ao_config.radio_rate];
548 #endif
549         ao_telemetry_interval = interval;
550 #if AO_SEND_MEGA
551         if (interval > 1)
552                 ao_telemetry_mega_data_max = 1;
553         else
554                 ao_telemetry_mega_data_max = 2;
555         if (ao_telemetry_mega_data_max > cur)
556                 cur++;
557         ao_telemetry_mega_data_cur = cur;
558 #endif
559 #if AO_SEND_METRUM
560         ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
561         if (ao_telemetry_metrum_data_max > cur)
562                 cur++;
563         ao_telemetry_metrum_data_cur = cur;
564 #endif
565
566 #if HAS_COMPANION
567         if (!ao_companion_setup.update_period)
568                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
569         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
570         if (ao_telemetry_companion_max > cur)
571                 cur++;
572         ao_telemetry_companion_cur = cur;
573 #endif
574
575 #if HAS_GPS
576         ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
577         if (ao_telemetry_gps_max > cur)
578                 cur++;
579         ao_telemetry_loc_cur = cur;
580         if (ao_telemetry_gps_max > cur)
581                 cur++;
582         ao_telemetry_sat_cur = cur;
583 #endif
584
585         ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
586         if (ao_telemetry_config_max > cur)
587                 cur++;
588         ao_telemetry_config_cur = cur;
589
590 #ifndef SIMPLIFY
591         ao_telemetry_time = 
592 #if HAS_RDF
593                 ao_rdf_time =
594 #endif
595 #if HAS_APRS
596                 ao_aprs_time =
597 #endif
598                 ao_time();
599 #endif
600         ao_wakeup(&telemetry);
601 }
602
603 #if HAS_RDF
604 void
605 ao_rdf_set(uint8_t rdf)
606 {
607         ao_rdf = rdf;
608         if (rdf == 0)
609                 ao_radio_rdf_abort();
610         else {
611                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
612         }
613 }
614 #endif
615
616 struct ao_task  ao_telemetry_task;
617
618 void
619 ao_telemetry_init()
620 {
621         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
622 }