2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #include "ao_product.h"
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;
25 static __pdata int8_t ao_telemetry_loc_cur;
26 static __pdata int8_t ao_telemetry_sat_cur;
29 static __pdata int8_t ao_telemetry_companion_max;
30 static __pdata int8_t ao_telemetry_companion_cur;
32 static __pdata uint8_t ao_rdf = 0;
33 static __pdata uint16_t ao_rdf_time;
35 #define AO_RDF_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
36 #define AO_RDF_LENGTH_MS 500
38 #if defined(TELEMETRUM_V_0_1) || defined(TELEMETRUM_V_0_2) || defined(TELEMETRUM_V_1_0) || defined(TELEMETRUM_V_1_1)
39 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMETRUM
42 #if defined(TELEMINI_V_1_0)
43 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELEMINI
46 #if defined(TELENANO_V_0_1)
47 #define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_TELENANO
50 static __xdata union ao_telemetry_all telemetry;
52 /* Send sensor packet */
57 sample = ao_sample_adc;
59 telemetry.generic.tick = ao_adc_ring[sample].tick;
60 telemetry.generic.type = AO_TELEMETRY_SENSOR;
62 telemetry.sensor.state = ao_flight_state;
64 telemetry.sensor.accel = ao_adc_ring[sample].accel;
66 telemetry.sensor.accel = 0;
68 telemetry.sensor.pres = ao_adc_ring[sample].pres;
69 telemetry.sensor.temp = ao_adc_ring[sample].temp;
70 telemetry.sensor.v_batt = ao_adc_ring[sample].v_batt;
72 telemetry.sensor.sense_d = ao_adc_ring[sample].sense_d;
73 telemetry.sensor.sense_m = ao_adc_ring[sample].sense_m;
75 telemetry.sensor.sense_d = 0;
76 telemetry.sensor.sense_m = 0;
79 telemetry.sensor.acceleration = ao_accel;
80 telemetry.sensor.speed = ao_speed;
81 telemetry.sensor.height = ao_height;
83 telemetry.sensor.ground_pres = ao_ground_pres;
85 telemetry.sensor.ground_accel = ao_ground_accel;
86 telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
87 telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
89 telemetry.sensor.ground_accel = 0;
90 telemetry.sensor.accel_plus_g = 0;
91 telemetry.sensor.accel_minus_g = 0;
94 ao_radio_send(&telemetry, sizeof (telemetry));
98 ao_send_configuration(void)
100 if (--ao_telemetry_config_cur <= 0)
102 telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
103 telemetry.configuration.device = AO_idProduct_NUMBER;
104 telemetry.configuration.flight = ao_log_full() ? 0 : ao_flight_number;
105 telemetry.configuration.config_major = AO_CONFIG_MAJOR;
106 telemetry.configuration.config_minor = AO_CONFIG_MINOR;
107 telemetry.configuration.apogee_delay = ao_config.apogee_delay;
108 telemetry.configuration.main_deploy = ao_config.main_deploy;
109 telemetry.configuration.flight_log_max = ao_config.flight_log_max >> 10;
110 memcpy (telemetry.configuration.callsign,
113 memcpy (telemetry.configuration.version,
116 ao_radio_send(&telemetry, sizeof (telemetry));
117 ao_telemetry_config_cur = ao_telemetry_config_max;
123 ao_send_location(void)
125 if (--ao_telemetry_loc_cur <= 0)
127 telemetry.generic.type = AO_TELEMETRY_LOCATION;
128 ao_mutex_get(&ao_gps_mutex);
129 memcpy(&telemetry.location.flags,
132 ao_mutex_put(&ao_gps_mutex);
133 ao_radio_send(&telemetry, sizeof (telemetry));
134 ao_telemetry_loc_cur = ao_telemetry_config_max;
139 ao_send_satellite(void)
141 if (--ao_telemetry_sat_cur <= 0)
143 telemetry.generic.type = AO_TELEMETRY_SATELLITE;
144 ao_mutex_get(&ao_gps_mutex);
145 telemetry.satellite.channels = ao_gps_tracking_data.channels;
146 memcpy(&telemetry.satellite.sats,
147 &ao_gps_tracking_data.sats,
148 AO_MAX_GPS_TRACKING * sizeof (struct ao_telemetry_satellite_info));
149 ao_mutex_put(&ao_gps_mutex);
150 ao_radio_send(&telemetry, sizeof (telemetry));
151 ao_telemetry_sat_cur = ao_telemetry_config_max;
158 ao_send_companion(void)
160 if (--ao_telemetry_companion_cur <= 0) {
161 telemetry.generic.type = AO_TELEMETRY_COMPANION;
162 telemetry.companion.board_id = ao_companion_setup.board_id;
163 telemetry.companion.update_period = ao_companion_setup.update_period;
164 telemetry.companion.channels = ao_companion_setup.channels;
165 ao_mutex_get(&ao_companion_mutex);
166 memcpy(&telemetry.companion.companion_data,
168 ao_companion_setup.channels * 2);
169 ao_mutex_put(&ao_companion_mutex);
170 ao_radio_send(&telemetry, sizeof (telemetry));
171 ao_telemetry_companion_cur = ao_telemetry_companion_max;
183 if (!ao_config.radio_enable)
185 while (!ao_flight_number)
186 ao_sleep(&ao_flight_number);
188 telemetry.generic.serial = ao_serial_number;
190 while (ao_telemetry_interval == 0)
191 ao_sleep(&telemetry);
192 time = ao_rdf_time = ao_time();
193 while (ao_telemetry_interval) {
198 if (ao_companion_running)
201 ao_send_configuration();
207 (int16_t) (ao_time() - ao_rdf_time) >= 0)
209 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
210 ao_radio_rdf(AO_RDF_LENGTH_MS);
212 time += ao_telemetry_interval;
213 delay = time - ao_time();
223 ao_telemetry_set_interval(uint16_t interval)
225 ao_telemetry_interval = interval;
228 if (!ao_companion_setup.update_period)
229 ao_companion_setup.update_period = AO_SEC_TO_TICKS(1);
230 ao_telemetry_companion_max = ao_companion_setup.update_period / interval;
231 ao_telemetry_companion_cur = 1;
234 ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval;
236 ao_telemetry_config_cur = ao_telemetry_companion_cur;
237 if (ao_telemetry_config_max > ao_telemetry_config_cur)
238 ao_telemetry_config_cur++;
240 ao_telemetry_config_cur = 1;
244 ao_telemetry_loc_cur = ao_telemetry_config_cur;
245 if (ao_telemetry_config_max > ao_telemetry_loc_cur)
246 ao_telemetry_loc_cur++;
247 ao_telemetry_sat_cur = ao_telemetry_loc_cur;
248 if (ao_telemetry_config_max > ao_telemetry_sat_cur)
249 ao_telemetry_sat_cur++;
251 ao_wakeup(&telemetry);
255 ao_rdf_set(uint8_t rdf)
259 ao_radio_rdf_abort();
261 ao_rdf_time = ao_time();
264 __xdata struct ao_task ao_telemetry_task;
269 ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");