altos: Record all failed sensors and report status at power up
[fw/altos] / src / kernel / ao_report.c
1 /*
2  * Copyright © 2009 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include <ao_flight.h>
21 #include <ao_sample.h>
22
23 #define BIT(i,x)           ((x) ? (1 << (i)) : 0)
24 #define MORSE1(a)          (1 | BIT(3,a))
25 #define MORSE2(a,b)        (2 | BIT(3,a) | BIT(4,b))
26 #define MORSE3(a,b,c)      (3 | BIT(3,a) | BIT(4,b) | BIT(5,c))
27 #define MORSE4(a,b,c,d)    (4 | BIT(3,a) | BIT(4,b) | BIT(5,c) | BIT(6,d))
28 #define MORSE5(a,b,c,d,e)  (5 | BIT(3,a) | BIT(4,b) | BIT(5,c) | BIT(6,d) | BIT(7,e))
29
30 static const uint8_t flight_reports[] = {
31         MORSE3(0,0,0),          /* startup, 'S' */
32         MORSE2(0,0),            /* idle 'I' */
33         MORSE4(0,1,1,0),        /* pad 'P' */
34         MORSE4(1,0,0,0),        /* boost 'B' */
35         MORSE4(0,0,1,0),        /* fast 'F' */
36         MORSE4(1,0,1,0),        /* coast 'C' */
37         MORSE3(1,0,0),          /* drogue 'D' */
38         MORSE2(1,1),            /* main 'M' */
39         MORSE4(0,1,0,0),        /* landed 'L' */
40         MORSE4(1,0,0,1),        /* invalid 'X' */
41 };
42
43 #if HAS_BEEP
44 #define low(time)       ao_beep_for(AO_BEEP_LOW, time)
45 #define mid(time)       ao_beep_for(AO_BEEP_MID, time)
46 #define high(time)      ao_beep_for(AO_BEEP_HIGH, time)
47 #else
48 #ifndef AO_LED_LOW
49 #define AO_LED_LOW      AO_LED_GREEN
50 #endif
51 #ifndef AO_LED_MID
52 #define AO_LED_MID      AO_LED_RED
53 #endif
54
55 #define low(time)       ao_led_for(AO_LED_LOW, time)
56 #define mid(time)       ao_led_for(AO_LED_MID, time)
57 #define high(time)      ao_led_for(AO_LED_MID|AO_LED_LOW, time)
58 #endif
59 #define pause(time)     ao_delay(time)
60
61 static enum ao_flight_state ao_report_state;
62
63 /*
64  * Farnsworth spacing
65  *
66  * From: http://www.arrl.org/files/file/Technology/x9004008.pdf
67  *
68  *      c:      character rate in wpm
69  *      s:      overall rate in wpm
70  *      u:      unit rate (dit speed)
71  *
72  *      dit:                    u
73  *      dah:                    3u
74  *      intra-character-time:   u
75  *
76  *      u = 1.2/c
77  *
78  * Because our clock runs at 10ms, we'll round up to 70ms for u, which
79  * is about 17wpm
80  *
81  *      Farnsworth adds space between characters and
82  *      words:
83  *           60 c - 37.2 s
84  *      Ta = -------------
85  *                sc
86  *
87  *           3 Ta
88  *      Tc = ----
89  *            19
90  *
91  *           7 Ta
92  *      Tw = ----
93  *            19
94  *
95  *      Ta = total delay to add to the characters (31 units)
96  *           of a standard 50-unit "word", in seconds
97  *
98  *      Tc = period between characters, in seconds
99  *
100  *      Tw = period between words, in seconds
101  *
102  * We'll use Farnsworth spacing with c=18 and s=12:
103  *
104  *      u = 1.2/18 = 0.0667
105  *
106  *      Ta = (60 * 17 - 37.2 * 12) / (17 * 12) = 2.812
107  *
108  *      Tc = 3 * Ta / 19 = .444
109  *
110  *      Tw = 1.036
111  *
112  * Note that the values below are all reduced by 10ms; that's because
113  * the timer always adds a tick to make sure the task actually sleeps
114  * at least as long as the argument.
115  */
116
117 static void
118 ao_report_beep(void) 
119 {
120         uint8_t r = flight_reports[ao_flight_state];
121         uint8_t l = r & 7;
122
123         if (!r)
124                 return;
125         while (l--) {
126                 if (r & 8)
127                         mid(AO_MS_TO_TICKS(200));
128                 else
129                         mid(AO_MS_TO_TICKS(60));
130                 pause(AO_MS_TO_TICKS(60));
131                 r >>= 1;
132         }
133         pause(AO_MS_TO_TICKS(360));
134 }
135
136 static void
137 ao_report_digit(uint8_t digit) 
138 {
139         if (!digit) {
140                 mid(AO_MS_TO_TICKS(500));
141                 pause(AO_MS_TO_TICKS(200));
142         } else {
143                 while (digit--) {
144                         mid(AO_MS_TO_TICKS(200));
145                         pause(AO_MS_TO_TICKS(200));
146                 }
147         }
148         pause(AO_MS_TO_TICKS(300));
149 }
150
151 static void
152 ao_report_number(int16_t n)
153 {
154         uint8_t digits[10];
155         uint8_t ndigits, i;
156
157         if (n < 0)
158                 n = 0;
159         ndigits = 0;
160         do {
161                 digits[ndigits++] = n % 10;
162                 n /= 10;
163         } while (n);
164
165         i = ndigits;
166         do
167                 ao_report_digit(digits[--i]);
168         while (i != 0);
169 }
170
171 static void
172 ao_report_altitude(void)
173 {
174         ao_report_number(ao_max_height);
175 }
176
177 #if HAS_BATTERY_REPORT
178 static void
179 ao_report_battery(void)
180 {
181         struct ao_data packet;
182         for (;;) {
183                 ao_data_get(&packet);
184                 if (packet.adc.v_batt != 0)
185                         break;
186                 ao_sleep(&ao_sample_data);
187         }
188         ao_report_number(ao_battery_decivolt(packet.adc.v_batt));
189 }
190 #endif
191
192 #if HAS_IGNITE_REPORT
193 static uint8_t
194 ao_report_igniter_ready(enum ao_igniter igniter)
195 {
196         return ao_igniter_status(igniter) == ao_igniter_ready ? 1 : 0;
197 }
198
199 uint8_t
200 ao_report_igniter(void)
201 {
202         return (ao_report_igniter_ready(ao_igniter_drogue) |
203                      (ao_report_igniter_ready(ao_igniter_main) << 1));
204 }
205
206 static void
207 ao_report_continuity(void) 
208 {
209         uint8_t c;
210
211 #if !HAS_IGNITE
212         if (!ao_igniter_present)
213                 return;
214 #endif
215         c = ao_report_igniter();
216         if (c) {
217                 while (c--) {
218                         high(AO_MS_TO_TICKS(25));
219                         pause(AO_MS_TO_TICKS(100));
220                 }
221         } else {
222                 c = 5;
223                 while (c--) {
224                         high(AO_MS_TO_TICKS(20));
225                         low(AO_MS_TO_TICKS(20));
226                 }
227         }
228 #if AO_PYRO_NUM
229         pause(AO_MS_TO_TICKS(250));
230         for(c = 0; c < AO_PYRO_NUM; c++) {
231                 enum ao_igniter_status  status = ao_pyro_status(c);
232                 if (status == ao_igniter_ready)
233                         mid(AO_MS_TO_TICKS(25));
234                 else
235                         low(AO_MS_TO_TICKS(25));
236                 pause(AO_MS_TO_TICKS(200));
237         }
238 #endif
239 #if HAS_LOG
240         if (ao_log_full()) {
241                 pause(AO_MS_TO_TICKS(100));
242                 c = 2;
243                 while (c--) {
244                         low(AO_MS_TO_TICKS(100));
245                         mid(AO_MS_TO_TICKS(100));
246                         high(AO_MS_TO_TICKS(100));
247                         mid(AO_MS_TO_TICKS(100));
248                 }
249         }
250 #endif
251 }
252 #endif
253
254 static void
255 ao_report(void)
256 {
257         for(;;) {
258                 ao_report_state = ao_flight_state;
259 #if HAS_BATTERY_REPORT
260                 if (ao_report_state == ao_flight_startup)
261                         ao_report_battery();
262                 else
263 #endif
264                         ao_report_beep();
265 #if HAS_SENSOR_ERRORS
266                 if (ao_report_state == ao_flight_invalid && ao_sensor_errors)
267                         ao_report_number(ao_sensor_errors);
268 #endif
269
270                 if (ao_report_state == ao_flight_landed) {
271                         ao_report_altitude();
272 #if HAS_FLIGHT
273                         ao_delay(AO_SEC_TO_TICKS(5));
274                         continue;
275 #endif
276                 }
277 #if HAS_IGNITE_REPORT
278                 if (ao_report_state == ao_flight_idle)
279                         ao_report_continuity();
280                 while (ao_flight_state == ao_flight_pad) {
281                         uint8_t c;
282                         ao_report_continuity();
283                         c = 50;
284                         while (c-- && ao_flight_state == ao_flight_pad)
285                                 pause(AO_MS_TO_TICKS(100));
286                 }
287 #endif
288                 while (ao_report_state == ao_flight_state)
289                         ao_sleep(&ao_flight_state);
290         }
291 }
292
293 static struct ao_task ao_report_task;
294
295 void
296 ao_report_init(void)
297 {
298         ao_add_task(&ao_report_task, ao_report, "report");
299 }