2 * Copyright © 2011 Keth Packard <keithp@keithp.com>
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.
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.
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.
21 #include "ao_product.h"
23 static uint16_t ao_telemetry_interval;
26 static uint16_t ao_telemetry_desired_interval;
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
34 #if defined(TELEMETRUM_V_1_0)
39 #define ao_telemetry_time time
43 static uint16_t ao_telemetry_time;
47 static RDF_SPACE uint8_t ao_rdf = 0;
48 static RDF_SPACE uint16_t ao_rdf_time;
52 static uint16_t ao_aprs_time;
58 #define AO_SEND_MEGA 1
61 #if defined (TELEMETRUM_V_2_0) || defined (TELEMETRUM_V_3_0)
62 #define AO_SEND_METRUM 1
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
69 #if defined(TELEMINI_V_1_0)
70 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
73 #if defined(TELENANO_V_0_1)
74 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
77 static union ao_telemetry_all telemetry;
80 ao_telemetry_send(void)
82 ao_radio_send(&telemetry, sizeof (telemetry));
86 #if defined AO_TELEMETRY_SENSOR
87 /* Send sensor packet */
91 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
93 telemetry.generic.tick = packet->tick;
94 telemetry.generic.type = AO_TELEMETRY_SENSOR;
96 telemetry.sensor.state = ao_flight_state;
98 telemetry.sensor.accel = packet->adc.accel;
100 telemetry.sensor.accel = 0;
102 telemetry.sensor.pres = ao_data_pres(packet);
103 telemetry.sensor.temp = packet->adc.temp;
104 telemetry.sensor.v_batt = packet->adc.v_batt;
106 telemetry.sensor.sense_d = packet->adc.sense_d;
107 telemetry.sensor.sense_m = packet->adc.sense_m;
109 telemetry.sensor.sense_d = 0;
110 telemetry.sensor.sense_m = 0;
113 telemetry.sensor.acceleration = ao_accel;
114 telemetry.sensor.speed = ao_speed;
115 telemetry.sensor.height = ao_height;
117 telemetry.sensor.ground_pres = ao_ground_pres;
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;
123 telemetry.sensor.ground_accel = 0;
124 telemetry.sensor.accel_plus_g = 0;
125 telemetry.sensor.accel_minus_g = 0;
135 /* Send mega sensor packet */
137 ao_send_mega_sensor(void)
139 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
141 telemetry.generic.tick = packet->tick;
142 #if AO_LOG_NORMALIZED
143 #if AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA_5
144 telemetry.generic.type = AO_TELEMETRY_MEGA_NORM_MPU6000_MMC5983;
146 #error unknown normalized log type
150 telemetry.mega_norm.orient = ao_sample_orient;
152 telemetry.mega_norm.accel = ao_data_accel(packet);
153 telemetry.mega_norm.pres = ao_data_pres(packet);
154 telemetry.mega_norm.temp = ao_data_temp(packet);
157 telemetry.mega_norm.accel_along = ao_data_along(packet);
158 telemetry.mega_norm.accel_across = ao_data_across(packet);
159 telemetry.mega_norm.accel_through = ao_data_through(packet);
161 telemetry.mega_norm.gyro_roll = ao_data_roll(packet);
162 telemetry.mega_norm.gyro_pitch = ao_data_pitch(packet);
163 telemetry.mega_norm.gyro_yaw = ao_data_yaw(packet);
167 telemetry.mega_norm.mag_along = ao_data_mag_along(packet);
168 telemetry.mega_norm.mag_across = ao_data_mag_across(packet);
169 telemetry.mega_norm.mag_through = ao_data_mag_through(packet);
175 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR_BMX160;
177 #if HAS_MPU6000 || HAS_MPU9250
178 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR_MPU;
185 telemetry.mega_sensor.orient = ao_sample_orient;
187 telemetry.mega_sensor.accel = ao_data_accel(packet);
188 telemetry.mega_sensor.pres = ao_data_pres(packet);
189 telemetry.mega_sensor.temp = ao_data_temp(packet);
192 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
193 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
194 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
196 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
197 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
198 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
202 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
203 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
204 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
208 telemetry.mega_sensor.accel_x = packet->mpu9250.accel_x;
209 telemetry.mega_sensor.accel_y = packet->mpu9250.accel_y;
210 telemetry.mega_sensor.accel_z = packet->mpu9250.accel_z;
212 telemetry.mega_sensor.gyro_x = packet->mpu9250.gyro_x;
213 telemetry.mega_sensor.gyro_y = packet->mpu9250.gyro_y;
214 telemetry.mega_sensor.gyro_z = packet->mpu9250.gyro_z;
216 telemetry.mega_sensor.mag_x = packet->mpu9250.mag_x;
217 telemetry.mega_sensor.mag_z = packet->mpu9250.mag_z;
218 telemetry.mega_sensor.mag_y = packet->mpu9250.mag_y;
222 telemetry.mega_sensor.accel_x = packet->bmx160.acc_x;
223 telemetry.mega_sensor.accel_y = packet->bmx160.acc_y;
224 telemetry.mega_sensor.accel_z = packet->bmx160.acc_z;
226 telemetry.mega_sensor.gyro_x = packet->bmx160.gyr_x;
227 telemetry.mega_sensor.gyro_y = packet->bmx160.gyr_y;
228 telemetry.mega_sensor.gyro_z = packet->bmx160.gyr_z;
230 telemetry.mega_sensor.mag_x = packet->bmx160.mag_x;
231 telemetry.mega_sensor.mag_z = packet->bmx160.mag_z;
232 telemetry.mega_sensor.mag_y = packet->bmx160.mag_y;
238 static int8_t ao_telemetry_mega_data_max;
239 static int8_t ao_telemetry_mega_data_cur;
241 /* Send mega data packet */
243 ao_send_mega_data(void)
245 if (--ao_telemetry_mega_data_cur <= 0) {
246 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
249 telemetry.generic.tick = packet->tick;
250 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
252 telemetry.mega_data.state = ao_flight_state;
253 telemetry.mega_data.v_batt = packet->adc.v_batt;
254 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
256 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
257 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
258 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
260 telemetry.mega_data.ground_pres = ao_ground_pres;
261 telemetry.mega_data.ground_accel = ao_ground_accel;
262 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
263 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
265 telemetry.mega_data.acceleration = ao_accel;
266 telemetry.mega_data.speed = ao_speed;
267 telemetry.mega_data.height = ao_height;
269 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
273 #endif /* AO_SEND_MEGA */
275 #ifdef AO_SEND_METRUM
276 /* Send telemetrum sensor packet */
278 ao_send_metrum_sensor(void)
280 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
282 telemetry.generic.tick = packet->tick;
283 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
285 telemetry.metrum_sensor.state = ao_flight_state;
287 telemetry.metrum_sensor.accel = ao_data_accel(packet);
289 telemetry.metrum_sensor.pres = ao_data_pres(packet);
290 telemetry.metrum_sensor.temp = ao_data_temp(packet);
292 telemetry.metrum_sensor.acceleration = ao_accel;
293 telemetry.metrum_sensor.speed = ao_speed;
294 telemetry.metrum_sensor.height = ao_height;
296 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
297 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
298 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
303 static int8_t ao_telemetry_metrum_data_max;
304 static int8_t ao_telemetry_metrum_data_cur;
306 /* Send telemetrum data packet */
308 ao_send_metrum_data(void)
310 if (--ao_telemetry_metrum_data_cur <= 0) {
311 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
313 telemetry.generic.tick = packet->tick;
314 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
316 telemetry.metrum_data.ground_pres = ao_ground_pres;
318 telemetry.metrum_data.ground_accel = ao_ground_accel;
319 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
320 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
322 telemetry.metrum_data.ground_accel = 1;
323 telemetry.metrum_data.accel_plus_g = 0;
324 telemetry.metrum_data.accel_minus_g = 2;
327 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
331 #endif /* AO_SEND_METRUM */
338 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
340 telemetry.generic.tick = packet->tick;
341 telemetry.generic.type = AO_SEND_MINI;
343 telemetry.mini.state = ao_flight_state;
345 telemetry.mini.v_batt = packet->adc.v_batt;
346 telemetry.mini.sense_a = packet->adc.sense_a;
347 telemetry.mini.sense_m = packet->adc.sense_m;
349 telemetry.mini.pres = ao_data_pres(packet);
350 telemetry.mini.temp = ao_data_temp(packet);
352 telemetry.mini.acceleration = ao_accel;
353 telemetry.mini.speed = ao_speed;
354 telemetry.mini.height = ao_height;
356 telemetry.mini.ground_pres = ao_ground_pres;
361 #endif /* AO_SEND_MINI */
363 static int8_t ao_telemetry_config_max;
364 static int8_t ao_telemetry_config_cur;
365 static uint16_t ao_telemetry_flight_number;
367 #ifndef ao_telemetry_battery_convert
368 #define ao_telemetry_battery_convert(a) (a)
372 ao_send_configuration(void)
374 if (--ao_telemetry_config_cur <= 0)
376 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
377 telemetry.configuration.device = AO_idProduct_NUMBER;
378 telemetry.configuration.flight = ao_telemetry_flight_number;
379 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
380 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
381 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
382 /* TeleGPS gets battery voltage instead of apogee delay */
383 telemetry.configuration.apogee_delay = ao_telemetry_battery_convert(ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt);
385 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
386 telemetry.configuration.main_deploy = ao_config.main_deploy;
389 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
390 memcpy (telemetry.configuration.callsign,
393 memcpy (telemetry.configuration.version,
396 ao_telemetry_config_cur = ao_telemetry_config_max;
403 static int8_t ao_telemetry_gps_max;
404 static int8_t ao_telemetry_loc_cur;
405 static int8_t ao_telemetry_sat_cur;
408 telemetry_bits(struct ao_telemetry_location *l)
410 return ((uint8_t *) l) + offsetof(struct ao_telemetry_location, flags);
416 return sizeof(struct ao_telemetry_location) - offsetof(struct ao_telemetry_location, flags);
420 ao_send_location(void)
422 if (--ao_telemetry_loc_cur <= 0)
424 telemetry.generic.type = AO_TELEMETRY_LOCATION;
425 ao_mutex_get(&ao_gps_mutex);
426 memcpy(telemetry_bits(&telemetry.location),
427 telemetry_bits(&ao_gps_data),
429 telemetry.location.tick = ao_gps_tick;
430 ao_mutex_put(&ao_gps_mutex);
431 ao_telemetry_loc_cur = ao_telemetry_gps_max;
437 ao_send_satellite(void)
439 if (--ao_telemetry_sat_cur <= 0)
441 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
442 ao_mutex_get(&ao_gps_mutex);
443 telemetry.satellite.channels = ao_gps_tracking_data.channels;
444 memcpy(&telemetry.satellite.sats,
445 &ao_gps_tracking_data.sats,
446 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
447 ao_mutex_put(&ao_gps_mutex);
448 ao_telemetry_sat_cur = ao_telemetry_gps_max;
456 static int8_t ao_telemetry_companion_max;
457 static int8_t ao_telemetry_companion_cur;
460 ao_send_companion(void)
462 if (--ao_telemetry_companion_cur <= 0) {
463 telemetry.generic.type = AO_TELEMETRY_COMPANION;
464 telemetry.companion.board_id = ao_companion_setup.board_id;
465 telemetry.companion.update_period = ao_companion_setup.update_period;
466 telemetry.companion.channels = ao_companion_setup.channels;
467 ao_mutex_get(&ao_companion_mutex);
468 memcpy(&telemetry.companion.companion_data,
470 ao_companion_setup.channels * 2);
471 ao_mutex_put(&ao_companion_mutex);
472 ao_telemetry_companion_cur = ao_telemetry_companion_max;
480 ao_set_aprs_time(void)
482 uint16_t interval = ao_config.aprs_interval;
484 if ((ao_gps_data.flags & AO_GPS_DATE_VALID) && interval != 0) {
485 int second = (ao_gps_data.second / interval + 1) * interval + ao_config.aprs_offset;
488 second = ao_config.aprs_offset;
489 delta = second + 60 - ao_gps_data.second;
491 delta = second - ao_gps_data.second;
493 ao_aprs_time = ao_gps_tick + AO_SEC_TO_TICKS(delta);
495 ao_aprs_time += AO_SEC_TO_TICKS(ao_config.aprs_interval);
507 if (!ao_config.radio_enable)
509 while (!ao_flight_number)
510 ao_sleep(&ao_flight_number);
512 ao_telemetry_flight_number = ao_flight_number;
515 ao_telemetry_flight_number = 0;
517 telemetry.generic.serial = ao_serial_number;
519 while (ao_telemetry_interval == 0)
520 ao_sleep(&telemetry);
522 ao_telemetry_time = time;
530 while (ao_telemetry_interval) {
531 time = ao_time() + AO_SEC_TO_TICKS(100);
533 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
537 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
540 ao_telemetry_time = ao_time() + ao_telemetry_interval;
542 ao_send_mega_sensor();
545 # ifdef AO_SEND_METRUM
546 ao_send_metrum_sensor();
547 ao_send_metrum_data();
552 # ifdef AO_TELEMETRY_SENSOR
556 if (ao_companion_running)
563 ao_send_configuration();
566 time = ao_telemetry_time;
572 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
576 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
577 #if HAS_IGNITE_REPORT
580 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
581 #if HAS_IGNITE_REPORT
582 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
583 ao_radio_continuity(c);
589 if ((int16_t) (time - ao_rdf_time) > 0)
595 if (ao_config.aprs_interval != 0) {
596 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
600 if ((int16_t) (time - ao_aprs_time) > 0)
603 #endif /* HAS_APRS */
604 delay = time - ao_time();
606 ao_sleep_for(&telemetry, delay);
614 ao_telemetry_reset_interval(void)
616 ao_telemetry_set_interval(ao_telemetry_desired_interval);
621 ao_telemetry_set_interval(uint16_t interval)
626 /* Limit max telemetry rate based on available radio bandwidth.
628 static const uint16_t min_interval[] = {
629 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
630 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
631 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
634 ao_telemetry_desired_interval = interval;
635 if (interval && interval < min_interval[ao_config.radio_rate])
636 interval = min_interval[ao_config.radio_rate];
638 ao_telemetry_interval = interval;
641 ao_telemetry_mega_data_max = 1;
643 ao_telemetry_mega_data_max = 2;
644 if (ao_telemetry_mega_data_max > cur)
646 ao_telemetry_mega_data_cur = cur;
649 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
650 if (ao_telemetry_metrum_data_max > cur)
652 ao_telemetry_metrum_data_cur = cur;
656 if (!ao_companion_setup.update_period)
657 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
658 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
659 if (ao_telemetry_companion_max > cur)
661 ao_telemetry_companion_cur = cur;
665 ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
666 if (ao_telemetry_gps_max > cur)
668 ao_telemetry_loc_cur = cur;
669 if (ao_telemetry_gps_max > cur)
671 ao_telemetry_sat_cur = cur;
674 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
675 if (ao_telemetry_config_max > cur)
677 ao_telemetry_config_cur = cur;
689 ao_wakeup(&telemetry);
694 ao_rdf_set(uint8_t rdf)
698 ao_radio_rdf_abort();
700 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
705 struct ao_task ao_telemetry_task;
710 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");