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;
25 #define AO_RDF_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
26 #define AO_RDF_LENGTH_MS 500
28 #if defined(MEGAMETRUM)
29 #define AO_SEND_MEGA 1
32 #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)
33 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
36 #if defined(TELEMINI_V_1_0)
37 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
40 #if defined(TELENANO_V_0_1)
41 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
44 static __xdata union ao_telemetry_all telemetry;
46 #if defined AO_TELEMETRY_SENSOR
47 /* Send sensor packet */
51 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
53 telemetry.generic.tick = packet->tick;
54 telemetry.generic.type = AO_TELEMETRY_SENSOR;
56 telemetry.sensor.state = ao_flight_state;
58 telemetry.sensor.accel = packet->adc.accel;
60 telemetry.sensor.accel = 0;
62 telemetry.sensor.pres = ao_data_pres(packet);
63 telemetry.sensor.temp = packet->adc.temp;
64 telemetry.sensor.v_batt = packet->adc.v_batt;
66 telemetry.sensor.sense_d = packet->adc.sense_d;
67 telemetry.sensor.sense_m = packet->adc.sense_m;
69 telemetry.sensor.sense_d = 0;
70 telemetry.sensor.sense_m = 0;
73 telemetry.sensor.acceleration = ao_accel;
74 telemetry.sensor.speed = ao_speed;
75 telemetry.sensor.height = ao_height;
77 telemetry.sensor.ground_pres = ao_ground_pres;
79 telemetry.sensor.ground_accel = ao_ground_accel;
80 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
81 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
83 telemetry.sensor.ground_accel = 0;
84 telemetry.sensor.accel_plus_g = 0;
85 telemetry.sensor.accel_minus_g = 0;
88 ao_radio_send(&telemetry, sizeof (telemetry));
94 /* Send mega sensor packet */
96 ao_send_mega_sensor(void)
98 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
100 telemetry.generic.tick = packet->tick;
101 telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
103 telemetry.mega_sensor.accel = ao_data_accel(packet);
104 telemetry.mega_sensor.pres = ao_data_pres(packet);
105 telemetry.mega_sensor.temp = ao_data_temp(packet);
108 telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
109 telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
110 telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
112 telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
113 telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
114 telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
118 telemetry.mega_sensor.mag_x = packet->hmc5883.x;
119 telemetry.mega_sensor.mag_y = packet->hmc5883.y;
120 telemetry.mega_sensor.mag_z = packet->hmc5883.z;
123 ao_radio_send(&telemetry, sizeof (telemetry));
126 static __pdata int8_t ao_telemetry_mega_data_max;
127 static __pdata int8_t ao_telemetry_mega_data_cur;
129 /* Send mega data packet */
131 ao_send_mega_data(void)
133 if (--ao_telemetry_mega_data_cur <= 0) {
134 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
137 telemetry.generic.tick = packet->tick;
138 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
140 telemetry.mega_data.state = ao_flight_state;
141 telemetry.mega_data.v_batt = packet->adc.v_batt;
142 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
144 /* XXX figure out right shift value; 4 might suffice */
145 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
146 telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 8;
148 telemetry.mega_data.ground_pres = ao_ground_pres;
149 telemetry.mega_data.ground_accel = ao_ground_accel;
150 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
151 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
153 telemetry.mega_data.acceleration = ao_accel;
154 telemetry.mega_data.speed = ao_speed;
155 telemetry.mega_data.height = ao_height;
157 ao_radio_send(&telemetry, sizeof (telemetry));
158 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
161 #endif /* AO_SEND_MEGA */
163 #ifdef AO_SEND_ALL_BARO
164 static uint8_t ao_baro_sample;
169 uint8_t sample = ao_sample_data;
170 uint8_t samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
173 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
176 telemetry.generic.tick = ao_data_ring[sample].tick;
177 telemetry.generic.type = AO_TELEMETRY_BARO;
178 telemetry.baro.samples = samples;
179 for (sample = 0; sample < samples; sample++) {
180 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
181 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
183 ao_radio_send(&telemetry, sizeof (telemetry));
187 static __pdata int8_t ao_telemetry_config_max;
188 static __pdata int8_t ao_telemetry_config_cur;
191 ao_send_configuration(void)
193 if (--ao_telemetry_config_cur <= 0)
195 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
196 telemetry.configuration.device = AO_idProduct_NUMBER;
197 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
198 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
199 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
200 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
201 telemetry.configuration.main_deploy = ao_config.main_deploy;
202 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
203 ao_xmemcpy (telemetry.configuration.callsign,
206 ao_xmemcpy (telemetry.configuration.version,
207 CODE_TO_XDATA(ao_version),
209 ao_radio_send(&telemetry, sizeof (telemetry));
210 ao_telemetry_config_cur = ao_telemetry_config_max;
216 static __pdata int8_t ao_telemetry_loc_cur;
217 static __pdata int8_t ao_telemetry_sat_cur;
220 ao_send_location(void)
222 if (--ao_telemetry_loc_cur <= 0)
224 telemetry.generic.type = AO_TELEMETRY_LOCATION;
225 ao_mutex_get(&ao_gps_mutex);
226 ao_xmemcpy(&telemetry.location.flags,
229 ao_mutex_put(&ao_gps_mutex);
230 ao_radio_send(&telemetry, sizeof (telemetry));
231 ao_telemetry_loc_cur = ao_telemetry_config_max;
236 ao_send_satellite(void)
238 if (--ao_telemetry_sat_cur <= 0)
240 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
241 ao_mutex_get(&ao_gps_mutex);
242 telemetry.satellite.channels = ao_gps_tracking_data.channels;
243 ao_xmemcpy(&telemetry.satellite.sats,
244 &ao_gps_tracking_data.sats,
245 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
246 ao_mutex_put(&ao_gps_mutex);
247 ao_radio_send(&telemetry, sizeof (telemetry));
248 ao_telemetry_sat_cur = ao_telemetry_config_max;
255 static __pdata int8_t ao_telemetry_companion_max;
256 static __pdata int8_t ao_telemetry_companion_cur;
259 ao_send_companion(void)
261 if (--ao_telemetry_companion_cur <= 0) {
262 telemetry.generic.type = AO_TELEMETRY_COMPANION;
263 telemetry.companion.board_id = ao_companion_setup.board_id;
264 telemetry.companion.update_period = ao_companion_setup.update_period;
265 telemetry.companion.channels = ao_companion_setup.channels;
266 ao_mutex_get(&ao_companion_mutex);
267 ao_xmemcpy(&telemetry.companion.companion_data,
269 ao_companion_setup.channels * 2);
270 ao_mutex_put(&ao_companion_mutex);
271 ao_radio_send(&telemetry, sizeof (telemetry));
272 ao_telemetry_companion_cur = ao_telemetry_companion_max;
284 if (!ao_config.radio_enable)
286 while (!ao_flight_number)
287 ao_sleep(&ao_flight_number);
289 telemetry.generic.serial = ao_serial_number;
291 while (ao_telemetry_interval == 0)
292 ao_sleep(&telemetry);
293 time = ao_rdf_time = ao_time();
294 while (ao_telemetry_interval) {
297 #ifdef AO_SEND_ALL_BARO
301 ao_send_mega_sensor();
308 if (ao_companion_running)
311 ao_send_configuration();
316 #ifndef AO_SEND_ALL_BARO
318 (int16_t) (ao_time() - ao_rdf_time) >= 0)
320 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
321 ao_radio_rdf(AO_MS_TO_RDF_LEN(AO_RDF_LENGTH_MS));
324 time += ao_telemetry_interval;
325 delay = time - ao_time();
328 ao_sleep(&telemetry);
338 ao_telemetry_set_interval(uint16_t interval)
341 ao_telemetry_interval = interval;
345 ao_telemetry_mega_data_max = 1;
347 ao_telemetry_mega_data_max = 2;
348 if (ao_telemetry_mega_data_max > cur)
350 ao_telemetry_mega_data_cur = cur;
354 if (!ao_companion_setup.update_period)
355 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
356 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
357 if (ao_telemetry_companion_max > cur)
359 ao_telemetry_companion_cur = cur;
362 ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
364 if (ao_telemetry_config_max > cur)
366 ao_telemetry_config_cur = cur;
370 if (ao_telemetry_config_max > cur)
372 ao_telemetry_loc_cur = cur;
373 if (ao_telemetry_config_max > cur)
375 ao_telemetry_sat_cur = cur;
377 ao_wakeup(&telemetry);
381 ao_rdf_set(uint8_t rdf)
385 ao_radio_rdf_abort();
387 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
390 __xdata struct ao_task ao_telemetry_task;
395 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");