]> git.gag.com Git - fw/altos/blob - src/core/ao_telemetry.c
altosdroid: Mike was right -- only need one LocationListener
[fw/altos] / src / core / ao_telemetry.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19 #include "ao_log.h"
20 #include "ao_product.h"
21
22 static __pdata uint16_t ao_telemetry_interval;
23 static __pdata uint8_t ao_rdf = 0;
24 static __pdata uint16_t ao_rdf_time;
25
26 #if HAS_APRS
27 static __pdata uint16_t ao_aprs_time;
28
29 #include <ao_aprs.h>
30 #endif
31
32 #if defined(MEGAMETRUM)
33 #define AO_SEND_MEGA    1
34 #endif
35
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
38 #endif
39
40 #if defined(TELEMINI_V_1_0)
41 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMINI
42 #endif
43
44 #if defined(TELENANO_V_0_1)
45 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELENANO
46 #endif
47
48 static __xdata union ao_telemetry_all   telemetry;
49
50 #if defined AO_TELEMETRY_SENSOR
51 /* Send sensor packet */
52 static void
53 ao_send_sensor(void)
54 {
55         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
56                         
57         telemetry.generic.tick = packet->tick;
58         telemetry.generic.type = AO_TELEMETRY_SENSOR;
59
60         telemetry.sensor.state = ao_flight_state;
61 #if HAS_ACCEL
62         telemetry.sensor.accel = packet->adc.accel;
63 #else
64         telemetry.sensor.accel = 0;
65 #endif
66         telemetry.sensor.pres = ao_data_pres(packet);
67         telemetry.sensor.temp = packet->adc.temp;
68         telemetry.sensor.v_batt = packet->adc.v_batt;
69 #if HAS_IGNITE
70         telemetry.sensor.sense_d = packet->adc.sense_d;
71         telemetry.sensor.sense_m = packet->adc.sense_m;
72 #else
73         telemetry.sensor.sense_d = 0;
74         telemetry.sensor.sense_m = 0;
75 #endif
76
77         telemetry.sensor.acceleration = ao_accel;
78         telemetry.sensor.speed = ao_speed;
79         telemetry.sensor.height = ao_height;
80
81         telemetry.sensor.ground_pres = ao_ground_pres;
82 #if HAS_ACCEL
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;
86 #else
87         telemetry.sensor.ground_accel = 0;
88         telemetry.sensor.accel_plus_g = 0;
89         telemetry.sensor.accel_minus_g = 0;
90 #endif
91
92         ao_radio_send(&telemetry, sizeof (telemetry));
93 }
94 #endif
95
96
97 #ifdef AO_SEND_MEGA
98 /* Send mega sensor packet */
99 static void
100 ao_send_mega_sensor(void)
101 {
102         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
103                         
104         telemetry.generic.tick = packet->tick;
105         telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
106
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);
110
111 #if HAS_MPU6000
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;
115
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;
119 #endif
120
121 #if HAS_HMC5883
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;
125 #endif
126
127         ao_radio_send(&telemetry, sizeof (telemetry));
128 }
129
130 static __pdata int8_t ao_telemetry_mega_data_max;
131 static __pdata int8_t ao_telemetry_mega_data_cur;
132
133 /* Send mega data packet */
134 static void
135 ao_send_mega_data(void)
136 {
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)];
139                 uint8_t i;
140
141                 telemetry.generic.tick = packet->tick;
142                 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
143
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;
147
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;
151
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;
156
157                 telemetry.mega_data.acceleration = ao_accel;
158                 telemetry.mega_data.speed = ao_speed;
159                 telemetry.mega_data.height = ao_height;
160
161                 ao_radio_send(&telemetry, sizeof (telemetry));
162                 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
163         }
164 }
165 #endif /* AO_SEND_MEGA */
166
167 #ifdef AO_SEND_ALL_BARO
168 static uint8_t          ao_baro_sample;
169
170 static void
171 ao_send_baro(void)
172 {
173         uint8_t         sample = ao_sample_data;
174         uint8_t         samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
175
176         if (samples > 12) {
177                 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
178                 samples = 12;
179         }
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);
186         }
187         ao_radio_send(&telemetry, sizeof (telemetry));
188 }
189 #endif
190
191 static __pdata int8_t ao_telemetry_config_max;
192 static __pdata int8_t ao_telemetry_config_cur;
193
194 static void
195 ao_send_configuration(void)
196 {
197         if (--ao_telemetry_config_cur <= 0)
198         {
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,
208                             ao_config.callsign,
209                             AO_MAX_CALLSIGN);
210                 ao_xmemcpy (telemetry.configuration.version,
211                             CODE_TO_XDATA(ao_version),
212                             AO_MAX_VERSION);
213                 ao_radio_send(&telemetry, sizeof (telemetry));
214                 ao_telemetry_config_cur = ao_telemetry_config_max;
215         }
216 }
217
218 #if HAS_GPS
219
220 static __pdata int8_t ao_telemetry_loc_cur;
221 static __pdata int8_t ao_telemetry_sat_cur;
222
223 static void
224 ao_send_location(void)
225 {
226         if (--ao_telemetry_loc_cur <= 0)
227         {
228                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
229                 ao_mutex_get(&ao_gps_mutex);
230                 ao_xmemcpy(&telemetry.location.flags,
231                        &ao_gps_data.flags,
232                        26);
233                 ao_mutex_put(&ao_gps_mutex);
234                 ao_radio_send(&telemetry, sizeof (telemetry));
235                 ao_telemetry_loc_cur = ao_telemetry_config_max;
236         }
237 }
238
239 static void
240 ao_send_satellite(void)
241 {
242         if (--ao_telemetry_sat_cur <= 0)
243         {
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;
253         }
254 }
255 #endif
256
257 #if HAS_COMPANION
258
259 static __pdata int8_t ao_telemetry_companion_max;
260 static __pdata int8_t ao_telemetry_companion_cur;
261
262 static void
263 ao_send_companion(void)
264 {
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,
272                        ao_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;
277         }
278 }
279 #endif
280
281 void
282 ao_telemetry(void)
283 {
284         uint16_t        time;
285         int16_t         delay;
286
287         ao_config_get();
288         if (!ao_config.radio_enable)
289                 ao_exit();
290         while (!ao_flight_number)
291                 ao_sleep(&ao_flight_number);
292
293         telemetry.generic.serial = ao_serial_number;
294         for (;;) {
295                 while (ao_telemetry_interval == 0)
296                         ao_sleep(&telemetry);
297                 time = ao_rdf_time = ao_time();
298 #if HAS_APRS
299                 ao_aprs_time = time;
300 #endif
301                 while (ao_telemetry_interval) {
302
303 #if HAS_APRS
304                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
305 #endif
306                         {
307 #ifdef AO_SEND_ALL_BARO
308                                 ao_send_baro();
309 #endif
310 #if HAS_FLIGHT
311 #ifdef AO_SEND_MEGA
312                                 ao_send_mega_sensor();
313                                 ao_send_mega_data();
314 #else
315                                 ao_send_sensor();
316 #endif
317 #endif
318
319 #if HAS_COMPANION
320                                 if (ao_companion_running)
321                                         ao_send_companion();
322 #endif
323                                 ao_send_configuration();
324 #if HAS_GPS
325                                 ao_send_location();
326                                 ao_send_satellite();
327 #endif
328                         }
329 #ifndef AO_SEND_ALL_BARO
330                         if (ao_rdf &&
331 #if HAS_APRS
332                             !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
333 #endif
334                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
335                         {
336 #if HAS_IGNITE_REPORT
337                                 uint8_t c;
338 #endif
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);
343                                 else
344 #endif
345                                         ao_radio_rdf();
346                         }
347 #if HAS_APRS
348                         if (ao_config.aprs_interval != 0 &&
349                             (int16_t) (ao_time() - ao_aprs_time) >= 0)
350                         {
351                                 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
352                                 ao_aprs_send();
353                         }
354 #endif
355 #endif
356                         time += ao_telemetry_interval;
357                         delay = time - ao_time();
358                         if (delay > 0) {
359                                 ao_alarm(delay);
360                                 ao_sleep(&telemetry);
361                                 ao_clear_alarm();
362                         }
363                         else
364                                 time = ao_time();
365                 bottom: ;
366                 }
367         }
368 }
369
370 void
371 ao_telemetry_set_interval(uint16_t interval)
372 {
373         int8_t  cur = 0;
374         ao_telemetry_interval = interval;
375         
376 #if AO_SEND_MEGA
377         if (interval > 1)
378                 ao_telemetry_mega_data_max = 1;
379         else
380                 ao_telemetry_mega_data_max = 2;
381         if (ao_telemetry_mega_data_max > cur)
382                 cur++;
383         ao_telemetry_mega_data_cur = cur;
384 #endif
385
386 #if HAS_COMPANION
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)
391                 cur++;
392         ao_telemetry_companion_cur = cur;
393 #endif
394
395         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
396 #if HAS_COMPANION
397         if (ao_telemetry_config_max > cur)
398                 cur++;
399         ao_telemetry_config_cur = cur;
400 #endif
401
402 #if HAS_GPS
403         if (ao_telemetry_config_max > cur)
404                 cur++;
405         ao_telemetry_loc_cur = cur;
406         if (ao_telemetry_config_max > cur)
407                 cur++;
408         ao_telemetry_sat_cur = cur;
409 #endif
410         ao_wakeup(&telemetry);
411 }
412
413 void
414 ao_rdf_set(uint8_t rdf)
415 {
416         ao_rdf = rdf;
417         if (rdf == 0)
418                 ao_radio_rdf_abort();
419         else {
420                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
421         }
422 }
423
424 __xdata struct ao_task  ao_telemetry_task;
425
426 void
427 ao_telemetry_init()
428 {
429         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
430 }