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