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_SEND_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;
299 #ifndef ao_telemetry_battery_convert
300 #define ao_telemetry_battery_convert(a) (a)
304 ao_send_configuration(void)
306 if (--ao_telemetry_config_cur <= 0)
308 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
309 telemetry.configuration.device = AO_idProduct_NUMBER;
310 telemetry.configuration.flight = ao_telemetry_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_telemetry_battery_convert(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_telemetry_config_cur = ao_telemetry_config_max;
335 static __pdata int8_t ao_telemetry_gps_max;
336 static __pdata int8_t ao_telemetry_loc_cur;
337 static __pdata int8_t ao_telemetry_sat_cur;
340 ao_send_location(void)
342 if (--ao_telemetry_loc_cur <= 0)
344 telemetry.generic.type = AO_TELEMETRY_LOCATION;
345 ao_mutex_get(&ao_gps_mutex);
346 ao_xmemcpy(&telemetry.location.flags,
349 telemetry.location.tick = ao_gps_tick;
350 ao_mutex_put(&ao_gps_mutex);
351 ao_telemetry_loc_cur = ao_telemetry_gps_max;
357 ao_send_satellite(void)
359 if (--ao_telemetry_sat_cur <= 0)
361 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
362 ao_mutex_get(&ao_gps_mutex);
363 telemetry.satellite.channels = ao_gps_tracking_data.channels;
364 ao_xmemcpy(&telemetry.satellite.sats,
365 &ao_gps_tracking_data.sats,
366 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
367 ao_mutex_put(&ao_gps_mutex);
368 ao_telemetry_sat_cur = ao_telemetry_gps_max;
376 static __pdata int8_t ao_telemetry_companion_max;
377 static __pdata int8_t ao_telemetry_companion_cur;
380 ao_send_companion(void)
382 if (--ao_telemetry_companion_cur <= 0) {
383 telemetry.generic.type = AO_TELEMETRY_COMPANION;
384 telemetry.companion.board_id = ao_companion_setup.board_id;
385 telemetry.companion.update_period = ao_companion_setup.update_period;
386 telemetry.companion.channels = ao_companion_setup.channels;
387 ao_mutex_get(&ao_companion_mutex);
388 ao_xmemcpy(&telemetry.companion.companion_data,
390 ao_companion_setup.channels * 2);
391 ao_mutex_put(&ao_companion_mutex);
392 ao_telemetry_companion_cur = ao_telemetry_companion_max;
405 if (!ao_config.radio_enable)
407 while (!ao_flight_number)
408 ao_sleep(&ao_flight_number);
410 ao_telemetry_flight_number = ao_flight_number;
413 ao_telemetry_flight_number = 0;
415 telemetry.generic.serial = ao_serial_number;
417 while (ao_telemetry_interval == 0)
418 ao_sleep(&telemetry);
420 ao_telemetry_time = time;
427 while (ao_telemetry_interval) {
428 time = ao_time() + AO_SEC_TO_TICKS(100);
430 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
434 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
437 ao_telemetry_time = ao_time() + ao_telemetry_interval;
439 ao_send_mega_sensor();
442 # ifdef AO_SEND_METRUM
443 ao_send_metrum_sensor();
444 ao_send_metrum_data();
449 # ifdef AO_TELEMETRY_SENSOR
453 if (ao_companion_running)
460 ao_send_configuration();
463 time = ao_telemetry_time;
469 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
473 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
474 #if HAS_IGNITE_REPORT
477 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
478 #if HAS_IGNITE_REPORT
479 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
480 ao_radio_continuity(c);
486 if ((int16_t) (time - ao_rdf_time) > 0)
492 if (ao_config.aprs_interval != 0) {
493 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
494 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
497 if ((int16_t) (time - ao_aprs_time) > 0)
500 #endif /* HAS_APRS */
501 delay = time - ao_time();
503 ao_sleep_for(&telemetry, delay);
511 ao_telemetry_reset_interval(void)
513 ao_telemetry_set_interval(ao_telemetry_desired_interval);
518 ao_telemetry_set_interval(uint16_t interval)
523 /* Limit max telemetry rate based on available radio bandwidth.
525 static __xdata const uint16_t min_interval[] = {
526 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
527 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
528 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
531 ao_telemetry_desired_interval = interval;
532 if (interval && interval < min_interval[ao_config.radio_rate])
533 interval = min_interval[ao_config.radio_rate];
535 ao_telemetry_interval = interval;
538 ao_telemetry_mega_data_max = 1;
540 ao_telemetry_mega_data_max = 2;
541 if (ao_telemetry_mega_data_max > cur)
543 ao_telemetry_mega_data_cur = cur;
546 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
547 if (ao_telemetry_metrum_data_max > cur)
549 ao_telemetry_metrum_data_cur = cur;
553 if (!ao_companion_setup.update_period)
554 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
555 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
556 if (ao_telemetry_companion_max > cur)
558 ao_telemetry_companion_cur = cur;
562 ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
563 if (ao_telemetry_gps_max > cur)
565 ao_telemetry_loc_cur = cur;
566 if (ao_telemetry_gps_max > cur)
568 ao_telemetry_sat_cur = cur;
571 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
572 if (ao_telemetry_config_max > cur)
574 ao_telemetry_config_cur = cur;
586 ao_wakeup(&telemetry);
591 ao_rdf_set(uint8_t rdf)
595 ao_radio_rdf_abort();
597 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
602 __xdata struct ao_task ao_telemetry_task;
607 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");