1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* Copyright © 2009 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "ao.h"
#include "ao_telem.h"
#include "ao_flight.h"
#if !HAS_MONITOR
#error Must define HAS_MONITOR to 1
#endif
#ifndef LEGACY_MONITOR
#error Must define LEGACY_MONITOR
#endif
#ifndef HAS_MONITOR_PUT
#define HAS_MONITOR_PUT 1
#endif
#ifndef AO_MONITOR_LED
#error Must define AO_MONITOR_LED
#endif
__xdata uint8_t ao_monitoring_mutex;
__data uint8_t ao_monitoring;
static __data uint8_t ao_monitor_disabled;
static __data uint8_t ao_internal_monitoring;
static __data uint8_t ao_external_monitoring;
__xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
__data uint8_t ao_monitor_head;
static void
_ao_monitor_adjust(void)
{
if (ao_monitoring)
ao_radio_recv_abort();
if (ao_monitor_disabled)
ao_monitoring = 0;
else {
if (ao_external_monitoring)
ao_monitoring = ao_external_monitoring;
else
ao_monitoring = ao_internal_monitoring;
}
ao_wakeup(DATA_TO_XDATA(&ao_monitoring));
}
void
ao_monitor_get(void)
{
uint8_t size;
for (;;) {
switch (ao_monitoring) {
case 0:
ao_sleep(DATA_TO_XDATA(&ao_monitoring));
continue;
#if LEGACY_MONITOR
case AO_MONITORING_ORIG:
size = sizeof (struct ao_telemetry_orig_recv);
break;
#endif
default:
if (ao_monitoring > AO_MAX_TELEMETRY)
ao_monitoring = AO_MAX_TELEMETRY;
size = ao_monitoring;
break;
}
if (!ao_radio_recv(&ao_monitor_ring[ao_monitor_head], size + 2, 0))
continue;
ao_monitor_head = ao_monitor_ring_next(ao_monitor_head);
ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
}
}
#if AO_MONITOR_LED
__xdata struct ao_task ao_monitor_blink_task;
void
ao_monitor_blink(void)
{
#ifdef AO_MONITOR_BAD
uint8_t *recv;
#endif
for (;;) {
ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
#ifdef AO_MONITOR_BAD
recv = (uint8_t *) &ao_monitor_ring[ao_monitor_ring_prev(ao_monitor_head)];
if (ao_monitoring && !(recv[ao_monitoring + 1] & AO_RADIO_STATUS_CRC_OK))
ao_led_for(AO_MONITOR_BAD, AO_MS_TO_TICKS(100));
else
#endif
ao_led_for(AO_MONITOR_LED, AO_MS_TO_TICKS(100));
}
}
#endif
#if HAS_MONITOR_PUT
static const char xdigit[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
#define hex(c) do { putchar(xdigit[(c) >> 4]); putchar(xdigit[(c)&0xf]); } while (0)
void
ao_monitor_put(void)
{
#if LEGACY_MONITOR
__xdata char callsign[AO_MAX_CALLSIGN+1];
#endif
#if LEGACY_MONITOR || HAS_RSSI
int16_t rssi;
#endif
uint8_t ao_monitor_tail;
uint8_t state;
uint8_t sum, byte;
__xdata union ao_monitor *m;
#define recv_raw ((m->raw))
#define recv_orig ((m->orig))
#define recv_tiny ((m->tiny))
ao_monitor_tail = ao_monitor_head;
for (;;) {
while (!ao_external_monitoring)
ao_sleep(DATA_TO_XDATA(&ao_external_monitoring));
while (ao_monitor_tail == ao_monitor_head && ao_external_monitoring)
ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
if (!ao_external_monitoring)
continue;
m = &ao_monitor_ring[ao_monitor_tail];
ao_monitor_tail = ao_monitor_ring_next(ao_monitor_tail);
switch (ao_monitoring) {
case 0:
break;
#if LEGACY_MONITOR
case AO_MONITORING_ORIG:
state = recv_orig.telemetry_orig.flight_state;
rssi = (int16_t) AO_RSSI_FROM_RADIO(recv_orig.rssi);
ao_xmemcpy(callsign, recv_orig.telemetry_orig.callsign, AO_MAX_CALLSIGN);
if (state > ao_flight_invalid)
state = ao_flight_invalid;
if (recv_orig.status & PKT_APPEND_STATUS_1_CRC_OK) {
/* General header fields */
printf(AO_TELEM_VERSION " %d "
AO_TELEM_CALL " %s "
AO_TELEM_SERIAL " %d "
AO_TELEM_FLIGHT " %d "
AO_TELEM_RSSI " %d "
AO_TELEM_STATE " %s "
AO_TELEM_TICK " %d ",
AO_TELEMETRY_VERSION,
callsign,
recv_orig.telemetry_orig.serial,
recv_orig.telemetry_orig.flight,
rssi,
ao_state_names[state],
recv_orig.telemetry_orig.adc.tick);
/* Raw sensor values */
printf(AO_TELEM_RAW_ACCEL " %d "
AO_TELEM_RAW_BARO " %d "
AO_TELEM_RAW_THERMO " %d "
AO_TELEM_RAW_BATT " %d "
AO_TELEM_RAW_DROGUE " %d "
AO_TELEM_RAW_MAIN " %d ",
recv_orig.telemetry_orig.adc.accel,
recv_orig.telemetry_orig.adc.pres,
recv_orig.telemetry_orig.adc.temp,
recv_orig.telemetry_orig.adc.v_batt,
recv_orig.telemetry_orig.adc.sense_d,
recv_orig.telemetry_orig.adc.sense_m);
/* Sensor calibration values */
printf(AO_TELEM_CAL_ACCEL_GROUND " %d "
AO_TELEM_CAL_BARO_GROUND " %d "
AO_TELEM_CAL_ACCEL_PLUS " %d "
AO_TELEM_CAL_ACCEL_MINUS " %d ",
recv_orig.telemetry_orig.ground_accel,
recv_orig.telemetry_orig.ground_pres,
recv_orig.telemetry_orig.accel_plus_g,
recv_orig.telemetry_orig.accel_minus_g);
if (recv_orig.telemetry_orig.u.k.unused == 0x8000) {
/* Kalman state values */
printf(AO_TELEM_KALMAN_HEIGHT " %d "
AO_TELEM_KALMAN_SPEED " %d "
AO_TELEM_KALMAN_ACCEL " %d ",
recv_orig.telemetry_orig.height,
recv_orig.telemetry_orig.u.k.speed,
recv_orig.telemetry_orig.accel);
} else {
/* Ad-hoc flight values */
printf(AO_TELEM_ADHOC_ACCEL " %d "
AO_TELEM_ADHOC_SPEED " %ld "
AO_TELEM_ADHOC_BARO " %d ",
recv_orig.telemetry_orig.accel,
recv_orig.telemetry_orig.u.flight_vel,
recv_orig.telemetry_orig.height);
}
ao_gps_print(&recv_orig.telemetry_orig.gps);
ao_gps_tracking_print(&recv_orig.telemetry_orig.gps_tracking);
putchar('\n');
#if HAS_RSSI
ao_rssi_set(rssi);
#endif
} else {
printf("CRC INVALID RSSI %3d\n", rssi);
}
break;
#endif /* LEGACY_MONITOR */
default:
#if AO_PROFILE
{
extern uint32_t ao_rx_start_tick, ao_rx_packet_tick, ao_rx_done_tick, ao_rx_last_done_tick;
extern uint32_t ao_fec_decode_start, ao_fec_decode_end;
printf ("between packet: %d\n", ao_rx_start_tick - ao_rx_last_done_tick);
printf ("receive start delay: %d\n", ao_rx_packet_tick - ao_rx_start_tick);
printf ("decode time: %d\n", ao_fec_decode_end - ao_fec_decode_start);
printf ("rx cleanup: %d\n", ao_rx_done_tick - ao_fec_decode_end);
}
#endif
ao_mutex_get(&ao_monitoring_mutex);
printf("TELEM ");
hex((uint8_t) (ao_monitoring + 2));
sum = 0x5a;
for (state = 0; state < ao_monitoring + 2; state++) {
byte = recv_raw.packet[state];
sum += byte;
hex(byte);
}
hex(sum);
putchar ('\n');
ao_mutex_put(&ao_monitoring_mutex);
#if HAS_RSSI
if (recv_raw.packet[ao_monitoring + 1] & AO_RADIO_STATUS_CRC_OK) {
rssi = AO_RSSI_FROM_RADIO(recv_raw.packet[ao_monitoring]);
ao_rssi_set(rssi);
}
#endif
break;
}
ao_usb_flush();
}
}
__xdata struct ao_task ao_monitor_put_task;
#endif
__xdata struct ao_task ao_monitor_get_task;
void
ao_monitor_set(uint8_t monitoring)
{
ao_internal_monitoring = monitoring;
_ao_monitor_adjust();
}
void
ao_monitor_disable(void)
{
++ao_monitor_disabled;
_ao_monitor_adjust();
}
void
ao_monitor_enable(void)
{
--ao_monitor_disabled;
_ao_monitor_adjust();
}
#if HAS_MONITOR_PUT
static void
set_monitor(void)
{
ao_cmd_hex();
ao_external_monitoring = ao_cmd_lex_i;
ao_wakeup(DATA_TO_XDATA(&ao_external_monitoring));
ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
_ao_monitor_adjust();
}
__code struct ao_cmds ao_monitor_cmds[] = {
{ set_monitor, "m <0 off, 1 old, 20 std>\0Set radio monitoring" },
{ 0, NULL },
};
#endif
void
ao_monitor_init(void) __reentrant
{
#if HAS_MONITOR_PUT
ao_cmd_register(&ao_monitor_cmds[0]);
ao_add_task(&ao_monitor_put_task, ao_monitor_put, "monitor_put");
#endif
ao_add_task(&ao_monitor_get_task, ao_monitor_get, "monitor_get");
#if AO_MONITOR_LED
ao_add_task(&ao_monitor_blink_task, ao_monitor_blink, "monitor_blink");
#endif
}
|