2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 #include "ao_flight.h"
23 #error Must define HAS_MONITOR to 1
26 #ifndef LEGACY_MONITOR
27 #error Must define LEGACY_MONITOR
30 #ifndef HAS_MONITOR_PUT
31 #define HAS_MONITOR_PUT 1
34 #ifndef AO_MONITOR_LED
35 #error Must define AO_MONITOR_LED
38 __data uint8_t ao_monitoring;
39 static __data uint8_t ao_monitor_disabled;
40 static __data uint8_t ao_internal_monitoring;
41 static __data uint8_t ao_external_monitoring;
43 __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
45 __data uint8_t ao_monitor_head;
48 _ao_monitor_adjust(void)
51 ao_radio_recv_abort();
52 if (ao_monitor_disabled)
55 if (ao_external_monitoring)
56 ao_monitoring = ao_external_monitoring;
58 ao_monitoring = ao_internal_monitoring;
60 ao_wakeup(DATA_TO_XDATA(&ao_monitoring));
69 switch (ao_monitoring) {
71 ao_sleep(DATA_TO_XDATA(&ao_monitoring));
74 case AO_MONITORING_ORIG:
75 size = sizeof (struct ao_telemetry_orig_recv);
79 if (ao_monitoring > AO_MAX_TELEMETRY)
80 ao_monitoring = AO_MAX_TELEMETRY;
84 if (!ao_radio_recv(&ao_monitor_ring[ao_monitor_head], size + 2))
86 ao_monitor_head = ao_monitor_ring_next(ao_monitor_head);
87 ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
92 __xdata struct ao_task ao_monitor_blink_task;
95 ao_monitor_blink(void)
98 ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
99 ao_led_for(AO_MONITOR_LED, AO_MS_TO_TICKS(100));
106 static const char xdigit[16] = {
107 '0', '1', '2', '3', '4', '5', '6', '7',
108 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
111 #define hex(c) do { putchar(xdigit[(c) >> 4]); putchar(xdigit[(c)&0xf]); } while (0)
117 __xdata char callsign[AO_MAX_CALLSIGN+1];
120 uint8_t ao_monitor_tail;
123 __xdata union ao_monitor *m;
125 #define recv_raw ((m->raw))
126 #define recv_orig ((m->orig))
127 #define recv_tiny ((m->tiny))
129 ao_monitor_tail = ao_monitor_head;
131 while (!ao_external_monitoring)
132 ao_sleep(DATA_TO_XDATA(&ao_external_monitoring));
133 while (ao_monitor_tail == ao_monitor_head && ao_external_monitoring)
134 ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
135 m = &ao_monitor_ring[ao_monitor_tail];
136 ao_monitor_tail = ao_monitor_ring_next(ao_monitor_tail);
137 switch (ao_monitoring) {
141 case AO_MONITORING_ORIG:
142 state = recv_orig.telemetry_orig.flight_state;
144 /* Typical RSSI offset for 38.4kBaud at 433 MHz is 74 */
145 rssi = (int16_t) (recv_orig.rssi >> 1) - 74;
146 ao_xmemcpy(callsign, recv_orig.telemetry_orig.callsign, AO_MAX_CALLSIGN);
147 if (state > ao_flight_invalid)
148 state = ao_flight_invalid;
149 if (recv_orig.status & PKT_APPEND_STATUS_1_CRC_OK) {
151 /* General header fields */
152 printf(AO_TELEM_VERSION " %d "
154 AO_TELEM_SERIAL " %d "
155 AO_TELEM_FLIGHT " %d "
157 AO_TELEM_STATE " %s "
158 AO_TELEM_TICK " %d ",
159 AO_TELEMETRY_VERSION,
161 recv_orig.telemetry_orig.serial,
162 recv_orig.telemetry_orig.flight,
164 ao_state_names[state],
165 recv_orig.telemetry_orig.adc.tick);
167 /* Raw sensor values */
168 printf(AO_TELEM_RAW_ACCEL " %d "
169 AO_TELEM_RAW_BARO " %d "
170 AO_TELEM_RAW_THERMO " %d "
171 AO_TELEM_RAW_BATT " %d "
172 AO_TELEM_RAW_DROGUE " %d "
173 AO_TELEM_RAW_MAIN " %d ",
174 recv_orig.telemetry_orig.adc.accel,
175 recv_orig.telemetry_orig.adc.pres,
176 recv_orig.telemetry_orig.adc.temp,
177 recv_orig.telemetry_orig.adc.v_batt,
178 recv_orig.telemetry_orig.adc.sense_d,
179 recv_orig.telemetry_orig.adc.sense_m);
181 /* Sensor calibration values */
182 printf(AO_TELEM_CAL_ACCEL_GROUND " %d "
183 AO_TELEM_CAL_BARO_GROUND " %d "
184 AO_TELEM_CAL_ACCEL_PLUS " %d "
185 AO_TELEM_CAL_ACCEL_MINUS " %d ",
186 recv_orig.telemetry_orig.ground_accel,
187 recv_orig.telemetry_orig.ground_pres,
188 recv_orig.telemetry_orig.accel_plus_g,
189 recv_orig.telemetry_orig.accel_minus_g);
191 if (recv_orig.telemetry_orig.u.k.unused == 0x8000) {
192 /* Kalman state values */
193 printf(AO_TELEM_KALMAN_HEIGHT " %d "
194 AO_TELEM_KALMAN_SPEED " %d "
195 AO_TELEM_KALMAN_ACCEL " %d ",
196 recv_orig.telemetry_orig.height,
197 recv_orig.telemetry_orig.u.k.speed,
198 recv_orig.telemetry_orig.accel);
200 /* Ad-hoc flight values */
201 printf(AO_TELEM_ADHOC_ACCEL " %d "
202 AO_TELEM_ADHOC_SPEED " %ld "
203 AO_TELEM_ADHOC_BARO " %d ",
204 recv_orig.telemetry_orig.accel,
205 recv_orig.telemetry_orig.u.flight_vel,
206 recv_orig.telemetry_orig.height);
208 ao_gps_print(&recv_orig.telemetry_orig.gps);
209 ao_gps_tracking_print(&recv_orig.telemetry_orig.gps_tracking);
215 printf("CRC INVALID RSSI %3d\n", rssi);
218 #endif /* LEGACY_MONITOR */
222 extern uint32_t ao_rx_start_tick, ao_rx_packet_tick, ao_rx_done_tick, ao_rx_last_done_tick;
223 extern uint32_t ao_fec_decode_start, ao_fec_decode_end;
225 printf ("between packet: %d\n", ao_rx_start_tick - ao_rx_last_done_tick);
226 printf ("receive start delay: %d\n", ao_rx_packet_tick - ao_rx_start_tick);
227 printf ("decode time: %d\n", ao_fec_decode_end - ao_fec_decode_start);
228 printf ("rx cleanup: %d\n", ao_rx_done_tick - ao_fec_decode_end);
232 hex((uint8_t) (ao_monitoring + 2));
234 for (state = 0; state < ao_monitoring + 2; state++) {
235 byte = recv_raw.packet[state];
242 if (recv_raw.packet[ao_monitoring + 1] & PKT_APPEND_STATUS_1_CRC_OK) {
243 rssi = ((int16_t) recv_raw.packet[ao_monitoring] >> 1) - 74;
253 __xdata struct ao_task ao_monitor_put_task;
256 __xdata struct ao_task ao_monitor_get_task;
259 ao_monitor_set(uint8_t monitoring)
261 ao_internal_monitoring = monitoring;
262 _ao_monitor_adjust();
266 ao_monitor_disable(void)
268 ++ao_monitor_disabled;
269 _ao_monitor_adjust();
273 ao_monitor_enable(void)
275 --ao_monitor_disabled;
276 _ao_monitor_adjust();
284 ao_external_monitoring = ao_cmd_lex_i;
285 ao_wakeup(DATA_TO_XDATA(&ao_external_monitoring));
286 ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
287 _ao_monitor_adjust();
290 __code struct ao_cmds ao_monitor_cmds[] = {
291 { set_monitor, "m <0 off, 1 old, 20 std>\0Set radio monitoring" },
297 ao_monitor_init(void) __reentrant
300 ao_cmd_register(&ao_monitor_cmds[0]);
301 ao_add_task(&ao_monitor_put_task, ao_monitor_put, "monitor_put");
303 ao_add_task(&ao_monitor_get_task, ao_monitor_get, "monitor_get");
305 ao_add_task(&ao_monitor_blink_task, ao_monitor_blink, "monitor_blink");