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