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 AO_TICK_TYPE ao_telemetry_time;
47 static RDF_SPACE uint8_t ao_rdf = 0;
48 static RDF_SPACE AO_TICK_TYPE ao_rdf_time;
52 static AO_TICK_TYPE ao_aprs_time;
58 #define AO_SEND_MEGA 1
61 #if defined (TELEMETRUM_V_2_0) || defined (TELEMETRUM_V_3_0) || defined (TELEMETRUM_V_4_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 = (uint16_t) 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;
145 #elif AO_LOG_FORMAT == AO_LOG_FORMAT_TELEMEGA_6
146 telemetry.generic.type = AO_TELEMETRY_MEGA_NORM_BMI088_MMC5983;
148 #error unknown normalized log type
152 telemetry.mega_norm.orient = (uint8_t) ao_sample_orient;
154 telemetry.mega_norm.accel = ao_data_accel(packet);
155 telemetry.mega_norm.pres = ao_data_pres(packet);
156 telemetry.mega_norm.temp = ao_data_temp(packet);
159 telemetry.mega_norm.accel_along = ao_data_along(packet);
160 telemetry.mega_norm.accel_across = ao_data_across(packet);
161 telemetry.mega_norm.accel_through = ao_data_through(packet);
163 telemetry.mega_norm.gyro_roll = ao_data_roll(packet);
164 telemetry.mega_norm.gyro_pitch = ao_data_pitch(packet);
165 telemetry.mega_norm.gyro_yaw = ao_data_yaw(packet);
168 #ifdef ao_data_mag_along
169 telemetry.mega_norm.mag_along = ao_data_mag_along(packet);
170 telemetry.mega_norm.mag_across = ao_data_mag_across(packet);
171 telemetry.mega_norm.mag_through = ao_data_mag_through(packet);
177 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR_BMX160;
179 #if HAS_MPU6000 || HAS_MPU9250
180 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR_MPU;
187 telemetry.mega_sensor.orient = (uint8_t) ao_sample_orient;
189 telemetry.mega_sensor.accel = ao_data_accel(packet);
190 telemetry.mega_sensor.pres = ao_data_pres(packet);
191 telemetry.mega_sensor.temp = ao_data_temp(packet);
194 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
195 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
196 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
198 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
199 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
200 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
204 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
205 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
206 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
210 telemetry.mega_sensor.accel_x = packet->mpu9250.accel_x;
211 telemetry.mega_sensor.accel_y = packet->mpu9250.accel_y;
212 telemetry.mega_sensor.accel_z = packet->mpu9250.accel_z;
214 telemetry.mega_sensor.gyro_x = packet->mpu9250.gyro_x;
215 telemetry.mega_sensor.gyro_y = packet->mpu9250.gyro_y;
216 telemetry.mega_sensor.gyro_z = packet->mpu9250.gyro_z;
218 telemetry.mega_sensor.mag_x = packet->mpu9250.mag_x;
219 telemetry.mega_sensor.mag_z = packet->mpu9250.mag_z;
220 telemetry.mega_sensor.mag_y = packet->mpu9250.mag_y;
224 telemetry.mega_sensor.accel_x = packet->bmx160.acc_x;
225 telemetry.mega_sensor.accel_y = packet->bmx160.acc_y;
226 telemetry.mega_sensor.accel_z = packet->bmx160.acc_z;
228 telemetry.mega_sensor.gyro_x = packet->bmx160.gyr_x;
229 telemetry.mega_sensor.gyro_y = packet->bmx160.gyr_y;
230 telemetry.mega_sensor.gyro_z = packet->bmx160.gyr_z;
232 telemetry.mega_sensor.mag_x = packet->bmx160.mag_x;
233 telemetry.mega_sensor.mag_z = packet->bmx160.mag_z;
234 telemetry.mega_sensor.mag_y = packet->bmx160.mag_y;
240 static int16_t ao_telemetry_mega_data_max;
241 static int16_t ao_telemetry_mega_data_cur;
243 /* Send mega data packet */
245 ao_send_mega_data(void)
247 if (--ao_telemetry_mega_data_cur <= 0) {
248 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
251 telemetry.generic.tick = (uint16_t) packet->tick;
252 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
254 telemetry.mega_data.state = ao_flight_state;
255 telemetry.mega_data.v_batt = packet->adc.v_batt;
256 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
258 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
259 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
260 telemetry.mega_data.sense[i] = (int8_t) (packet->adc.sense[i] >> 4);
262 telemetry.mega_data.ground_pres = ao_ground_pres;
263 telemetry.mega_data.ground_accel = ao_ground_accel;
264 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
265 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
267 telemetry.mega_data.acceleration = (int16_t) ao_accel;
268 telemetry.mega_data.speed = (int16_t) ao_speed;
269 telemetry.mega_data.height = (int16_t) ao_height;
271 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
275 #endif /* AO_SEND_MEGA */
277 #ifdef AO_SEND_METRUM
278 /* Send telemetrum sensor packet */
280 ao_send_metrum_sensor(void)
282 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
284 telemetry.generic.tick = (uint16_t) packet->tick;
285 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
287 telemetry.metrum_sensor.state = ao_flight_state;
289 telemetry.metrum_sensor.accel = ao_data_accel(packet);
291 telemetry.metrum_sensor.pres = ao_data_pres(packet);
292 telemetry.metrum_sensor.temp = ao_data_temp(packet);
294 telemetry.metrum_sensor.acceleration = (int16_t) ao_accel;
295 telemetry.metrum_sensor.speed = (int16_t) ao_speed;
296 telemetry.metrum_sensor.height = (int16_t) ao_height;
298 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
299 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
300 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
305 static int16_t ao_telemetry_metrum_data_max;
306 static int16_t ao_telemetry_metrum_data_cur;
308 /* Send telemetrum data packet */
310 ao_send_metrum_data(void)
312 if (--ao_telemetry_metrum_data_cur <= 0) {
313 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
315 telemetry.generic.tick = (uint16_t) packet->tick;
316 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
318 telemetry.metrum_data.ground_pres = ao_ground_pres;
320 telemetry.metrum_data.ground_accel = ao_ground_accel;
321 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
322 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
324 telemetry.metrum_data.ground_accel = 1;
325 telemetry.metrum_data.accel_plus_g = 0;
326 telemetry.metrum_data.accel_minus_g = 2;
329 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
333 #endif /* AO_SEND_METRUM */
340 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
342 telemetry.generic.tick = (uint16_t) packet->tick;
343 telemetry.generic.type = AO_SEND_MINI;
345 telemetry.mini.state = ao_flight_state;
347 telemetry.mini.v_batt = packet->adc.v_batt;
348 telemetry.mini.sense_a = packet->adc.sense_a;
349 telemetry.mini.sense_m = packet->adc.sense_m;
351 telemetry.mini.pres = ao_data_pres(packet);
352 telemetry.mini.temp = ao_data_temp(packet);
354 telemetry.mini.acceleration = (int16_t) ao_accel;
355 telemetry.mini.speed = (int16_t) ao_speed;
356 telemetry.mini.height = (int16_t) ao_height;
358 telemetry.mini.ground_pres = ao_ground_pres;
363 #endif /* AO_SEND_MINI */
365 static int16_t ao_telemetry_config_max;
366 static int16_t ao_telemetry_config_cur;
367 static uint16_t ao_telemetry_flight_number;
369 #ifndef ao_telemetry_battery_convert
370 #define ao_telemetry_battery_convert(a) (a)
374 ao_send_configuration(void)
376 if (--ao_telemetry_config_cur <= 0)
378 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
379 telemetry.configuration.device = AO_idProduct_NUMBER;
380 telemetry.configuration.flight = ao_telemetry_flight_number;
381 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
382 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
383 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
384 /* TeleGPS gets battery voltage instead of apogee delay */
385 telemetry.configuration.apogee_delay = (uint16_t) ao_telemetry_battery_convert(ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt);
387 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
388 telemetry.configuration.main_deploy = ao_config.main_deploy;
391 telemetry.configuration.flight_log_max = (uint16_t) (ao_config.flight_log_max >> 10);
392 memcpy (telemetry.configuration.callsign,
395 memcpy (telemetry.configuration.version,
398 ao_telemetry_config_cur = ao_telemetry_config_max;
405 static int16_t ao_telemetry_gps_max;
406 static int16_t ao_telemetry_loc_cur;
407 static int16_t ao_telemetry_sat_cur;
410 telemetry_bits(struct ao_telemetry_location *l)
412 return ((uint8_t *) l) + offsetof(struct ao_telemetry_location, flags);
418 return sizeof(struct ao_telemetry_location) - offsetof(struct ao_telemetry_location, flags);
422 ao_send_location(void)
424 if (--ao_telemetry_loc_cur <= 0)
426 telemetry.generic.type = AO_TELEMETRY_LOCATION;
427 ao_mutex_get(&ao_gps_mutex);
428 memcpy(telemetry_bits(&telemetry.location),
429 telemetry_bits(&ao_gps_data),
431 telemetry.location.tick = (uint16_t) ao_gps_tick;
432 ao_mutex_put(&ao_gps_mutex);
433 ao_telemetry_loc_cur = ao_telemetry_gps_max;
439 ao_send_satellite(void)
441 if (--ao_telemetry_sat_cur <= 0)
443 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
444 ao_mutex_get(&ao_gps_mutex);
445 telemetry.satellite.channels = ao_gps_tracking_data.channels;
446 memcpy(&telemetry.satellite.sats,
447 &ao_gps_tracking_data.sats,
448 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
449 ao_mutex_put(&ao_gps_mutex);
450 ao_telemetry_sat_cur = ao_telemetry_gps_max;
458 static int16_t ao_telemetry_companion_max;
459 static int16_t ao_telemetry_companion_cur;
462 ao_send_companion(void)
464 if (--ao_telemetry_companion_cur <= 0) {
465 telemetry.generic.type = AO_TELEMETRY_COMPANION;
466 telemetry.companion.board_id = (uint8_t) ao_companion_setup.board_id;
467 telemetry.companion.update_period = ao_companion_setup.update_period;
468 telemetry.companion.channels = ao_companion_setup.channels;
469 ao_mutex_get(&ao_companion_mutex);
470 memcpy(&telemetry.companion.companion_data,
472 ao_companion_setup.channels * 2);
473 ao_mutex_put(&ao_companion_mutex);
474 ao_telemetry_companion_cur = ao_telemetry_companion_max;
482 ao_set_aprs_time(void)
484 uint16_t interval = ao_config.aprs_interval;
486 if ((ao_gps_data.flags & AO_GPS_DATE_VALID) && interval != 0) {
487 int second = (ao_gps_data.second / interval + 1) * interval + ao_config.aprs_offset;
490 second = ao_config.aprs_offset;
491 delta = second + 60 - ao_gps_data.second;
493 delta = second - ao_gps_data.second;
495 if (delta < (interval >> 1))
498 ao_aprs_time = ao_gps_utc_tick + AO_SEC_TO_TICKS(delta);
500 ao_aprs_time += AO_SEC_TO_TICKS(ao_config.aprs_interval);
509 AO_TICK_SIGNED delay;
512 if (!ao_config.radio_enable)
514 while (!ao_flight_number)
515 ao_sleep(&ao_flight_number);
517 ao_telemetry_flight_number = ao_flight_number;
520 ao_telemetry_flight_number = 0;
522 telemetry.generic.serial = ao_serial_number;
524 while (ao_telemetry_interval == 0)
525 ao_sleep(&telemetry);
527 ao_telemetry_time = time;
535 while (ao_telemetry_interval) {
536 time = ao_time() + AO_SEC_TO_TICKS(100);
538 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
542 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
545 ao_telemetry_time = ao_time() + ao_telemetry_interval;
547 ao_send_mega_sensor();
550 # ifdef AO_SEND_METRUM
551 ao_send_metrum_sensor();
552 ao_send_metrum_data();
557 # ifdef AO_TELEMETRY_SENSOR
561 if (ao_companion_running)
568 ao_send_configuration();
571 time = ao_telemetry_time;
577 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
581 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
582 #if HAS_IGNITE_REPORT
585 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
586 #if HAS_IGNITE_REPORT
587 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
588 ao_radio_continuity(c);
594 if ((int16_t) (time - ao_rdf_time) > 0)
600 if (ao_config.aprs_interval != 0) {
601 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
605 if ((int16_t) (time - ao_aprs_time) > 0)
608 #endif /* HAS_APRS */
609 delay = (AO_TICK_SIGNED) (time - ao_time());
611 ao_sleep_for(&telemetry, (AO_TICK_TYPE) delay);
619 ao_telemetry_reset_interval(void)
621 ao_telemetry_set_interval(ao_telemetry_desired_interval);
626 ao_telemetry_set_interval(uint16_t interval)
631 /* Limit max telemetry rate based on available radio bandwidth.
633 static const uint16_t min_interval[] = {
634 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
635 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
636 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
639 ao_telemetry_desired_interval = interval;
640 if (interval && interval < min_interval[ao_config.radio_rate])
641 interval = min_interval[ao_config.radio_rate];
643 ao_telemetry_interval = interval;
647 ao_telemetry_mega_data_max = 1;
649 ao_telemetry_mega_data_max = 2;
650 if (ao_telemetry_mega_data_max > cur)
652 ao_telemetry_mega_data_cur = cur;
655 ao_telemetry_metrum_data_max = (int16_t) (AO_SEC_TO_TICKS(1) / interval);
656 if (ao_telemetry_metrum_data_max > cur)
658 ao_telemetry_metrum_data_cur = cur;
662 if (!ao_companion_setup.update_period)
663 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
664 ao_telemetry_companion_max = (int16_t) (ao_companion_setup.update_period / interval);
665 if (ao_telemetry_companion_max > cur)
667 ao_telemetry_companion_cur = cur;
671 ao_telemetry_gps_max = (int16_t) (AO_SEC_TO_TICKS(1) / interval);
672 if (ao_telemetry_gps_max > cur)
674 ao_telemetry_loc_cur = cur;
675 if (ao_telemetry_gps_max > cur)
677 ao_telemetry_sat_cur = cur;
680 ao_telemetry_config_max = (int16_t) (AO_SEC_TO_TICKS(5) / interval);
681 if (ao_telemetry_config_max > cur)
683 ao_telemetry_config_cur = cur;
696 ao_wakeup(&telemetry);
701 ao_rdf_set(uint8_t rdf)
705 ao_radio_rdf_abort();
707 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
712 struct ao_task ao_telemetry_task;
717 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");