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.
22 #error Must define HAS_MONITOR to 1
25 #ifndef LEGACY_MONITOR
26 #error Must define LEGACY_MONITOR
29 #ifndef HAS_MONITOR_PUT
30 #define HAS_MONIOTOR_PUT 1
33 #ifndef AO_MONITOR_LED
34 #error Must define AO_MONITOR_LED
37 __data uint8_t ao_monitoring;
38 static __data uint8_t ao_monitor_disabled;
39 static __data uint8_t ao_internal_monitoring;
40 static __data uint8_t ao_external_monitoring;
42 __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
44 __data uint8_t ao_monitor_head;
47 _ao_monitor_adjust(void)
50 ao_radio_recv_abort();
51 if (ao_monitor_disabled)
54 if (ao_external_monitoring)
55 ao_monitoring = ao_external_monitoring;
57 ao_monitoring = ao_internal_monitoring;
59 ao_wakeup(DATA_TO_XDATA(&ao_monitoring));
68 switch (ao_monitoring) {
70 ao_sleep(DATA_TO_XDATA(&ao_monitoring));
73 case AO_MONITORING_ORIG:
74 size = sizeof (struct ao_telemetry_orig_recv);
78 if (ao_monitoring > AO_MAX_TELEMETRY)
79 ao_monitoring = AO_MAX_TELEMETRY;
83 if (!ao_radio_recv(&ao_monitor_ring[ao_monitor_head], size + 2))
85 ao_monitor_head = ao_monitor_ring_next(ao_monitor_head);
86 ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
91 __xdata struct ao_task ao_monitor_blink_task;
94 ao_monitor_blink(void)
97 ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
98 ao_led_for(AO_MONITOR_LED, AO_MS_TO_TICKS(100));
109 __xdata char callsign[AO_MAX_CALLSIGN+1];
112 uint8_t ao_monitor_tail;
115 __xdata union ao_monitor *m;
117 #define recv_raw ((m->raw))
118 #define recv_orig ((m->orig))
119 #define recv_tiny ((m->tiny))
121 ao_monitor_tail = ao_monitor_head;
123 while (!ao_external_monitoring)
124 ao_sleep(DATA_TO_XDATA(&ao_external_monitoring));
125 while (ao_monitor_tail == ao_monitor_head && ao_external_monitoring)
126 ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
127 m = &ao_monitor_ring[ao_monitor_tail];
128 ao_monitor_tail = ao_monitor_ring_next(ao_monitor_tail);
129 switch (ao_monitoring) {
133 case AO_MONITORING_ORIG:
134 state = recv_orig.telemetry_orig.flight_state;
136 /* Typical RSSI offset for 38.4kBaud at 433 MHz is 74 */
137 rssi = (int16_t) (recv_orig.rssi >> 1) - 74;
138 ao_xmemcpy(callsign, recv_orig.telemetry_orig.callsign, AO_MAX_CALLSIGN);
139 if (state > ao_flight_invalid)
140 state = ao_flight_invalid;
141 if (recv_orig.status & PKT_APPEND_STATUS_1_CRC_OK) {
143 /* General header fields */
144 printf(AO_TELEM_VERSION " %d "
146 AO_TELEM_SERIAL " %d "
147 AO_TELEM_FLIGHT " %d "
149 AO_TELEM_STATE " %s "
150 AO_TELEM_TICK " %d ",
151 AO_TELEMETRY_VERSION,
153 recv_orig.telemetry_orig.serial,
154 recv_orig.telemetry_orig.flight,
156 ao_state_names[state],
157 recv_orig.telemetry_orig.adc.tick);
159 /* Raw sensor values */
160 printf(AO_TELEM_RAW_ACCEL " %d "
161 AO_TELEM_RAW_BARO " %d "
162 AO_TELEM_RAW_THERMO " %d "
163 AO_TELEM_RAW_BATT " %d "
164 AO_TELEM_RAW_DROGUE " %d "
165 AO_TELEM_RAW_MAIN " %d ",
166 recv_orig.telemetry_orig.adc.accel,
167 recv_orig.telemetry_orig.adc.pres,
168 recv_orig.telemetry_orig.adc.temp,
169 recv_orig.telemetry_orig.adc.v_batt,
170 recv_orig.telemetry_orig.adc.sense_d,
171 recv_orig.telemetry_orig.adc.sense_m);
173 /* Sensor calibration values */
174 printf(AO_TELEM_CAL_ACCEL_GROUND " %d "
175 AO_TELEM_CAL_BARO_GROUND " %d "
176 AO_TELEM_CAL_ACCEL_PLUS " %d "
177 AO_TELEM_CAL_ACCEL_MINUS " %d ",
178 recv_orig.telemetry_orig.ground_accel,
179 recv_orig.telemetry_orig.ground_pres,
180 recv_orig.telemetry_orig.accel_plus_g,
181 recv_orig.telemetry_orig.accel_minus_g);
183 if (recv_orig.telemetry_orig.u.k.unused == 0x8000) {
184 /* Kalman state values */
185 printf(AO_TELEM_KALMAN_HEIGHT " %d "
186 AO_TELEM_KALMAN_SPEED " %d "
187 AO_TELEM_KALMAN_ACCEL " %d ",
188 recv_orig.telemetry_orig.height,
189 recv_orig.telemetry_orig.u.k.speed,
190 recv_orig.telemetry_orig.accel);
192 /* Ad-hoc flight values */
193 printf(AO_TELEM_ADHOC_ACCEL " %d "
194 AO_TELEM_ADHOC_SPEED " %ld "
195 AO_TELEM_ADHOC_BARO " %d ",
196 recv_orig.telemetry_orig.accel,
197 recv_orig.telemetry_orig.u.flight_vel,
198 recv_orig.telemetry_orig.height);
200 ao_gps_print(&recv_orig.telemetry_orig.gps);
201 ao_gps_tracking_print(&recv_orig.telemetry_orig.gps_tracking);
207 printf("CRC INVALID RSSI %3d\n", rssi);
210 #endif /* LEGACY_MONITOR */
212 printf ("TELEM %02x", ao_monitoring + 2);
214 for (state = 0; state < ao_monitoring + 2; state++) {
215 byte = recv_raw.packet[state];
217 printf("%02x", byte);
219 printf("%02x\n", sum);
221 if (recv_raw.packet[ao_monitoring + 1] & PKT_APPEND_STATUS_1_CRC_OK) {
222 rssi = ((int16_t) recv_raw.packet[ao_monitoring] >> 1) - 74;
232 __xdata struct ao_task ao_monitor_put_task;
235 __xdata struct ao_task ao_monitor_get_task;
238 ao_monitor_set(uint8_t monitoring)
240 ao_internal_monitoring = monitoring;
241 _ao_monitor_adjust();
245 ao_monitor_disable(void)
247 ++ao_monitor_disabled;
248 _ao_monitor_adjust();
252 ao_monitor_enable(void)
254 --ao_monitor_disabled;
255 _ao_monitor_adjust();
263 ao_external_monitoring = ao_cmd_lex_i;
264 ao_wakeup(DATA_TO_XDATA(&ao_external_monitoring));
265 ao_wakeup(DATA_TO_XDATA(&ao_monitor_head));
266 _ao_monitor_adjust();
269 __code struct ao_cmds ao_monitor_cmds[] = {
270 { set_monitor, "m <0 off, 1 old, 20 std>\0Enable/disable radio monitoring" },
276 ao_monitor_init(void) __reentrant
279 ao_cmd_register(&ao_monitor_cmds[0]);
280 ao_add_task(&ao_monitor_put_task, ao_monitor_put, "monitor_put");
282 ao_add_task(&ao_monitor_get_task, ao_monitor_get, "monitor_get");
284 ao_add_task(&ao_monitor_blink_task, ao_monitor_blink, "monitor_blink");