2 * Copyright © 2011 Keith 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 __pdata uint8_t ao_rdf = 0;
30 static __pdata uint16_t ao_rdf_time;
34 static __pdata uint16_t ao_aprs_time;
40 #define AO_SEND_MEGA 1
43 #if defined (TELEMETRUM_V_2_0)
44 #define AO_SEND_METRUM 1
47 #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)
48 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
51 #if defined(TELEMINI_V_1_0)
52 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
55 #if defined(TELENANO_V_0_1)
56 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
59 static __xdata union ao_telemetry_all telemetry;
61 #if defined AO_TELEMETRY_SENSOR
62 /* Send sensor packet */
66 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
68 telemetry.generic.tick = packet->tick;
69 telemetry.generic.type = AO_TELEMETRY_SENSOR;
71 telemetry.sensor.state = ao_flight_state;
73 telemetry.sensor.accel = packet->adc.accel;
75 telemetry.sensor.accel = 0;
77 telemetry.sensor.pres = ao_data_pres(packet);
78 telemetry.sensor.temp = packet->adc.temp;
79 telemetry.sensor.v_batt = packet->adc.v_batt;
81 telemetry.sensor.sense_d = packet->adc.sense_d;
82 telemetry.sensor.sense_m = packet->adc.sense_m;
84 telemetry.sensor.sense_d = 0;
85 telemetry.sensor.sense_m = 0;
88 telemetry.sensor.acceleration = ao_accel;
89 telemetry.sensor.speed = ao_speed;
90 telemetry.sensor.height = ao_height;
92 telemetry.sensor.ground_pres = ao_ground_pres;
94 telemetry.sensor.ground_accel = ao_ground_accel;
95 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
96 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
98 telemetry.sensor.ground_accel = 0;
99 telemetry.sensor.accel_plus_g = 0;
100 telemetry.sensor.accel_minus_g = 0;
103 ao_radio_send(&telemetry, sizeof (telemetry));
109 /* Send mega sensor packet */
111 ao_send_mega_sensor(void)
113 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
115 telemetry.generic.tick = packet->tick;
116 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
118 telemetry.mega_sensor.orient = ao_sample_orient;
119 telemetry.mega_sensor.accel = ao_data_accel(packet);
120 telemetry.mega_sensor.pres = ao_data_pres(packet);
121 telemetry.mega_sensor.temp = ao_data_temp(packet);
124 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
125 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
126 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
128 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
129 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
130 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
134 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
135 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
136 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
139 ao_radio_send(&telemetry, sizeof (telemetry));
142 static __pdata int8_t ao_telemetry_mega_data_max;
143 static __pdata int8_t ao_telemetry_mega_data_cur;
145 /* Send mega data packet */
147 ao_send_mega_data(void)
149 if (--ao_telemetry_mega_data_cur <= 0) {
150 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
153 telemetry.generic.tick = packet->tick;
154 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
156 telemetry.mega_data.state = ao_flight_state;
157 telemetry.mega_data.v_batt = packet->adc.v_batt;
158 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
160 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
161 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
162 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
164 telemetry.mega_data.ground_pres = ao_ground_pres;
165 telemetry.mega_data.ground_accel = ao_ground_accel;
166 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
167 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
169 telemetry.mega_data.acceleration = ao_accel;
170 telemetry.mega_data.speed = ao_speed;
171 telemetry.mega_data.height = ao_height;
173 ao_radio_send(&telemetry, sizeof (telemetry));
174 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
177 #endif /* AO_SEND_MEGA */
179 #ifdef AO_SEND_METRUM
180 /* Send telemetrum sensor packet */
182 ao_send_metrum_sensor(void)
184 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
186 telemetry.generic.tick = packet->tick;
187 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
189 telemetry.metrum_sensor.state = ao_flight_state;
191 telemetry.metrum_sensor.accel = ao_data_accel(packet);
193 telemetry.metrum_sensor.pres = ao_data_pres(packet);
194 telemetry.metrum_sensor.temp = ao_data_temp(packet);
196 telemetry.metrum_sensor.acceleration = ao_accel;
197 telemetry.metrum_sensor.speed = ao_speed;
198 telemetry.metrum_sensor.height = ao_height;
200 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
201 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
202 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
204 ao_radio_send(&telemetry, sizeof (telemetry));
207 static __pdata int8_t ao_telemetry_metrum_data_max;
208 static __pdata int8_t ao_telemetry_metrum_data_cur;
210 /* Send telemetrum data packet */
212 ao_send_metrum_data(void)
214 if (--ao_telemetry_metrum_data_cur <= 0) {
215 __xdata struct ao_data *packet = (__xdata 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_METRUM_DATA;
220 telemetry.metrum_data.ground_pres = ao_ground_pres;
222 telemetry.metrum_data.ground_accel = ao_ground_accel;
223 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
224 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
226 telemetry.metrum_data.ground_accel = 1;
227 telemetry.metrum_data.accel_plus_g = 0;
228 telemetry.metrum_data.accel_minus_g = 2;
231 ao_radio_send(&telemetry, sizeof (telemetry));
232 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
235 #endif /* AO_SEND_METRUM */
242 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
244 telemetry.generic.tick = packet->tick;
245 telemetry.generic.type = AO_TELEMETRY_MINI;
247 telemetry.mini.state = ao_flight_state;
249 telemetry.mini.v_batt = packet->adc.v_batt;
250 telemetry.mini.sense_a = packet->adc.sense_a;
251 telemetry.mini.sense_m = packet->adc.sense_m;
253 telemetry.mini.pres = ao_data_pres(packet);
254 telemetry.mini.temp = ao_data_temp(packet);
256 telemetry.mini.acceleration = ao_accel;
257 telemetry.mini.speed = ao_speed;
258 telemetry.mini.height = ao_height;
260 telemetry.mini.ground_pres = ao_ground_pres;
262 ao_radio_send(&telemetry, sizeof (telemetry));
265 #endif /* AO_SEND_MINI */
267 #ifdef AO_SEND_ALL_BARO
268 static uint8_t ao_baro_sample;
273 uint8_t sample = ao_sample_data;
274 uint8_t samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
277 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
280 telemetry.generic.tick = ao_data_ring[sample].tick;
281 telemetry.generic.type = AO_TELEMETRY_BARO;
282 telemetry.baro.samples = samples;
283 for (sample = 0; sample < samples; sample++) {
284 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
285 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
287 ao_radio_send(&telemetry, sizeof (telemetry));
291 static __pdata int8_t ao_telemetry_config_max;
292 static __pdata int8_t ao_telemetry_config_cur;
295 ao_send_configuration(void)
297 if (--ao_telemetry_config_cur <= 0)
299 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
300 telemetry.configuration.device = AO_idProduct_NUMBER;
302 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
304 telemetry.configuration.flight = ao_flight_number;
306 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
307 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
308 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
309 /* TeleGPS gets battery voltage instead of apogee delay */
310 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
312 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
313 telemetry.configuration.main_deploy = ao_config.main_deploy;
316 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
317 ao_xmemcpy (telemetry.configuration.callsign,
320 ao_xmemcpy (telemetry.configuration.version,
321 CODE_TO_XDATA(ao_version),
323 ao_radio_send(&telemetry, sizeof (telemetry));
324 ao_telemetry_config_cur = ao_telemetry_config_max;
330 static __pdata int8_t ao_telemetry_loc_cur;
331 static __pdata int8_t ao_telemetry_sat_cur;
334 ao_send_location(void)
336 if (--ao_telemetry_loc_cur <= 0)
338 telemetry.generic.type = AO_TELEMETRY_LOCATION;
339 ao_mutex_get(&ao_gps_mutex);
340 ao_xmemcpy(&telemetry.location.flags,
343 telemetry.location.tick = ao_gps_tick;
344 ao_mutex_put(&ao_gps_mutex);
345 ao_radio_send(&telemetry, sizeof (telemetry));
346 ao_telemetry_loc_cur = ao_telemetry_config_max;
351 ao_send_satellite(void)
353 if (--ao_telemetry_sat_cur <= 0)
355 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
356 ao_mutex_get(&ao_gps_mutex);
357 telemetry.satellite.channels = ao_gps_tracking_data.channels;
358 ao_xmemcpy(&telemetry.satellite.sats,
359 &ao_gps_tracking_data.sats,
360 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
361 ao_mutex_put(&ao_gps_mutex);
362 ao_radio_send(&telemetry, sizeof (telemetry));
363 ao_telemetry_sat_cur = ao_telemetry_config_max;
370 static __pdata int8_t ao_telemetry_companion_max;
371 static __pdata int8_t ao_telemetry_companion_cur;
374 ao_send_companion(void)
376 if (--ao_telemetry_companion_cur <= 0) {
377 telemetry.generic.type = AO_TELEMETRY_COMPANION;
378 telemetry.companion.board_id = ao_companion_setup.board_id;
379 telemetry.companion.update_period = ao_companion_setup.update_period;
380 telemetry.companion.channels = ao_companion_setup.channels;
381 ao_mutex_get(&ao_companion_mutex);
382 ao_xmemcpy(&telemetry.companion.companion_data,
384 ao_companion_setup.channels * 2);
385 ao_mutex_put(&ao_companion_mutex);
386 ao_radio_send(&telemetry, sizeof (telemetry));
387 ao_telemetry_companion_cur = ao_telemetry_companion_max;
399 if (!ao_config.radio_enable)
401 while (!ao_flight_number)
402 ao_sleep(&ao_flight_number);
404 telemetry.generic.serial = ao_serial_number;
406 while (ao_telemetry_interval == 0)
407 ao_sleep(&telemetry);
415 while (ao_telemetry_interval) {
417 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
420 #ifdef AO_SEND_ALL_BARO
426 ao_send_mega_sensor();
429 # ifdef AO_SEND_METRUM
430 ao_send_metrum_sensor();
431 ao_send_metrum_data();
436 # ifdef AO_TELEMETRY_SENSOR
439 #endif /* HAS_FLIGHT */
442 if (ao_companion_running)
445 ao_send_configuration();
451 #ifndef AO_SEND_ALL_BARO
455 !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
456 #endif /* HAS_APRS */
457 (int16_t) (ao_time() - ao_rdf_time) >= 0)
459 #if HAS_IGNITE_REPORT
461 #endif /* HAS_IGNITE_REPORT */
462 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
463 #if HAS_IGNITE_REPORT
464 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
465 ao_radio_continuity(c);
467 #endif /* HAS_IGNITE_REPORT*/
472 if (ao_config.aprs_interval != 0 &&
473 (int16_t) (ao_time() - ao_aprs_time) >= 0)
475 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
478 #endif /* HAS_APRS */
479 #endif /* !AO_SEND_ALL_BARO */
480 time += ao_telemetry_interval;
481 delay = time - ao_time();
484 ao_sleep(&telemetry);
494 ao_telemetry_set_interval(uint16_t interval)
497 ao_telemetry_interval = interval;
501 ao_telemetry_mega_data_max = 1;
503 ao_telemetry_mega_data_max = 2;
504 if (ao_telemetry_mega_data_max > cur)
506 ao_telemetry_mega_data_cur = cur;
509 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
510 if (ao_telemetry_metrum_data_max > cur)
512 ao_telemetry_metrum_data_cur = cur;
516 if (!ao_companion_setup.update_period)
517 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
518 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
519 if (ao_telemetry_companion_max > cur)
521 ao_telemetry_companion_cur = cur;
524 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
526 if (ao_telemetry_config_max > cur)
528 ao_telemetry_config_cur = cur;
532 if (ao_telemetry_config_max > cur)
534 ao_telemetry_loc_cur = cur;
535 if (ao_telemetry_config_max > cur)
537 ao_telemetry_sat_cur = cur;
539 ao_wakeup(&telemetry);
544 ao_rdf_set(uint8_t rdf)
548 ao_radio_rdf_abort();
550 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
555 __xdata struct ao_task ao_telemetry_task;
560 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");