altos: Switch ao_dbg.c __xdata to __pdata
[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         telemetry.sensor.ground_accel = ao_ground_accel;
81         telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
82         telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
83
84         ao_radio_send(&telemetry, sizeof (telemetry));
85 }
86
87 static void
88 ao_send_configuration(void)
89 {
90         if (--ao_telemetry_config_cur <= 0)
91         {
92                 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
93                 telemetry.configuration.device = AO_idProduct_NUMBER;
94                 telemetry.configuration.flight = ao_flight_number;
95                 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
96                 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
97                 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
98                 telemetry.configuration.main_deploy = ao_config.main_deploy;
99                 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
100                 memcpy (telemetry.configuration.callsign,
101                         ao_config.callsign,
102                         AO_MAX_CALLSIGN);
103                 memcpy (telemetry.configuration.version,
104                         ao_version,
105                         AO_MAX_VERSION);
106                 ao_radio_send(&telemetry, sizeof (telemetry));
107                 ao_telemetry_config_cur = ao_telemetry_config_max;
108         }
109 }
110
111 #if HAS_GPS
112 static void
113 ao_send_location(void)
114 {
115         if (--ao_telemetry_loc_cur <= 0)
116         {
117                 telemetry.generic.type = AO_TELEMETRY_LOCATION;
118                 ao_mutex_get(&ao_gps_mutex);
119                 memcpy(&telemetry.location.flags,
120                        &ao_gps_data.flags,
121                        26);
122                 ao_mutex_put(&ao_gps_mutex);
123                 ao_radio_send(&telemetry, sizeof (telemetry));
124                 ao_telemetry_loc_cur = ao_telemetry_config_max;
125         }
126 }
127
128 static void
129 ao_send_satellite(void)
130 {
131         if (--ao_telemetry_sat_cur <= 0)
132         {
133                 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
134                 ao_mutex_get(&ao_gps_mutex);
135                 telemetry.satellite.channels = ao_gps_tracking_data.channels;
136                 memcpy(&telemetry.satellite.sats,
137                        &ao_gps_tracking_data.sats,
138                        AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
139                 ao_mutex_put(&ao_gps_mutex);
140                 ao_radio_send(&telemetry, sizeof (telemetry));
141                 ao_telemetry_sat_cur = ao_telemetry_config_max;
142         }
143 }
144 #endif
145
146 void
147 ao_telemetry(void)
148 {
149         uint16_t        time;
150         int16_t         delay;
151
152         ao_config_get();
153         while (!ao_flight_number)
154                 ao_sleep(&ao_flight_number);
155
156         telemetry.generic.serial = ao_serial_number;
157         for (;;) {
158                 while (ao_telemetry_interval == 0)
159                         ao_sleep(&telemetry);
160                 time = ao_rdf_time = ao_time();
161                 while (ao_telemetry_interval) {
162
163
164                         ao_send_sensor();
165                         ao_send_configuration();
166 #if HAS_GPS
167                         ao_send_location();
168                         ao_send_satellite();
169 #endif
170                         if (ao_rdf &&
171                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
172                         {
173                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
174                                 ao_radio_rdf(AO_RDF_LENGTH_MS);
175                         }
176                         time += ao_telemetry_interval;
177                         delay = time - ao_time();
178                         if (delay > 0)
179                                 ao_delay(delay);
180                         else
181                                 time = ao_time();
182                 }
183         }
184 }
185
186 void
187 ao_telemetry_set_interval(uint16_t interval)
188 {
189         ao_telemetry_interval = interval;
190         ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
191         ao_telemetry_config_cur = 0;
192 #if HAS_GPS
193         ao_telemetry_loc_cur = 0;
194         if (ao_telemetry_config_max - 1 > ao_telemetry_loc_cur)
195                 ao_telemetry_loc_cur++;
196         ao_telemetry_sat_cur = ao_telemetry_loc_cur;
197         if (ao_telemetry_config_max - 1 > ao_telemetry_sat_cur)
198                 ao_telemetry_sat_cur++;
199 #endif
200         ao_wakeup(&telemetry);
201 }
202
203 void
204 ao_rdf_set(uint8_t rdf)
205 {
206         ao_rdf = rdf;
207         if (rdf == 0)
208                 ao_radio_rdf_abort();
209         else
210                 ao_rdf_time = ao_time();
211 }
212
213 __xdata struct ao_task  ao_telemetry_task;
214
215 void
216 ao_telemetry_init()
217 {
218         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
219 }