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 __pdata uint16_t ao_telemetry_interval;
26 static __xdata 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
40 #define RDF_SPACE __pdata
42 #define RDF_SPACE __xdata
43 static __pdata 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 __pdata 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 __xdata 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 __xdata struct ao_data *packet = (__xdata 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 __xdata struct ao_data *packet = (__xdata 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;
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_y = packet->hmc5883.y;
164 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
170 static __pdata int8_t ao_telemetry_mega_data_max;
171 static __pdata int8_t ao_telemetry_mega_data_cur;
173 /* Send mega data packet */
175 ao_send_mega_data(void)
177 if (--ao_telemetry_mega_data_cur <= 0) {
178 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
181 telemetry.generic.tick = packet->tick;
182 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
184 telemetry.mega_data.state = ao_flight_state;
185 telemetry.mega_data.v_batt = packet->adc.v_batt;
186 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
188 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
189 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
190 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
192 telemetry.mega_data.ground_pres = ao_ground_pres;
193 telemetry.mega_data.ground_accel = ao_ground_accel;
194 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
195 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
197 telemetry.mega_data.acceleration = ao_accel;
198 telemetry.mega_data.speed = ao_speed;
199 telemetry.mega_data.height = ao_height;
201 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
205 #endif /* AO_SEND_MEGA */
207 #ifdef AO_SEND_METRUM
208 /* Send telemetrum sensor packet */
210 ao_send_metrum_sensor(void)
212 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
214 telemetry.generic.tick = packet->tick;
215 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
217 telemetry.metrum_sensor.state = ao_flight_state;
219 telemetry.metrum_sensor.accel = ao_data_accel(packet);
221 telemetry.metrum_sensor.pres = ao_data_pres(packet);
222 telemetry.metrum_sensor.temp = ao_data_temp(packet);
224 telemetry.metrum_sensor.acceleration = ao_accel;
225 telemetry.metrum_sensor.speed = ao_speed;
226 telemetry.metrum_sensor.height = ao_height;
228 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
229 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
230 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
235 static __pdata int8_t ao_telemetry_metrum_data_max;
236 static __pdata int8_t ao_telemetry_metrum_data_cur;
238 /* Send telemetrum data packet */
240 ao_send_metrum_data(void)
242 if (--ao_telemetry_metrum_data_cur <= 0) {
243 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
245 telemetry.generic.tick = packet->tick;
246 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
248 telemetry.metrum_data.ground_pres = ao_ground_pres;
250 telemetry.metrum_data.ground_accel = ao_ground_accel;
251 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
252 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
254 telemetry.metrum_data.ground_accel = 1;
255 telemetry.metrum_data.accel_plus_g = 0;
256 telemetry.metrum_data.accel_minus_g = 2;
259 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
263 #endif /* AO_SEND_METRUM */
270 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
272 telemetry.generic.tick = packet->tick;
273 telemetry.generic.type = AO_TELEMETRY_MINI;
275 telemetry.mini.state = ao_flight_state;
277 telemetry.mini.v_batt = packet->adc.v_batt;
278 telemetry.mini.sense_a = packet->adc.sense_a;
279 telemetry.mini.sense_m = packet->adc.sense_m;
281 telemetry.mini.pres = ao_data_pres(packet);
282 telemetry.mini.temp = ao_data_temp(packet);
284 telemetry.mini.acceleration = ao_accel;
285 telemetry.mini.speed = ao_speed;
286 telemetry.mini.height = ao_height;
288 telemetry.mini.ground_pres = ao_ground_pres;
293 #endif /* AO_SEND_MINI */
295 static __pdata int8_t ao_telemetry_config_max;
296 static __pdata int8_t ao_telemetry_config_cur;
297 static __pdata uint16_t ao_telemetry_flight_number;
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;
306 telemetry.configuration.flight = ao_telemetry_flight_number;
307 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
308 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
309 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
310 /* TeleGPS gets battery voltage instead of apogee delay */
311 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
313 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
314 telemetry.configuration.main_deploy = ao_config.main_deploy;
317 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
318 ao_xmemcpy (telemetry.configuration.callsign,
321 ao_xmemcpy (telemetry.configuration.version,
322 CODE_TO_XDATA(ao_version),
324 ao_telemetry_config_cur = ao_telemetry_config_max;
331 static __pdata int8_t ao_telemetry_gps_max;
332 static __pdata int8_t ao_telemetry_loc_cur;
333 static __pdata int8_t ao_telemetry_sat_cur;
336 ao_send_location(void)
338 if (--ao_telemetry_loc_cur <= 0)
340 telemetry.generic.type = AO_TELEMETRY_LOCATION;
341 ao_mutex_get(&ao_gps_mutex);
342 ao_xmemcpy(&telemetry.location.flags,
345 telemetry.location.tick = ao_gps_tick;
346 ao_mutex_put(&ao_gps_mutex);
347 ao_telemetry_loc_cur = ao_telemetry_gps_max;
353 ao_send_satellite(void)
355 if (--ao_telemetry_sat_cur <= 0)
357 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
358 ao_mutex_get(&ao_gps_mutex);
359 telemetry.satellite.channels = ao_gps_tracking_data.channels;
360 ao_xmemcpy(&telemetry.satellite.sats,
361 &ao_gps_tracking_data.sats,
362 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
363 ao_mutex_put(&ao_gps_mutex);
364 ao_telemetry_sat_cur = ao_telemetry_gps_max;
372 static __pdata int8_t ao_telemetry_companion_max;
373 static __pdata int8_t ao_telemetry_companion_cur;
376 ao_send_companion(void)
378 if (--ao_telemetry_companion_cur <= 0) {
379 telemetry.generic.type = AO_TELEMETRY_COMPANION;
380 telemetry.companion.board_id = ao_companion_setup.board_id;
381 telemetry.companion.update_period = ao_companion_setup.update_period;
382 telemetry.companion.channels = ao_companion_setup.channels;
383 ao_mutex_get(&ao_companion_mutex);
384 ao_xmemcpy(&telemetry.companion.companion_data,
386 ao_companion_setup.channels * 2);
387 ao_mutex_put(&ao_companion_mutex);
388 ao_telemetry_companion_cur = ao_telemetry_companion_max;
401 if (!ao_config.radio_enable)
403 while (!ao_flight_number)
404 ao_sleep(&ao_flight_number);
406 ao_telemetry_flight_number = ao_flight_number;
409 ao_telemetry_flight_number = 0;
411 telemetry.generic.serial = ao_serial_number;
413 while (ao_telemetry_interval == 0)
414 ao_sleep(&telemetry);
416 ao_telemetry_time = time;
423 while (ao_telemetry_interval) {
424 time = ao_time() + AO_SEC_TO_TICKS(100);
426 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
430 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
433 ao_telemetry_time = ao_time() + ao_telemetry_interval;
435 ao_send_mega_sensor();
438 # ifdef AO_SEND_METRUM
439 ao_send_metrum_sensor();
440 ao_send_metrum_data();
445 # ifdef AO_TELEMETRY_SENSOR
449 if (ao_companion_running)
456 ao_send_configuration();
459 time = ao_telemetry_time;
465 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
469 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
470 #if HAS_IGNITE_REPORT
473 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
474 #if HAS_IGNITE_REPORT
475 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
476 ao_radio_continuity(c);
482 if ((int16_t) (time - ao_rdf_time) > 0)
488 if (ao_config.aprs_interval != 0) {
489 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
490 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
493 if ((int16_t) (time - ao_aprs_time) > 0)
496 #endif /* HAS_APRS */
497 delay = time - ao_time();
499 ao_sleep_for(&telemetry, delay);
507 ao_telemetry_reset_interval(void)
509 ao_telemetry_set_interval(ao_telemetry_desired_interval);
514 ao_telemetry_set_interval(uint16_t interval)
519 /* Limit max telemetry rate based on available radio bandwidth.
521 static __xdata const uint16_t min_interval[] = {
522 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
523 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
524 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
527 ao_telemetry_desired_interval = interval;
528 if (interval && interval < min_interval[ao_config.radio_rate])
529 interval = min_interval[ao_config.radio_rate];
531 ao_telemetry_interval = interval;
534 ao_telemetry_mega_data_max = 1;
536 ao_telemetry_mega_data_max = 2;
537 if (ao_telemetry_mega_data_max > cur)
539 ao_telemetry_mega_data_cur = cur;
542 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
543 if (ao_telemetry_metrum_data_max > cur)
545 ao_telemetry_metrum_data_cur = cur;
549 if (!ao_companion_setup.update_period)
550 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
551 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
552 if (ao_telemetry_companion_max > cur)
554 ao_telemetry_companion_cur = cur;
558 ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
559 if (ao_telemetry_gps_max > cur)
561 ao_telemetry_loc_cur = cur;
562 if (ao_telemetry_gps_max > cur)
564 ao_telemetry_sat_cur = cur;
567 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
568 if (ao_telemetry_config_max > cur)
570 ao_telemetry_config_cur = cur;
582 ao_wakeup(&telemetry);
587 ao_rdf_set(uint8_t rdf)
591 ao_radio_rdf_abort();
593 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
598 __xdata struct ao_task ao_telemetry_task;
603 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");