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
|
/*
* 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; version 2 of the License.
*
* 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"
#if !HAS_MONITOR
#error Must define HAS_MONITOR to 1
#endif
__xdata uint8_t ao_monitoring;
__pdata uint8_t ao_monitor_led;
#define AO_MONITOR_RING 8
__xdata union ao_monitor {
struct ao_telemetry_raw_recv raw;
struct ao_telemetry_orig_recv orig;
struct ao_telemetry_tiny_recv tiny;
} ao_monitor_ring[AO_MONITOR_RING];
#define ao_monitor_ring_next(n) (((n) + 1) & (AO_MONITOR_RING - 1))
__data uint8_t ao_monitor_head;
void
ao_monitor_get(void)
{
uint8_t size;
for (;;) {
switch (ao_monitoring) {
case 0:
ao_sleep(&ao_monitoring);
continue;
case AO_MONITORING_ORIG:
size = sizeof (struct ao_telemetry_orig_recv);
break;
case AO_MONITORING_TINY:
size = sizeof (struct ao_telemetry_tiny_recv);
break;
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))
continue;
ao_monitor_head = ao_monitor_ring_next(ao_monitor_head);
ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
ao_led_toggle(ao_monitor_led);
}
}
void
ao_monitor_put(void)
{
__xdata char callsign[AO_MAX_CALLSIGN+1];
uint8_t ao_monitor_tail;
uint8_t state;
uint8_t sum, byte;
int16_t rssi;
__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_monitor_tail == ao_monitor_head)
ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
m = &ao_monitor_ring[ao_monitor_tail];
ao_monitor_tail = ao_monitor_ring_next(ao_monitor_tail);
switch (ao_monitoring) {
case AO_MONITORING_ORIG:
state = recv_orig.telemetry_orig.flight_state;
/* Typical RSSI offset for 38.4kBaud at 433 MHz is 74 */
rssi = (int16_t) (recv_orig.rssi >> 1) - 74;
memcpy(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');
ao_rssi_set(rssi);
} else {
printf("CRC INVALID RSSI %3d\n", rssi);
}
break;
case AO_MONITORING_TINY:
state = recv_tiny.telemetry_tiny.flight_state;
/* Typical RSSI offset for 38.4kBaud at 433 MHz is 74 */
rssi = (int16_t) (recv_tiny.rssi >> 1) - 74;
memcpy(callsign, recv_tiny.telemetry_tiny.callsign, AO_MAX_CALLSIGN);
if (state > ao_flight_invalid)
state = ao_flight_invalid;
if (recv_tiny.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_tiny.telemetry_tiny.serial,
recv_tiny.telemetry_tiny.flight,
rssi,
ao_state_names[state],
recv_tiny.telemetry_tiny.adc.tick);
/* Raw sensor values */
printf(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_tiny.telemetry_tiny.adc.pres,
recv_tiny.telemetry_tiny.adc.temp,
recv_tiny.telemetry_tiny.adc.v_batt,
recv_tiny.telemetry_tiny.adc.sense_d,
recv_tiny.telemetry_tiny.adc.sense_m);
/* Sensor calibration values */
printf(AO_TELEM_CAL_BARO_GROUND " %d ",
recv_tiny.telemetry_tiny.ground_pres);
#if 1
/* Kalman state values */
printf(AO_TELEM_KALMAN_HEIGHT " %d "
AO_TELEM_KALMAN_SPEED " %d "
AO_TELEM_KALMAN_ACCEL " %d\n",
recv_tiny.telemetry_tiny.height,
recv_tiny.telemetry_tiny.speed,
recv_tiny.telemetry_tiny.accel);
#else
/* Ad-hoc flight values */
printf(AO_TELEM_ADHOC_ACCEL " %d "
AO_TELEM_ADHOC_SPEED " %ld "
AO_TELEM_ADHOC_BARO " %d\n",
recv_tiny.telemetry_tiny.flight_accel,
recv_tiny.telemetry_tiny.flight_vel,
recv_tiny.telemetry_tiny.flight_pres);
#endif
ao_rssi_set(rssi);
} else {
printf("CRC INVALID RSSI %3d\n", rssi);
}
break;
default:
printf ("TELEM %02x", ao_monitoring + 2);
sum = 0x5a;
for (state = 0; state < ao_monitoring + 2; state++) {
byte = recv_raw.packet[state];
sum += byte;
printf("%02x", byte);
}
printf("%02x\n", sum);
break;
}
ao_usb_flush();
}
}
__xdata struct ao_task ao_monitor_get_task;
__xdata struct ao_task ao_monitor_put_task;
void
ao_set_monitor(uint8_t monitoring)
{
if (ao_monitoring)
ao_radio_recv_abort();
ao_monitoring = monitoring;
ao_wakeup(&ao_monitoring);
}
static void
set_monitor(void)
{
ao_cmd_hex();
ao_set_monitor(ao_cmd_lex_i);
}
__code struct ao_cmds ao_monitor_cmds[] = {
{ set_monitor, "m <0 off, 1 full, 2 tiny>\0Enable/disable radio monitoring" },
{ 0, NULL },
};
void
ao_monitor_init(uint8_t monitor_led, uint8_t monitoring) __reentrant
{
ao_monitor_led = monitor_led;
ao_monitoring = monitoring;
ao_cmd_register(&ao_monitor_cmds[0]);
ao_add_task(&ao_monitor_get_task, ao_monitor_get, "monitor_get");
ao_add_task(&ao_monitor_put_task, ao_monitor_put, "monitor_put");
}
|