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