Merge branch 'master' into micropeak-logging
[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 #if HAS_APRS
303                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
304 #endif
305                         {
306 #ifdef AO_SEND_ALL_BARO
307                                 ao_send_baro();
308 #endif
309 #ifdef AO_SEND_MEGA
310                                 ao_send_mega_sensor();
311                                 ao_send_mega_data();
312 #else
313                                 ao_send_sensor();
314 #endif
315
316 #if HAS_COMPANION
317                                 if (ao_companion_running)
318                                         ao_send_companion();
319 #endif
320                                 ao_send_configuration();
321 #if HAS_GPS
322                                 ao_send_location();
323                                 ao_send_satellite();
324 #endif
325                         }
326 #ifndef AO_SEND_ALL_BARO
327                         if (ao_rdf &&
328 #if HAS_APRS
329                             !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
330 #endif
331                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
332                         {
333 #if HAS_IGNITE_REPORT
334                                 uint8_t c;
335 #endif
336                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
337 #if HAS_IGNITE_REPORT
338                                 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
339                                         ao_radio_continuity(c);
340                                 else
341 #endif
342                                         ao_radio_rdf();
343                         }
344 #if HAS_APRS
345                         if (ao_config.aprs_interval != 0 &&
346                             (int16_t) (ao_time() - ao_aprs_time) >= 0)
347                         {
348                                 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
349                                 ao_aprs_send();
350                         }
351 #endif
352 #endif
353                         time += ao_telemetry_interval;
354                         delay = time - ao_time();
355                         if (delay > 0) {
356                                 ao_alarm(delay);
357                                 ao_sleep(&telemetry);
358                                 ao_clear_alarm();
359                         }
360                         else
361                                 time = ao_time();
362                 bottom: ;
363                 }
364         }
365 }
366
367 void
368 ao_telemetry_set_interval(uint16_t interval)
369 {
370         int8_t  cur = 0;
371         ao_telemetry_interval = interval;
372         
373 #if AO_SEND_MEGA
374         if (interval > 1)
375                 ao_telemetry_mega_data_max = 1;
376         else
377                 ao_telemetry_mega_data_max = 2;
378         if (ao_telemetry_mega_data_max > cur)
379                 cur++;
380         ao_telemetry_mega_data_cur = cur;
381 #endif
382
383 #if HAS_COMPANION
384         if (!ao_companion_setup.update_period)
385                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
386         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
387         if (ao_telemetry_companion_max > cur)
388                 cur++;
389         ao_telemetry_companion_cur = cur;
390 #endif
391
392         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
393 #if HAS_COMPANION
394         if (ao_telemetry_config_max > cur)
395                 cur++;
396         ao_telemetry_config_cur = cur;
397 #endif
398
399 #if HAS_GPS
400         if (ao_telemetry_config_max > cur)
401                 cur++;
402         ao_telemetry_loc_cur = cur;
403         if (ao_telemetry_config_max > cur)
404                 cur++;
405         ao_telemetry_sat_cur = cur;
406 #endif
407         ao_wakeup(&telemetry);
408 }
409
410 void
411 ao_rdf_set(uint8_t rdf)
412 {
413         ao_rdf = rdf;
414         if (rdf == 0)
415                 ao_radio_rdf_abort();
416         else {
417                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
418         }
419 }
420
421 __xdata struct ao_task  ao_telemetry_task;
422
423 void
424 ao_telemetry_init()
425 {
426         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
427 }