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; version 2 of the License.
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.
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.
20 #include "ao_product.h"
26 static __pdata uint16_t ao_telemetry_interval;
29 static __xdata uint16_t ao_telemetry_desired_interval;
33 static __pdata uint8_t ao_rdf = 0;
34 static __pdata uint16_t ao_rdf_time;
38 static __pdata uint16_t ao_aprs_time;
44 #define AO_SEND_MEGA 1
47 #if defined (TELEMETRUM_V_2_0)
48 #define AO_SEND_METRUM 1
51 #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)
52 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
55 #if defined(TELEMINI_V_1_0)
56 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
59 #if defined(TELENANO_V_0_1)
60 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
63 static __xdata union ao_telemetry_all telemetry;
65 #if defined AO_TELEMETRY_SENSOR
66 /* Send sensor packet */
70 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
72 telemetry.generic.tick = packet->tick;
73 telemetry.generic.type = AO_TELEMETRY_SENSOR;
75 telemetry.sensor.state = ao_flight_state;
77 telemetry.sensor.accel = packet->adc.accel;
79 telemetry.sensor.accel = 0;
81 telemetry.sensor.pres = ao_data_pres(packet);
82 telemetry.sensor.temp = packet->adc.temp;
83 telemetry.sensor.v_batt = packet->adc.v_batt;
85 telemetry.sensor.sense_d = packet->adc.sense_d;
86 telemetry.sensor.sense_m = packet->adc.sense_m;
88 telemetry.sensor.sense_d = 0;
89 telemetry.sensor.sense_m = 0;
92 telemetry.sensor.acceleration = ao_accel;
93 telemetry.sensor.speed = ao_speed;
94 telemetry.sensor.height = ao_height;
96 telemetry.sensor.ground_pres = ao_ground_pres;
98 telemetry.sensor.ground_accel = ao_ground_accel;
99 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
100 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
102 telemetry.sensor.ground_accel = 0;
103 telemetry.sensor.accel_plus_g = 0;
104 telemetry.sensor.accel_minus_g = 0;
107 ao_radio_send(&telemetry, sizeof (telemetry));
114 /* Send mega sensor packet */
116 ao_send_mega_sensor(void)
118 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
120 telemetry.generic.tick = packet->tick;
121 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
123 telemetry.mega_sensor.orient = ao_sample_orient;
124 telemetry.mega_sensor.accel = ao_data_accel(packet);
125 telemetry.mega_sensor.pres = ao_data_pres(packet);
126 telemetry.mega_sensor.temp = ao_data_temp(packet);
129 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
130 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
131 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
133 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
134 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
135 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
139 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
140 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
141 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
144 ao_radio_send(&telemetry, sizeof (telemetry));
147 static __pdata int8_t ao_telemetry_mega_data_max;
148 static __pdata int8_t ao_telemetry_mega_data_cur;
150 /* Send mega data packet */
152 ao_send_mega_data(void)
154 if (--ao_telemetry_mega_data_cur <= 0) {
155 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
158 telemetry.generic.tick = packet->tick;
159 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
161 telemetry.mega_data.state = ao_flight_state;
162 telemetry.mega_data.v_batt = packet->adc.v_batt;
163 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
165 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
166 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
167 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
169 telemetry.mega_data.ground_pres = ao_ground_pres;
170 telemetry.mega_data.ground_accel = ao_ground_accel;
171 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
172 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
174 telemetry.mega_data.acceleration = ao_accel;
175 telemetry.mega_data.speed = ao_speed;
176 telemetry.mega_data.height = ao_height;
178 ao_radio_send(&telemetry, sizeof (telemetry));
179 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
182 #endif /* AO_SEND_MEGA */
184 #ifdef AO_SEND_METRUM
185 /* Send telemetrum sensor packet */
187 ao_send_metrum_sensor(void)
189 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
191 telemetry.generic.tick = packet->tick;
192 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
194 telemetry.metrum_sensor.state = ao_flight_state;
196 telemetry.metrum_sensor.accel = ao_data_accel(packet);
198 telemetry.metrum_sensor.pres = ao_data_pres(packet);
199 telemetry.metrum_sensor.temp = ao_data_temp(packet);
201 telemetry.metrum_sensor.acceleration = ao_accel;
202 telemetry.metrum_sensor.speed = ao_speed;
203 telemetry.metrum_sensor.height = ao_height;
205 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
206 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
207 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
209 ao_radio_send(&telemetry, sizeof (telemetry));
212 static __pdata int8_t ao_telemetry_metrum_data_max;
213 static __pdata int8_t ao_telemetry_metrum_data_cur;
215 /* Send telemetrum data packet */
217 ao_send_metrum_data(void)
219 if (--ao_telemetry_metrum_data_cur <= 0) {
220 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
222 telemetry.generic.tick = packet->tick;
223 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
225 telemetry.metrum_data.ground_pres = ao_ground_pres;
227 telemetry.metrum_data.ground_accel = ao_ground_accel;
228 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
229 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
231 telemetry.metrum_data.ground_accel = 1;
232 telemetry.metrum_data.accel_plus_g = 0;
233 telemetry.metrum_data.accel_minus_g = 2;
236 ao_radio_send(&telemetry, sizeof (telemetry));
237 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
240 #endif /* AO_SEND_METRUM */
247 __xdata struct ao_data *packet = (__xdata 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_MINI;
252 telemetry.mini.state = ao_flight_state;
254 telemetry.mini.v_batt = packet->adc.v_batt;
255 telemetry.mini.sense_a = packet->adc.sense_a;
256 telemetry.mini.sense_m = packet->adc.sense_m;
258 telemetry.mini.pres = ao_data_pres(packet);
259 telemetry.mini.temp = ao_data_temp(packet);
261 telemetry.mini.acceleration = ao_accel;
262 telemetry.mini.speed = ao_speed;
263 telemetry.mini.height = ao_height;
265 telemetry.mini.ground_pres = ao_ground_pres;
267 ao_radio_send(&telemetry, sizeof (telemetry));
270 #endif /* AO_SEND_MINI */
272 #ifdef AO_SEND_ALL_BARO
273 static uint8_t ao_baro_sample;
278 uint8_t sample = ao_sample_data;
279 uint8_t samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
282 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
285 telemetry.generic.tick = ao_data_ring[sample].tick;
286 telemetry.generic.type = AO_TELEMETRY_BARO;
287 telemetry.baro.samples = samples;
288 for (sample = 0; sample < samples; sample++) {
289 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
290 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
292 ao_radio_send(&telemetry, sizeof (telemetry));
296 static __pdata int8_t ao_telemetry_config_max;
297 static __pdata int8_t ao_telemetry_config_cur;
300 ao_send_configuration(void)
302 if (--ao_telemetry_config_cur <= 0)
304 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
305 telemetry.configuration.device = AO_idProduct_NUMBER;
307 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
309 telemetry.configuration.flight = ao_flight_number;
311 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
312 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
313 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
314 /* TeleGPS gets battery voltage instead of apogee delay */
315 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
317 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
318 telemetry.configuration.main_deploy = ao_config.main_deploy;
321 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
322 ao_xmemcpy (telemetry.configuration.callsign,
325 ao_xmemcpy (telemetry.configuration.version,
326 CODE_TO_XDATA(ao_version),
328 ao_radio_send(&telemetry, sizeof (telemetry));
329 ao_telemetry_config_cur = ao_telemetry_config_max;
335 static __pdata int8_t ao_telemetry_loc_cur;
336 static __pdata int8_t ao_telemetry_sat_cur;
339 ao_send_location(void)
341 if (--ao_telemetry_loc_cur <= 0)
343 telemetry.generic.type = AO_TELEMETRY_LOCATION;
344 ao_mutex_get(&ao_gps_mutex);
345 ao_xmemcpy(&telemetry.location.flags,
348 telemetry.location.tick = ao_gps_tick;
349 ao_mutex_put(&ao_gps_mutex);
350 ao_radio_send(&telemetry, sizeof (telemetry));
351 ao_telemetry_loc_cur = ao_telemetry_config_max;
356 ao_send_satellite(void)
358 if (--ao_telemetry_sat_cur <= 0)
360 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
361 ao_mutex_get(&ao_gps_mutex);
362 telemetry.satellite.channels = ao_gps_tracking_data.channels;
363 ao_xmemcpy(&telemetry.satellite.sats,
364 &ao_gps_tracking_data.sats,
365 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
366 ao_mutex_put(&ao_gps_mutex);
367 ao_radio_send(&telemetry, sizeof (telemetry));
368 ao_telemetry_sat_cur = ao_telemetry_config_max;
375 static __pdata int8_t ao_telemetry_companion_max;
376 static __pdata int8_t ao_telemetry_companion_cur;
379 ao_send_companion(void)
381 if (--ao_telemetry_companion_cur <= 0) {
382 telemetry.generic.type = AO_TELEMETRY_COMPANION;
383 telemetry.companion.board_id = ao_companion_setup.board_id;
384 telemetry.companion.update_period = ao_companion_setup.update_period;
385 telemetry.companion.channels = ao_companion_setup.channels;
386 ao_mutex_get(&ao_companion_mutex);
387 ao_xmemcpy(&telemetry.companion.companion_data,
389 ao_companion_setup.channels * 2);
390 ao_mutex_put(&ao_companion_mutex);
391 ao_radio_send(&telemetry, sizeof (telemetry));
392 ao_telemetry_companion_cur = ao_telemetry_companion_max;
404 if (!ao_config.radio_enable)
406 while (!ao_flight_number)
407 ao_sleep(&ao_flight_number);
409 telemetry.generic.serial = ao_serial_number;
411 while (ao_telemetry_interval == 0)
412 ao_sleep(&telemetry);
420 while (ao_telemetry_interval) {
422 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
425 #ifdef AO_SEND_ALL_BARO
431 ao_send_mega_sensor();
434 # ifdef AO_SEND_METRUM
435 ao_send_metrum_sensor();
436 ao_send_metrum_data();
441 # ifdef AO_TELEMETRY_SENSOR
444 #endif /* HAS_FLIGHT */
447 if (ao_companion_running)
450 ao_send_configuration();
456 #ifndef AO_SEND_ALL_BARO
460 !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
461 #endif /* HAS_APRS */
462 (int16_t) (ao_time() - ao_rdf_time) >= 0)
464 #if HAS_IGNITE_REPORT
466 #endif /* HAS_IGNITE_REPORT */
467 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
468 #if HAS_IGNITE_REPORT
469 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
470 ao_radio_continuity(c);
472 #endif /* HAS_IGNITE_REPORT*/
477 if (ao_config.aprs_interval != 0 &&
478 (int16_t) (ao_time() - ao_aprs_time) >= 0)
480 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
483 #endif /* HAS_APRS */
484 #endif /* !AO_SEND_ALL_BARO */
485 time += ao_telemetry_interval;
486 delay = time - ao_time();
489 ao_sleep(&telemetry);
500 ao_telemetry_reset_interval(void)
502 ao_telemetry_set_interval(ao_telemetry_desired_interval);
507 ao_telemetry_set_interval(uint16_t interval)
512 /* Limit max telemetry rate based on available radio bandwidth.
514 static __xdata const uint16_t min_interval[] = {
515 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
516 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
517 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
520 ao_telemetry_desired_interval = interval;
521 if (interval < min_interval[ao_config.radio_rate])
522 interval = min_interval[ao_config.radio_rate];
524 ao_telemetry_interval = interval;
527 ao_telemetry_mega_data_max = 1;
529 ao_telemetry_mega_data_max = 2;
530 if (ao_telemetry_mega_data_max > cur)
532 ao_telemetry_mega_data_cur = cur;
535 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
536 if (ao_telemetry_metrum_data_max > cur)
538 ao_telemetry_metrum_data_cur = cur;
542 if (!ao_companion_setup.update_period)
543 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
544 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
545 if (ao_telemetry_companion_max > cur)
547 ao_telemetry_companion_cur = cur;
550 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
552 if (ao_telemetry_config_max > cur)
554 ao_telemetry_config_cur = cur;
558 if (ao_telemetry_config_max > cur)
560 ao_telemetry_loc_cur = cur;
561 if (ao_telemetry_config_max > cur)
563 ao_telemetry_sat_cur = cur;
565 ao_wakeup(&telemetry);
570 ao_rdf_set(uint8_t rdf)
574 ao_radio_rdf_abort();
576 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
581 __xdata struct ao_task ao_telemetry_task;
586 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");