altos: Ensure low-rate telem packets interleave with sensor telem packets
[fw/altos] / src / 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 static __pdata uint8_t ao_rdf = 0;
29 static __pdata uint16_t ao_rdf_time;
30
31 #define AO_RDF_INTERVAL_TICKS   AO_SEC_TO_TICKS(5)
32 #define AO_RDF_LENGTH_MS        500
33
34 #if defined(TELEMETRUM_V_0_1) || defined(TELEMETRUM_V_0_2) || defined(TELEMETRUM_V_1_0) || defined(TELEMETRUM_V_1_1)
35 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMETRUM
36 #endif
37
38 #if defined(TELEMINI_V_0_1)
39 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMINI
40 #endif
41
42 #if defined(TELENANO_V_0_1)
43 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELENANO
44 #endif
45
46 static __xdata union ao_telemetry_all   telemetry;
47
48 /* Send sensor packet */
49 static void
50 ao_send_sensor(void)
51 {
52         uint8_t         sample;
53         sample = ao_sample_adc;
54                         
55         telemetry.generic.tick = ao_adc_ring[sample].tick;
56         telemetry.generic.type = AO_TELEMETRY_SENSOR;
57
58         telemetry.sensor.state = ao_flight_state;
59 #if HAS_ACCEL
60         telemetry.sensor.accel = ao_adc_ring[sample].accel;
61 #else
62         telemetry.sensor.accel = 0;
63 #endif
64         telemetry.sensor.pres = ao_adc_ring[sample].pres;
65         telemetry.sensor.temp = ao_adc_ring[sample].temp;
66         telemetry.sensor.v_batt = ao_adc_ring[sample].v_batt;
67 #if HAS_IGNITE
68         telemetry.sensor.sense_d = ao_adc_ring[sample].sense_d;
69         telemetry.sensor.sense_m = ao_adc_ring[sample].sense_m;
70 #else
71         telemetry.sensor.sense_d = 0;
72         telemetry.sensor.sense_m = 0;
73 #endif
74
75         telemetry.sensor.acceleration = ao_accel;
76         telemetry.sensor.speed = ao_speed;
77         telemetry.sensor.height = ao_height;
78
79         telemetry.sensor.ground_pres = ao_ground_pres;
80 #if HAS_ACCEL
81         telemetry.sensor.ground_accel = ao_ground_accel;
82         telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
83         telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
84 #else
85         telemetry.sensor.ground_accel = 0;
86         telemetry.sensor.accel_plus_g = 0;
87         telemetry.sensor.accel_minus_g = 0;
88 #endif
89
90         ao_radio_send(&telemetry, sizeof (telemetry));
91 }
92
93 static void
94 ao_send_configuration(void)
95 {
96         if (--ao_telemetry_config_cur <= 0)
97         {
98                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
99                 telemetry.configuration.device = AO_idProduct_NUMBER;
100                 telemetry.configuration.flight = ao_flight_number;
101                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
102                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
103                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
104                 telemetry.configuration.main_deploy = ao_config.main_deploy;
105                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
106                 memcpy (telemetry.configuration.callsign,
107                         ao_config.callsign,
108                         AO_MAX_CALLSIGN);
109                 memcpy (telemetry.configuration.version,
110                         ao_version,
111                         AO_MAX_VERSION);
112                 ao_radio_send(&telemetry, sizeof (telemetry));
113                 ao_telemetry_config_cur = ao_telemetry_config_max;
114         }
115 }
116
117 #if HAS_GPS
118 static void
119 ao_send_location(void)
120 {
121         if (--ao_telemetry_loc_cur <= 0)
122         {
123                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
124                 ao_mutex_get(&ao_gps_mutex);
125                 memcpy(&telemetry.location.flags,
126                        &ao_gps_data.flags,
127                        26);
128                 ao_mutex_put(&ao_gps_mutex);
129                 ao_radio_send(&telemetry, sizeof (telemetry));
130                 ao_telemetry_loc_cur = ao_telemetry_config_max;
131         }
132 }
133
134 static void
135 ao_send_satellite(void)
136 {
137         if (--ao_telemetry_sat_cur <= 0)
138         {
139                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
140                 ao_mutex_get(&ao_gps_mutex);
141                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
142                 memcpy(&telemetry.satellite.sats,
143                        &ao_gps_tracking_data.sats,
144                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
145                 ao_mutex_put(&ao_gps_mutex);
146                 ao_radio_send(&telemetry, sizeof (telemetry));
147                 ao_telemetry_sat_cur = ao_telemetry_config_max;
148         }
149 }
150 #endif
151
152 void
153 ao_telemetry(void)
154 {
155         uint16_t        time;
156         int16_t         delay;
157
158         ao_config_get();
159         while (!ao_flight_number)
160                 ao_sleep(&ao_flight_number);
161
162         telemetry.generic.serial = ao_serial_number;
163         for (;;) {
164                 while (ao_telemetry_interval == 0)
165                         ao_sleep(&telemetry);
166                 time = ao_rdf_time = ao_time();
167                 while (ao_telemetry_interval) {
168
169
170                         ao_send_sensor();
171                         ao_send_configuration();
172 #if HAS_GPS
173                         ao_send_location();
174                         ao_send_satellite();
175 #endif
176                         if (ao_rdf &&
177                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
178                         {
179                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
180                                 ao_radio_rdf(AO_RDF_LENGTH_MS);
181                         }
182                         time += ao_telemetry_interval;
183                         delay = time - ao_time();
184                         if (delay > 0)
185                                 ao_delay(delay);
186                         else
187                                 time = ao_time();
188                 }
189         }
190 }
191
192 void
193 ao_telemetry_set_interval(uint16_t interval)
194 {
195         ao_telemetry_interval = interval;
196         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
197         ao_telemetry_config_cur = 1;
198 #if HAS_GPS
199         ao_telemetry_loc_cur = 1;
200         if (ao_telemetry_config_max > ao_telemetry_loc_cur)
201                 ao_telemetry_loc_cur++;
202         ao_telemetry_sat_cur = ao_telemetry_loc_cur;
203         if (ao_telemetry_config_max > ao_telemetry_sat_cur)
204                 ao_telemetry_sat_cur++;
205 #endif
206         ao_wakeup(&telemetry);
207 }
208
209 void
210 ao_rdf_set(uint8_t rdf)
211 {
212         ao_rdf = rdf;
213         if (rdf == 0)
214                 ao_radio_rdf_abort();
215         else
216                 ao_rdf_time = ao_time();
217 }
218
219 __xdata struct ao_task  ao_telemetry_task;
220
221 void
222 ao_telemetry_init()
223 {
224         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
225 }