altos: Send APRS packets even during ascent
[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_product.h"
20
21 static __pdata uint16_t ao_telemetry_interval;
22 static __pdata uint8_t ao_rdf = 0;
23 static __pdata uint16_t ao_rdf_time;
24
25 #if HAS_APRS
26 static __pdata uint16_t ao_aprs_time;
27
28 #include <ao_aprs.h>
29 #endif
30
31 #if defined(MEGAMETRUM)
32 #define AO_SEND_MEGA    1
33 #endif
34
35 #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)
36 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMETRUM
37 #endif
38
39 #if defined(TELEMINI_V_1_0)
40 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMINI
41 #endif
42
43 #if defined(TELENANO_V_0_1)
44 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELENANO
45 #endif
46
47 static __xdata union ao_telemetry_all   telemetry;
48
49 #if defined AO_TELEMETRY_SENSOR
50 /* Send sensor packet */
51 static void
52 ao_send_sensor(void)
53 {
54         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
55                         
56         telemetry.generic.tick = packet->tick;
57         telemetry.generic.type = AO_TELEMETRY_SENSOR;
58
59         telemetry.sensor.state = ao_flight_state;
60 #if HAS_ACCEL
61         telemetry.sensor.accel = packet->adc.accel;
62 #else
63         telemetry.sensor.accel = 0;
64 #endif
65         telemetry.sensor.pres = ao_data_pres(packet);
66         telemetry.sensor.temp = packet->adc.temp;
67         telemetry.sensor.v_batt = packet->adc.v_batt;
68 #if HAS_IGNITE
69         telemetry.sensor.sense_d = packet->adc.sense_d;
70         telemetry.sensor.sense_m = packet->adc.sense_m;
71 #else
72         telemetry.sensor.sense_d = 0;
73         telemetry.sensor.sense_m = 0;
74 #endif
75
76         telemetry.sensor.acceleration = ao_accel;
77         telemetry.sensor.speed = ao_speed;
78         telemetry.sensor.height = ao_height;
79
80         telemetry.sensor.ground_pres = ao_ground_pres;
81 #if HAS_ACCEL
82         telemetry.sensor.ground_accel = ao_ground_accel;
83         telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
84         telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
85 #else
86         telemetry.sensor.ground_accel = 0;
87         telemetry.sensor.accel_plus_g = 0;
88         telemetry.sensor.accel_minus_g = 0;
89 #endif
90
91         ao_radio_send(&telemetry, sizeof (telemetry));
92 }
93 #endif
94
95
96 #ifdef AO_SEND_MEGA
97 /* Send mega sensor packet */
98 static void
99 ao_send_mega_sensor(void)
100 {
101         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
102                         
103         telemetry.generic.tick = packet->tick;
104         telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR;
105
106         telemetry.mega_sensor.accel = ao_data_accel(packet);
107         telemetry.mega_sensor.pres = ao_data_pres(packet);
108         telemetry.mega_sensor.temp = ao_data_temp(packet);
109
110 #if HAS_MPU6000
111         telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
112         telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
113         telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
114
115         telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
116         telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
117         telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
118 #endif
119
120 #if HAS_HMC5883
121         telemetry.mega_sensor.mag_x = packet->hmc5883.x;
122         telemetry.mega_sensor.mag_y = packet->hmc5883.y;
123         telemetry.mega_sensor.mag_z = packet->hmc5883.z;
124 #endif
125
126         ao_radio_send(&telemetry, sizeof (telemetry));
127 }
128
129 static __pdata int8_t ao_telemetry_mega_data_max;
130 static __pdata int8_t ao_telemetry_mega_data_cur;
131
132 /* Send mega data packet */
133 static void
134 ao_send_mega_data(void)
135 {
136         if (--ao_telemetry_mega_data_cur <= 0) {
137                 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
138                 uint8_t i;
139
140                 telemetry.generic.tick = packet->tick;
141                 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
142
143                 telemetry.mega_data.state = ao_flight_state;
144                 telemetry.mega_data.v_batt = packet->adc.v_batt;
145                 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
146
147                 /* ADC range is 0-4095, so shift by four to save the high 8 bits */
148                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
149                         telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 4;
150
151                 telemetry.mega_data.ground_pres = ao_ground_pres;
152                 telemetry.mega_data.ground_accel = ao_ground_accel;
153                 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
154                 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
155
156                 telemetry.mega_data.acceleration = ao_accel;
157                 telemetry.mega_data.speed = ao_speed;
158                 telemetry.mega_data.height = ao_height;
159
160                 ao_radio_send(&telemetry, sizeof (telemetry));
161                 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
162         }
163 }
164 #endif /* AO_SEND_MEGA */
165
166 #ifdef AO_SEND_ALL_BARO
167 static uint8_t          ao_baro_sample;
168
169 static void
170 ao_send_baro(void)
171 {
172         uint8_t         sample = ao_sample_data;
173         uint8_t         samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
174
175         if (samples > 12) {
176                 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
177                 samples = 12;
178         }
179         telemetry.generic.tick = ao_data_ring[sample].tick;
180         telemetry.generic.type = AO_TELEMETRY_BARO;
181         telemetry.baro.samples = samples;
182         for (sample = 0; sample < samples; sample++) {
183                 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
184                 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
185         }
186         ao_radio_send(&telemetry, sizeof (telemetry));
187 }
188 #endif
189
190 static __pdata int8_t ao_telemetry_config_max;
191 static __pdata int8_t ao_telemetry_config_cur;
192
193 static void
194 ao_send_configuration(void)
195 {
196         if (--ao_telemetry_config_cur <= 0)
197         {
198                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
199                 telemetry.configuration.device = AO_idProduct_NUMBER;
200                 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
201                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
202                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
203                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
204                 telemetry.configuration.main_deploy = ao_config.main_deploy;
205                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
206                 ao_xmemcpy (telemetry.configuration.callsign,
207                             ao_config.callsign,
208                             AO_MAX_CALLSIGN);
209                 ao_xmemcpy (telemetry.configuration.version,
210                             CODE_TO_XDATA(ao_version),
211                             AO_MAX_VERSION);
212                 ao_radio_send(&telemetry, sizeof (telemetry));
213                 ao_telemetry_config_cur = ao_telemetry_config_max;
214         }
215 }
216
217 #if HAS_GPS
218
219 static __pdata int8_t ao_telemetry_loc_cur;
220 static __pdata int8_t ao_telemetry_sat_cur;
221
222 static void
223 ao_send_location(void)
224 {
225         if (--ao_telemetry_loc_cur <= 0)
226         {
227                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
228                 ao_mutex_get(&ao_gps_mutex);
229                 ao_xmemcpy(&telemetry.location.flags,
230                        &ao_gps_data.flags,
231                        26);
232                 ao_mutex_put(&ao_gps_mutex);
233                 ao_radio_send(&telemetry, sizeof (telemetry));
234                 ao_telemetry_loc_cur = ao_telemetry_config_max;
235         }
236 }
237
238 static void
239 ao_send_satellite(void)
240 {
241         if (--ao_telemetry_sat_cur <= 0)
242         {
243                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
244                 ao_mutex_get(&ao_gps_mutex);
245                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
246                 ao_xmemcpy(&telemetry.satellite.sats,
247                        &ao_gps_tracking_data.sats,
248                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
249                 ao_mutex_put(&ao_gps_mutex);
250                 ao_radio_send(&telemetry, sizeof (telemetry));
251                 ao_telemetry_sat_cur = ao_telemetry_config_max;
252         }
253 }
254 #endif
255
256 #if HAS_COMPANION
257
258 static __pdata int8_t ao_telemetry_companion_max;
259 static __pdata int8_t ao_telemetry_companion_cur;
260
261 static void
262 ao_send_companion(void)
263 {
264         if (--ao_telemetry_companion_cur <= 0) {
265                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
266                 telemetry.companion.board_id = ao_companion_setup.board_id;
267                 telemetry.companion.update_period = ao_companion_setup.update_period;
268                 telemetry.companion.channels = ao_companion_setup.channels;
269                 ao_mutex_get(&ao_companion_mutex);
270                 ao_xmemcpy(&telemetry.companion.companion_data,
271                        ao_companion_data,
272                        ao_companion_setup.channels * 2);
273                 ao_mutex_put(&ao_companion_mutex);
274                 ao_radio_send(&telemetry, sizeof (telemetry));
275                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
276         }
277 }
278 #endif
279
280 void
281 ao_telemetry(void)
282 {
283         uint16_t        time;
284         int16_t         delay;
285
286         ao_config_get();
287         if (!ao_config.radio_enable)
288                 ao_exit();
289         while (!ao_flight_number)
290                 ao_sleep(&ao_flight_number);
291
292         telemetry.generic.serial = ao_serial_number;
293         for (;;) {
294                 while (ao_telemetry_interval == 0)
295                         ao_sleep(&telemetry);
296                 time = ao_rdf_time = ao_time();
297 #if HAS_APRS
298                 ao_aprs_time = time;
299 #endif
300                 while (ao_telemetry_interval) {
301
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 #ifdef AO_SEND_MEGA
311                                 ao_send_mega_sensor();
312                                 ao_send_mega_data();
313 #else
314                                 ao_send_sensor();
315 #endif
316
317 #if HAS_COMPANION
318                                 if (ao_companion_running)
319                                         ao_send_companion();
320 #endif
321                                 ao_send_configuration();
322 #if HAS_GPS
323                                 ao_send_location();
324                                 ao_send_satellite();
325 #endif
326                         }
327 #ifndef AO_SEND_ALL_BARO
328                         if (ao_rdf &&
329 #if HAS_APRS
330                             !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
331 #endif
332                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
333                         {
334 #if HAS_IGNITE_REPORT
335                                 uint8_t c;
336 #endif
337                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
338 #if HAS_IGNITE_REPORT
339                                 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
340                                         ao_radio_continuity(c);
341                                 else
342 #endif
343                                         ao_radio_rdf();
344                         }
345 #if HAS_APRS
346                         if ((ao_config.radio_enable & AO_RADIO_ENABLE_APRS) &&
347                             (int16_t) (ao_time() - ao_aprs_time) >= 0)
348                         {
349                                 ao_aprs_time = ao_time() + AO_APRS_INTERVAL_TICKS;
350                                 ao_aprs_send();
351                         }
352 #endif
353 #endif
354                         time += ao_telemetry_interval;
355                         delay = time - ao_time();
356                         if (delay > 0) {
357                                 ao_alarm(delay);
358                                 ao_sleep(&telemetry);
359                                 ao_clear_alarm();
360                         }
361                         else
362                                 time = ao_time();
363                 bottom: ;
364                 }
365         }
366 }
367
368 void
369 ao_telemetry_set_interval(uint16_t interval)
370 {
371         int8_t  cur = 0;
372         ao_telemetry_interval = interval;
373         
374 #if AO_SEND_MEGA
375         if (interval > 1)
376                 ao_telemetry_mega_data_max = 1;
377         else
378                 ao_telemetry_mega_data_max = 2;
379         if (ao_telemetry_mega_data_max > cur)
380                 cur++;
381         ao_telemetry_mega_data_cur = cur;
382 #endif
383
384 #if HAS_COMPANION
385         if (!ao_companion_setup.update_period)
386                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
387         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
388         if (ao_telemetry_companion_max > cur)
389                 cur++;
390         ao_telemetry_companion_cur = cur;
391 #endif
392
393         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
394 #if HAS_COMPANION
395         if (ao_telemetry_config_max > cur)
396                 cur++;
397         ao_telemetry_config_cur = cur;
398 #endif
399
400 #if HAS_GPS
401         if (ao_telemetry_config_max > cur)
402                 cur++;
403         ao_telemetry_loc_cur = cur;
404         if (ao_telemetry_config_max > cur)
405                 cur++;
406         ao_telemetry_sat_cur = cur;
407 #endif
408         ao_wakeup(&telemetry);
409 }
410
411 void
412 ao_rdf_set(uint8_t rdf)
413 {
414         ao_rdf = rdf;
415         if (rdf == 0)
416                 ao_radio_rdf_abort();
417         else {
418                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
419         }
420 }
421
422 __xdata struct ao_task  ao_telemetry_task;
423
424 void
425 ao_telemetry_init()
426 {
427         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
428 }