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;
143 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR_BMX160;
145 #if HAS_MPU6000 || HAS_MPU9250
146 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR_MPU;
153 telemetry.mega_sensor.orient = ao_sample_orient;
155 telemetry.mega_sensor.accel = ao_data_accel(packet);
156 telemetry.mega_sensor.pres = ao_data_pres(packet);
157 telemetry.mega_sensor.temp = ao_data_temp(packet);
160 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
161 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
162 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
164 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
165 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
166 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
170 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
171 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
172 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
176 telemetry.mega_sensor.accel_x = packet->mpu9250.accel_x;
177 telemetry.mega_sensor.accel_y = packet->mpu9250.accel_y;
178 telemetry.mega_sensor.accel_z = packet->mpu9250.accel_z;
180 telemetry.mega_sensor.gyro_x = packet->mpu9250.gyro_x;
181 telemetry.mega_sensor.gyro_y = packet->mpu9250.gyro_y;
182 telemetry.mega_sensor.gyro_z = packet->mpu9250.gyro_z;
184 telemetry.mega_sensor.mag_x = packet->mpu9250.mag_x;
185 telemetry.mega_sensor.mag_z = packet->mpu9250.mag_z;
186 telemetry.mega_sensor.mag_y = packet->mpu9250.mag_y;
190 telemetry.mega_sensor.accel_x = packet->bmx160.acc_x;
191 telemetry.mega_sensor.accel_y = packet->bmx160.acc_y;
192 telemetry.mega_sensor.accel_z = packet->bmx160.acc_z;
194 telemetry.mega_sensor.gyro_x = packet->bmx160.gyr_x;
195 telemetry.mega_sensor.gyro_y = packet->bmx160.gyr_y;
196 telemetry.mega_sensor.gyro_z = packet->bmx160.gyr_z;
198 telemetry.mega_sensor.mag_x = packet->bmx160.mag_x;
199 telemetry.mega_sensor.mag_z = packet->bmx160.mag_z;
200 telemetry.mega_sensor.mag_y = packet->bmx160.mag_y;
206 static int8_t ao_telemetry_mega_data_max;
207 static int8_t ao_telemetry_mega_data_cur;
209 /* Send mega data packet */
211 ao_send_mega_data(void)
213 if (--ao_telemetry_mega_data_cur <= 0) {
214 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
217 telemetry.generic.tick = packet->tick;
218 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
220 telemetry.mega_data.state = ao_flight_state;
221 telemetry.mega_data.v_batt = packet->adc.v_batt;
222 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
224 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
225 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
226 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
228 telemetry.mega_data.ground_pres = ao_ground_pres;
229 telemetry.mega_data.ground_accel = ao_ground_accel;
230 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
231 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
233 telemetry.mega_data.acceleration = ao_accel;
234 telemetry.mega_data.speed = ao_speed;
235 telemetry.mega_data.height = ao_height;
237 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
241 #endif /* AO_SEND_MEGA */
243 #ifdef AO_SEND_METRUM
244 /* Send telemetrum sensor packet */
246 ao_send_metrum_sensor(void)
248 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
250 telemetry.generic.tick = packet->tick;
251 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
253 telemetry.metrum_sensor.state = ao_flight_state;
255 telemetry.metrum_sensor.accel = ao_data_accel(packet);
257 telemetry.metrum_sensor.pres = ao_data_pres(packet);
258 telemetry.metrum_sensor.temp = ao_data_temp(packet);
260 telemetry.metrum_sensor.acceleration = ao_accel;
261 telemetry.metrum_sensor.speed = ao_speed;
262 telemetry.metrum_sensor.height = ao_height;
264 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
265 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
266 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
271 static int8_t ao_telemetry_metrum_data_max;
272 static int8_t ao_telemetry_metrum_data_cur;
274 /* Send telemetrum data packet */
276 ao_send_metrum_data(void)
278 if (--ao_telemetry_metrum_data_cur <= 0) {
279 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
281 telemetry.generic.tick = packet->tick;
282 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
284 telemetry.metrum_data.ground_pres = ao_ground_pres;
286 telemetry.metrum_data.ground_accel = ao_ground_accel;
287 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
288 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
290 telemetry.metrum_data.ground_accel = 1;
291 telemetry.metrum_data.accel_plus_g = 0;
292 telemetry.metrum_data.accel_minus_g = 2;
295 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
299 #endif /* AO_SEND_METRUM */
306 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
308 telemetry.generic.tick = packet->tick;
309 telemetry.generic.type = AO_SEND_MINI;
311 telemetry.mini.state = ao_flight_state;
313 telemetry.mini.v_batt = packet->adc.v_batt;
314 telemetry.mini.sense_a = packet->adc.sense_a;
315 telemetry.mini.sense_m = packet->adc.sense_m;
317 telemetry.mini.pres = ao_data_pres(packet);
318 telemetry.mini.temp = ao_data_temp(packet);
320 telemetry.mini.acceleration = ao_accel;
321 telemetry.mini.speed = ao_speed;
322 telemetry.mini.height = ao_height;
324 telemetry.mini.ground_pres = ao_ground_pres;
329 #endif /* AO_SEND_MINI */
331 static int8_t ao_telemetry_config_max;
332 static int8_t ao_telemetry_config_cur;
333 static uint16_t ao_telemetry_flight_number;
335 #ifndef ao_telemetry_battery_convert
336 #define ao_telemetry_battery_convert(a) (a)
340 ao_send_configuration(void)
342 if (--ao_telemetry_config_cur <= 0)
344 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
345 telemetry.configuration.device = AO_idProduct_NUMBER;
346 telemetry.configuration.flight = ao_telemetry_flight_number;
347 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
348 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
349 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
350 /* TeleGPS gets battery voltage instead of apogee delay */
351 telemetry.configuration.apogee_delay = ao_telemetry_battery_convert(ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt);
353 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
354 telemetry.configuration.main_deploy = ao_config.main_deploy;
357 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
358 memcpy (telemetry.configuration.callsign,
361 memcpy (telemetry.configuration.version,
364 ao_telemetry_config_cur = ao_telemetry_config_max;
371 static int8_t ao_telemetry_gps_max;
372 static int8_t ao_telemetry_loc_cur;
373 static int8_t ao_telemetry_sat_cur;
376 telemetry_bits(struct ao_telemetry_location *l)
378 return ((uint8_t *) l) + offsetof(struct ao_telemetry_location, flags);
384 return sizeof(struct ao_telemetry_location) - offsetof(struct ao_telemetry_location, flags);
388 ao_send_location(void)
390 if (--ao_telemetry_loc_cur <= 0)
392 telemetry.generic.type = AO_TELEMETRY_LOCATION;
393 ao_mutex_get(&ao_gps_mutex);
394 memcpy(telemetry_bits(&telemetry.location),
395 telemetry_bits(&ao_gps_data),
397 telemetry.location.tick = ao_gps_tick;
398 ao_mutex_put(&ao_gps_mutex);
399 ao_telemetry_loc_cur = ao_telemetry_gps_max;
405 ao_send_satellite(void)
407 if (--ao_telemetry_sat_cur <= 0)
409 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
410 ao_mutex_get(&ao_gps_mutex);
411 telemetry.satellite.channels = ao_gps_tracking_data.channels;
412 memcpy(&telemetry.satellite.sats,
413 &ao_gps_tracking_data.sats,
414 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
415 ao_mutex_put(&ao_gps_mutex);
416 ao_telemetry_sat_cur = ao_telemetry_gps_max;
424 static int8_t ao_telemetry_companion_max;
425 static int8_t ao_telemetry_companion_cur;
428 ao_send_companion(void)
430 if (--ao_telemetry_companion_cur <= 0) {
431 telemetry.generic.type = AO_TELEMETRY_COMPANION;
432 telemetry.companion.board_id = ao_companion_setup.board_id;
433 telemetry.companion.update_period = ao_companion_setup.update_period;
434 telemetry.companion.channels = ao_companion_setup.channels;
435 ao_mutex_get(&ao_companion_mutex);
436 memcpy(&telemetry.companion.companion_data,
438 ao_companion_setup.channels * 2);
439 ao_mutex_put(&ao_companion_mutex);
440 ao_telemetry_companion_cur = ao_telemetry_companion_max;
448 ao_set_aprs_time(void)
450 uint16_t interval = ao_config.aprs_interval;
452 if ((ao_gps_data.flags & AO_GPS_DATE_VALID) && interval != 0) {
453 int second = (ao_gps_data.second / interval + 1) * interval + ao_config.aprs_offset;
456 second = ao_config.aprs_offset;
457 delta = second + 60 - ao_gps_data.second;
459 delta = second - ao_gps_data.second;
461 ao_aprs_time = ao_gps_tick + AO_SEC_TO_TICKS(delta);
463 ao_aprs_time += AO_SEC_TO_TICKS(ao_config.aprs_interval);
475 if (!ao_config.radio_enable)
477 while (!ao_flight_number)
478 ao_sleep(&ao_flight_number);
480 ao_telemetry_flight_number = ao_flight_number;
483 ao_telemetry_flight_number = 0;
485 telemetry.generic.serial = ao_serial_number;
487 while (ao_telemetry_interval == 0)
488 ao_sleep(&telemetry);
490 ao_telemetry_time = time;
498 while (ao_telemetry_interval) {
499 time = ao_time() + AO_SEC_TO_TICKS(100);
501 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
505 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
508 ao_telemetry_time = ao_time() + ao_telemetry_interval;
510 ao_send_mega_sensor();
513 # ifdef AO_SEND_METRUM
514 ao_send_metrum_sensor();
515 ao_send_metrum_data();
520 # ifdef AO_TELEMETRY_SENSOR
524 if (ao_companion_running)
531 ao_send_configuration();
534 time = ao_telemetry_time;
540 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
544 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
545 #if HAS_IGNITE_REPORT
548 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
549 #if HAS_IGNITE_REPORT
550 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
551 ao_radio_continuity(c);
557 if ((int16_t) (time - ao_rdf_time) > 0)
563 if (ao_config.aprs_interval != 0) {
564 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
568 if ((int16_t) (time - ao_aprs_time) > 0)
571 #endif /* HAS_APRS */
572 delay = time - ao_time();
574 ao_sleep_for(&telemetry, delay);
582 ao_telemetry_reset_interval(void)
584 ao_telemetry_set_interval(ao_telemetry_desired_interval);
589 ao_telemetry_set_interval(uint16_t interval)
594 /* Limit max telemetry rate based on available radio bandwidth.
596 static const uint16_t min_interval[] = {
597 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
598 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
599 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
602 ao_telemetry_desired_interval = interval;
603 if (interval && interval < min_interval[ao_config.radio_rate])
604 interval = min_interval[ao_config.radio_rate];
606 ao_telemetry_interval = interval;
609 ao_telemetry_mega_data_max = 1;
611 ao_telemetry_mega_data_max = 2;
612 if (ao_telemetry_mega_data_max > cur)
614 ao_telemetry_mega_data_cur = cur;
617 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
618 if (ao_telemetry_metrum_data_max > cur)
620 ao_telemetry_metrum_data_cur = cur;
624 if (!ao_companion_setup.update_period)
625 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
626 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
627 if (ao_telemetry_companion_max > cur)
629 ao_telemetry_companion_cur = cur;
633 ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
634 if (ao_telemetry_gps_max > cur)
636 ao_telemetry_loc_cur = cur;
637 if (ao_telemetry_gps_max > cur)
639 ao_telemetry_sat_cur = cur;
642 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
643 if (ao_telemetry_config_max > cur)
645 ao_telemetry_config_cur = cur;
657 ao_wakeup(&telemetry);
662 ao_rdf_set(uint8_t rdf)
666 ao_radio_rdf_abort();
668 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
673 struct ao_task ao_telemetry_task;
678 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");