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