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