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;
32 #if defined(MEGAMETRUM)
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;
201 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
202 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
203 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
204 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
205 telemetry.configuration.main_deploy = ao_config.main_deploy;
206 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
207 ao_xmemcpy (telemetry.configuration.callsign,
210 ao_xmemcpy (telemetry.configuration.version,
211 CODE_TO_XDATA(ao_version),
213 ao_radio_send(&telemetry, sizeof (telemetry));
214 ao_telemetry_config_cur = ao_telemetry_config_max;
220 static __pdata int8_t ao_telemetry_loc_cur;
221 static __pdata int8_t ao_telemetry_sat_cur;
224 ao_send_location(void)
226 if (--ao_telemetry_loc_cur <= 0)
228 telemetry.generic.type = AO_TELEMETRY_LOCATION;
229 ao_mutex_get(&ao_gps_mutex);
230 ao_xmemcpy(&telemetry.location.flags,
233 ao_mutex_put(&ao_gps_mutex);
234 ao_radio_send(&telemetry, sizeof (telemetry));
235 ao_telemetry_loc_cur = ao_telemetry_config_max;
240 ao_send_satellite(void)
242 if (--ao_telemetry_sat_cur <= 0)
244 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
245 ao_mutex_get(&ao_gps_mutex);
246 telemetry.satellite.channels = ao_gps_tracking_data.channels;
247 ao_xmemcpy(&telemetry.satellite.sats,
248 &ao_gps_tracking_data.sats,
249 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
250 ao_mutex_put(&ao_gps_mutex);
251 ao_radio_send(&telemetry, sizeof (telemetry));
252 ao_telemetry_sat_cur = ao_telemetry_config_max;
259 static __pdata int8_t ao_telemetry_companion_max;
260 static __pdata int8_t ao_telemetry_companion_cur;
263 ao_send_companion(void)
265 if (--ao_telemetry_companion_cur <= 0) {
266 telemetry.generic.type = AO_TELEMETRY_COMPANION;
267 telemetry.companion.board_id = ao_companion_setup.board_id;
268 telemetry.companion.update_period = ao_companion_setup.update_period;
269 telemetry.companion.channels = ao_companion_setup.channels;
270 ao_mutex_get(&ao_companion_mutex);
271 ao_xmemcpy(&telemetry.companion.companion_data,
273 ao_companion_setup.channels * 2);
274 ao_mutex_put(&ao_companion_mutex);
275 ao_radio_send(&telemetry, sizeof (telemetry));
276 ao_telemetry_companion_cur = ao_telemetry_companion_max;
288 if (!ao_config.radio_enable)
290 while (!ao_flight_number)
291 ao_sleep(&ao_flight_number);
293 telemetry.generic.serial = ao_serial_number;
295 while (ao_telemetry_interval == 0)
296 ao_sleep(&telemetry);
297 time = ao_rdf_time = ao_time();
301 while (ao_telemetry_interval) {
304 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
307 #ifdef AO_SEND_ALL_BARO
312 ao_send_mega_sensor();
320 if (ao_companion_running)
323 ao_send_configuration();
329 #ifndef AO_SEND_ALL_BARO
332 !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
334 (int16_t) (ao_time() - ao_rdf_time) >= 0)
336 #if HAS_IGNITE_REPORT
339 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
340 #if HAS_IGNITE_REPORT
341 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
342 ao_radio_continuity(c);
348 if (ao_config.aprs_interval != 0 &&
349 (int16_t) (ao_time() - ao_aprs_time) >= 0)
351 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
356 time += ao_telemetry_interval;
357 delay = time - ao_time();
360 ao_sleep(&telemetry);
371 ao_telemetry_set_interval(uint16_t interval)
374 ao_telemetry_interval = interval;
378 ao_telemetry_mega_data_max = 1;
380 ao_telemetry_mega_data_max = 2;
381 if (ao_telemetry_mega_data_max > cur)
383 ao_telemetry_mega_data_cur = cur;
387 if (!ao_companion_setup.update_period)
388 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
389 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
390 if (ao_telemetry_companion_max > cur)
392 ao_telemetry_companion_cur = cur;
395 ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
397 if (ao_telemetry_config_max > cur)
399 ao_telemetry_config_cur = cur;
403 if (ao_telemetry_config_max > cur)
405 ao_telemetry_loc_cur = cur;
406 if (ao_telemetry_config_max > cur)
408 ao_telemetry_sat_cur = cur;
410 ao_wakeup(&telemetry);
414 ao_rdf_set(uint8_t rdf)
418 ao_radio_rdf_abort();
420 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
424 __xdata struct ao_task ao_telemetry_task;
429 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");