altos: Send serial/flight to companion board
[fw/altos] / src / ao_monitor.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; 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_telem.h"
20
21 #if !HAS_MONITOR
22 #error Must define HAS_MONITOR to 1
23 #endif
24
25 __xdata uint8_t ao_monitoring;
26 __pdata uint8_t ao_monitor_led;
27
28 #define AO_MONITOR_RING 8
29
30 __xdata union ao_monitor {
31                 struct ao_telemetry_raw_recv    raw;
32                 struct ao_telemetry_orig_recv   orig;
33                 struct ao_telemetry_tiny_recv   tiny;
34 } ao_monitor_ring[AO_MONITOR_RING];
35
36 #define ao_monitor_ring_next(n) (((n) + 1) & (AO_MONITOR_RING - 1))
37
38 __data uint8_t  ao_monitor_head;
39
40 void
41 ao_monitor_get(void)
42 {
43         uint8_t size;
44
45         for (;;) {
46                 switch (ao_monitoring) {
47                 case 0:
48                         ao_sleep(&ao_monitoring);
49                         continue;
50                 case AO_MONITORING_ORIG:
51                         size = sizeof (struct ao_telemetry_orig_recv);
52                         break;
53                 case AO_MONITORING_TINY:
54                         size = sizeof (struct ao_telemetry_tiny_recv);
55                         break;
56                 default:
57                         if (ao_monitoring > AO_MAX_TELEMETRY)
58                                 ao_monitoring = AO_MAX_TELEMETRY;
59                         size = ao_monitoring;
60                         break;
61                 }
62                 if (!ao_radio_recv(&ao_monitor_ring[ao_monitor_head], size + 2))
63                         continue;
64                 ao_monitor_head = ao_monitor_ring_next(ao_monitor_head);
65                 ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
66                 ao_led_toggle(ao_monitor_led);
67         }
68 }
69
70 void
71 ao_monitor_put(void)
72 {
73         __xdata char callsign[AO_MAX_CALLSIGN+1];
74
75         uint8_t ao_monitor_tail;
76         uint8_t state;
77         uint8_t sum, byte;
78         int16_t rssi;
79         __xdata union ao_monitor        *m;
80
81 #define recv_raw        ((m->raw))
82 #define recv_orig       ((m->orig))
83 #define recv_tiny       ((m->tiny))
84
85         ao_monitor_tail = ao_monitor_head;
86         for (;;) {
87                 while (ao_monitor_tail == ao_monitor_head)
88                         ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
89                 m = &ao_monitor_ring[ao_monitor_tail];
90                 ao_monitor_tail = ao_monitor_ring_next(ao_monitor_tail);
91                 switch (ao_monitoring) {
92                 case AO_MONITORING_ORIG:
93                         state = recv_orig.telemetry_orig.flight_state;
94
95                         /* Typical RSSI offset for 38.4kBaud at 433 MHz is 74 */
96                         rssi = (int16_t) (recv_orig.rssi >> 1) - 74;
97                         memcpy(callsign, recv_orig.telemetry_orig.callsign, AO_MAX_CALLSIGN);
98                         if (state > ao_flight_invalid)
99                                 state = ao_flight_invalid;
100                         if (recv_orig.status & PKT_APPEND_STATUS_1_CRC_OK) {
101
102                                 /* General header fields */
103                                 printf(AO_TELEM_VERSION " %d "
104                                        AO_TELEM_CALL " %s "
105                                        AO_TELEM_SERIAL " %d "
106                                        AO_TELEM_FLIGHT " %d "
107                                        AO_TELEM_RSSI " %d "
108                                        AO_TELEM_STATE " %s "
109                                        AO_TELEM_TICK " %d ",
110                                        AO_TELEMETRY_VERSION,
111                                        callsign,
112                                        recv_orig.telemetry_orig.serial,
113                                        recv_orig.telemetry_orig.flight,
114                                        rssi,
115                                        ao_state_names[state],
116                                        recv_orig.telemetry_orig.adc.tick);
117
118                                 /* Raw sensor values */
119                                 printf(AO_TELEM_RAW_ACCEL " %d "
120                                        AO_TELEM_RAW_BARO " %d "
121                                        AO_TELEM_RAW_THERMO " %d "
122                                        AO_TELEM_RAW_BATT " %d "
123                                        AO_TELEM_RAW_DROGUE " %d "
124                                        AO_TELEM_RAW_MAIN " %d ",
125                                        recv_orig.telemetry_orig.adc.accel,
126                                        recv_orig.telemetry_orig.adc.pres,
127                                        recv_orig.telemetry_orig.adc.temp,
128                                        recv_orig.telemetry_orig.adc.v_batt,
129                                        recv_orig.telemetry_orig.adc.sense_d,
130                                        recv_orig.telemetry_orig.adc.sense_m);
131
132                                 /* Sensor calibration values */
133                                 printf(AO_TELEM_CAL_ACCEL_GROUND " %d "
134                                        AO_TELEM_CAL_BARO_GROUND " %d "
135                                        AO_TELEM_CAL_ACCEL_PLUS " %d "
136                                        AO_TELEM_CAL_ACCEL_MINUS " %d ",
137                                        recv_orig.telemetry_orig.ground_accel,
138                                        recv_orig.telemetry_orig.ground_pres,
139                                        recv_orig.telemetry_orig.accel_plus_g,
140                                        recv_orig.telemetry_orig.accel_minus_g);
141
142                                 if (recv_orig.telemetry_orig.u.k.unused == 0x8000) {
143                                         /* Kalman state values */
144                                         printf(AO_TELEM_KALMAN_HEIGHT " %d "
145                                                AO_TELEM_KALMAN_SPEED " %d "
146                                                AO_TELEM_KALMAN_ACCEL " %d ",
147                                                recv_orig.telemetry_orig.height,
148                                                recv_orig.telemetry_orig.u.k.speed,
149                                                recv_orig.telemetry_orig.accel);
150                                 } else {
151                                         /* Ad-hoc flight values */
152                                         printf(AO_TELEM_ADHOC_ACCEL " %d "
153                                                AO_TELEM_ADHOC_SPEED " %ld "
154                                                AO_TELEM_ADHOC_BARO " %d ",
155                                                recv_orig.telemetry_orig.accel,
156                                                recv_orig.telemetry_orig.u.flight_vel,
157                                                recv_orig.telemetry_orig.height);
158                                 }
159                                 ao_gps_print(&recv_orig.telemetry_orig.gps);
160                                 ao_gps_tracking_print(&recv_orig.telemetry_orig.gps_tracking);
161                                 putchar('\n');
162                                 ao_rssi_set(rssi);
163                         } else {
164                                 printf("CRC INVALID RSSI %3d\n", rssi);
165                         }
166                         break;
167                 case AO_MONITORING_TINY:
168                         state = recv_tiny.telemetry_tiny.flight_state;
169
170                         /* Typical RSSI offset for 38.4kBaud at 433 MHz is 74 */
171                         rssi = (int16_t) (recv_tiny.rssi >> 1) - 74;
172                         memcpy(callsign, recv_tiny.telemetry_tiny.callsign, AO_MAX_CALLSIGN);
173                         if (state > ao_flight_invalid)
174                                 state = ao_flight_invalid;
175                         if (recv_tiny.status & PKT_APPEND_STATUS_1_CRC_OK) {
176                                 /* General header fields */
177                                 printf(AO_TELEM_VERSION " %d "
178                                        AO_TELEM_CALL " %s "
179                                        AO_TELEM_SERIAL " %d "
180                                        AO_TELEM_FLIGHT " %d "
181                                        AO_TELEM_RSSI " %d "
182                                        AO_TELEM_STATE " %s "
183                                        AO_TELEM_TICK " %d ",
184                                        AO_TELEMETRY_VERSION,
185                                        callsign,
186                                        recv_tiny.telemetry_tiny.serial,
187                                        recv_tiny.telemetry_tiny.flight,
188                                        rssi,
189                                        ao_state_names[state],
190                                        recv_tiny.telemetry_tiny.adc.tick);
191
192                                 /* Raw sensor values */
193                                 printf(AO_TELEM_RAW_BARO " %d "
194                                        AO_TELEM_RAW_THERMO " %d "
195                                        AO_TELEM_RAW_BATT " %d "
196                                        AO_TELEM_RAW_DROGUE " %d "
197                                        AO_TELEM_RAW_MAIN " %d ",
198                                        recv_tiny.telemetry_tiny.adc.pres,
199                                        recv_tiny.telemetry_tiny.adc.temp,
200                                        recv_tiny.telemetry_tiny.adc.v_batt,
201                                        recv_tiny.telemetry_tiny.adc.sense_d,
202                                        recv_tiny.telemetry_tiny.adc.sense_m);
203
204                                 /* Sensor calibration values */
205                                 printf(AO_TELEM_CAL_BARO_GROUND " %d ",
206                                        recv_tiny.telemetry_tiny.ground_pres);
207
208 #if 1
209                                 /* Kalman state values */
210                                 printf(AO_TELEM_KALMAN_HEIGHT " %d "
211                                        AO_TELEM_KALMAN_SPEED " %d "
212                                        AO_TELEM_KALMAN_ACCEL " %d\n",
213                                        recv_tiny.telemetry_tiny.height,
214                                        recv_tiny.telemetry_tiny.speed,
215                                        recv_tiny.telemetry_tiny.accel);
216 #else
217                                 /* Ad-hoc flight values */
218                                 printf(AO_TELEM_ADHOC_ACCEL " %d "
219                                        AO_TELEM_ADHOC_SPEED " %ld "
220                                        AO_TELEM_ADHOC_BARO " %d\n",
221                                        recv_tiny.telemetry_tiny.flight_accel,
222                                        recv_tiny.telemetry_tiny.flight_vel,
223                                        recv_tiny.telemetry_tiny.flight_pres);
224 #endif
225                                 ao_rssi_set(rssi);
226                         } else {
227                                 printf("CRC INVALID RSSI %3d\n", rssi);
228                         }
229                         break;
230                 default:
231                         printf ("TELEM %02x", ao_monitoring + 2);
232                         sum = 0x5a;
233                         for (state = 0; state < ao_monitoring + 2; state++) {
234                                 byte = recv_raw.packet[state];
235                                 sum += byte;
236                                 printf("%02x", byte);
237                         }
238                         printf("%02x\n", sum);
239                         break;
240                 }
241                 ao_usb_flush();
242         }
243 }
244
245 __xdata struct ao_task ao_monitor_get_task;
246 __xdata struct ao_task ao_monitor_put_task;
247
248 void
249 ao_set_monitor(uint8_t monitoring)
250 {
251         if (ao_monitoring)
252                 ao_radio_recv_abort();
253         ao_monitoring = monitoring;
254         ao_wakeup(&ao_monitoring);
255 }
256
257 static void
258 set_monitor(void)
259 {
260         ao_cmd_hex();
261         ao_set_monitor(ao_cmd_lex_i);
262 }
263
264 __code struct ao_cmds ao_monitor_cmds[] = {
265         { set_monitor,  "m <0 off, 1 full, 2 tiny>\0Enable/disable radio monitoring" },
266         { 0,    NULL },
267 };
268
269 void
270 ao_monitor_init(uint8_t monitor_led, uint8_t monitoring) __reentrant
271 {
272         ao_monitor_led = monitor_led;
273         ao_monitoring = monitoring;
274         ao_cmd_register(&ao_monitor_cmds[0]);
275         ao_add_task(&ao_monitor_get_task, ao_monitor_get, "monitor_get");
276         ao_add_task(&ao_monitor_put_task, ao_monitor_put, "monitor_put");
277 }