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;
79 ao_telemetry_send(void)
81 ao_radio_send(&telemetry, sizeof (telemetry));
85 #if defined AO_TELEMETRY_SENSOR
86 /* Send sensor packet */
90 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
92 telemetry.generic.tick = packet->tick;
93 telemetry.generic.type = AO_TELEMETRY_SENSOR;
95 telemetry.sensor.state = ao_flight_state;
97 telemetry.sensor.accel = packet->adc.accel;
99 telemetry.sensor.accel = 0;
101 telemetry.sensor.pres = ao_data_pres(packet);
102 telemetry.sensor.temp = packet->adc.temp;
103 telemetry.sensor.v_batt = packet->adc.v_batt;
105 telemetry.sensor.sense_d = packet->adc.sense_d;
106 telemetry.sensor.sense_m = packet->adc.sense_m;
108 telemetry.sensor.sense_d = 0;
109 telemetry.sensor.sense_m = 0;
112 telemetry.sensor.acceleration = ao_accel;
113 telemetry.sensor.speed = ao_speed;
114 telemetry.sensor.height = ao_height;
116 telemetry.sensor.ground_pres = ao_ground_pres;
118 telemetry.sensor.ground_accel = ao_ground_accel;
119 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
120 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
122 telemetry.sensor.ground_accel = 0;
123 telemetry.sensor.accel_plus_g = 0;
124 telemetry.sensor.accel_minus_g = 0;
134 /* Send mega sensor packet */
136 ao_send_mega_sensor(void)
138 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
140 telemetry.generic.tick = packet->tick;
141 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
144 telemetry.mega_sensor.orient = ao_sample_orient;
146 telemetry.mega_sensor.accel = ao_data_accel(packet);
147 telemetry.mega_sensor.pres = ao_data_pres(packet);
148 telemetry.mega_sensor.temp = ao_data_temp(packet);
151 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
152 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
153 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
155 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
156 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
157 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
161 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
162 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
163 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
169 static __pdata int8_t ao_telemetry_mega_data_max;
170 static __pdata int8_t ao_telemetry_mega_data_cur;
172 /* Send mega data packet */
174 ao_send_mega_data(void)
176 if (--ao_telemetry_mega_data_cur <= 0) {
177 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
180 telemetry.generic.tick = packet->tick;
181 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
183 telemetry.mega_data.state = ao_flight_state;
184 telemetry.mega_data.v_batt = packet->adc.v_batt;
185 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
187 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
188 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
189 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
191 telemetry.mega_data.ground_pres = ao_ground_pres;
192 telemetry.mega_data.ground_accel = ao_ground_accel;
193 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
194 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
196 telemetry.mega_data.acceleration = ao_accel;
197 telemetry.mega_data.speed = ao_speed;
198 telemetry.mega_data.height = ao_height;
200 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
204 #endif /* AO_SEND_MEGA */
206 #ifdef AO_SEND_METRUM
207 /* Send telemetrum sensor packet */
209 ao_send_metrum_sensor(void)
211 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
213 telemetry.generic.tick = packet->tick;
214 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
216 telemetry.metrum_sensor.state = ao_flight_state;
218 telemetry.metrum_sensor.accel = ao_data_accel(packet);
220 telemetry.metrum_sensor.pres = ao_data_pres(packet);
221 telemetry.metrum_sensor.temp = ao_data_temp(packet);
223 telemetry.metrum_sensor.acceleration = ao_accel;
224 telemetry.metrum_sensor.speed = ao_speed;
225 telemetry.metrum_sensor.height = ao_height;
227 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
228 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
229 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
234 static __pdata int8_t ao_telemetry_metrum_data_max;
235 static __pdata int8_t ao_telemetry_metrum_data_cur;
237 /* Send telemetrum data packet */
239 ao_send_metrum_data(void)
241 if (--ao_telemetry_metrum_data_cur <= 0) {
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_METRUM_DATA;
247 telemetry.metrum_data.ground_pres = ao_ground_pres;
249 telemetry.metrum_data.ground_accel = ao_ground_accel;
250 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
251 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
253 telemetry.metrum_data.ground_accel = 1;
254 telemetry.metrum_data.accel_plus_g = 0;
255 telemetry.metrum_data.accel_minus_g = 2;
258 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
262 #endif /* AO_SEND_METRUM */
269 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
271 telemetry.generic.tick = packet->tick;
272 telemetry.generic.type = AO_TELEMETRY_MINI;
274 telemetry.mini.state = ao_flight_state;
276 telemetry.mini.v_batt = packet->adc.v_batt;
277 telemetry.mini.sense_a = packet->adc.sense_a;
278 telemetry.mini.sense_m = packet->adc.sense_m;
280 telemetry.mini.pres = ao_data_pres(packet);
281 telemetry.mini.temp = ao_data_temp(packet);
283 telemetry.mini.acceleration = ao_accel;
284 telemetry.mini.speed = ao_speed;
285 telemetry.mini.height = ao_height;
287 telemetry.mini.ground_pres = ao_ground_pres;
292 #endif /* AO_SEND_MINI */
294 static __pdata int8_t ao_telemetry_config_max;
295 static __pdata int8_t ao_telemetry_config_cur;
298 ao_send_configuration(void)
300 if (--ao_telemetry_config_cur <= 0)
302 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
303 telemetry.configuration.device = AO_idProduct_NUMBER;
305 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
307 telemetry.configuration.flight = ao_flight_number;
309 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
310 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
311 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
312 /* TeleGPS gets battery voltage instead of apogee delay */
313 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
315 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
316 telemetry.configuration.main_deploy = ao_config.main_deploy;
319 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
320 ao_xmemcpy (telemetry.configuration.callsign,
323 ao_xmemcpy (telemetry.configuration.version,
324 CODE_TO_XDATA(ao_version),
326 ao_telemetry_config_cur = ao_telemetry_config_max;
333 static __pdata int8_t ao_telemetry_gps_max;
334 static __pdata int8_t ao_telemetry_loc_cur;
335 static __pdata int8_t ao_telemetry_sat_cur;
338 ao_send_location(void)
340 if (--ao_telemetry_loc_cur <= 0)
342 telemetry.generic.type = AO_TELEMETRY_LOCATION;
343 ao_mutex_get(&ao_gps_mutex);
344 ao_xmemcpy(&telemetry.location.flags,
347 telemetry.location.tick = ao_gps_tick;
348 ao_mutex_put(&ao_gps_mutex);
349 ao_telemetry_loc_cur = ao_telemetry_gps_max;
355 ao_send_satellite(void)
357 if (--ao_telemetry_sat_cur <= 0)
359 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
360 ao_mutex_get(&ao_gps_mutex);
361 telemetry.satellite.channels = ao_gps_tracking_data.channels;
362 ao_xmemcpy(&telemetry.satellite.sats,
363 &ao_gps_tracking_data.sats,
364 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
365 ao_mutex_put(&ao_gps_mutex);
366 ao_telemetry_sat_cur = ao_telemetry_gps_max;
374 static __pdata int8_t ao_telemetry_companion_max;
375 static __pdata int8_t ao_telemetry_companion_cur;
378 ao_send_companion(void)
380 if (--ao_telemetry_companion_cur <= 0) {
381 telemetry.generic.type = AO_TELEMETRY_COMPANION;
382 telemetry.companion.board_id = ao_companion_setup.board_id;
383 telemetry.companion.update_period = ao_companion_setup.update_period;
384 telemetry.companion.channels = ao_companion_setup.channels;
385 ao_mutex_get(&ao_companion_mutex);
386 ao_xmemcpy(&telemetry.companion.companion_data,
388 ao_companion_setup.channels * 2);
389 ao_mutex_put(&ao_companion_mutex);
390 ao_telemetry_companion_cur = ao_telemetry_companion_max;
403 if (!ao_config.radio_enable)
405 while (!ao_flight_number)
406 ao_sleep(&ao_flight_number);
408 telemetry.generic.serial = ao_serial_number;
410 while (ao_telemetry_interval == 0)
411 ao_sleep(&telemetry);
413 ao_telemetry_time = time;
420 while (ao_telemetry_interval) {
421 time = ao_time() + AO_SEC_TO_TICKS(100);
423 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
427 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
430 ao_telemetry_time = ao_time() + ao_telemetry_interval;
432 ao_send_mega_sensor();
435 # ifdef AO_SEND_METRUM
436 ao_send_metrum_sensor();
437 ao_send_metrum_data();
442 # ifdef AO_TELEMETRY_SENSOR
446 if (ao_companion_running)
453 ao_send_configuration();
456 time = ao_telemetry_time;
462 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
466 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
467 #if HAS_IGNITE_REPORT
470 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
471 #if HAS_IGNITE_REPORT
472 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
473 ao_radio_continuity(c);
479 if ((int16_t) (time - ao_rdf_time) > 0)
485 if (ao_config.aprs_interval != 0) {
486 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
487 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
490 if ((int16_t) (time - ao_aprs_time) > 0)
493 #endif /* HAS_APRS */
494 delay = time - ao_time();
496 ao_sleep_for(&telemetry, delay);
504 ao_telemetry_reset_interval(void)
506 ao_telemetry_set_interval(ao_telemetry_desired_interval);
511 ao_telemetry_set_interval(uint16_t interval)
516 /* Limit max telemetry rate based on available radio bandwidth.
518 static __xdata const uint16_t min_interval[] = {
519 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
520 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
521 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
524 ao_telemetry_desired_interval = interval;
525 if (interval && interval < min_interval[ao_config.radio_rate])
526 interval = min_interval[ao_config.radio_rate];
528 ao_telemetry_interval = interval;
531 ao_telemetry_mega_data_max = 1;
533 ao_telemetry_mega_data_max = 2;
534 if (ao_telemetry_mega_data_max > cur)
536 ao_telemetry_mega_data_cur = cur;
539 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
540 if (ao_telemetry_metrum_data_max > cur)
542 ao_telemetry_metrum_data_cur = cur;
546 if (!ao_companion_setup.update_period)
547 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
548 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
549 if (ao_telemetry_companion_max > cur)
551 ao_telemetry_companion_cur = cur;
555 ao_telemetry_gps_max = AO_SEC_TO_TICKS(1) / interval;
556 if (ao_telemetry_gps_max > cur)
558 ao_telemetry_loc_cur = cur;
559 if (ao_telemetry_gps_max > cur)
561 ao_telemetry_sat_cur = cur;
564 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
565 if (ao_telemetry_config_max > cur)
567 ao_telemetry_config_cur = cur;
579 ao_wakeup(&telemetry);
584 ao_rdf_set(uint8_t rdf)
588 ao_radio_rdf_abort();
590 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
595 __xdata struct ao_task ao_telemetry_task;
600 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");