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.
20 #include "ao_product.h"
22 static __pdata uint16_t ao_telemetry_interval;
23 static __pdata uint8_t ao_rdf = 0;
24 static __pdata uint16_t ao_rdf_time;
27 static __pdata uint16_t ao_aprs_time;
33 #define AO_SEND_MEGA 1
36 #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)
37 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
40 #if defined(TELEMINI_V_1_0)
41 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
44 #if defined(TELENANO_V_0_1)
45 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
48 static __xdata union ao_telemetry_all telemetry;
50 #if defined AO_TELEMETRY_SENSOR
51 /* Send sensor packet */
55 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
57 telemetry.generic.tick = packet->tick;
58 telemetry.generic.type = AO_TELEMETRY_SENSOR;
60 telemetry.sensor.state = ao_flight_state;
62 telemetry.sensor.accel = packet->adc.accel;
64 telemetry.sensor.accel = 0;
66 telemetry.sensor.pres = ao_data_pres(packet);
67 telemetry.sensor.temp = packet->adc.temp;
68 telemetry.sensor.v_batt = packet->adc.v_batt;
70 telemetry.sensor.sense_d = packet->adc.sense_d;
71 telemetry.sensor.sense_m = packet->adc.sense_m;
73 telemetry.sensor.sense_d = 0;
74 telemetry.sensor.sense_m = 0;
77 telemetry.sensor.acceleration = ao_accel;
78 telemetry.sensor.speed = ao_speed;
79 telemetry.sensor.height = ao_height;
81 telemetry.sensor.ground_pres = ao_ground_pres;
83 telemetry.sensor.ground_accel = ao_ground_accel;
84 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
85 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
87 telemetry.sensor.ground_accel = 0;
88 telemetry.sensor.accel_plus_g = 0;
89 telemetry.sensor.accel_minus_g = 0;
92 ao_radio_send(&telemetry, sizeof (telemetry));
98 /* Send mega sensor packet */
100 ao_send_mega_sensor(void)
102 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
104 telemetry.generic.tick = packet->tick;
105 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
107 telemetry.mega_sensor.accel = ao_data_accel(packet);
108 telemetry.mega_sensor.pres = ao_data_pres(packet);
109 telemetry.mega_sensor.temp = ao_data_temp(packet);
112 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
113 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
114 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
116 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
117 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
118 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
122 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
123 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
124 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
127 ao_radio_send(&telemetry, sizeof (telemetry));
130 static __pdata int8_t ao_telemetry_mega_data_max;
131 static __pdata int8_t ao_telemetry_mega_data_cur;
133 /* Send mega data packet */
135 ao_send_mega_data(void)
137 if (--ao_telemetry_mega_data_cur <= 0) {
138 __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_DATA;
144 telemetry.mega_data.state = ao_flight_state;
145 telemetry.mega_data.v_batt = packet->adc.v_batt;
146 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
148 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
149 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
150 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
152 telemetry.mega_data.ground_pres = ao_ground_pres;
153 telemetry.mega_data.ground_accel = ao_ground_accel;
154 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
155 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
157 telemetry.mega_data.acceleration = ao_accel;
158 telemetry.mega_data.speed = ao_speed;
159 telemetry.mega_data.height = ao_height;
161 ao_radio_send(&telemetry, sizeof (telemetry));
162 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
165 #endif /* AO_SEND_MEGA */
167 #ifdef AO_SEND_ALL_BARO
168 static uint8_t ao_baro_sample;
173 uint8_t sample = ao_sample_data;
174 uint8_t samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
177 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
180 telemetry.generic.tick = ao_data_ring[sample].tick;
181 telemetry.generic.type = AO_TELEMETRY_BARO;
182 telemetry.baro.samples = samples;
183 for (sample = 0; sample < samples; sample++) {
184 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
185 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
187 ao_radio_send(&telemetry, sizeof (telemetry));
191 static __pdata int8_t ao_telemetry_config_max;
192 static __pdata int8_t ao_telemetry_config_cur;
195 ao_send_configuration(void)
197 if (--ao_telemetry_config_cur <= 0)
199 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
200 telemetry.configuration.device = AO_idProduct_NUMBER;
202 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
204 telemetry.configuration.flight = ao_flight_number;
206 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
207 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
208 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
209 telemetry.configuration.main_deploy = ao_config.main_deploy;
210 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
211 ao_xmemcpy (telemetry.configuration.callsign,
214 ao_xmemcpy (telemetry.configuration.version,
215 CODE_TO_XDATA(ao_version),
217 ao_radio_send(&telemetry, sizeof (telemetry));
218 ao_telemetry_config_cur = ao_telemetry_config_max;
224 static __pdata int8_t ao_telemetry_loc_cur;
225 static __pdata int8_t ao_telemetry_sat_cur;
228 ao_send_location(void)
230 if (--ao_telemetry_loc_cur <= 0)
232 telemetry.generic.type = AO_TELEMETRY_LOCATION;
233 ao_mutex_get(&ao_gps_mutex);
234 ao_xmemcpy(&telemetry.location.flags,
237 telemetry.location.tick = ao_gps_tick;
238 ao_mutex_put(&ao_gps_mutex);
239 ao_radio_send(&telemetry, sizeof (telemetry));
240 ao_telemetry_loc_cur = ao_telemetry_config_max;
245 ao_send_satellite(void)
247 if (--ao_telemetry_sat_cur <= 0)
249 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
250 ao_mutex_get(&ao_gps_mutex);
251 telemetry.satellite.channels = ao_gps_tracking_data.channels;
252 ao_xmemcpy(&telemetry.satellite.sats,
253 &ao_gps_tracking_data.sats,
254 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
255 ao_mutex_put(&ao_gps_mutex);
256 ao_radio_send(&telemetry, sizeof (telemetry));
257 ao_telemetry_sat_cur = ao_telemetry_config_max;
264 static __pdata int8_t ao_telemetry_companion_max;
265 static __pdata int8_t ao_telemetry_companion_cur;
268 ao_send_companion(void)
270 if (--ao_telemetry_companion_cur <= 0) {
271 telemetry.generic.type = AO_TELEMETRY_COMPANION;
272 telemetry.companion.board_id = ao_companion_setup.board_id;
273 telemetry.companion.update_period = ao_companion_setup.update_period;
274 telemetry.companion.channels = ao_companion_setup.channels;
275 ao_mutex_get(&ao_companion_mutex);
276 ao_xmemcpy(&telemetry.companion.companion_data,
278 ao_companion_setup.channels * 2);
279 ao_mutex_put(&ao_companion_mutex);
280 ao_radio_send(&telemetry, sizeof (telemetry));
281 ao_telemetry_companion_cur = ao_telemetry_companion_max;
293 if (!ao_config.radio_enable)
295 while (!ao_flight_number)
296 ao_sleep(&ao_flight_number);
298 telemetry.generic.serial = ao_serial_number;
300 while (ao_telemetry_interval == 0)
301 ao_sleep(&telemetry);
302 time = ao_rdf_time = ao_time();
306 while (ao_telemetry_interval) {
309 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
312 #ifdef AO_SEND_ALL_BARO
317 ao_send_mega_sensor();
325 if (ao_companion_running)
328 ao_send_configuration();
334 #ifndef AO_SEND_ALL_BARO
337 !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
339 (int16_t) (ao_time() - ao_rdf_time) >= 0)
341 #if HAS_IGNITE_REPORT
344 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
345 #if HAS_IGNITE_REPORT
346 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
347 ao_radio_continuity(c);
353 if (ao_config.aprs_interval != 0 &&
354 (int16_t) (ao_time() - ao_aprs_time) >= 0)
356 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
361 time += ao_telemetry_interval;
362 delay = time - ao_time();
365 ao_sleep(&telemetry);
376 ao_telemetry_set_interval(uint16_t interval)
379 ao_telemetry_interval = interval;
383 ao_telemetry_mega_data_max = 1;
385 ao_telemetry_mega_data_max = 2;
386 if (ao_telemetry_mega_data_max > cur)
388 ao_telemetry_mega_data_cur = cur;
392 if (!ao_companion_setup.update_period)
393 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
394 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
395 if (ao_telemetry_companion_max > cur)
397 ao_telemetry_companion_cur = cur;
400 ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
402 if (ao_telemetry_config_max > cur)
404 ao_telemetry_config_cur = cur;
408 if (ao_telemetry_config_max > cur)
410 ao_telemetry_loc_cur = cur;
411 if (ao_telemetry_config_max > cur)
413 ao_telemetry_sat_cur = cur;
415 ao_wakeup(&telemetry);
419 ao_rdf_set(uint8_t rdf)
423 ao_radio_rdf_abort();
425 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
429 __xdata struct ao_task ao_telemetry_task;
434 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");