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"
50 #define SERVO_NBITS 10
52 #define RX_DB_THRESHOLD 65 /* mean -65 dB */
54 /* XXX make it configurable */
55 /* min time between 2 servo_send */
56 #define SEND_SERVO_MIN_TIME_MS 50
57 /* max time between 2 servo_send */
58 #define SEND_SERVO_MAX_TIME_MS 300
59 /* time before switching into bypass mode when no servo command received */
60 #define AUTO_BYPASS_TIME_MS 500
62 /* rc_proto statistics */
63 struct rc_proto_stats_data {
70 uint32_t power_probe_rx;
71 uint32_t power_probe_tx;
79 static struct rc_proto_stats_data stats; /* local stats */
80 static struct rc_proto_stats_data peer_stats; /* peer stats */
82 /* store last received power probes */
83 struct rc_proto_power_levels {
87 static struct rc_proto_power_levels power_levels[MAX_POWER_LEVEL];
89 /* address of the peer */
90 static uint64_t rc_proto_dstaddr = 0xFFFF; /* broadcast by default */
92 /* state sent to the xbee peer (used on RC) */
94 uint16_t servos[N_SERVO];
95 uint8_t bypass; /* ask the wing to bypass servos = use legacy controller */
96 uint8_t seq; /* from 0 to 15, 4 bits */
97 uint16_t time; /* time of last xmit */
99 static struct servo_tx servo_tx;
101 /* state received from the xbee peer (used on WING) */
103 uint16_t servos[N_SERVO];
104 uint16_t time; /* time of last xmit */
106 static struct servo_rx servo_rx;
108 /* the received seq value (acknowledged by the wing, received on the rc) */
111 /* define tx mode (disabled, send from spi, bypass), rx mode (auto-bypass),
113 static uint8_t rc_proto_flags;
115 /* callout managing rc_proto (ex: sending of servos periodically) */
116 static struct callout rc_proto_timer;
118 /* a negative value (-1 or -4) means we don't know the best level, but it stores
119 * the previous PL value (0 or 4) so we can alternate. */
120 int8_t power_level_global = -1;
122 /* update power level when we receive the answer from DB. The request is sent by
123 * rc_proto_rx_power_probe(). */
124 static int8_t update_power_level(int8_t retcode, void *frame, unsigned len,
127 struct xbee_atresp_hdr *atresp = (struct xbee_atresp_hdr *)frame;
128 int level = (intptr_t)arg;
131 /* nothing more to do, error is already logged by xbee_user */
135 if (len < sizeof(struct xbee_atresp_hdr) + sizeof(uint8_t))
138 db = atresp->data[0];
139 power_levels[level].power_db = db;
140 power_levels[level].ttl = 10; /* valid during 10 seconds */
144 /* when we receive a power probe, ask the DB value to the xbee */
145 static void rc_proto_rx_power_probe(int power_level)
147 xbeeapp_send_atcmd("DB", NULL, 0, update_power_level,
148 (void *)(intptr_t)power_level);
151 /* called every second */
152 static void compute_best_power(void)
154 int8_t best_power_level = -1;
158 for (i = 0; i < MAX_POWER_LEVEL; i++) {
159 if (power_levels[i].ttl > 0)
160 power_levels[i].ttl--;
163 for (i = 0; i < MAX_POWER_LEVEL; i++) {
164 if (power_levels[i].ttl == 0)
167 /* if signal is powerful enough, select this as level */
168 if (power_levels[i].power_db < RX_DB_THRESHOLD) {
169 best_power_level = i;
174 /* we have no info, don't touch the negative value */
175 if (best_power_level < 0 && power_level_global < 0)
178 if (power_level_global != best_power_level) {
179 DEBUG(E_USER_RC_PROTO, "changing power level %d => %d\n",
180 power_level_global, best_power_level);
182 power_level_global = best_power_level;
185 static uint8_t get_best_power(void)
188 if (power_level_global == -1) {
189 power_level_global = -4;
192 else if (power_level_global == -4) {
193 power_level_global = -1;
197 return power_level_global;
200 /* send a hello message: no answer expected */
201 int8_t rc_proto_send_hello(uint64_t addr, void *data, uint8_t data_len,
204 struct rc_proto_hello hdr;
209 hdr.type = RC_PROTO_HELLO;
210 hdr.datalen = data_len;
213 msg.iov[0].buf = &hdr;
214 msg.iov[0].len = sizeof(hdr);
215 msg.iov[1].buf = data;
216 msg.iov[1].len = data_len;
218 /* set power level */
220 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
222 /* we need to lock callout to increment stats */
223 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
225 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
226 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
231 /* send an echo message: expect a reply */
232 int8_t rc_proto_send_echo_req(uint64_t addr, void *data, uint8_t data_len,
235 struct rc_proto_echo_req hdr;
240 hdr.type = RC_PROTO_ECHO_REQ;
242 hdr.datalen = data_len;
245 msg.iov[0].buf = &hdr;
246 msg.iov[0].len = sizeof(hdr);
247 msg.iov[1].buf = data;
248 msg.iov[1].len = data_len;
250 /* set power level */
252 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
254 /* we need to lock callout to increment stats */
255 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
257 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
258 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
263 /* send an echo message: expect a reply */
264 int8_t rc_proto_send_echo_ans(uint64_t addr, void *data, uint8_t data_len,
267 struct rc_proto_echo_ans hdr;
272 hdr.type = RC_PROTO_ECHO_ANS;
273 hdr.datalen = data_len;
276 msg.iov[0].buf = &hdr;
277 msg.iov[0].len = sizeof(hdr);
278 msg.iov[1].buf = data;
279 msg.iov[1].len = data_len;
281 /* set power level */
283 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
285 /* we need to lock callout to increment stats */
286 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
288 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
289 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
294 /* send an echo message: expect a reply */
295 int8_t rc_proto_send_power_probe(uint64_t addr, uint8_t power)
297 struct rc_proto_power_probe hdr;
302 hdr.type = RC_PROTO_POWER_PROBE;
303 hdr.power_level = power;
306 msg.iov[0].buf = &hdr;
307 msg.iov[0].len = sizeof(hdr);
309 /* set power level */
310 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
312 /* we need to lock callout to increment stats */
313 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
314 stats.power_probe_tx++;
315 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
316 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
321 /* convert values from servo_tx.servos into a xbee frame */
322 static int8_t servo2buf(uint8_t buf[RC_PROTO_SERVO_LEN],
323 uint8_t seq, uint8_t bypass, uint8_t pow, const uint16_t servos[N_SERVO])
327 buf[i++] = RC_PROTO_SERVO;
328 buf[i++] = ((seq & 0xf) << 4) | (bypass << 3) | (pow & 0x7);
330 buf[i++] = servos[0] >> 2;
331 buf[i] = (servos[0] & 0x3) << 6;
333 buf[i++] |= servos[1] >> 4;
334 buf[i] = (servos[1] & 0xf) << 4;
336 buf[i++] |= servos[2] >> 6;
337 buf[i] = (servos[2] & 0x3f) << 2;
339 buf[i++] |= servos[3] >> 8;
340 buf[i++] = servos[3] & 0xff;
342 buf[i++] = servos[4] >> 2;
343 buf[i] = (servos[4] & 0x3) << 6;
345 buf[i++] |= servos[5] >> 4;
346 buf[i] = (servos[5] & 0xf) << 4;
351 /* send servos, called periodically with prio = XBEE_PRIO */
352 static int8_t rc_proto_send_servos(void)
354 struct rc_proto_servo hdr;
356 uint8_t i, updated = 0;
357 uint16_t ms, diff, servo_val;
358 uint8_t frame[RC_PROTO_SERVO_LEN];
362 /* servo send disabled */
363 if ((rc_proto_flags & RC_PROTO_FLAGS_TX_MASK) == RC_PROTO_FLAGS_TX_OFF)
366 /* if we transmitted servos values recently, nothing to do */
368 diff = ms - servo_tx.time;
369 if (diff < SEND_SERVO_MIN_TIME_MS)
372 /* prepare values to send */
373 if ((rc_proto_flags & RC_PROTO_FLAGS_TX_MASK) ==
374 RC_PROTO_FLAGS_TX_COPY_SPI) {
376 /* set bypass to 0 */
377 if (servo_tx.bypass == 1) {
382 /* copy values from spi */
383 for (i = 0; i < N_SERVO; i++) {
384 servo_val = spi_servo_get(i);
385 if (servo_val != servo_tx.servos[i]) {
386 servo_tx.servos[i] = servo_val;
392 /* set bypass to 1 */
393 if (servo_tx.bypass == 0) {
399 /* if no value changed and last message is acknowledged, don't transmit
400 * if we already transmitted quite recently */
401 if (updated == 0 && ack == servo_tx.seq &&
402 diff < SEND_SERVO_MAX_TIME_MS)
405 /* ok, we need to transmit */
407 /* get the new seq value */
410 servo_tx.seq &= 0x1f;
411 if (servo_tx.seq == ack)
412 servo_tx.seq = (ack - 1) & 0x1f;
414 /* reset the "updated" flag and save time */
417 /* set power level */
418 power = get_best_power();
419 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
421 /* create frame and send it */
422 servo2buf(frame, servo_tx.seq, servo_tx.bypass, power, servo_tx.servos);
423 hdr.type = RC_PROTO_SERVO;
426 msg.iov[0].buf = &hdr;
427 msg.iov[0].len = sizeof(hdr);
428 msg.iov[1].buf = frame;
429 msg.iov[1].len = RC_PROTO_SERVO_LEN;
432 ret = xbeeapp_send_msg(rc_proto_dstaddr, &msg, NULL, NULL);
438 /* send a ack message: no answer expected */
439 int8_t rc_proto_send_ack(uint64_t addr, uint8_t seq, int8_t power)
441 struct rc_proto_ack hdr;
446 hdr.type = RC_PROTO_ACK;
450 msg.iov[0].buf = &hdr;
451 msg.iov[0].len = sizeof(hdr);
453 /* set power level */
455 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
457 /* we need to lock callout to increment stats */
458 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
460 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
461 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
466 /* send a hello message: no answer expected */
467 int8_t rc_proto_send_stats(uint64_t addr, int8_t power)
469 struct rc_proto_stats hdr;
474 hdr.type = RC_PROTO_STATS;
477 msg.iov[0].buf = &hdr;
478 msg.iov[0].len = sizeof(hdr);
479 msg.iov[1].buf = &stats;
480 msg.iov[1].len = sizeof(stats);
482 /* set power level */
484 xbeeapp_send_atcmd("PL", &power, sizeof(power), NULL, NULL);
486 /* we need to lock callout to increment stats */
487 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
489 ret = xbeeapp_send_msg(addr, &msg, NULL, NULL);
490 callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
495 void rc_proto_set_mode(uint8_t flags)
497 rc_proto_flags = flags;
500 uint8_t rc_proto_get_mode(void)
502 return rc_proto_flags;
505 /* convert a receved servo frame into servo values */
506 static int8_t buf2servo(uint16_t servos[N_SERVO], const uint8_t *buf)
512 val |= (buf[2] >> 6);
517 val |= (buf[3] >> 4);
522 val |= (buf[4] >> 2);
532 val |= (buf[7] >> 6);
537 val |= (buf[8] >> 4);
543 /* process a received servo frame */
544 static int8_t rc_proto_rx_servo(struct rc_proto_servo *rcs)
549 bypass = !!(rcs->data[0] & 0x08);
550 pow = rcs->data[0] & 0x07;
552 /* convert it in a table of servo values */
553 if (bypass == 0 && buf2servo(servo_rx.servos, rcs->data) < 0)
557 servo_rx.time = get_time_ms();
559 /* acknowledge received frame */
560 seq = rcs->data[0] >> 4;
561 rc_proto_send_ack(rc_proto_dstaddr, seq, pow);
563 /* copy values to spi */
564 if (rc_proto_flags & RC_PROTO_FLAGS_RX_COPY_SPI) {
565 spi_servo_set_bypass(bypass);
568 for (i = 0; i < N_SERVO; i++)
569 spi_servo_set(i, servo_rx.servos[i]);
575 /* receive a rc_proto message */
576 int rc_proto_rx(struct xbee_recv_hdr *recvframe, unsigned len)
578 unsigned int datalen;
579 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
581 if (len < sizeof(*recvframe))
584 datalen = len - sizeof(*recvframe);
585 if (datalen < sizeof(struct rc_proto_hdr))
588 /* other command types */
590 case RC_PROTO_HELLO: {
591 struct rc_proto_hello *rch =
592 (struct rc_proto_hello *) recvframe->data;
594 NOTICE(E_USER_XBEE, "recv hello len=%d", rch->datalen);
599 case RC_PROTO_ECHO_REQ: {
600 struct rc_proto_echo_req *rce =
601 (struct rc_proto_echo_req *) recvframe->data;
602 int8_t power = rce->power;
604 NOTICE(E_USER_XBEE, "recv echo len=%d", rce->datalen);
607 if (rc_proto_send_echo_ans(ntohll(recvframe->srcaddr),
608 rce->data, rce->datalen, power) < 0)
614 case RC_PROTO_ECHO_ANS: {
615 struct rc_proto_echo_ans *rce =
616 (struct rc_proto_echo_ans *) recvframe->data;
618 NOTICE(E_USER_XBEE, "recv echo_ans len=%d", rce->datalen);
623 /* received by the radio controller every ~500ms */
624 case RC_PROTO_POWER_PROBE: {
625 struct rc_proto_power_probe *rcpb =
626 (struct rc_proto_power_probe *) recvframe->data;
628 NOTICE(E_USER_XBEE, "recv power_probe");
630 if (datalen != sizeof(*rcpb))
633 if (rcpb->power_level >= MAX_POWER_LEVEL)
636 stats.power_probe_rx++;
637 /* ask the DB value to the xbee module */
638 rc_proto_rx_power_probe(rcpb->power_level);
643 /* received by the radio controller */
645 struct rc_proto_ack *rca =
646 (struct rc_proto_ack *) recvframe->data;
648 NOTICE(E_USER_XBEE, "recv ack, ack=%d", rca->seq);
653 /* received by the wing */
654 case RC_PROTO_SERVO: {
655 struct rc_proto_servo *rcs =
656 (struct rc_proto_servo *) recvframe->data;
658 NOTICE(E_USER_XBEE, "recv servo");
660 if (datalen != RC_PROTO_SERVO_LEN)
664 return rc_proto_rx_servo(rcs);
667 /* received by the radio controller */
668 case RC_PROTO_STATS: {
669 struct rc_proto_stats *rcs =
670 (struct rc_proto_stats *) recvframe->data;
672 NOTICE(E_USER_XBEE, "recv stats");
674 if (datalen != sizeof(*rcs) + sizeof(peer_stats))
678 memcpy(&peer_stats, rcs->stats, sizeof(peer_stats));
690 /* called by the scheduler, manage rc_proto periodical tasks */
691 static void rc_proto_cb(struct callout_mgr *cm, struct callout *tim, void *arg)
694 static uint16_t prev_stats_send;
695 static uint16_t prev_compute_pow;
696 static uint16_t prev_power_probe;
697 static uint8_t pow_probe;
702 /* send servo values if flags are enabled. The function will decide
703 * by itself if it's time to send or not */
704 rc_proto_send_servos();
706 /* send power probe periodically */
707 if (rc_proto_flags & RC_PROTO_FLAGS_TX_POW_PROBE) {
708 diff = t - prev_power_probe;
709 if (diff > AUTO_BYPASS_TIME_MS) {
713 rc_proto_send_power_probe(rc_proto_dstaddr, pow_probe);
714 prev_power_probe = t;
718 /* on wing, auto bypass servos if no commands since some time */
719 if (rc_proto_flags & RC_PROTO_FLAGS_RX_AUTOBYPASS) {
720 diff = t - servo_rx.time;
721 if (diff > AUTO_BYPASS_TIME_MS)
722 spi_servo_set_bypass(1);
725 /* send stats to peer every second */
726 diff = t - prev_compute_pow;
728 compute_best_power();
729 prev_compute_pow = t;
732 /* send stats to peer every second */
733 if (rc_proto_flags & RC_PROTO_FLAGS_TX_STATS) {
734 diff = t - prev_stats_send;
736 rc_proto_send_stats(rc_proto_dstaddr, get_best_power());
741 callout_schedule(cm, tim, 0);
744 void rc_proto_dump_stats(void)
746 printf_P(PSTR("rc_proto stats LOCAL\r\n"));
747 printf_P(PSTR(" hello_tx: %"PRIu32"\r\n"), stats.hello_tx);
748 printf_P(PSTR(" hello_rx: %"PRIu32"\r\n"), stats.hello_rx);
749 printf_P(PSTR(" echo_req_rx: %"PRIu32"\r\n"), stats.echo_req_rx);
750 printf_P(PSTR(" echo_req_tx: %"PRIu32"\r\n"), stats.echo_req_tx);
751 printf_P(PSTR(" echo_ans_rx: %"PRIu32"\r\n"), stats.echo_ans_rx);
752 printf_P(PSTR(" echo_ans_tx: %"PRIu32"\r\n"), stats.echo_ans_tx);
753 printf_P(PSTR(" power_probe_rx: %"PRIu32"\r\n"), stats.power_probe_rx);
754 printf_P(PSTR(" power_probe_tx: %"PRIu32"\r\n"), stats.power_probe_tx);
755 printf_P(PSTR(" ack_rx: %"PRIu32"\r\n"), stats.ack_rx);
756 printf_P(PSTR(" ack_tx: %"PRIu32"\r\n"), stats.ack_tx);
757 printf_P(PSTR(" servo_rx: %"PRIu32"\r\n"), stats.servo_rx);
758 printf_P(PSTR(" servo_tx: %"PRIu32"\r\n"), stats.servo_tx);
759 printf_P(PSTR(" stats_rx: %"PRIu32"\r\n"), stats.stats_rx);
760 printf_P(PSTR(" stats_tx: %"PRIu32"\r\n"), stats.stats_tx);
762 printf_P(PSTR("rc_proto stats PEER\r\n"));
763 printf_P(PSTR(" hello_tx: %"PRIu32"\r\n"), peer_stats.hello_tx);
764 printf_P(PSTR(" hello_rx: %"PRIu32"\r\n"), peer_stats.hello_rx);
765 printf_P(PSTR(" echo_req_rx: %"PRIu32"\r\n"), peer_stats.echo_req_rx);
766 printf_P(PSTR(" echo_req_tx: %"PRIu32"\r\n"), peer_stats.echo_req_tx);
767 printf_P(PSTR(" echo_ans_rx: %"PRIu32"\r\n"), peer_stats.echo_ans_rx);
768 printf_P(PSTR(" echo_ans_tx: %"PRIu32"\r\n"), peer_stats.echo_ans_tx);
769 printf_P(PSTR(" power_probe_rx: %"PRIu32"\r\n"), peer_stats.power_probe_rx);
770 printf_P(PSTR(" power_probe_tx: %"PRIu32"\r\n"), peer_stats.power_probe_tx);
771 printf_P(PSTR(" ack_rx: %"PRIu32"\r\n"), peer_stats.ack_rx);
772 printf_P(PSTR(" ack_tx: %"PRIu32"\r\n"), peer_stats.ack_tx);
773 printf_P(PSTR(" servo_rx: %"PRIu32"\r\n"), peer_stats.servo_rx);
774 printf_P(PSTR(" servo_tx: %"PRIu32"\r\n"), peer_stats.servo_tx);
775 printf_P(PSTR(" stats_rx: %"PRIu32"\r\n"), peer_stats.stats_rx);
776 printf_P(PSTR(" stats_tx: %"PRIu32"\r\n"), peer_stats.stats_tx);
779 void rc_proto_dump_servos(void)
783 printf_P(PSTR("servo rx\r\n"));
784 for (i = 0; i < N_SERVO; i++) {
785 printf_P(PSTR(" servo[%d] = %d\r\n"), i, servo_rx.servos[i]);
787 printf_P(PSTR("servo tx\r\n"));
788 printf_P(PSTR(" bypass=%d\r\n"), servo_tx.bypass);
789 printf_P(PSTR(" seq=%d\r\n"), servo_tx.seq);
790 printf_P(PSTR(" time=%d\r\n"), servo_tx.time);
791 for (i = 0; i < N_SERVO; i++) {
792 printf_P(PSTR(" servo[%d] = %d\r\n"), i, servo_tx.servos[i]);
796 void rc_proto_set_dstaddr(uint64_t addr)
801 rc_proto_dstaddr = addr;
805 uint64_t rc_proto_get_dstaddr(void)
811 addr = rc_proto_dstaddr;
816 void rc_proto_init(void)
818 callout_init(&rc_proto_timer, rc_proto_cb, NULL, XBEE_PRIO);
819 callout_schedule(&xbeeboard.intr_cm, &rc_proto_timer, 0);