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"
22 static __pdata uint16_t ao_telemetry_interval;
25 static __xdata uint16_t ao_telemetry_desired_interval;
28 /* TeleMetrum v1.0 just doesn't have enough space to
29 * manage the more complicated telemetry scheduling, so
30 * it loses the ability to disable telem/rdf separately
33 #if defined(TELEMETRUM_V_1_0)
38 #define ao_telemetry_time time
39 #define RDF_SPACE __pdata
41 #define RDF_SPACE __xdata
42 static __pdata uint16_t ao_telemetry_time;
46 static RDF_SPACE uint8_t ao_rdf = 0;
47 static RDF_SPACE uint16_t ao_rdf_time;
51 static __pdata uint16_t ao_aprs_time;
57 #define AO_SEND_MEGA 1
60 #if defined (TELEMETRUM_V_2_0)
61 #define AO_SEND_METRUM 1
64 #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)
65 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
68 #if defined(TELEMINI_V_1_0)
69 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
72 #if defined(TELENANO_V_0_1)
73 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
76 static __xdata union ao_telemetry_all telemetry;
78 #if defined AO_TELEMETRY_SENSOR
79 /* Send sensor packet */
83 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
85 telemetry.generic.tick = packet->tick;
86 telemetry.generic.type = AO_TELEMETRY_SENSOR;
88 telemetry.sensor.state = ao_flight_state;
90 telemetry.sensor.accel = packet->adc.accel;
92 telemetry.sensor.accel = 0;
94 telemetry.sensor.pres = ao_data_pres(packet);
95 telemetry.sensor.temp = packet->adc.temp;
96 telemetry.sensor.v_batt = packet->adc.v_batt;
98 telemetry.sensor.sense_d = packet->adc.sense_d;
99 telemetry.sensor.sense_m = packet->adc.sense_m;
101 telemetry.sensor.sense_d = 0;
102 telemetry.sensor.sense_m = 0;
105 telemetry.sensor.acceleration = ao_accel;
106 telemetry.sensor.speed = ao_speed;
107 telemetry.sensor.height = ao_height;
109 telemetry.sensor.ground_pres = ao_ground_pres;
111 telemetry.sensor.ground_accel = ao_ground_accel;
112 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
113 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
115 telemetry.sensor.ground_accel = 0;
116 telemetry.sensor.accel_plus_g = 0;
117 telemetry.sensor.accel_minus_g = 0;
120 ao_radio_send(&telemetry, sizeof (telemetry));
127 /* Send mega sensor packet */
129 ao_send_mega_sensor(void)
131 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
133 telemetry.generic.tick = packet->tick;
134 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
137 telemetry.mega_sensor.orient = ao_sample_orient;
139 telemetry.mega_sensor.accel = ao_data_accel(packet);
140 telemetry.mega_sensor.pres = ao_data_pres(packet);
141 telemetry.mega_sensor.temp = ao_data_temp(packet);
144 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
145 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
146 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
148 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
149 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
150 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
154 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
155 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
156 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
159 ao_radio_send(&telemetry, sizeof (telemetry));
162 static __pdata int8_t ao_telemetry_mega_data_max;
163 static __pdata int8_t ao_telemetry_mega_data_cur;
165 /* Send mega data packet */
167 ao_send_mega_data(void)
169 if (--ao_telemetry_mega_data_cur <= 0) {
170 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
173 telemetry.generic.tick = packet->tick;
174 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
176 telemetry.mega_data.state = ao_flight_state;
177 telemetry.mega_data.v_batt = packet->adc.v_batt;
178 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
180 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
181 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
182 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
184 telemetry.mega_data.ground_pres = ao_ground_pres;
185 telemetry.mega_data.ground_accel = ao_ground_accel;
186 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
187 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
189 telemetry.mega_data.acceleration = ao_accel;
190 telemetry.mega_data.speed = ao_speed;
191 telemetry.mega_data.height = ao_height;
193 ao_radio_send(&telemetry, sizeof (telemetry));
194 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
197 #endif /* AO_SEND_MEGA */
199 #ifdef AO_SEND_METRUM
200 /* Send telemetrum sensor packet */
202 ao_send_metrum_sensor(void)
204 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
206 telemetry.generic.tick = packet->tick;
207 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
209 telemetry.metrum_sensor.state = ao_flight_state;
211 telemetry.metrum_sensor.accel = ao_data_accel(packet);
213 telemetry.metrum_sensor.pres = ao_data_pres(packet);
214 telemetry.metrum_sensor.temp = ao_data_temp(packet);
216 telemetry.metrum_sensor.acceleration = ao_accel;
217 telemetry.metrum_sensor.speed = ao_speed;
218 telemetry.metrum_sensor.height = ao_height;
220 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
221 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
222 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
224 ao_radio_send(&telemetry, sizeof (telemetry));
227 static __pdata int8_t ao_telemetry_metrum_data_max;
228 static __pdata int8_t ao_telemetry_metrum_data_cur;
230 /* Send telemetrum data packet */
232 ao_send_metrum_data(void)
234 if (--ao_telemetry_metrum_data_cur <= 0) {
235 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
237 telemetry.generic.tick = packet->tick;
238 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
240 telemetry.metrum_data.ground_pres = ao_ground_pres;
242 telemetry.metrum_data.ground_accel = ao_ground_accel;
243 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
244 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
246 telemetry.metrum_data.ground_accel = 1;
247 telemetry.metrum_data.accel_plus_g = 0;
248 telemetry.metrum_data.accel_minus_g = 2;
251 ao_radio_send(&telemetry, sizeof (telemetry));
252 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
255 #endif /* AO_SEND_METRUM */
262 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
264 telemetry.generic.tick = packet->tick;
265 telemetry.generic.type = AO_TELEMETRY_MINI;
267 telemetry.mini.state = ao_flight_state;
269 telemetry.mini.v_batt = packet->adc.v_batt;
270 telemetry.mini.sense_a = packet->adc.sense_a;
271 telemetry.mini.sense_m = packet->adc.sense_m;
273 telemetry.mini.pres = ao_data_pres(packet);
274 telemetry.mini.temp = ao_data_temp(packet);
276 telemetry.mini.acceleration = ao_accel;
277 telemetry.mini.speed = ao_speed;
278 telemetry.mini.height = ao_height;
280 telemetry.mini.ground_pres = ao_ground_pres;
282 ao_radio_send(&telemetry, sizeof (telemetry));
285 #endif /* AO_SEND_MINI */
287 static __pdata int8_t ao_telemetry_config_max;
288 static __pdata int8_t ao_telemetry_config_cur;
291 ao_send_configuration(void)
293 if (--ao_telemetry_config_cur <= 0)
295 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
296 telemetry.configuration.device = AO_idProduct_NUMBER;
298 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
300 telemetry.configuration.flight = ao_flight_number;
302 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
303 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
304 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
305 /* TeleGPS gets battery voltage instead of apogee delay */
306 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
308 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
309 telemetry.configuration.main_deploy = ao_config.main_deploy;
312 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
313 ao_xmemcpy (telemetry.configuration.callsign,
316 ao_xmemcpy (telemetry.configuration.version,
317 CODE_TO_XDATA(ao_version),
319 ao_radio_send(&telemetry, sizeof (telemetry));
320 ao_telemetry_config_cur = ao_telemetry_config_max;
326 static __pdata int8_t ao_telemetry_gps_max;
327 static __pdata int8_t ao_telemetry_loc_cur;
328 static __pdata int8_t ao_telemetry_sat_cur;
331 ao_send_location(void)
333 if (--ao_telemetry_loc_cur <= 0)
335 telemetry.generic.type = AO_TELEMETRY_LOCATION;
336 ao_mutex_get(&ao_gps_mutex);
337 ao_xmemcpy(&telemetry.location.flags,
340 telemetry.location.tick = ao_gps_tick;
341 ao_mutex_put(&ao_gps_mutex);
342 ao_radio_send(&telemetry, sizeof (telemetry));
343 ao_telemetry_loc_cur = ao_telemetry_gps_max;
348 ao_send_satellite(void)
350 if (--ao_telemetry_sat_cur <= 0)
352 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
353 ao_mutex_get(&ao_gps_mutex);
354 telemetry.satellite.channels = ao_gps_tracking_data.channels;
355 ao_xmemcpy(&telemetry.satellite.sats,
356 &ao_gps_tracking_data.sats,
357 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
358 ao_mutex_put(&ao_gps_mutex);
359 ao_radio_send(&telemetry, sizeof (telemetry));
360 ao_telemetry_sat_cur = ao_telemetry_gps_max;
367 static __pdata int8_t ao_telemetry_companion_max;
368 static __pdata int8_t ao_telemetry_companion_cur;
371 ao_send_companion(void)
373 if (--ao_telemetry_companion_cur <= 0) {
374 telemetry.generic.type = AO_TELEMETRY_COMPANION;
375 telemetry.companion.board_id = ao_companion_setup.board_id;
376 telemetry.companion.update_period = ao_companion_setup.update_period;
377 telemetry.companion.channels = ao_companion_setup.channels;
378 ao_mutex_get(&ao_companion_mutex);
379 ao_xmemcpy(&telemetry.companion.companion_data,
381 ao_companion_setup.channels * 2);
382 ao_mutex_put(&ao_companion_mutex);
383 ao_radio_send(&telemetry, sizeof (telemetry));
384 ao_telemetry_companion_cur = ao_telemetry_companion_max;
396 if (!ao_config.radio_enable)
398 while (!ao_flight_number)
399 ao_sleep(&ao_flight_number);
401 telemetry.generic.serial = ao_serial_number;
403 while (ao_telemetry_interval == 0)
404 ao_sleep(&telemetry);
406 ao_telemetry_time = time;
413 while (ao_telemetry_interval) {
414 time = ao_time() + AO_SEC_TO_TICKS(100);
416 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
420 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
423 ao_telemetry_time = ao_time() + ao_telemetry_interval;
425 ao_send_mega_sensor();
428 # ifdef AO_SEND_METRUM
429 ao_send_metrum_sensor();
430 ao_send_metrum_data();
435 # ifdef AO_TELEMETRY_SENSOR
439 if (ao_companion_running)
446 ao_send_configuration();
449 time = ao_telemetry_time;
455 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
459 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
460 #if HAS_IGNITE_REPORT
463 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
464 #if HAS_IGNITE_REPORT
465 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
466 ao_radio_continuity(c);
472 if ((int16_t) (time - ao_rdf_time) > 0)
478 if (ao_config.aprs_interval != 0) {
479 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
480 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
483 if ((int16_t) (time - ao_aprs_time) > 0)
486 #endif /* HAS_APRS */
487 delay = time - ao_time();
489 ao_sleep_for(&telemetry, delay);
497 ao_telemetry_reset_interval(void)
499 ao_telemetry_set_interval(ao_telemetry_desired_interval);
504 ao_telemetry_set_interval(uint16_t interval)
509 /* Limit max telemetry rate based on available radio bandwidth.
511 static __xdata const uint16_t min_interval[] = {
512 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
513 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
514 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
517 ao_telemetry_desired_interval = interval;
518 if (interval && interval < min_interval[ao_config.radio_rate])
519 interval = min_interval[ao_config.radio_rate];
521 ao_telemetry_interval = interval;
524 ao_telemetry_mega_data_max = 1;
526 ao_telemetry_mega_data_max = 2;
527 if (ao_telemetry_mega_data_max > cur)
529 ao_telemetry_mega_data_cur = cur;
532 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
533 if (ao_telemetry_metrum_data_max > cur)
535 ao_telemetry_metrum_data_cur = cur;
539 if (!ao_companion_setup.update_period)
540 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
541 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
542 if (ao_telemetry_companion_max > cur)
544 ao_telemetry_companion_cur = cur;
548 ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
549 if (ao_telemetry_gps_max > cur)
551 ao_telemetry_loc_cur = cur;
552 if (ao_telemetry_gps_max > cur)
554 ao_telemetry_sat_cur = cur;
557 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
558 if (ao_telemetry_config_max > cur)
560 ao_telemetry_config_cur = cur;
572 ao_wakeup(&telemetry);
577 ao_rdf_set(uint8_t rdf)
581 ao_radio_rdf_abort();
583 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
588 __xdata struct ao_task ao_telemetry_task;
593 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");