altos: Make gcc happy with ao_telemetry_set_interval
[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         telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x;
108         telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y;
109         telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z;
110
111         telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x;
112         telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y;
113         telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z;
114
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
119         ao_radio_send(&telemetry, sizeof (telemetry));
120 }
121
122 static __pdata int8_t ao_telemetry_mega_data_max;
123 static __pdata int8_t ao_telemetry_mega_data_cur;
124
125 /* Send mega data packet */
126 static void
127 ao_send_mega_data(void)
128 {
129         if (--ao_telemetry_mega_data_cur <= 0) {
130                 __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
131                 uint8_t i;
132
133                 telemetry.generic.tick = packet->tick;
134                 telemetry.generic.type = AO_TELEMETRY_MEGA_DATA;
135
136                 telemetry.mega_data.state = ao_flight_state;
137                 telemetry.mega_data.v_batt = packet->adc.v_batt;
138                 telemetry.mega_data.v_pyro = packet->adc.v_pbatt;
139
140                 /* XXX figure out right shift value; 4 might suffice */
141                 for (i = 0; i < AO_ADC_NUM_SENSE; i++)
142                         telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 8;
143
144                 telemetry.mega_data.ground_pres = ao_ground_pres;
145                 telemetry.mega_data.ground_accel = ao_ground_accel;
146                 telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g;
147                 telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g;
148
149                 telemetry.mega_data.acceleration = ao_accel;
150                 telemetry.mega_data.speed = ao_speed;
151                 telemetry.mega_data.height = ao_height;
152
153                 ao_radio_send(&telemetry, sizeof (telemetry));
154                 ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max;
155         }
156 }
157 #endif /* AO_SEND_MEGA */
158
159 #ifdef AO_SEND_ALL_BARO
160 static uint8_t          ao_baro_sample;
161
162 static void
163 ao_send_baro(void)
164 {
165         uint8_t         sample = ao_sample_data;
166         uint8_t         samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
167
168         if (samples > 12) {
169                 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
170                 samples = 12;
171         }
172         telemetry.generic.tick = ao_data_ring[sample].tick;
173         telemetry.generic.type = AO_TELEMETRY_BARO;
174         telemetry.baro.samples = samples;
175         for (sample = 0; sample < samples; sample++) {
176                 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
177                 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
178         }
179         ao_radio_send(&telemetry, sizeof (telemetry));
180 }
181 #endif
182
183 static __pdata int8_t ao_telemetry_config_max;
184 static __pdata int8_t ao_telemetry_config_cur;
185
186 static void
187 ao_send_configuration(void)
188 {
189         if (--ao_telemetry_config_cur <= 0)
190         {
191                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
192                 telemetry.configuration.device = AO_idProduct_NUMBER;
193                 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
194                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
195                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
196                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
197                 telemetry.configuration.main_deploy = ao_config.main_deploy;
198                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
199                 ao_xmemcpy (telemetry.configuration.callsign,
200                             ao_config.callsign,
201                             AO_MAX_CALLSIGN);
202                 ao_xmemcpy (telemetry.configuration.version,
203                             CODE_TO_XDATA(ao_version),
204                             AO_MAX_VERSION);
205                 ao_radio_send(&telemetry, sizeof (telemetry));
206                 ao_telemetry_config_cur = ao_telemetry_config_max;
207         }
208 }
209
210 #if HAS_GPS
211
212 static __pdata int8_t ao_telemetry_loc_cur;
213 static __pdata int8_t ao_telemetry_sat_cur;
214
215 static void
216 ao_send_location(void)
217 {
218         if (--ao_telemetry_loc_cur <= 0)
219         {
220                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
221                 ao_mutex_get(&ao_gps_mutex);
222                 ao_xmemcpy(&telemetry.location.flags,
223                        &ao_gps_data.flags,
224                        26);
225                 ao_mutex_put(&ao_gps_mutex);
226                 ao_radio_send(&telemetry, sizeof (telemetry));
227                 ao_telemetry_loc_cur = ao_telemetry_config_max;
228         }
229 }
230
231 static void
232 ao_send_satellite(void)
233 {
234         if (--ao_telemetry_sat_cur <= 0)
235         {
236                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
237                 ao_mutex_get(&ao_gps_mutex);
238                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
239                 ao_xmemcpy(&telemetry.satellite.sats,
240                        &ao_gps_tracking_data.sats,
241                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
242                 ao_mutex_put(&ao_gps_mutex);
243                 ao_radio_send(&telemetry, sizeof (telemetry));
244                 ao_telemetry_sat_cur = ao_telemetry_config_max;
245         }
246 }
247 #endif
248
249 #if HAS_COMPANION
250
251 static __pdata int8_t ao_telemetry_companion_max;
252 static __pdata int8_t ao_telemetry_companion_cur;
253
254 static void
255 ao_send_companion(void)
256 {
257         if (--ao_telemetry_companion_cur <= 0) {
258                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
259                 telemetry.companion.board_id = ao_companion_setup.board_id;
260                 telemetry.companion.update_period = ao_companion_setup.update_period;
261                 telemetry.companion.channels = ao_companion_setup.channels;
262                 ao_mutex_get(&ao_companion_mutex);
263                 ao_xmemcpy(&telemetry.companion.companion_data,
264                        ao_companion_data,
265                        ao_companion_setup.channels * 2);
266                 ao_mutex_put(&ao_companion_mutex);
267                 ao_radio_send(&telemetry, sizeof (telemetry));
268                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
269         }
270 }
271 #endif
272
273 void
274 ao_telemetry(void)
275 {
276         uint16_t        time;
277         int16_t         delay;
278
279         ao_config_get();
280         if (!ao_config.radio_enable)
281                 ao_exit();
282         while (!ao_flight_number)
283                 ao_sleep(&ao_flight_number);
284
285         telemetry.generic.serial = ao_serial_number;
286         for (;;) {
287                 while (ao_telemetry_interval == 0)
288                         ao_sleep(&telemetry);
289                 time = ao_rdf_time = ao_time();
290                 while (ao_telemetry_interval) {
291
292
293 #ifdef AO_SEND_ALL_BARO
294                         ao_send_baro();
295 #endif
296 #ifdef AO_SEND_MEGA
297                         ao_send_mega_sensor();
298                         ao_send_mega_data();
299 #else
300                         ao_send_sensor();
301 #endif
302
303 #if HAS_COMPANION
304                         if (ao_companion_running)
305                                 ao_send_companion();
306 #endif
307                         ao_send_configuration();
308 #if HAS_GPS
309                         ao_send_location();
310                         ao_send_satellite();
311 #endif
312 #ifndef AO_SEND_ALL_BARO
313                         if (ao_rdf &&
314                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
315                         {
316                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
317                                 ao_radio_rdf(AO_MS_TO_RDF_LEN(AO_RDF_LENGTH_MS));
318                         }
319 #endif
320                         time += ao_telemetry_interval;
321                         delay = time - ao_time();
322                         if (delay > 0) {
323                                 ao_alarm(delay);
324                                 ao_sleep(&telemetry);
325                                 ao_clear_alarm();
326                         }
327                         else
328                                 time = ao_time();
329                 }
330         }
331 }
332
333 void
334 ao_telemetry_set_interval(uint16_t interval)
335 {
336         int8_t  cur = 0;
337         ao_telemetry_interval = interval;
338         
339 #if AO_SEND_MEGA
340         if (interval > 1)
341                 ao_telemetry_mega_data_max = 1;
342         else
343                 ao_telemetry_mega_data_max = 2;
344         if (ao_telemetry_mega_data_max > cur)
345                 cur++;
346         ao_telemetry_mega_data_cur = cur;
347 #endif
348
349 #if HAS_COMPANION
350         if (!ao_companion_setup.update_period)
351                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
352         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
353         if (ao_telemetry_companion_max > cur)
354                 cur++;
355         ao_telemetry_companion_cur = cur;
356 #endif
357
358         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
359 #if HAS_COMPANION
360         if (ao_telemetry_config_max > cur)
361                 cur++;
362         ao_telemetry_config_cur = cur;
363 #endif
364
365 #if HAS_GPS
366         if (ao_telemetry_config_max > cur)
367                 cur++;
368         ao_telemetry_loc_cur = cur;
369         if (ao_telemetry_config_max > cur)
370                 cur++;
371         ao_telemetry_sat_cur = cur;
372 #endif
373         ao_wakeup(&telemetry);
374 }
375
376 void
377 ao_rdf_set(uint8_t rdf)
378 {
379         ao_rdf = rdf;
380         if (rdf == 0)
381                 ao_radio_rdf_abort();
382         else
383                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
384 }
385
386 __xdata struct ao_task  ao_telemetry_task;
387
388 void
389 ao_telemetry_init()
390 {
391         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
392 }