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;
136 telemetry.mega_sensor.orient = ao_sample_orient;
137 telemetry.mega_sensor.accel = ao_data_accel(packet);
138 telemetry.mega_sensor.pres = ao_data_pres(packet);
139 telemetry.mega_sensor.temp = ao_data_temp(packet);
142 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
143 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
144 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
146 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
147 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
148 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
152 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
153 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
154 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
157 ao_radio_send(&telemetry, sizeof (telemetry));
160 static __pdata int8_t ao_telemetry_mega_data_max;
161 static __pdata int8_t ao_telemetry_mega_data_cur;
163 /* Send mega data packet */
165 ao_send_mega_data(void)
167 if (--ao_telemetry_mega_data_cur <= 0) {
168 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
171 telemetry.generic.tick = packet->tick;
172 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
174 telemetry.mega_data.state = ao_flight_state;
175 telemetry.mega_data.v_batt = packet->adc.v_batt;
176 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
178 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
179 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
180 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
182 telemetry.mega_data.ground_pres = ao_ground_pres;
183 telemetry.mega_data.ground_accel = ao_ground_accel;
184 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
185 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
187 telemetry.mega_data.acceleration = ao_accel;
188 telemetry.mega_data.speed = ao_speed;
189 telemetry.mega_data.height = ao_height;
191 ao_radio_send(&telemetry, sizeof (telemetry));
192 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
195 #endif /* AO_SEND_MEGA */
197 #ifdef AO_SEND_METRUM
198 /* Send telemetrum sensor packet */
200 ao_send_metrum_sensor(void)
202 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
204 telemetry.generic.tick = packet->tick;
205 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
207 telemetry.metrum_sensor.state = ao_flight_state;
209 telemetry.metrum_sensor.accel = ao_data_accel(packet);
211 telemetry.metrum_sensor.pres = ao_data_pres(packet);
212 telemetry.metrum_sensor.temp = ao_data_temp(packet);
214 telemetry.metrum_sensor.acceleration = ao_accel;
215 telemetry.metrum_sensor.speed = ao_speed;
216 telemetry.metrum_sensor.height = ao_height;
218 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
219 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
220 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
222 ao_radio_send(&telemetry, sizeof (telemetry));
225 static __pdata int8_t ao_telemetry_metrum_data_max;
226 static __pdata int8_t ao_telemetry_metrum_data_cur;
228 /* Send telemetrum data packet */
230 ao_send_metrum_data(void)
232 if (--ao_telemetry_metrum_data_cur <= 0) {
233 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
235 telemetry.generic.tick = packet->tick;
236 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
238 telemetry.metrum_data.ground_pres = ao_ground_pres;
240 telemetry.metrum_data.ground_accel = ao_ground_accel;
241 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
242 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
244 telemetry.metrum_data.ground_accel = 1;
245 telemetry.metrum_data.accel_plus_g = 0;
246 telemetry.metrum_data.accel_minus_g = 2;
249 ao_radio_send(&telemetry, sizeof (telemetry));
250 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
253 #endif /* AO_SEND_METRUM */
260 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
262 telemetry.generic.tick = packet->tick;
263 telemetry.generic.type = AO_TELEMETRY_MINI;
265 telemetry.mini.state = ao_flight_state;
267 telemetry.mini.v_batt = packet->adc.v_batt;
268 telemetry.mini.sense_a = packet->adc.sense_a;
269 telemetry.mini.sense_m = packet->adc.sense_m;
271 telemetry.mini.pres = ao_data_pres(packet);
272 telemetry.mini.temp = ao_data_temp(packet);
274 telemetry.mini.acceleration = ao_accel;
275 telemetry.mini.speed = ao_speed;
276 telemetry.mini.height = ao_height;
278 telemetry.mini.ground_pres = ao_ground_pres;
280 ao_radio_send(&telemetry, sizeof (telemetry));
283 #endif /* AO_SEND_MINI */
285 static __pdata int8_t ao_telemetry_config_max;
286 static __pdata int8_t ao_telemetry_config_cur;
289 ao_send_configuration(void)
291 if (--ao_telemetry_config_cur <= 0)
293 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
294 telemetry.configuration.device = AO_idProduct_NUMBER;
296 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
298 telemetry.configuration.flight = ao_flight_number;
300 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
301 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
302 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
303 /* TeleGPS gets battery voltage instead of apogee delay */
304 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
306 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
307 telemetry.configuration.main_deploy = ao_config.main_deploy;
310 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
311 ao_xmemcpy (telemetry.configuration.callsign,
314 ao_xmemcpy (telemetry.configuration.version,
315 CODE_TO_XDATA(ao_version),
317 ao_radio_send(&telemetry, sizeof (telemetry));
318 ao_telemetry_config_cur = ao_telemetry_config_max;
324 static __pdata int8_t ao_telemetry_gps_max;
325 static __pdata int8_t ao_telemetry_loc_cur;
326 static __pdata int8_t ao_telemetry_sat_cur;
329 ao_send_location(void)
331 if (--ao_telemetry_loc_cur <= 0)
333 telemetry.generic.type = AO_TELEMETRY_LOCATION;
334 ao_mutex_get(&ao_gps_mutex);
335 ao_xmemcpy(&telemetry.location.flags,
338 telemetry.location.tick = ao_gps_tick;
339 ao_mutex_put(&ao_gps_mutex);
340 ao_radio_send(&telemetry, sizeof (telemetry));
341 ao_telemetry_loc_cur = ao_telemetry_gps_max;
346 ao_send_satellite(void)
348 if (--ao_telemetry_sat_cur <= 0)
350 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
351 ao_mutex_get(&ao_gps_mutex);
352 telemetry.satellite.channels = ao_gps_tracking_data.channels;
353 ao_xmemcpy(&telemetry.satellite.sats,
354 &ao_gps_tracking_data.sats,
355 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
356 ao_mutex_put(&ao_gps_mutex);
357 ao_radio_send(&telemetry, sizeof (telemetry));
358 ao_telemetry_sat_cur = ao_telemetry_gps_max;
365 static __pdata int8_t ao_telemetry_companion_max;
366 static __pdata int8_t ao_telemetry_companion_cur;
369 ao_send_companion(void)
371 if (--ao_telemetry_companion_cur <= 0) {
372 telemetry.generic.type = AO_TELEMETRY_COMPANION;
373 telemetry.companion.board_id = ao_companion_setup.board_id;
374 telemetry.companion.update_period = ao_companion_setup.update_period;
375 telemetry.companion.channels = ao_companion_setup.channels;
376 ao_mutex_get(&ao_companion_mutex);
377 ao_xmemcpy(&telemetry.companion.companion_data,
379 ao_companion_setup.channels * 2);
380 ao_mutex_put(&ao_companion_mutex);
381 ao_radio_send(&telemetry, sizeof (telemetry));
382 ao_telemetry_companion_cur = ao_telemetry_companion_max;
394 if (!ao_config.radio_enable)
396 while (!ao_flight_number)
397 ao_sleep(&ao_flight_number);
399 telemetry.generic.serial = ao_serial_number;
401 while (ao_telemetry_interval == 0)
402 ao_sleep(&telemetry);
404 ao_telemetry_time = time;
411 while (ao_telemetry_interval) {
412 time = ao_time() + AO_SEC_TO_TICKS(100);
414 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
418 if ( (int16_t) (ao_time() - ao_telemetry_time) >= 0)
421 ao_telemetry_time = ao_time() + ao_telemetry_interval;
423 ao_send_mega_sensor();
426 # ifdef AO_SEND_METRUM
427 ao_send_metrum_sensor();
428 ao_send_metrum_data();
433 # ifdef AO_TELEMETRY_SENSOR
437 if (ao_companion_running)
444 ao_send_configuration();
447 time = ao_telemetry_time;
453 && !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF)
457 if ((int16_t) (ao_time() - ao_rdf_time) >= 0) {
458 #if HAS_IGNITE_REPORT
461 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
462 #if HAS_IGNITE_REPORT
463 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
464 ao_radio_continuity(c);
470 if ((int16_t) (time - ao_rdf_time) > 0)
476 if (ao_config.aprs_interval != 0) {
477 if ((int16_t) (ao_time() - ao_aprs_time) >= 0) {
478 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
481 if ((int16_t) (time - ao_aprs_time) > 0)
484 #endif /* HAS_APRS */
485 delay = time - ao_time();
488 ao_sleep(&telemetry);
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");