b207c56204d866fae30eb33e53b5c263223f46ab
[fw/altos] / src / product / ao_tracker.c
1 /*
2  * Copyright © 2014 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_flight.h>
20 #include <ao_distance.h>
21 #include <ao_exti.h>
22
23 enum ao_flight_state    ao_flight_state;
24
25 /* Speeds for the various modes, 2m/s seems reasonable for 'not moving' */
26 #define AO_TRACKER_NOT_MOVING   200
27
28 static uint8_t  ao_tracker_force_telem;
29 static uint8_t  ao_tracker_force_launch;
30
31 #if HAS_USB_CONNECT
32 static inline uint8_t
33 ao_usb_connected(void)
34 {
35         return ao_gpio_get(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, AO_USB_CONNECT) != 0;
36 }
37 #else
38 #define ao_usb_connected()      1
39 #endif
40
41 #define STARTUP_AVERAGE 5
42
43 static void
44 ao_tracker_start_flight(void)
45 {
46         struct ao_log_mega log;
47         ao_log_start();
48         log.type = AO_LOG_FLIGHT;
49         log.tick = ao_time();
50 #if HAS_ACCEL
51         log.u.flight.ground_accel = ao_ground_accel;
52 #endif
53 #if HAS_GYRO
54         log.u.flight.ground_accel_along = ao_ground_accel_along;
55         log.u.flight.ground_accel_across = ao_ground_accel_across;
56         log.u.flight.ground_accel_through = ao_ground_accel_through;
57         log.u.flight.ground_roll = ao_ground_roll;
58         log.u.flight.ground_pitch = ao_ground_pitch;
59         log.u.flight.ground_yaw = ao_ground_yaw;
60 #endif
61 #if HAS_FLIGHT
62         log.u.flight.ground_pres = ao_ground_pres;
63 #endif
64         log.u.flight.flight = ao_flight_number;
65         ao_log_mega(&log);
66 }
67
68 static void
69 ao_tracker(void)
70 {
71         uint16_t        telem_rate = AO_SEC_TO_TICKS(1), new_telem_rate;
72         uint8_t         gps_rate = 1, new_gps_rate;
73         uint8_t         telem_enabled = 0, new_telem_enabled;
74         int32_t         start_latitude = 0, start_longitude = 0;
75         int16_t         start_altitude = 0;
76         uint32_t        ground_distance;
77         int16_t         height;
78         uint16_t        speed;
79         int64_t         lat_sum = 0, lon_sum = 0;
80         int32_t         alt_sum = 0;
81         int             nsamples = 0;
82
83 #if HAS_ADC
84         ao_timer_set_adc_interval(100);
85 #endif
86
87 #if !HAS_USB_CONNECT
88         ao_tracker_force_telem = 1;
89 #endif
90
91         ao_log_scan();
92
93         ao_rdf_set(1);
94
95         ao_telemetry_set_interval(0);
96
97         ao_flight_state = ao_flight_startup;
98         for (;;) {
99                 ao_sleep(&ao_gps_new);
100
101                 new_gps_rate = gps_rate;
102                 new_telem_rate = telem_rate;
103
104                 new_telem_enabled = ao_tracker_force_telem || !ao_usb_connected();
105
106                 ao_mutex_get(&ao_gps_mutex);
107
108                 /* Don't change anything if GPS isn't locked */
109                 if ((ao_gps_data.flags & (AO_GPS_VALID|AO_GPS_COURSE_VALID)) ==
110                     (AO_GPS_VALID|AO_GPS_COURSE_VALID))
111                 {
112                         switch (ao_flight_state) {
113                         case ao_flight_startup:
114                                 /* startup to pad when GPS locks */
115
116                                 lat_sum += ao_gps_data.latitude;
117                                 lon_sum += ao_gps_data.longitude;
118                                 alt_sum += ao_gps_data.altitude;
119
120                                 if (++nsamples >= STARTUP_AVERAGE) {
121                                         ao_flight_state = ao_flight_pad;
122                                         ao_wakeup(&ao_flight_state);
123                                         start_latitude = lat_sum / nsamples;
124                                         start_longitude = lon_sum / nsamples;
125                                         start_altitude = alt_sum / nsamples;
126                                 }
127                                 break;
128                         case ao_flight_pad:
129                                 ground_distance = ao_distance(ao_gps_data.latitude,
130                                                               ao_gps_data.longitude,
131                                                               start_latitude,
132                                                               start_longitude);
133                                 height = ao_gps_data.altitude - start_altitude;
134                                 if (height < 0)
135                                         height = -height;
136
137                                 if (ground_distance >= ao_config.tracker_start_horiz ||
138                                     height >= ao_config.tracker_start_vert ||
139                                     ao_tracker_force_launch)
140                                 {
141                                         ao_flight_state = ao_flight_drogue;
142                                         ao_wakeup(&ao_flight_state);
143                                         ao_log_start();
144                                         ao_tracker_start_flight();
145                                 }
146                                 break;
147                         case ao_flight_drogue:
148                                 /* Modulate data rates based on speed (in cm/s) */
149                                 if (ao_gps_data.climb_rate < 0)
150                                         speed = -ao_gps_data.climb_rate;
151                                 else
152                                         speed = ao_gps_data.climb_rate;
153                                 speed += ao_gps_data.ground_speed;
154
155                                 if (speed < AO_TRACKER_NOT_MOVING) {
156                                         new_telem_rate = AO_SEC_TO_TICKS(10);
157                                         new_gps_rate = 10;
158                                 } else {
159                                         new_telem_rate = AO_SEC_TO_TICKS(1);
160                                         new_gps_rate = 1;
161                                 }
162                                 break;
163                         default:
164                                 break;
165                         }
166                 }
167                 ao_mutex_put(&ao_gps_mutex);
168
169                 if (new_telem_rate != telem_rate || new_telem_enabled != telem_enabled) {
170                         if (new_telem_enabled)
171                                 ao_telemetry_set_interval(new_telem_rate);
172                         else
173                                 ao_telemetry_set_interval(0);
174                         telem_rate = new_telem_rate;
175                         telem_enabled = new_telem_enabled;
176                 }
177
178                 if (new_gps_rate != gps_rate) {
179                         ao_gps_set_rate(new_gps_rate);
180                         gps_rate = new_gps_rate;
181                 }
182         }
183 }
184
185 static struct ao_task ao_tracker_task;
186
187 static void
188 ao_tracker_set_telem(void)
189 {
190         ao_cmd_hex();
191         if (ao_cmd_status == ao_cmd_success) {
192                 ao_tracker_force_telem = (ao_cmd_lex_i & 1) != 0;
193                 ao_tracker_force_launch = (ao_cmd_lex_i & 2) != 0;
194         }
195         printf ("flight %d force telem %d force launch %d\n",
196                 ao_flight_number, ao_tracker_force_telem, ao_tracker_force_launch);
197 }
198
199 static const struct ao_cmds ao_tracker_cmds[] = {
200         { ao_tracker_set_telem, "t <d>\0Set telem on USB" },
201         { 0, NULL },
202 };
203
204 void
205 ao_tracker_init(void)
206 {
207 #if HAS_USB_CONNECT
208         ao_enable_input(AO_USB_CONNECT_PORT, AO_USB_CONNECT_PIN, 0);
209 #endif
210         ao_cmd_register(&ao_tracker_cmds[0]);
211         ao_add_task(&ao_tracker_task, ao_tracker, "tracker");
212 }