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"
26 static __pdata uint16_t ao_telemetry_interval;
27 static __pdata uint8_t ao_rdf = 0;
30 static __pdata uint16_t ao_rdf_time;
34 static __pdata uint16_t ao_aprs_time;
40 #define AO_SEND_MEGA 1
43 #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)
44 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
47 #if defined(TELEMINI_V_1_0)
48 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
51 #if defined(TELENANO_V_0_1)
52 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
55 static __xdata union ao_telemetry_all telemetry;
57 #if defined AO_TELEMETRY_SENSOR
58 /* Send sensor packet */
62 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
64 telemetry.generic.tick = packet->tick;
65 telemetry.generic.type = AO_TELEMETRY_SENSOR;
67 telemetry.sensor.state = ao_flight_state;
69 telemetry.sensor.accel = packet->adc.accel;
71 telemetry.sensor.accel = 0;
73 telemetry.sensor.pres = ao_data_pres(packet);
74 telemetry.sensor.temp = packet->adc.temp;
75 telemetry.sensor.v_batt = packet->adc.v_batt;
77 telemetry.sensor.sense_d = packet->adc.sense_d;
78 telemetry.sensor.sense_m = packet->adc.sense_m;
80 telemetry.sensor.sense_d = 0;
81 telemetry.sensor.sense_m = 0;
84 telemetry.sensor.acceleration = ao_accel;
85 telemetry.sensor.speed = ao_speed;
86 telemetry.sensor.height = ao_height;
88 telemetry.sensor.ground_pres = ao_ground_pres;
90 telemetry.sensor.ground_accel = ao_ground_accel;
91 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
92 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
94 telemetry.sensor.ground_accel = 0;
95 telemetry.sensor.accel_plus_g = 0;
96 telemetry.sensor.accel_minus_g = 0;
99 ao_radio_send(&telemetry, sizeof (telemetry));
105 /* Send mega sensor packet */
107 ao_send_mega_sensor(void)
109 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
111 telemetry.generic.tick = packet->tick;
112 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
114 telemetry.mega_sensor.accel = ao_data_accel(packet);
115 telemetry.mega_sensor.pres = ao_data_pres(packet);
116 telemetry.mega_sensor.temp = ao_data_temp(packet);
119 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
120 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
121 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
123 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
124 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
125 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
129 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
130 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
131 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
134 ao_radio_send(&telemetry, sizeof (telemetry));
137 static __pdata int8_t ao_telemetry_mega_data_max;
138 static __pdata int8_t ao_telemetry_mega_data_cur;
140 /* Send mega data packet */
142 ao_send_mega_data(void)
144 if (--ao_telemetry_mega_data_cur <= 0) {
145 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
148 telemetry.generic.tick = packet->tick;
149 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
151 telemetry.mega_data.state = ao_flight_state;
152 telemetry.mega_data.v_batt = packet->adc.v_batt;
153 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
155 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
156 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
157 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
159 telemetry.mega_data.ground_pres = ao_ground_pres;
160 telemetry.mega_data.ground_accel = ao_ground_accel;
161 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
162 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
164 telemetry.mega_data.acceleration = ao_accel;
165 telemetry.mega_data.speed = ao_speed;
166 telemetry.mega_data.height = ao_height;
168 ao_radio_send(&telemetry, sizeof (telemetry));
169 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
172 #endif /* AO_SEND_MEGA */
174 #ifdef AO_SEND_ALL_BARO
175 static uint8_t ao_baro_sample;
180 uint8_t sample = ao_sample_data;
181 uint8_t samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
184 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
187 telemetry.generic.tick = ao_data_ring[sample].tick;
188 telemetry.generic.type = AO_TELEMETRY_BARO;
189 telemetry.baro.samples = samples;
190 for (sample = 0; sample < samples; sample++) {
191 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
192 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
194 ao_radio_send(&telemetry, sizeof (telemetry));
198 static __pdata int8_t ao_telemetry_config_max;
199 static __pdata int8_t ao_telemetry_config_cur;
202 ao_send_configuration(void)
204 if (--ao_telemetry_config_cur <= 0)
206 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
207 telemetry.configuration.device = AO_idProduct_NUMBER;
209 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
211 telemetry.configuration.flight = ao_flight_number;
213 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
214 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
215 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
216 telemetry.configuration.main_deploy = ao_config.main_deploy;
217 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
218 ao_xmemcpy (telemetry.configuration.callsign,
221 ao_xmemcpy (telemetry.configuration.version,
222 CODE_TO_XDATA(ao_version),
224 ao_radio_send(&telemetry, sizeof (telemetry));
225 ao_telemetry_config_cur = ao_telemetry_config_max;
231 static __pdata int8_t ao_telemetry_loc_cur;
232 static __pdata int8_t ao_telemetry_sat_cur;
235 ao_send_location(void)
237 if (--ao_telemetry_loc_cur <= 0)
239 telemetry.generic.type = AO_TELEMETRY_LOCATION;
240 ao_mutex_get(&ao_gps_mutex);
241 ao_xmemcpy(&telemetry.location.flags,
244 telemetry.location.tick = ao_gps_tick;
245 ao_mutex_put(&ao_gps_mutex);
246 ao_radio_send(&telemetry, sizeof (telemetry));
247 ao_telemetry_loc_cur = ao_telemetry_config_max;
252 ao_send_satellite(void)
254 if (--ao_telemetry_sat_cur <= 0)
256 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
257 ao_mutex_get(&ao_gps_mutex);
258 telemetry.satellite.channels = ao_gps_tracking_data.channels;
259 ao_xmemcpy(&telemetry.satellite.sats,
260 &ao_gps_tracking_data.sats,
261 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
262 ao_mutex_put(&ao_gps_mutex);
263 ao_radio_send(&telemetry, sizeof (telemetry));
264 ao_telemetry_sat_cur = ao_telemetry_config_max;
271 static __pdata int8_t ao_telemetry_companion_max;
272 static __pdata int8_t ao_telemetry_companion_cur;
275 ao_send_companion(void)
277 if (--ao_telemetry_companion_cur <= 0) {
278 telemetry.generic.type = AO_TELEMETRY_COMPANION;
279 telemetry.companion.board_id = ao_companion_setup.board_id;
280 telemetry.companion.update_period = ao_companion_setup.update_period;
281 telemetry.companion.channels = ao_companion_setup.channels;
282 ao_mutex_get(&ao_companion_mutex);
283 ao_xmemcpy(&telemetry.companion.companion_data,
285 ao_companion_setup.channels * 2);
286 ao_mutex_put(&ao_companion_mutex);
287 ao_radio_send(&telemetry, sizeof (telemetry));
288 ao_telemetry_companion_cur = ao_telemetry_companion_max;
300 if (!ao_config.radio_enable)
302 while (!ao_flight_number)
303 ao_sleep(&ao_flight_number);
305 telemetry.generic.serial = ao_serial_number;
307 while (ao_telemetry_interval == 0)
308 ao_sleep(&telemetry);
316 while (ao_telemetry_interval) {
319 if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
322 #ifdef AO_SEND_ALL_BARO
327 ao_send_mega_sensor();
335 if (ao_companion_running)
338 ao_send_configuration();
344 #ifndef AO_SEND_ALL_BARO
348 !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
350 (int16_t) (ao_time() - ao_rdf_time) >= 0)
352 #if HAS_IGNITE_REPORT
355 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
356 #if HAS_IGNITE_REPORT
357 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
358 ao_radio_continuity(c);
365 if (ao_config.aprs_interval != 0 &&
366 (int16_t) (ao_time() - ao_aprs_time) >= 0)
368 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
373 time += ao_telemetry_interval;
374 delay = time - ao_time();
377 ao_sleep(&telemetry);
388 ao_telemetry_set_interval(uint16_t interval)
391 ao_telemetry_interval = interval;
395 ao_telemetry_mega_data_max = 1;
397 ao_telemetry_mega_data_max = 2;
398 if (ao_telemetry_mega_data_max > cur)
400 ao_telemetry_mega_data_cur = cur;
404 if (!ao_companion_setup.update_period)
405 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
406 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
407 if (ao_telemetry_companion_max > cur)
409 ao_telemetry_companion_cur = cur;
412 ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
414 if (ao_telemetry_config_max > cur)
416 ao_telemetry_config_cur = cur;
420 if (ao_telemetry_config_max > cur)
422 ao_telemetry_loc_cur = cur;
423 if (ao_telemetry_config_max > cur)
425 ao_telemetry_sat_cur = cur;
427 ao_wakeup(&telemetry);
432 ao_rdf_set(uint8_t rdf)
436 ao_radio_rdf_abort();
438 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
443 __xdata struct ao_task ao_telemetry_task;
448 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");