altos: Add split telemetry code
[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 __xdata uint16_t ao_telemetry_interval = 0;
22 __xdata uint8_t ao_rdf = 0;
23 __xdata uint16_t ao_rdf_time;
24
25 #define AO_RDF_INTERVAL_TICKS   AO_SEC_TO_TICKS(5)
26 #define AO_RDF_LENGTH_MS        500
27
28 #if defined(TELEMETRUM_V_0_1) || defined(TELEMETRUM_V_0_2) || defined(TELEMETRUM_V_1_0) || defined(TELEMETRUM_V_1_1)
29 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMETRUM
30 #endif
31
32 #if defined(TELEMINI_V_0_1)
33 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELEMINI
34 #endif
35
36 #if defined(TELENANO_V_0_1)
37 #define AO_TELEMETRY_SENSOR     AO_TELEMETRY_SENSOR_TELENANO
38 #endif
39
40 void
41 ao_telemetry(void)
42 {
43         uint16_t        time;
44         int16_t         delay;
45         static __xdata union ao_telemetry_all   telemetry;
46         uint8_t         sample;
47
48         ao_config_get();
49         while (!ao_flight_number)
50                 ao_sleep(&ao_flight_number);
51
52         telemetry.generic.serial = ao_serial_number;
53         for (;;) {
54                 while (ao_telemetry_interval == 0)
55                         ao_sleep(&ao_telemetry_interval);
56                 time = ao_rdf_time = ao_time();
57                 while (ao_telemetry_interval) {
58
59                         /* Send sensor packet */
60                         sample = ao_sample_adc;
61                         
62                         telemetry.generic.tick = ao_adc_ring[sample].tick;
63                         telemetry.generic.type = AO_TELEMETRY_SENSOR;
64
65                         telemetry.sensor.state = ao_flight_state;
66 #if HAS_ACCEL
67                         telemetry.sensor.accel = ao_adc_ring[sample].accel;
68 #else
69                         telemetry.sensor.accel = 0;
70 #endif
71                         telemetry.sensor.pres = ao_adc_ring[sample].pres;
72                         telemetry.sensor.temp = ao_adc_ring[sample].temp;
73                         telemetry.sensor.v_batt = ao_adc_ring[sample].v_batt;
74 #if HAS_IGNITE
75                         telemetry.sensor.sense_d = ao_adc_ring[sample].sense_d;
76                         telemetry.sensor.sense_m = ao_adc_ring[sample].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                         telemetry.sensor.ground_accel = ao_ground_accel;
88                         telemetry.sensor.accel_plus_g = ao_config.accel_plus_g;
89                         telemetry.sensor.accel_minus_g = ao_config.accel_minus_g;
90
91                         ao_radio_send(&telemetry, sizeof (telemetry));
92
93                         telemetry.generic.type = AO_TELEMETRY_CONFIGURATION;
94                         telemetry.configuration.device = AO_idProduct_NUMBER;
95                         telemetry.configuration.flight = ao_flight_number;
96                         telemetry.configuration.config_major = AO_CONFIG_MAJOR;
97                         telemetry.configuration.config_minor = AO_CONFIG_MINOR;
98                         telemetry.configuration.main_deploy = ao_config.main_deploy;
99                         telemetry.configuration.flight_log_max = ao_config.flight_log_max;
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
107                         if (ao_rdf &&
108                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
109                         {
110                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
111                                 ao_radio_rdf(AO_RDF_LENGTH_MS);
112                         }
113                         time += ao_telemetry_interval;
114                         delay = time - ao_time();
115                         if (delay > 0)
116                                 ao_delay(delay);
117                         else
118                                 time = ao_time();
119                 }
120         }
121 }
122
123 void
124 ao_telemetry_set_interval(uint16_t interval)
125 {
126         ao_telemetry_interval = interval;
127         ao_wakeup(&ao_telemetry_interval);
128 }
129
130 void
131 ao_rdf_set(uint8_t rdf)
132 {
133         ao_rdf = rdf;
134         if (rdf == 0)
135                 ao_radio_rdf_abort();
136         else
137                 ao_rdf_time = ao_time();
138 }
139
140 __xdata struct ao_task  ao_telemetry_task;
141
142 void
143 ao_telemetry_init()
144 {
145         ao_add_task(&ao_telemetry_task, ao_telemetry, "telemetry");
146 }