altos: Add telemetry to megametrum
[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 int8_t ao_telemetry_config_max;
23 static __pdata int8_t ao_telemetry_config_cur;
24 #if HAS_GPS
25 static __pdata int8_t ao_telemetry_loc_cur;
26 static __pdata int8_t ao_telemetry_sat_cur;
27 #endif
28 #if HAS_COMPANION
29 static __pdata int8_t ao_telemetry_companion_max;
30 static __pdata int8_t ao_telemetry_companion_cur;
31 #endif
32 static __pdata uint8_t ao_rdf = 0;
33 static __pdata uint16_t ao_rdf_time;
34
35 #define AO_RDF_INTERVAL_TICKS   AO_SEC_TO_TICKS(5)
36 #define AO_RDF_LENGTH_MS        500
37
38 #if defined(MEGAMETRUM)
39 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_MEGAMETRUM
40 #endif
41
42 #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)
43 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMETRUM
44 #endif
45
46 #if defined(TELEMINI_V_1_0)
47 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMINI
48 #endif
49
50 #if defined(TELENANO_V_0_1)
51 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELENANO
52 #endif
53
54 static __xdata union ao_telemetry_all   telemetry;
55
56 /* Send sensor packet */
57 static void
58 ao_send_sensor(void)
59 {
60         __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)];
61                         
62         telemetry.generic.tick = packet->tick;
63         telemetry.generic.type = AO_TELEMETRY_SENSOR;
64
65         telemetry.sensor.state = ao_flight_state;
66 #if HAS_ACCEL
67         telemetry.sensor.accel = packet->adc.accel;
68 #else
69         telemetry.sensor.accel = 0;
70 #endif
71         telemetry.sensor.pres = ao_data_pres(packet);
72         telemetry.sensor.temp = packet->adc.temp;
73         telemetry.sensor.v_batt = packet->adc.v_batt;
74 #if HAS_IGNITE
75         telemetry.sensor.sense_d = packet->adc.sense_d;
76         telemetry.sensor.sense_m = packet->adc.sense_m;
77 #else
78         telemetry.sensor.sense_d = 0;
79         telemetry.sensor.sense_m = 0;
80 #endif
81
82         telemetry.sensor.acceleration = ao_accel;
83         telemetry.sensor.speed = ao_speed;
84         telemetry.sensor.height = ao_height;
85
86         telemetry.sensor.ground_pres = ao_ground_pres;
87 #if HAS_ACCEL
88         telemetry.sensor.ground_accel = ao_ground_accel;
89         telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
90         telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
91 #else
92         telemetry.sensor.ground_accel = 0;
93         telemetry.sensor.accel_plus_g = 0;
94         telemetry.sensor.accel_minus_g = 0;
95 #endif
96
97         ao_radio_send(&telemetry, sizeof (telemetry));
98 }
99
100 static uint8_t          ao_baro_sample;
101
102 #ifdef AO_SEND_ALL_BARO
103 static void
104 ao_send_baro(void)
105 {
106         uint8_t         sample = ao_sample_data;
107         uint8_t         samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1);
108
109         if (samples > 12) {
110                 ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1);
111                 samples = 12;
112         }
113         telemetry.generic.tick = ao_data_ring[sample].tick;
114         telemetry.generic.type = AO_TELEMETRY_BARO;
115         telemetry.baro.samples = samples;
116         for (sample = 0; sample < samples; sample++) {
117                 telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres;
118                 ao_baro_sample = ao_data_ring_next(ao_baro_sample);
119         }
120         ao_radio_send(&telemetry, sizeof (telemetry));
121 }
122 #endif
123
124 static void
125 ao_send_configuration(void)
126 {
127         if (--ao_telemetry_config_cur <= 0)
128         {
129                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
130                 telemetry.configuration.device = AO_idProduct_NUMBER;
131                 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
132                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
133                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
134                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
135                 telemetry.configuration.main_deploy = ao_config.main_deploy;
136                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
137                 ao_xmemcpy (telemetry.configuration.callsign,
138                             ao_config.callsign,
139                             AO_MAX_CALLSIGN);
140                 ao_xmemcpy (telemetry.configuration.version,
141                             CODE_TO_XDATA(ao_version),
142                             AO_MAX_VERSION);
143                 ao_radio_send(&telemetry, sizeof (telemetry));
144                 ao_telemetry_config_cur = ao_telemetry_config_max;
145         }
146 }
147
148 #if HAS_GPS
149 static void
150 ao_send_location(void)
151 {
152         if (--ao_telemetry_loc_cur <= 0)
153         {
154                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
155                 ao_mutex_get(&ao_gps_mutex);
156                 ao_xmemcpy(&telemetry.location.flags,
157                        &ao_gps_data.flags,
158                        26);
159                 ao_mutex_put(&ao_gps_mutex);
160                 ao_radio_send(&telemetry, sizeof (telemetry));
161                 ao_telemetry_loc_cur = ao_telemetry_config_max;
162         }
163 }
164
165 static void
166 ao_send_satellite(void)
167 {
168         if (--ao_telemetry_sat_cur <= 0)
169         {
170                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
171                 ao_mutex_get(&ao_gps_mutex);
172                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
173                 ao_xmemcpy(&telemetry.satellite.sats,
174                        &ao_gps_tracking_data.sats,
175                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
176                 ao_mutex_put(&ao_gps_mutex);
177                 ao_radio_send(&telemetry, sizeof (telemetry));
178                 ao_telemetry_sat_cur = ao_telemetry_config_max;
179         }
180 }
181 #endif
182
183 #if HAS_COMPANION
184 static void
185 ao_send_companion(void)
186 {
187         if (--ao_telemetry_companion_cur <= 0) {
188                 telemetry.generic.type = AO_TELEMETRY_COMPANION;
189                 telemetry.companion.board_id = ao_companion_setup.board_id;
190                 telemetry.companion.update_period = ao_companion_setup.update_period;
191                 telemetry.companion.channels = ao_companion_setup.channels;
192                 ao_mutex_get(&ao_companion_mutex);
193                 ao_xmemcpy(&telemetry.companion.companion_data,
194                        ao_companion_data,
195                        ao_companion_setup.channels * 2);
196                 ao_mutex_put(&ao_companion_mutex);
197                 ao_radio_send(&telemetry, sizeof (telemetry));
198                 ao_telemetry_companion_cur = ao_telemetry_companion_max;
199         }
200 }
201 #endif
202
203 void
204 ao_telemetry(void)
205 {
206         uint16_t        time;
207         int16_t         delay;
208
209         ao_config_get();
210         if (!ao_config.radio_enable)
211                 ao_exit();
212         while (!ao_flight_number)
213                 ao_sleep(&ao_flight_number);
214
215         telemetry.generic.serial = ao_serial_number;
216         for (;;) {
217                 while (ao_telemetry_interval == 0)
218                         ao_sleep(&telemetry);
219                 time = ao_rdf_time = ao_time();
220                 while (ao_telemetry_interval) {
221
222
223 #ifdef AO_SEND_ALL_BARO
224                         ao_send_baro();
225 #endif
226                         ao_send_sensor();
227 #if HAS_COMPANION
228                         if (ao_companion_running)
229                                 ao_send_companion();
230 #endif
231                         ao_send_configuration();
232 #if HAS_GPS
233                         ao_send_location();
234                         ao_send_satellite();
235 #endif
236 #ifndef AO_SEND_ALL_BARO
237                         if (ao_rdf &&
238                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
239                         {
240                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
241                                 ao_radio_rdf(AO_MS_TO_RDF_LEN(AO_RDF_LENGTH_MS));
242                         }
243 #endif
244                         time += ao_telemetry_interval;
245                         delay = time - ao_time();
246                         if (delay > 0) {
247                                 ao_alarm(delay);
248                                 ao_sleep(&telemetry);
249                                 ao_clear_alarm();
250                         }
251                         else
252                                 time = ao_time();
253                 }
254         }
255 }
256
257 void
258 ao_telemetry_set_interval(uint16_t interval)
259 {
260         ao_telemetry_interval = interval;
261
262 #if HAS_COMPANION
263         if (!ao_companion_setup.update_period)
264                 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
265         ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
266         ao_telemetry_companion_cur = 1;
267 #endif
268
269         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
270 #if HAS_COMPANION
271         ao_telemetry_config_cur = ao_telemetry_companion_cur;
272         if (ao_telemetry_config_max > ao_telemetry_config_cur)
273                 ao_telemetry_config_cur++;
274 #else
275         ao_telemetry_config_cur = 1;
276 #endif
277
278 #if HAS_GPS
279         ao_telemetry_loc_cur = ao_telemetry_config_cur;
280         if (ao_telemetry_config_max > ao_telemetry_loc_cur)
281                 ao_telemetry_loc_cur++;
282         ao_telemetry_sat_cur = ao_telemetry_loc_cur;
283         if (ao_telemetry_config_max > ao_telemetry_sat_cur)
284                 ao_telemetry_sat_cur++;
285 #endif
286         ao_wakeup(&telemetry);
287 }
288
289 void
290 ao_rdf_set(uint8_t rdf)
291 {
292         ao_rdf = rdf;
293         if (rdf == 0)
294                 ao_radio_rdf_abort();
295         else
296                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
297 }
298
299 __xdata struct ao_task  ao_telemetry_task;
300
301 void
302 ao_telemetry_init()
303 {
304         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
305 }