dfde22350d84c0be1a64a1fe43ebfca1e2fd124e
[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(TELEMEGA)
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 #if HAS_LOG
202                 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
203 #else
204                 telemetry.configuration.flight = ao_flight_number;
205 #endif
206                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
207                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
208                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
209                 telemetry.configuration.main_deploy = ao_config.main_deploy;
210                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
211                 ao_xmemcpy (telemetry.configuration.callsign,
212                             ao_config.callsign,
213                             AO_MAX_CALLSIGN);
214                 ao_xmemcpy (telemetry.configuration.version,
215                             CODE_TO_XDATA(ao_version),
216                             AO_MAX_VERSION);
217                 ao_radio_send(&telemetry, sizeof (telemetry));
218                 ao_telemetry_config_cur = ao_telemetry_config_max;
219         }
220 }
221
222 #if HAS_GPS
223
224 static __pdata int8_t ao_telemetry_loc_cur;
225 static __pdata int8_t ao_telemetry_sat_cur;
226
227 static void
228 ao_send_location(void)
229 {
230         if (--ao_telemetry_loc_cur <= 0)
231         {
232                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
233                 ao_mutex_get(&ao_gps_mutex);
234                 ao_xmemcpy(&telemetry.location.flags,
235                        &ao_gps_data.flags,
236                        26);
237                 telemetry.location.tick = ao_gps_tick;
238                 ao_mutex_put(&ao_gps_mutex);
239                 ao_radio_send(&telemetry, sizeof (telemetry));
240                 ao_telemetry_loc_cur = ao_telemetry_config_max;
241         }
242 }
243
244 static void
245 ao_send_satellite(void)
246 {
247         if (--ao_telemetry_sat_cur <= 0)
248         {
249                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
250                 ao_mutex_get(&ao_gps_mutex);
251                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
252                 ao_xmemcpy(&telemetry.satellite.sats,
253                        &ao_gps_tracking_data.sats,
254                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
255                 ao_mutex_put(&ao_gps_mutex);
256                 ao_radio_send(&telemetry, sizeof (telemetry));
257                 ao_telemetry_sat_cur = ao_telemetry_config_max;
258         }
259 }
260 #endif
261
262 #if HAS_COMPANION
263
264 static __pdata int8_t ao_telemetry_companion_max;
265 static __pdata int8_t ao_telemetry_companion_cur;
266
267 static void
268 ao_send_companion(void)
269 {
270         if (--ao_telemetry_companion_cur <= 0) {
271                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
272                 telemetry.companion.board_id = ao_companion_setup.board_id;
273                 telemetry.companion.update_period = ao_companion_setup.update_period;
274                 telemetry.companion.channels = ao_companion_setup.channels;
275                 ao_mutex_get(&ao_companion_mutex);
276                 ao_xmemcpy(&telemetry.companion.companion_data,
277                        ao_companion_data,
278                        ao_companion_setup.channels * 2);
279                 ao_mutex_put(&ao_companion_mutex);
280                 ao_radio_send(&telemetry, sizeof (telemetry));
281                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
282         }
283 }
284 #endif
285
286 void
287 ao_telemetry(void)
288 {
289         uint16_t        time;
290         int16_t         delay;
291
292         ao_config_get();
293         if (!ao_config.radio_enable)
294                 ao_exit();
295         while (!ao_flight_number)
296                 ao_sleep(&ao_flight_number);
297
298         telemetry.generic.serial = ao_serial_number;
299         for (;;) {
300                 while (ao_telemetry_interval == 0)
301                         ao_sleep(&telemetry);
302                 time = ao_rdf_time = ao_time();
303 #if HAS_APRS
304                 ao_aprs_time = time;
305 #endif
306                 while (ao_telemetry_interval) {
307
308 #if HAS_APRS
309                         if (!(ao_config.radio_enable & AO_RADIO_DISABLE_TELEMETRY))
310 #endif
311                         {
312 #ifdef AO_SEND_ALL_BARO
313                                 ao_send_baro();
314 #endif
315 #if HAS_FLIGHT
316 #ifdef AO_SEND_MEGA
317                                 ao_send_mega_sensor();
318                                 ao_send_mega_data();
319 #else
320                                 ao_send_sensor();
321 #endif
322 #endif
323
324 #if HAS_COMPANION
325                                 if (ao_companion_running)
326                                         ao_send_companion();
327 #endif
328                                 ao_send_configuration();
329 #if HAS_GPS
330                                 ao_send_location();
331                                 ao_send_satellite();
332 #endif
333                         }
334 #ifndef AO_SEND_ALL_BARO
335                         if (ao_rdf &&
336 #if HAS_APRS
337                             !(ao_config.radio_enable & AO_RADIO_DISABLE_RDF) &&
338 #endif
339                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
340                         {
341 #if HAS_IGNITE_REPORT
342                                 uint8_t c;
343 #endif
344                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
345 #if HAS_IGNITE_REPORT
346                                 if (ao_flight_state == ao_flight_pad && (c = ao_report_igniter()))
347                                         ao_radio_continuity(c);
348                                 else
349 #endif
350                                         ao_radio_rdf();
351                         }
352 #if HAS_APRS
353                         if (ao_config.aprs_interval != 0 &&
354                             (int16_t) (ao_time() - ao_aprs_time) >= 0)
355                         {
356                                 ao_aprs_time = ao_time() + AO_SEC_TO_TICKS(ao_config.aprs_interval);
357                                 ao_aprs_send();
358                         }
359 #endif
360 #endif
361                         time += ao_telemetry_interval;
362                         delay = time - ao_time();
363                         if (delay > 0) {
364                                 ao_alarm(delay);
365                                 ao_sleep(&telemetry);
366                                 ao_clear_alarm();
367                         }
368                         else
369                                 time = ao_time();
370                 bottom: ;
371                 }
372         }
373 }
374
375 void
376 ao_telemetry_set_interval(uint16_t interval)
377 {
378         int8_t  cur = 0;
379         ao_telemetry_interval = interval;
380         
381 #if AO_SEND_MEGA
382         if (interval > 1)
383                 ao_telemetry_mega_data_max = 1;
384         else
385                 ao_telemetry_mega_data_max = 2;
386         if (ao_telemetry_mega_data_max > cur)
387                 cur++;
388         ao_telemetry_mega_data_cur = cur;
389 #endif
390
391 #if HAS_COMPANION
392         if (!ao_companion_setup.update_period)
393                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
394         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
395         if (ao_telemetry_companion_max > cur)
396                 cur++;
397         ao_telemetry_companion_cur = cur;
398 #endif
399
400         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
401 #if HAS_COMPANION
402         if (ao_telemetry_config_max > cur)
403                 cur++;
404         ao_telemetry_config_cur = cur;
405 #endif
406
407 #if HAS_GPS
408         if (ao_telemetry_config_max > cur)
409                 cur++;
410         ao_telemetry_loc_cur = cur;
411         if (ao_telemetry_config_max > cur)
412                 cur++;
413         ao_telemetry_sat_cur = cur;
414 #endif
415         ao_wakeup(&telemetry);
416 }
417
418 void
419 ao_rdf_set(uint8_t rdf)
420 {
421         ao_rdf = rdf;
422         if (rdf == 0)
423                 ao_radio_rdf_abort();
424         else {
425                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
426         }
427 }
428
429 __xdata struct ao_task  ao_telemetry_task;
430
431 void
432 ao_telemetry_init()
433 {
434         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
435 }