2 * Copyright (c) 2013, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <aversive/queue.h>
32 #include <aversive/endian.h>
45 #include "xbee_user.h"
46 #include "spi_servo.h"
47 #include "../common/i2c_commands.h"
48 #include "i2c_protocol.h"
52 #define SERVO_NBITS 10
54 #define RX_DB_THRESHOLD 65 /* mean -65 dB */
57 struct rc_proto_timers rc_proto_timers = {
58 .send_servo_min_ms = 50,
59 .send_servo_max_ms = 300,
60 .send_power_probe_ms = 500,
61 .send_gps_pos_ms = 500,
62 .send_imu_pos_ms = 100,
66 /* rc_proto statistics, accessed with sched_prio=XBEE_PRIO */
67 struct rc_proto_stats_data {
74 uint32_t power_probe_rx;
75 uint32_t power_probe_tx;
86 uint32_t echo_ans_latency_sum;
88 static struct rc_proto_stats_data stats; /* local stats */
89 static struct rc_proto_stats_data peer_stats; /* peer stats */
91 /* store last received power probes */
92 struct rc_proto_power_levels {
96 static struct rc_proto_power_levels power_levels[MAX_POWER_LEVEL];
98 /* address of the peer */
99 static uint64_t rc_proto_dstaddr = 0xFFFF; /* broadcast by default */
101 /* state sent to the xbee peer (used on RC) */
103 uint16_t servos[N_SERVO];
104 uint8_t bypass; /* ask the wing to bypass servos = use legacy controller */
105 uint8_t seq; /* from 0 to 15, 4 bits */
106 uint16_t time; /* time of last xmit */
108 static struct servo_tx servo_tx;
110 /* state received from the xbee peer (used on WING) */
112 uint16_t servos[N_SERVO];
113 uint16_t time; /* time of last xmit */
115 static struct servo_rx servo_rx;
117 /* the received seq value (acknowledged by the wing, received on the rc) */
120 /* define tx mode (disabled, send from spi, bypass), rx mode (auto-bypass),
122 static uint16_t rc_proto_flags;
124 /* callout managing rc_proto (ex: sending of servos periodically) */
125 static struct callout rc_proto_timer;
127 /* a negative value (-1 or -4) means we don't know the best level, but it stores
128 * the previous PL value (0 or 4) so we can alternate. */
129 int8_t power_level_global = -1;
131 #define POW_IMU_GPS 4
133 /* update power level when we receive the answer from DB. The request is sent by
134 * rc_proto_rx_power_probe(). */
135 static int8_t update_power_level(int8_t retcode, void *frame, unsigned len,
138 struct xbee_atresp_hdr *atresp = (struct xbee_atresp_hdr *)frame;
139 int level = (intptr_t)arg;
142 /* nothing more to do, error is already logged by xbee_user */
146 if (len < sizeof(struct xbee_atresp_hdr) + sizeof(uint8_t))
149 db = atresp->data[0];
150 power_levels[level].power_db = db;
151 power_levels[level].ttl = 10; /* valid during 10 seconds */
155 /* when we receive a power probe, ask the DB value to the xbee */
156 static void rc_proto_rx_power_probe(int power_level)
158 xbeeapp_send_atcmd("DB", NULL, 0, update_power_level,
159 (void *)(intptr_t)power_level);
162 /* called every second to decide which power should be used */
163 static void compute_best_power(void)
165 int8_t best_power_level = -1;
169 for (i = 0; i < MAX_POWER_LEVEL; i++) {
170 if (power_levels[i].ttl > 0)
171 power_levels[i].ttl--;
174 for (i = 0; i < MAX_POWER_LEVEL; i++) {
175 if (power_levels[i].ttl == 0)
178 /* if signal is powerful enough, select this as level */
179 if (power_levels[i].power_db < RX_DB_THRESHOLD) {
180 best_power_level = i;
185 /* we have no info, don't touch the negative value */
186 if (best_power_level < 0 && power_level_global < 0)
189 if (power_level_global != best_power_level) {
190 DEBUG(E_USER_RC_PROTO, "changing power level %d => %d\n",
191 power_level_global, best_power_level);
193 power_level_global = best_power_level;
196 /* return the best power level, or -1 if best power level computation is
198 static int8_t get_best_power(void)
200 if ((rc_proto_flags & RC_PROTO_FLAGS_COMPUTE_BEST_POW) == 0)
204 if (power_level_global == -1) {
205 power_level_global = -4;
208 else if (power_level_global == -4) {
209 power_level_global = -1;
213 return power_level_global;
216 /* send a hello message: no answer expected */
217 int8_t rc_proto_send_hello(uint64_t addr, void *data, uint8_t data_len,
220 struct rc_proto_hello hdr;
225 hdr.type = RC_PROTO_HELLO;
226 hdr.datalen = data_len;
229 msg.iov[0].buf = &hdr;
230 msg.iov[0].len = sizeof(hdr);
231 msg.iov[1].buf = data;
232 msg.iov[1].len = data_len;
234 /* set power level */
236 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
238 /* we need to lock callout to increment stats */
239 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
241 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
242 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
247 /* send an echo message: expect a reply */
248 int8_t rc_proto_send_echo_req(uint64_t addr, void *data, uint8_t data_len,
251 struct rc_proto_echo_req hdr;
256 hdr.type = RC_PROTO_ECHO_REQ;
258 hdr.timestamp = get_time_ms();
259 hdr.datalen = data_len;
262 msg.iov[0].buf = &hdr;
263 msg.iov[0].len = sizeof(hdr);
264 msg.iov[1].buf = data;
265 msg.iov[1].len = data_len;
267 /* set power level */
269 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
271 /* we need to lock callout to increment stats */
272 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
274 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
275 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
280 /* send an echo message: expect a reply */
281 int8_t rc_proto_send_echo_ans(uint64_t addr, void *data, uint8_t data_len,
282 int8_t power, uint16_t timestamp)
284 struct rc_proto_echo_ans hdr;
289 hdr.type = RC_PROTO_ECHO_ANS;
290 hdr.datalen = data_len;
291 hdr.timestamp = timestamp;
294 msg.iov[0].buf = &hdr;
295 msg.iov[0].len = sizeof(hdr);
296 msg.iov[1].buf = data;
297 msg.iov[1].len = data_len;
299 /* set power level */
301 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
303 /* we need to lock callout to increment stats */
304 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
306 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
307 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
312 /* send an echo message: expect a reply */
313 int8_t rc_proto_send_power_probe(uint64_t addr, uint8_t power)
315 struct rc_proto_power_probe hdr;
320 hdr.type = RC_PROTO_POWER_PROBE;
321 hdr.power_level = power;
324 msg.iov[0].buf = &hdr;
325 msg.iov[0].len = sizeof(hdr);
327 /* set power level */
328 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
330 /* we need to lock callout to increment stats */
331 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
332 stats.power_probe_tx++;
333 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
334 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
339 /* convert values from servo_tx.servos into a xbee frame */
340 static int8_t servo2buf(uint8_t buf[RC_PROTO_SERVO_LEN],
341 uint8_t seq, uint8_t bypass, uint8_t pow, const uint16_t servos[N_SERVO])
345 buf[i++] = RC_PROTO_SERVO;
346 buf[i++] = ((seq & 0xf) << 4) | (bypass << 3) | (pow & 0x7);
348 buf[i++] = servos[0] >> 2;
349 buf[i] = (servos[0] & 0x3) << 6;
351 buf[i++] |= servos[1] >> 4;
352 buf[i] = (servos[1] & 0xf) << 4;
354 buf[i++] |= servos[2] >> 6;
355 buf[i] = (servos[2] & 0x3f) << 2;
357 buf[i++] |= servos[3] >> 8;
358 buf[i++] = servos[3] & 0xff;
360 buf[i++] = servos[4] >> 2;
361 buf[i] = (servos[4] & 0x3) << 6;
363 buf[i++] |= servos[5] >> 4;
364 buf[i] = (servos[5] & 0xf) << 4;
369 /* send servos, called periodically with prio = XBEE_PRIO */
370 static int8_t rc_proto_send_servos(void)
372 struct rc_proto_servo hdr;
374 uint8_t i, updated = 0;
375 uint16_t ms, diff, servo_val;
376 uint8_t frame[RC_PROTO_SERVO_LEN];
380 /* servo send disabled */
381 if ((rc_proto_flags & RC_PROTO_FLAGS_TX_MASK) == RC_PROTO_FLAGS_TX_OFF)
384 /* if we transmitted servos values recently, nothing to do */
386 diff = ms - servo_tx.time;
387 if (diff < rc_proto_timers.send_servo_min_ms)
390 /* prepare values to send */
391 if ((rc_proto_flags & RC_PROTO_FLAGS_TX_MASK) ==
392 RC_PROTO_FLAGS_TX_COPY_SPI) {
394 /* set bypass to 0 */
395 if (servo_tx.bypass == 1) {
400 /* copy values from spi */
401 for (i = 0; i < N_SERVO; i++) {
402 servo_val = spi_servo_get(i);
403 if (servo_val != servo_tx.servos[i]) {
404 servo_tx.servos[i] = servo_val;
410 /* set bypass to 1 */
411 if (servo_tx.bypass == 0) {
417 /* if no value changed and last message is acknowledged, don't transmit
418 * if we already transmitted quite recently */
419 if (updated == 0 && ack == servo_tx.seq &&
420 diff < rc_proto_timers.send_servo_max_ms)
423 /* ok, we need to transmit */
425 /* get the new seq value */
428 servo_tx.seq &= 0x1f;
429 if (servo_tx.seq == ack)
430 servo_tx.seq = (ack - 1) & 0x1f;
432 /* reset the "updated" flag and save time */
435 /* set power level */
436 power = get_best_power();
437 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
439 /* create frame and send it */
440 servo2buf(frame, servo_tx.seq, servo_tx.bypass, power, servo_tx.servos);
441 hdr.type = RC_PROTO_SERVO;
444 msg.iov[0].buf = &hdr;
445 msg.iov[0].len = sizeof(hdr);
446 msg.iov[1].buf = frame;
447 msg.iov[1].len = RC_PROTO_SERVO_LEN;
450 ret = xbeeapp_send_msg(rc_proto_dstaddr, &msg, NULL, NULL);
456 /* send a ack message: no answer expected */
457 int8_t rc_proto_send_ack(uint64_t addr, uint8_t seq, int8_t power)
459 struct rc_proto_ack hdr;
464 hdr.type = RC_PROTO_ACK;
468 msg.iov[0].buf = &hdr;
469 msg.iov[0].len = sizeof(hdr);
471 /* set power level */
473 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
475 /* we need to lock callout to increment stats */
476 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
478 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
479 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
484 /* send a hello message: no answer expected */
485 int8_t rc_proto_send_stats(uint64_t addr, int8_t power)
487 struct rc_proto_stats hdr;
492 hdr.type = RC_PROTO_STATS;
495 msg.iov[0].buf = &hdr;
496 msg.iov[0].len = sizeof(hdr);
497 msg.iov[1].buf = &stats;
498 msg.iov[1].len = sizeof(stats);
500 /* set power level */
502 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
504 /* we need to lock callout to increment stats */
505 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
507 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
508 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
513 /* send a gps_pos message: no answer expected */
514 int8_t rc_proto_send_gps_pos(uint64_t addr, int8_t power)
516 struct rc_proto_gps_pos gps_msg;
522 gps_msg.type = RC_PROTO_GPS_POS;
525 gps_msg.valid = !!(imuboard_status.flags & I2C_IMUBOARD_STATUS_GPS_OK);
526 gps_msg.latitude = imuboard_status.latitude;
527 gps_msg.longitude = imuboard_status.longitude;
528 gps_msg.altitude = imuboard_status.altitude;
529 IRQ_UNLOCK(irq_flags);
532 msg.iov[0].buf = &gps_msg;
533 msg.iov[0].len = sizeof(gps_msg);
535 /* set power level */
537 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
539 /* we also need to lock callout to increment stats */
540 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
542 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
543 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
548 /* send a imu_pos message: no answer expected */
549 int8_t rc_proto_send_imu_pos(uint64_t addr, int8_t power)
551 struct rc_proto_imu_pos imu_msg;
557 imu_msg.type = RC_PROTO_IMU_POS;
560 imu_msg.valid = !!(imuboard_status.flags & I2C_IMUBOARD_STATUS_IMU_OK);
561 imu_msg.roll = imuboard_status.roll;
562 imu_msg.pitch = imuboard_status.pitch;
563 imu_msg.yaw = imuboard_status.yaw;
564 IRQ_UNLOCK(irq_flags);
567 msg.iov[0].buf = &imu_msg;
568 msg.iov[0].len = sizeof(imu_msg);
570 /* set power level */
572 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
574 /* we also need to lock callout to increment stats */
575 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
577 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
578 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
585 void rc_proto_set_mode(uint16_t flags)
587 rc_proto_flags = flags;
590 uint16_t rc_proto_get_mode(void)
592 return rc_proto_flags;
595 /* convert a receved servo frame into servo values */
596 static int8_t buf2servo(uint16_t servos[N_SERVO], const uint8_t *buf)
602 val |= (buf[2] >> 6);
607 val |= (buf[3] >> 4);
612 val |= (buf[4] >> 2);
622 val |= (buf[7] >> 6);
627 val |= (buf[8] >> 4);
633 /* process a received servo frame */
634 static int8_t rc_proto_rx_servo(struct rc_proto_servo *rcs)
639 bypass = !!(rcs->data[0] & 0x08);
640 pow = rcs->data[0] & 0x07;
642 /* convert it in a table of servo values */
643 if (bypass == 0 && buf2servo(servo_rx.servos, rcs->data) < 0)
647 servo_rx.time = get_time_ms();
649 /* acknowledge received frame */
650 seq = rcs->data[0] >> 4;
651 rc_proto_send_ack(rc_proto_dstaddr, seq, pow);
653 /* copy values to spi */
654 if (rc_proto_flags & RC_PROTO_FLAGS_RX_COPY_SPI) {
655 spi_servo_set_bypass(bypass);
658 for (i = 0; i < N_SERVO; i++)
659 spi_servo_set(i, servo_rx.servos[i]);
665 /* receive a rc_proto message */
666 int rc_proto_rx(struct xbee_recv_hdr *recvframe, unsigned len)
668 unsigned int datalen;
669 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
671 if (len < sizeof(*recvframe))
674 datalen = len - sizeof(*recvframe);
675 if (datalen < sizeof(struct rc_proto_hdr))
678 /* other command types */
680 case RC_PROTO_HELLO: {
681 struct rc_proto_hello *rch =
682 (struct rc_proto_hello *) recvframe->data;
684 NOTICE(E_USER_RC_PROTO, "recv hello len=%d", rch->datalen);
689 case RC_PROTO_ECHO_REQ: {
690 struct rc_proto_echo_req *rce =
691 (struct rc_proto_echo_req *) recvframe->data;
692 int8_t power = rce->power;
694 NOTICE(E_USER_RC_PROTO, "recv echo len=%d", rce->datalen);
697 if (rc_proto_send_echo_ans(ntohll(recvframe->srcaddr),
698 rce->data, rce->datalen, power,
705 case RC_PROTO_ECHO_ANS: {
706 struct rc_proto_echo_ans *rce =
707 (struct rc_proto_echo_ans *) recvframe->data;
710 NOTICE(E_USER_RC_PROTO, "recv echo_ans len=%d", rce->datalen);
712 diff = get_time_ms() - rce->timestamp;
713 stats.echo_ans_latency_sum += diff;
717 /* received by the radio controller every ~500ms */
718 case RC_PROTO_POWER_PROBE: {
719 struct rc_proto_power_probe *rcpb =
720 (struct rc_proto_power_probe *) recvframe->data;
722 NOTICE(E_USER_RC_PROTO, "recv power_probe");
724 if (datalen != sizeof(*rcpb))
727 if (rcpb->power_level >= MAX_POWER_LEVEL)
730 stats.power_probe_rx++;
731 /* ask the DB value to the xbee module */
732 rc_proto_rx_power_probe(rcpb->power_level);
737 /* received by the radio controller */
739 struct rc_proto_ack *rca =
740 (struct rc_proto_ack *) recvframe->data;
742 NOTICE(E_USER_RC_PROTO, "recv ack, ack=%d", rca->seq);
747 /* received by the wing */
748 case RC_PROTO_SERVO: {
749 struct rc_proto_servo *rcs =
750 (struct rc_proto_servo *) recvframe->data;
752 NOTICE(E_USER_RC_PROTO, "recv servo");
754 if (datalen != RC_PROTO_SERVO_LEN)
758 return rc_proto_rx_servo(rcs);
761 /* received by the radio controller */
762 case RC_PROTO_STATS: {
763 struct rc_proto_stats *rcs =
764 (struct rc_proto_stats *) recvframe->data;
766 NOTICE(E_USER_RC_PROTO, "recv stats");
768 if (datalen != sizeof(*rcs) + sizeof(peer_stats))
772 memcpy(&peer_stats, rcs->stats, sizeof(peer_stats));
776 /* received by the radio controller */
777 case RC_PROTO_GPS_POS: {
778 struct rc_proto_gps_pos *rcmsg =
779 (struct rc_proto_gps_pos *) recvframe->data;
781 if (datalen != sizeof(*rcmsg))
785 NOTICE(E_USER_RC_PROTO, "GPS received valid=%d %"PRIu32" %"
787 rcmsg->valid, rcmsg->latitude, rcmsg->longitude,
792 /* received by the radio controller */
793 case RC_PROTO_IMU_POS: {
794 struct rc_proto_imu_pos *rcmsg =
795 (struct rc_proto_imu_pos *) recvframe->data;
797 if (datalen != sizeof(*rcmsg))
801 NOTICE(E_USER_RC_PROTO,
802 "IMU received %d %d %d",
803 rcmsg->roll, rcmsg->pitch, rcmsg->yaw);
815 /* called by the scheduler, manage rc_proto periodical tasks */
816 static void rc_proto_cb(struct callout_mgr *cm, struct callout *tim, void *arg)
819 static uint16_t prev_stats_send;
820 static uint16_t prev_compute_pow;
821 static uint16_t prev_power_probe;
822 static uint16_t prev_gps_pos;
823 static uint16_t prev_imu_pos;
824 static uint8_t pow_probe;
829 /* send servo values if flags are enabled. The function will decide
830 * by itself if it's time to send or not */
831 rc_proto_send_servos();
833 /* send power probe periodically */
834 if (rc_proto_flags & RC_PROTO_FLAGS_TX_POW_PROBE) {
835 diff = t - prev_power_probe;
836 if (diff > rc_proto_timers.send_power_probe_ms) {
840 rc_proto_send_power_probe(rc_proto_dstaddr, pow_probe);
841 prev_power_probe = t;
845 /* send gps periodically */
846 if (rc_proto_flags & RC_PROTO_FLAGS_TX_GPS_POS) {
847 diff = t - prev_gps_pos;
848 if (diff > rc_proto_timers.send_gps_pos_ms) {
849 rc_proto_send_gps_pos(rc_proto_dstaddr, POW_IMU_GPS);
854 /* send imu periodically */
855 if (rc_proto_flags & RC_PROTO_FLAGS_TX_IMU_POS) {
856 diff = t - prev_imu_pos;
857 if (diff > rc_proto_timers.send_imu_pos_ms) {
858 rc_proto_send_imu_pos(rc_proto_dstaddr, POW_IMU_GPS);
863 /* on wing, auto bypass servos if no commands since some time */
864 if (rc_proto_flags & RC_PROTO_FLAGS_RX_AUTOBYPASS) {
865 diff = t - servo_rx.time;
866 if (diff > rc_proto_timers.autobypass_ms)
867 spi_servo_set_bypass(1);
870 /* send stats to peer every second */
871 if (rc_proto_flags & RC_PROTO_FLAGS_COMPUTE_BEST_POW) {
872 diff = t - prev_compute_pow;
874 compute_best_power();
875 prev_compute_pow = t;
879 /* send stats to peer every second */
880 if (rc_proto_flags & RC_PROTO_FLAGS_TX_STATS) {
881 diff = t - prev_stats_send;
883 rc_proto_send_stats(rc_proto_dstaddr, get_best_power());
888 callout_schedule(cm, tim, 0);
891 static void __dump_stats(struct rc_proto_stats_data *s)
893 printf_P(PSTR(" hello_tx: %"PRIu32"\r\n"), s->hello_tx);
894 printf_P(PSTR(" hello_rx: %"PRIu32"\r\n"), s->hello_rx);
895 printf_P(PSTR(" echo_req_rx: %"PRIu32"\r\n"), s->echo_req_rx);
896 printf_P(PSTR(" echo_req_tx: %"PRIu32"\r\n"), s->echo_req_tx);
897 printf_P(PSTR(" echo_ans_rx: %"PRIu32"\r\n"), s->echo_ans_rx);
898 printf_P(PSTR(" echo_ans_tx: %"PRIu32"\r\n"), s->echo_ans_tx);
899 printf_P(PSTR(" power_probe_rx: %"PRIu32"\r\n"), s->power_probe_rx);
900 printf_P(PSTR(" power_probe_tx: %"PRIu32"\r\n"), s->power_probe_tx);
901 printf_P(PSTR(" ack_rx: %"PRIu32"\r\n"), s->ack_rx);
902 printf_P(PSTR(" ack_tx: %"PRIu32"\r\n"), s->ack_tx);
903 printf_P(PSTR(" servo_rx: %"PRIu32"\r\n"), s->servo_rx);
904 printf_P(PSTR(" servo_tx: %"PRIu32"\r\n"), s->servo_tx);
905 printf_P(PSTR(" stats_rx: %"PRIu32"\r\n"), s->stats_rx);
906 printf_P(PSTR(" stats_tx: %"PRIu32"\r\n"), s->stats_tx);
907 printf_P(PSTR(" gps_pos_rx: %"PRIu32"\r\n"), s->gps_pos_rx);
908 printf_P(PSTR(" gps_pos_tx: %"PRIu32"\r\n"), s->gps_pos_tx);
909 printf_P(PSTR(" imu_pos_rx: %"PRIu32"\r\n"), s->imu_pos_rx);
910 printf_P(PSTR(" imu_pos_tx: %"PRIu32"\r\n"), s->imu_pos_tx);
911 if (s->echo_ans_rx != 0) {
912 printf_P(PSTR(" echo_ans_latency_ms: %"PRIu32"\r\n"),
913 s->echo_ans_latency_sum / s->echo_ans_rx);
917 void rc_proto_dump_stats(void)
919 printf_P(PSTR("rc_proto stats LOCAL\r\n"));
920 __dump_stats(&stats);
922 printf_P(PSTR("rc_proto stats PEER\r\n"));
923 __dump_stats(&peer_stats);
926 void rc_proto_reset_stats(void)
930 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
931 memset(&stats, 0, sizeof(stats));
932 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
935 void rc_proto_dump_servos(void)
939 printf_P(PSTR("servo rx\r\n"));
940 for (i = 0; i < N_SERVO; i++) {
941 printf_P(PSTR(" servo[%d] = %d\r\n"), i, servo_rx.servos[i]);
943 printf_P(PSTR("servo tx\r\n"));
944 printf_P(PSTR(" bypass=%d\r\n"), servo_tx.bypass);
945 printf_P(PSTR(" seq=%d\r\n"), servo_tx.seq);
946 printf_P(PSTR(" time=%d\r\n"), servo_tx.time);
947 for (i = 0; i < N_SERVO; i++) {
948 printf_P(PSTR(" servo[%d] = %d\r\n"), i, servo_tx.servos[i]);
952 void rc_proto_set_dstaddr(uint64_t addr)
957 rc_proto_dstaddr = addr;
961 uint64_t rc_proto_get_dstaddr(void)
967 addr = rc_proto_dstaddr;
972 void rc_proto_init(void)
974 callout_init(&rc_proto_timer, rc_proto_cb, NULL, XBEE_PRIO);
975 callout_schedule(&xbeeboard.intr_cm, &rc_proto_timer, 0);