2 * Copyright © 2011 Keith 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.
19 #include "ao_product.h"
21 static __pdata uint16_t ao_telemetry_interval;
22 static __pdata uint8_t ao_rdf = 0;
23 static __pdata uint16_t ao_rdf_time;
26 static __pdata uint16_t ao_aprs_time;
31 #if defined(MEGAMETRUM)
32 #define AO_SEND_MEGA 1
35 #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)
36 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
39 #if defined(TELEMINI_V_1_0)
40 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
43 #if defined(TELENANO_V_0_1)
44 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
47 static __xdata union ao_telemetry_all telemetry;
49 #if defined AO_TELEMETRY_SENSOR
50 /* Send sensor packet */
54 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
56 telemetry.generic.tick = packet->tick;
57 telemetry.generic.type = AO_TELEMETRY_SENSOR;
59 telemetry.sensor.state = ao_flight_state;
61 telemetry.sensor.accel = packet->adc.accel;
63 telemetry.sensor.accel = 0;
65 telemetry.sensor.pres = ao_data_pres(packet);
66 telemetry.sensor.temp = packet->adc.temp;
67 telemetry.sensor.v_batt = packet->adc.v_batt;
69 telemetry.sensor.sense_d = packet->adc.sense_d;
70 telemetry.sensor.sense_m = packet->adc.sense_m;
72 telemetry.sensor.sense_d = 0;
73 telemetry.sensor.sense_m = 0;
76 telemetry.sensor.acceleration = ao_accel;
77 telemetry.sensor.speed = ao_speed;
78 telemetry.sensor.height = ao_height;
80 telemetry.sensor.ground_pres = ao_ground_pres;
82 telemetry.sensor.ground_accel = ao_ground_accel;
83 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
84 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
86 telemetry.sensor.ground_accel = 0;
87 telemetry.sensor.accel_plus_g = 0;
88 telemetry.sensor.accel_minus_g = 0;
91 ao_radio_send(&telemetry, sizeof (telemetry));
97 /* Send mega sensor packet */
99 ao_send_mega_sensor(void)
101 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
103 telemetry.generic.tick = packet->tick;
104 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
106 telemetry.mega_sensor.accel = ao_data_accel(packet);
107 telemetry.mega_sensor.pres = ao_data_pres(packet);
108 telemetry.mega_sensor.temp = ao_data_temp(packet);
111 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
112 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
113 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
115 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
116 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
117 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
121 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
122 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
123 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
126 ao_radio_send(&telemetry, sizeof (telemetry));
129 static __pdata int8_t ao_telemetry_mega_data_max;
130 static __pdata int8_t ao_telemetry_mega_data_cur;
132 /* Send mega data packet */
134 ao_send_mega_data(void)
136 if (--ao_telemetry_mega_data_cur <= 0) {
137 __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_DATA;
143 telemetry.mega_data.state = ao_flight_state;
144 telemetry.mega_data.v_batt = packet->adc.v_batt;
145 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
147 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
148 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
149 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
151 telemetry.mega_data.ground_pres = ao_ground_pres;
152 telemetry.mega_data.ground_accel = ao_ground_accel;
153 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
154 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
156 telemetry.mega_data.acceleration = ao_accel;
157 telemetry.mega_data.speed = ao_speed;
158 telemetry.mega_data.height = ao_height;
160 ao_radio_send(&telemetry, sizeof (telemetry));
161 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
164 #endif /* AO_SEND_MEGA */
166 #ifdef AO_SEND_ALL_BARO
167 static uint8_t ao_baro_sample;
172 uint8_t sample = ao_sample_data;
173 uint8_t samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
176 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
179 telemetry.generic.tick = ao_data_ring[sample].tick;
180 telemetry.generic.type = AO_TELEMETRY_BARO;
181 telemetry.baro.samples = samples;
182 for (sample = 0; sample < samples; sample++) {
183 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
184 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
186 ao_radio_send(&telemetry, sizeof (telemetry));
190 static __pdata int8_t ao_telemetry_config_max;
191 static __pdata int8_t ao_telemetry_config_cur;
194 ao_send_configuration(void)
196 if (--ao_telemetry_config_cur <= 0)
198 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
199 telemetry.configuration.device = AO_idProduct_NUMBER;
200 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
201 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
202 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
203 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
204 telemetry.configuration.main_deploy = ao_config.main_deploy;
205 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
206 ao_xmemcpy (telemetry.configuration.callsign,
209 ao_xmemcpy (telemetry.configuration.version,
210 CODE_TO_XDATA(ao_version),
212 ao_radio_send(&telemetry, sizeof (telemetry));
213 ao_telemetry_config_cur = ao_telemetry_config_max;
219 static __pdata int8_t ao_telemetry_loc_cur;
220 static __pdata int8_t ao_telemetry_sat_cur;
223 ao_send_location(void)
225 if (--ao_telemetry_loc_cur <= 0)
227 telemetry.generic.type = AO_TELEMETRY_LOCATION;
228 ao_mutex_get(&ao_gps_mutex);
229 ao_xmemcpy(&telemetry.location.flags,
232 ao_mutex_put(&ao_gps_mutex);
233 ao_radio_send(&telemetry, sizeof (telemetry));
234 ao_telemetry_loc_cur = ao_telemetry_config_max;
239 ao_send_satellite(void)
241 if (--ao_telemetry_sat_cur <= 0)
243 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
244 ao_mutex_get(&ao_gps_mutex);
245 telemetry.satellite.channels = ao_gps_tracking_data.channels;
246 ao_xmemcpy(&telemetry.satellite.sats,
247 &ao_gps_tracking_data.sats,
248 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
249 ao_mutex_put(&ao_gps_mutex);
250 ao_radio_send(&telemetry, sizeof (telemetry));
251 ao_telemetry_sat_cur = ao_telemetry_config_max;
258 static __pdata int8_t ao_telemetry_companion_max;
259 static __pdata int8_t ao_telemetry_companion_cur;
262 ao_send_companion(void)
264 if (--ao_telemetry_companion_cur <= 0) {
265 telemetry.generic.type = AO_TELEMETRY_COMPANION;
266 telemetry.companion.board_id = ao_companion_setup.board_id;
267 telemetry.companion.update_period = ao_companion_setup.update_period;
268 telemetry.companion.channels = ao_companion_setup.channels;
269 ao_mutex_get(&ao_companion_mutex);
270 ao_xmemcpy(&telemetry.companion.companion_data,
272 ao_companion_setup.channels * 2);
273 ao_mutex_put(&ao_companion_mutex);
274 ao_radio_send(&telemetry, sizeof (telemetry));
275 ao_telemetry_companion_cur = ao_telemetry_companion_max;
287 if (!ao_config.radio_enable)
289 while (!ao_flight_number)
290 ao_sleep(&ao_flight_number);
292 telemetry.generic.serial = ao_serial_number;
294 while (ao_telemetry_interval == 0)
295 ao_sleep(&telemetry);
296 time = ao_rdf_time = ao_time();
300 while (ao_telemetry_interval) {
303 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
306 #ifdef AO_SEND_ALL_BARO
310 ao_send_mega_sensor();
317 if (ao_companion_running)
320 ao_send_configuration();
326 #ifndef AO_SEND_ALL_BARO
329 !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
331 (int16_t) (ao_time() - ao_rdf_time) >= 0)
333 #if HAS_IGNITE_REPORT
336 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
337 #if HAS_IGNITE_REPORT
338 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
339 ao_radio_continuity(c);
345 if (ao_config.aprs_interval != 0 &&
346 (int16_t) (ao_time() - ao_aprs_time) >= 0)
348 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
353 time += ao_telemetry_interval;
354 delay = time - ao_time();
357 ao_sleep(&telemetry);
368 ao_telemetry_set_interval(uint16_t interval)
371 ao_telemetry_interval = interval;
375 ao_telemetry_mega_data_max = 1;
377 ao_telemetry_mega_data_max = 2;
378 if (ao_telemetry_mega_data_max > cur)
380 ao_telemetry_mega_data_cur = cur;
384 if (!ao_companion_setup.update_period)
385 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
386 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
387 if (ao_telemetry_companion_max > cur)
389 ao_telemetry_companion_cur = cur;
392 ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
394 if (ao_telemetry_config_max > cur)
396 ao_telemetry_config_cur = cur;
400 if (ao_telemetry_config_max > cur)
402 ao_telemetry_loc_cur = cur;
403 if (ao_telemetry_config_max > cur)
405 ao_telemetry_sat_cur = cur;
407 ao_wakeup(&telemetry);
411 ao_rdf_set(uint8_t rdf)
415 ao_radio_rdf_abort();
417 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
421 __xdata struct ao_task ao_telemetry_task;
426 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");