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; 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 #if HAS_MPU6000
137         telemetry.mega_sensor.orient = ao_sample_orient;
138 #endif
139         telemetry.mega_sensor.accel = ao_data_accel(packet);
140         telemetry.mega_sensor.pres = ao_data_pres(packet);
141         telemetry.mega_sensor.temp = ao_data_temp(packet);
142
143 #if HAS_MPU6000
144         telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
145         telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
146         telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
147
148         telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
149         telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
150         telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
151 #endif
152
153 #if HAS_HMC5883
154         telemetry.mega_sensor.mag_x = packet->hmc5883.x;
155         telemetry.mega_sensor.mag_y = packet->hmc5883.y;
156         telemetry.mega_sensor.mag_z = packet->hmc5883.z;
157 #endif
158
159         ao_radio_send(&telemetry, sizeof (telemetry));
160 }
161
162 static __pdata int8_t ao_telemetry_mega_data_max;
163 static __pdata int8_t ao_telemetry_mega_data_cur;
164
165 /* Send mega data packet */
166 static void
167 ao_send_mega_data(void)
168 {
169         if (--ao_telemetry_mega_data_cur <= 0) {
170                 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
171                 uint8_t i;
172
173                 telemetry.generic.tick = packet->tick;
174                 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
175
176                 telemetry.mega_data.state = ao_flight_state;
177                 telemetry.mega_data.v_batt = packet->adc.v_batt;
178                 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
179
180                 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
181                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
182                         telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
183
184                 telemetry.mega_data.ground_pres = ao_ground_pres;
185                 telemetry.mega_data.ground_accel = ao_ground_accel;
186                 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
187                 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
188
189                 telemetry.mega_data.acceleration = ao_accel;
190                 telemetry.mega_data.speed = ao_speed;
191                 telemetry.mega_data.height = ao_height;
192
193                 ao_radio_send(&telemetry, sizeof (telemetry));
194                 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
195         }
196 }
197 #endif /* AO_SEND_MEGA */
198
199 #ifdef AO_SEND_METRUM
200 /* Send telemetrum sensor packet */
201 static void
202 ao_send_metrum_sensor(void)
203 {
204         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
205
206         telemetry.generic.tick = packet->tick;
207         telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
208
209         telemetry.metrum_sensor.state = ao_flight_state;
210 #if HAS_ACCEL
211         telemetry.metrum_sensor.accel = ao_data_accel(packet);
212 #endif
213         telemetry.metrum_sensor.pres = ao_data_pres(packet);
214         telemetry.metrum_sensor.temp = ao_data_temp(packet);
215
216         telemetry.metrum_sensor.acceleration = ao_accel;
217         telemetry.metrum_sensor.speed = ao_speed;
218         telemetry.metrum_sensor.height = ao_height;
219
220         telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
221         telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
222         telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
223
224         ao_radio_send(&telemetry, sizeof (telemetry));
225 }
226
227 static __pdata int8_t ao_telemetry_metrum_data_max;
228 static __pdata int8_t ao_telemetry_metrum_data_cur;
229
230 /* Send telemetrum data packet */
231 static void
232 ao_send_metrum_data(void)
233 {
234         if (--ao_telemetry_metrum_data_cur <= 0) {
235                 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
236
237                 telemetry.generic.tick = packet->tick;
238                 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
239
240                 telemetry.metrum_data.ground_pres = ao_ground_pres;
241 #if HAS_ACCEL
242                 telemetry.metrum_data.ground_accel = ao_ground_accel;
243                 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
244                 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
245 #else
246                 telemetry.metrum_data.ground_accel = 1;
247                 telemetry.metrum_data.accel_plus_g = 0;
248                 telemetry.metrum_data.accel_minus_g = 2;
249 #endif
250
251                 ao_radio_send(&telemetry, sizeof (telemetry));
252                 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
253         }
254 }
255 #endif /* AO_SEND_METRUM */
256
257 #ifdef AO_SEND_MINI
258
259 static void
260 ao_send_mini(void)
261 {
262         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
263
264         telemetry.generic.tick = packet->tick;
265         telemetry.generic.type = AO_TELEMETRY_MINI;
266
267         telemetry.mini.state = ao_flight_state;
268
269         telemetry.mini.v_batt = packet->adc.v_batt;
270         telemetry.mini.sense_a = packet->adc.sense_a;
271         telemetry.mini.sense_m = packet->adc.sense_m;
272
273         telemetry.mini.pres = ao_data_pres(packet);
274         telemetry.mini.temp = ao_data_temp(packet);
275
276         telemetry.mini.acceleration = ao_accel;
277         telemetry.mini.speed = ao_speed;
278         telemetry.mini.height = ao_height;
279
280         telemetry.mini.ground_pres = ao_ground_pres;
281
282         ao_radio_send(&telemetry, sizeof (telemetry));
283 }
284
285 #endif /* AO_SEND_MINI */
286
287 static __pdata int8_t ao_telemetry_config_max;
288 static __pdata int8_t ao_telemetry_config_cur;
289
290 static void
291 ao_send_configuration(void)
292 {
293         if (--ao_telemetry_config_cur <= 0)
294         {
295                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
296                 telemetry.configuration.device = AO_idProduct_NUMBER;
297 #if HAS_LOG
298                 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
299 #else
300                 telemetry.configuration.flight = ao_flight_number;
301 #endif
302                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
303                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
304 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
305                 /* TeleGPS gets battery voltage instead of apogee delay */
306                 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
307 #else
308                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
309                 telemetry.configuration.main_deploy = ao_config.main_deploy;
310 #endif
311
312                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
313                 ao_xmemcpy (telemetry.configuration.callsign,
314                             ao_config.callsign,
315                             AO_MAX_CALLSIGN);
316                 ao_xmemcpy (telemetry.configuration.version,
317                             CODE_TO_XDATA(ao_version),
318                             AO_MAX_VERSION);
319                 ao_radio_send(&telemetry, sizeof (telemetry));
320                 ao_telemetry_config_cur = ao_telemetry_config_max;
321         }
322 }
323
324 #if HAS_GPS
325
326 static __pdata int8_t ao_telemetry_gps_max;
327 static __pdata int8_t ao_telemetry_loc_cur;
328 static __pdata int8_t ao_telemetry_sat_cur;
329
330 static void
331 ao_send_location(void)
332 {
333         if (--ao_telemetry_loc_cur <= 0)
334         {
335                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
336                 ao_mutex_get(&ao_gps_mutex);
337                 ao_xmemcpy(&telemetry.location.flags,
338                        &ao_gps_data.flags,
339                        27);
340                 telemetry.location.tick = ao_gps_tick;
341                 ao_mutex_put(&ao_gps_mutex);
342                 ao_radio_send(&telemetry, sizeof (telemetry));
343                 ao_telemetry_loc_cur = ao_telemetry_gps_max;
344         }
345 }
346
347 static void
348 ao_send_satellite(void)
349 {
350         if (--ao_telemetry_sat_cur <= 0)
351         {
352                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
353                 ao_mutex_get(&ao_gps_mutex);
354                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
355                 ao_xmemcpy(&telemetry.satellite.sats,
356                        &ao_gps_tracking_data.sats,
357                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
358                 ao_mutex_put(&ao_gps_mutex);
359                 ao_radio_send(&telemetry, sizeof (telemetry));
360                 ao_telemetry_sat_cur = ao_telemetry_gps_max;
361         }
362 }
363 #endif
364
365 #if HAS_COMPANION
366
367 static __pdata int8_t ao_telemetry_companion_max;
368 static __pdata int8_t ao_telemetry_companion_cur;
369
370 static void
371 ao_send_companion(void)
372 {
373         if (--ao_telemetry_companion_cur <= 0) {
374                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
375                 telemetry.companion.board_id = ao_companion_setup.board_id;
376                 telemetry.companion.update_period = ao_companion_setup.update_period;
377                 telemetry.companion.channels = ao_companion_setup.channels;
378                 ao_mutex_get(&ao_companion_mutex);
379                 ao_xmemcpy(&telemetry.companion.companion_data,
380                        ao_companion_data,
381                        ao_companion_setup.channels * 2);
382                 ao_mutex_put(&ao_companion_mutex);
383                 ao_radio_send(&telemetry, sizeof (telemetry));
384                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
385         }
386 }
387 #endif
388
389 void
390 ao_telemetry(void)
391 {
392         uint16_t        time;
393         int16_t         delay;
394
395         ao_config_get();
396         if (!ao_config.radio_enable)
397                 ao_exit();
398         while (!ao_flight_number)
399                 ao_sleep(&ao_flight_number);
400
401         telemetry.generic.serial = ao_serial_number;
402         for (;;) {
403                 while (ao_telemetry_interval == 0)
404                         ao_sleep(&telemetry);
405                 time = ao_time();
406                 ao_telemetry_time = time;
407 #if HAS_RDF
408                 ao_rdf_time = time;
409 #endif
410 #if HAS_APRS
411                 ao_aprs_time = time;
412 #endif
413                 while (ao_telemetry_interval) {
414                         time = ao_time() + AO_SEC_TO_TICKS(100);
415 #ifndef SIMPLIFY
416                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
417 #endif
418                         {
419 #ifndef SIMPLIFY
420                                 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
421 #endif
422                                 {
423                                         ao_telemetry_time = ao_time() + ao_telemetry_interval;
424 # ifdef AO_SEND_MEGA
425                                         ao_send_mega_sensor();
426                                         ao_send_mega_data();
427 # endif
428 # ifdef AO_SEND_METRUM
429                                         ao_send_metrum_sensor();
430                                         ao_send_metrum_data();
431 # endif
432 # ifdef AO_SEND_MINI
433                                         ao_send_mini();
434 # endif
435 # ifdef AO_TELEMETRY_SENSOR
436                                         ao_send_sensor();
437 # endif
438 #if HAS_COMPANION
439                                         if (ao_companion_running)
440                                                 ao_send_companion();
441 #endif
442 #if HAS_GPS
443                                         ao_send_location();
444                                         ao_send_satellite();
445 #endif
446                                         ao_send_configuration();
447                                 }
448 #ifndef SIMPLIFY
449                                 time = ao_telemetry_time;
450 #endif
451                         }
452 #if HAS_RDF
453                         if (ao_rdf
454 #ifndef SIMPLIFY
455                             && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
456 #endif
457                                 )
458                         {
459                                 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
460 #if HAS_IGNITE_REPORT
461                                         uint8_t c;
462 #endif
463                                         ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
464 #if HAS_IGNITE_REPORT
465                                         if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
466                                                 ao_radio_continuity(c);
467                                         else
468 #endif
469                                                 ao_radio_rdf();
470                                 }
471 #ifndef SIMPLIFY
472                                 if ((int16_t) (time - ao_rdf_time) > 0)
473                                         time = ao_rdf_time;
474 #endif
475                         }
476 #endif /* HAS_RDF */
477 #if HAS_APRS
478                         if (ao_config.aprs_interval != 0) {
479                                 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
480                                         ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
481                                         ao_aprs_send();
482                                 }
483                                 if ((int16_t) (time - ao_aprs_time) > 0)
484                                         time = ao_aprs_time;
485                         }
486 #endif /* HAS_APRS */
487                         delay = time - ao_time();
488                         if (delay > 0) {
489                                 ao_sleep_for(&telemetry, delay);
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 }