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)
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 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
144 #if HAS_MPU6000 || HAS_MPU9250
145 telemetry.mega_sensor.orient = ao_sample_orient;
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);
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;
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;
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;
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;
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;
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;
184 static int8_t ao_telemetry_mega_data_max;
185 static int8_t ao_telemetry_mega_data_cur;
187 /* Send mega data packet */
189 ao_send_mega_data(void)
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)];
195 telemetry.generic.tick = packet->tick;
196 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
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;
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;
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;
211 telemetry.mega_data.acceleration = ao_accel;
212 telemetry.mega_data.speed = ao_speed;
213 telemetry.mega_data.height = ao_height;
215 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
219 #endif /* AO_SEND_MEGA */
221 #ifdef AO_SEND_METRUM
222 /* Send telemetrum sensor packet */
224 ao_send_metrum_sensor(void)
226 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
228 telemetry.generic.tick = packet->tick;
229 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
231 telemetry.metrum_sensor.state = ao_flight_state;
233 telemetry.metrum_sensor.accel = ao_data_accel(packet);
235 telemetry.metrum_sensor.pres = ao_data_pres(packet);
236 telemetry.metrum_sensor.temp = ao_data_temp(packet);
238 telemetry.metrum_sensor.acceleration = ao_accel;
239 telemetry.metrum_sensor.speed = ao_speed;
240 telemetry.metrum_sensor.height = ao_height;
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;
249 static int8_t ao_telemetry_metrum_data_max;
250 static int8_t ao_telemetry_metrum_data_cur;
252 /* Send telemetrum data packet */
254 ao_send_metrum_data(void)
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)];
259 telemetry.generic.tick = packet->tick;
260 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
262 telemetry.metrum_data.ground_pres = ao_ground_pres;
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;
268 telemetry.metrum_data.ground_accel = 1;
269 telemetry.metrum_data.accel_plus_g = 0;
270 telemetry.metrum_data.accel_minus_g = 2;
273 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
277 #endif /* AO_SEND_METRUM */
284 struct ao_data *packet = (struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
286 telemetry.generic.tick = packet->tick;
287 telemetry.generic.type = AO_SEND_MINI;
289 telemetry.mini.state = ao_flight_state;
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;
295 telemetry.mini.pres = ao_data_pres(packet);
296 telemetry.mini.temp = ao_data_temp(packet);
298 telemetry.mini.acceleration = ao_accel;
299 telemetry.mini.speed = ao_speed;
300 telemetry.mini.height = ao_height;
302 telemetry.mini.ground_pres = ao_ground_pres;
307 #endif /* AO_SEND_MINI */
309 static int8_t ao_telemetry_config_max;
310 static int8_t ao_telemetry_config_cur;
311 static uint16_t ao_telemetry_flight_number;
313 #ifndef ao_telemetry_battery_convert
314 #define ao_telemetry_battery_convert(a) (a)
318 ao_send_configuration(void)
320 if (--ao_telemetry_config_cur <= 0)
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);
331 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
332 telemetry.configuration.main_deploy = ao_config.main_deploy;
335 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
336 ao_xmemcpy (telemetry.configuration.callsign,
339 ao_xmemcpy (telemetry.configuration.version,
342 ao_telemetry_config_cur = ao_telemetry_config_max;
349 static int8_t ao_telemetry_gps_max;
350 static int8_t ao_telemetry_loc_cur;
351 static int8_t ao_telemetry_sat_cur;
354 ao_send_location(void)
356 if (--ao_telemetry_loc_cur <= 0)
358 telemetry.generic.type = AO_TELEMETRY_LOCATION;
359 ao_mutex_get(&ao_gps_mutex);
360 ao_xmemcpy(&telemetry.location.flags,
363 telemetry.location.tick = ao_gps_tick;
364 ao_mutex_put(&ao_gps_mutex);
365 ao_telemetry_loc_cur = ao_telemetry_gps_max;
371 ao_send_satellite(void)
373 if (--ao_telemetry_sat_cur <= 0)
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;
390 static int8_t ao_telemetry_companion_max;
391 static int8_t ao_telemetry_companion_cur;
394 ao_send_companion(void)
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,
404 ao_companion_setup.channels * 2);
405 ao_mutex_put(&ao_companion_mutex);
406 ao_telemetry_companion_cur = ao_telemetry_companion_max;
419 if (!ao_config.radio_enable)
421 while (!ao_flight_number)
422 ao_sleep(&ao_flight_number);
424 ao_telemetry_flight_number = ao_flight_number;
427 ao_telemetry_flight_number = 0;
429 telemetry.generic.serial = ao_serial_number;
431 while (ao_telemetry_interval == 0)
432 ao_sleep(&telemetry);
434 ao_telemetry_time = time;
441 while (ao_telemetry_interval) {
442 time = ao_time() + AO_SEC_TO_TICKS(100);
444 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
448 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
451 ao_telemetry_time = ao_time() + ao_telemetry_interval;
453 ao_send_mega_sensor();
456 # ifdef AO_SEND_METRUM
457 ao_send_metrum_sensor();
458 ao_send_metrum_data();
463 # ifdef AO_TELEMETRY_SENSOR
467 if (ao_companion_running)
474 ao_send_configuration();
477 time = ao_telemetry_time;
483 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
487 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
488 #if HAS_IGNITE_REPORT
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);
500 if ((int16_t) (time - ao_rdf_time) > 0)
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);
511 if ((int16_t) (time - ao_aprs_time) > 0)
514 #endif /* HAS_APRS */
515 delay = time - ao_time();
517 ao_sleep_for(&telemetry, delay);
525 ao_telemetry_reset_interval(void)
527 ao_telemetry_set_interval(ao_telemetry_desired_interval);
532 ao_telemetry_set_interval(uint16_t interval)
537 /* Limit max telemetry rate based on available radio bandwidth.
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)
545 ao_telemetry_desired_interval = interval;
546 if (interval && interval < min_interval[ao_config.radio_rate])
547 interval = min_interval[ao_config.radio_rate];
549 ao_telemetry_interval = interval;
552 ao_telemetry_mega_data_max = 1;
554 ao_telemetry_mega_data_max = 2;
555 if (ao_telemetry_mega_data_max > cur)
557 ao_telemetry_mega_data_cur = cur;
560 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
561 if (ao_telemetry_metrum_data_max > cur)
563 ao_telemetry_metrum_data_cur = cur;
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)
572 ao_telemetry_companion_cur = cur;
576 ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
577 if (ao_telemetry_gps_max > cur)
579 ao_telemetry_loc_cur = cur;
580 if (ao_telemetry_gps_max > cur)
582 ao_telemetry_sat_cur = cur;
585 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
586 if (ao_telemetry_config_max > cur)
588 ao_telemetry_config_cur = cur;
600 ao_wakeup(&telemetry);
605 ao_rdf_set(uint8_t rdf)
609 ao_radio_rdf_abort();
611 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
616 struct ao_task ao_telemetry_task;
621 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");