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"
26 static __pdata uint16_t ao_telemetry_interval;
29 static __xdata uint16_t ao_telemetry_desired_interval;
33 static __pdata uint8_t ao_rdf = 0;
34 static __pdata uint16_t ao_rdf_time;
38 static __pdata uint16_t ao_aprs_time;
44 #define AO_SEND_MEGA 1
47 #if defined (TELEMETRUM_V_2_0)
48 #define AO_SEND_METRUM 1
51 #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)
52 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
55 #if defined(TELEMINI_V_1_0)
56 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
59 #if defined(TELENANO_V_0_1)
60 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
63 static __xdata union ao_telemetry_all telemetry;
65 #if defined AO_TELEMETRY_SENSOR
66 /* Send sensor packet */
70 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
72 telemetry.generic.tick = packet->tick;
73 telemetry.generic.type = AO_TELEMETRY_SENSOR;
75 telemetry.sensor.state = ao_flight_state;
77 telemetry.sensor.accel = packet->adc.accel;
79 telemetry.sensor.accel = 0;
81 telemetry.sensor.pres = ao_data_pres(packet);
82 telemetry.sensor.temp = packet->adc.temp;
83 telemetry.sensor.v_batt = packet->adc.v_batt;
85 telemetry.sensor.sense_d = packet->adc.sense_d;
86 telemetry.sensor.sense_m = packet->adc.sense_m;
88 telemetry.sensor.sense_d = 0;
89 telemetry.sensor.sense_m = 0;
92 telemetry.sensor.acceleration = ao_accel;
93 telemetry.sensor.speed = ao_speed;
94 telemetry.sensor.height = ao_height;
96 telemetry.sensor.ground_pres = ao_ground_pres;
98 telemetry.sensor.ground_accel = ao_ground_accel;
99 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
100 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
102 telemetry.sensor.ground_accel = 0;
103 telemetry.sensor.accel_plus_g = 0;
104 telemetry.sensor.accel_minus_g = 0;
107 ao_radio_send(&telemetry, sizeof (telemetry));
114 /* Send mega sensor packet */
116 ao_send_mega_sensor(void)
118 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
120 telemetry.generic.tick = packet->tick;
121 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
123 telemetry.mega_sensor.orient = ao_sample_orient;
124 telemetry.mega_sensor.accel = ao_data_accel(packet);
125 telemetry.mega_sensor.pres = ao_data_pres(packet);
126 telemetry.mega_sensor.temp = ao_data_temp(packet);
129 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
130 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
131 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
133 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
134 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
135 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
139 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
140 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
141 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
144 ao_radio_send(&telemetry, sizeof (telemetry));
147 static __pdata int8_t ao_telemetry_mega_data_max;
148 static __pdata int8_t ao_telemetry_mega_data_cur;
150 /* Send mega data packet */
152 ao_send_mega_data(void)
154 if (--ao_telemetry_mega_data_cur <= 0) {
155 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
158 telemetry.generic.tick = packet->tick;
159 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
161 telemetry.mega_data.state = ao_flight_state;
162 telemetry.mega_data.v_batt = packet->adc.v_batt;
163 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
165 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
166 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
167 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
169 telemetry.mega_data.ground_pres = ao_ground_pres;
170 telemetry.mega_data.ground_accel = ao_ground_accel;
171 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
172 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
174 telemetry.mega_data.acceleration = ao_accel;
175 telemetry.mega_data.speed = ao_speed;
176 telemetry.mega_data.height = ao_height;
178 ao_radio_send(&telemetry, sizeof (telemetry));
179 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
182 #endif /* AO_SEND_MEGA */
184 #ifdef AO_SEND_METRUM
185 /* Send telemetrum sensor packet */
187 ao_send_metrum_sensor(void)
189 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
191 telemetry.generic.tick = packet->tick;
192 telemetry.generic.type = AO_TELEMETRY_METRUM_SENSOR;
194 telemetry.metrum_sensor.state = ao_flight_state;
196 telemetry.metrum_sensor.accel = ao_data_accel(packet);
198 telemetry.metrum_sensor.pres = ao_data_pres(packet);
199 telemetry.metrum_sensor.temp = ao_data_temp(packet);
201 telemetry.metrum_sensor.acceleration = ao_accel;
202 telemetry.metrum_sensor.speed = ao_speed;
203 telemetry.metrum_sensor.height = ao_height;
205 telemetry.metrum_sensor.v_batt = packet->adc.v_batt;
206 telemetry.metrum_sensor.sense_a = packet->adc.sense_a;
207 telemetry.metrum_sensor.sense_m = packet->adc.sense_m;
209 ao_radio_send(&telemetry, sizeof (telemetry));
212 static __pdata int8_t ao_telemetry_metrum_data_max;
213 static __pdata int8_t ao_telemetry_metrum_data_cur;
215 /* Send telemetrum data packet */
217 ao_send_metrum_data(void)
219 if (--ao_telemetry_metrum_data_cur <= 0) {
220 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
222 telemetry.generic.tick = packet->tick;
223 telemetry.generic.type = AO_TELEMETRY_METRUM_DATA;
225 telemetry.metrum_data.ground_pres = ao_ground_pres;
227 telemetry.metrum_data.ground_accel = ao_ground_accel;
228 telemetry.metrum_data.accel_plus_g = ao_config.accel_plus_g;
229 telemetry.metrum_data.accel_minus_g = ao_config.accel_minus_g;
231 telemetry.metrum_data.ground_accel = 1;
232 telemetry.metrum_data.accel_plus_g = 0;
233 telemetry.metrum_data.accel_minus_g = 2;
236 ao_radio_send(&telemetry, sizeof (telemetry));
237 ao_telemetry_metrum_data_cur = ao_telemetry_metrum_data_max;
240 #endif /* AO_SEND_METRUM */
247 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
249 telemetry.generic.tick = packet->tick;
250 telemetry.generic.type = AO_TELEMETRY_MINI;
252 telemetry.mini.state = ao_flight_state;
254 telemetry.mini.v_batt = packet->adc.v_batt;
255 telemetry.mini.sense_a = packet->adc.sense_a;
256 telemetry.mini.sense_m = packet->adc.sense_m;
258 telemetry.mini.pres = ao_data_pres(packet);
259 telemetry.mini.temp = ao_data_temp(packet);
261 telemetry.mini.acceleration = ao_accel;
262 telemetry.mini.speed = ao_speed;
263 telemetry.mini.height = ao_height;
265 telemetry.mini.ground_pres = ao_ground_pres;
267 ao_radio_send(&telemetry, sizeof (telemetry));
270 #endif /* AO_SEND_MINI */
272 static __pdata int8_t ao_telemetry_config_max;
273 static __pdata int8_t ao_telemetry_config_cur;
276 ao_send_configuration(void)
278 if (--ao_telemetry_config_cur <= 0)
280 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
281 telemetry.configuration.device = AO_idProduct_NUMBER;
283 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
285 telemetry.configuration.flight = ao_flight_number;
287 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
288 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
289 #if AO_idProduct_NUMBER == 0x25 && HAS_ADC
290 /* TeleGPS gets battery voltage instead of apogee delay */
291 telemetry.configuration.apogee_delay = ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.v_batt;
293 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
294 telemetry.configuration.main_deploy = ao_config.main_deploy;
297 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
298 ao_xmemcpy (telemetry.configuration.callsign,
301 ao_xmemcpy (telemetry.configuration.version,
302 CODE_TO_XDATA(ao_version),
304 ao_radio_send(&telemetry, sizeof (telemetry));
305 ao_telemetry_config_cur = ao_telemetry_config_max;
311 static __pdata int8_t ao_telemetry_loc_cur;
312 static __pdata int8_t ao_telemetry_sat_cur;
315 ao_send_location(void)
317 if (--ao_telemetry_loc_cur <= 0)
319 telemetry.generic.type = AO_TELEMETRY_LOCATION;
320 ao_mutex_get(&ao_gps_mutex);
321 ao_xmemcpy(&telemetry.location.flags,
324 telemetry.location.tick = ao_gps_tick;
325 ao_mutex_put(&ao_gps_mutex);
326 ao_radio_send(&telemetry, sizeof (telemetry));
327 ao_telemetry_loc_cur = ao_telemetry_config_max;
332 ao_send_satellite(void)
334 if (--ao_telemetry_sat_cur <= 0)
336 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
337 ao_mutex_get(&ao_gps_mutex);
338 telemetry.satellite.channels = ao_gps_tracking_data.channels;
339 ao_xmemcpy(&telemetry.satellite.sats,
340 &ao_gps_tracking_data.sats,
341 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
342 ao_mutex_put(&ao_gps_mutex);
343 ao_radio_send(&telemetry, sizeof (telemetry));
344 ao_telemetry_sat_cur = ao_telemetry_config_max;
351 static __pdata int8_t ao_telemetry_companion_max;
352 static __pdata int8_t ao_telemetry_companion_cur;
355 ao_send_companion(void)
357 if (--ao_telemetry_companion_cur <= 0) {
358 telemetry.generic.type = AO_TELEMETRY_COMPANION;
359 telemetry.companion.board_id = ao_companion_setup.board_id;
360 telemetry.companion.update_period = ao_companion_setup.update_period;
361 telemetry.companion.channels = ao_companion_setup.channels;
362 ao_mutex_get(&ao_companion_mutex);
363 ao_xmemcpy(&telemetry.companion.companion_data,
365 ao_companion_setup.channels * 2);
366 ao_mutex_put(&ao_companion_mutex);
367 ao_radio_send(&telemetry, sizeof (telemetry));
368 ao_telemetry_companion_cur = ao_telemetry_companion_max;
380 if (!ao_config.radio_enable)
382 while (!ao_flight_number)
383 ao_sleep(&ao_flight_number);
385 telemetry.generic.serial = ao_serial_number;
387 while (ao_telemetry_interval == 0)
388 ao_sleep(&telemetry);
396 while (ao_telemetry_interval) {
398 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
403 ao_send_mega_sensor();
406 # ifdef AO_SEND_METRUM
407 ao_send_metrum_sensor();
408 ao_send_metrum_data();
413 # ifdef AO_TELEMETRY_SENSOR
416 #endif /* HAS_FLIGHT */
419 if (ao_companion_running)
422 ao_send_configuration();
431 !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
432 #endif /* HAS_APRS */
433 (int16_t) (ao_time() - ao_rdf_time) >= 0)
435 #if HAS_IGNITE_REPORT
437 #endif /* HAS_IGNITE_REPORT */
438 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
439 #if HAS_IGNITE_REPORT
440 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
441 ao_radio_continuity(c);
443 #endif /* HAS_IGNITE_REPORT*/
448 if (ao_config.aprs_interval != 0 &&
449 (int16_t) (ao_time() - ao_aprs_time) >= 0)
451 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
454 #endif /* HAS_APRS */
455 time += ao_telemetry_interval;
456 delay = time - ao_time();
459 ao_sleep(&telemetry);
470 ao_telemetry_reset_interval(void)
472 ao_telemetry_set_interval(ao_telemetry_desired_interval);
477 ao_telemetry_set_interval(uint16_t interval)
482 /* Limit max telemetry rate based on available radio bandwidth.
484 static __xdata const uint16_t min_interval[] = {
485 /* [AO_RADIO_RATE_38400] = */ AO_MS_TO_TICKS(100),
486 /* [AO_RADIO_RATE_9600] = */ AO_MS_TO_TICKS(500),
487 /* [AO_RADIO_RATE_2400] = */ AO_MS_TO_TICKS(1000)
490 ao_telemetry_desired_interval = interval;
491 if (interval && interval < min_interval[ao_config.radio_rate])
492 interval = min_interval[ao_config.radio_rate];
494 ao_telemetry_interval = interval;
497 ao_telemetry_mega_data_max = 1;
499 ao_telemetry_mega_data_max = 2;
500 if (ao_telemetry_mega_data_max > cur)
502 ao_telemetry_mega_data_cur = cur;
505 ao_telemetry_metrum_data_max = AO_SEC_TO_TICKS(1) / interval;
506 if (ao_telemetry_metrum_data_max > cur)
508 ao_telemetry_metrum_data_cur = cur;
512 if (!ao_companion_setup.update_period)
513 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
514 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
515 if (ao_telemetry_companion_max > cur)
517 ao_telemetry_companion_cur = cur;
520 ao_telemetry_config_max = AO_SEC_TO_TICKS(5) / interval;
522 if (ao_telemetry_config_max > cur)
524 ao_telemetry_config_cur = cur;
528 if (ao_telemetry_config_max > cur)
530 ao_telemetry_loc_cur = cur;
531 if (ao_telemetry_config_max > cur)
533 ao_telemetry_sat_cur = cur;
535 ao_wakeup(&telemetry);
540 ao_rdf_set(uint8_t rdf)
544 ao_radio_rdf_abort();
546 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
551 __xdata struct ao_task ao_telemetry_task;
556 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");