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