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>
37 #include <scheduler.h>
38 #include <clock_time.h>
49 struct rc_proto_power_levels {
53 static struct rc_proto_power_levels power_levels[MAX_POWER_LEVEL];
55 /* update power level when we receive the answer from DB */
56 static int update_power_level(void *frame, unsigned len, void *arg)
58 struct xbee_atresp_hdr *atresp = (struct xbee_atresp_hdr *)frame;
59 int level = (intptr_t)arg;
62 /* XXX check if this test is correct */
63 if (len < sizeof(struct xbee_atresp_hdr) + sizeof(uint8_t)) {
69 power_levels[level].power_db = db;
70 power_levels[level].ttl = 2;
74 /* when we receive a power probe, ask the DB value to the xbee */
75 void rc_proto_rx_power_probe(int power_level)
78 xbeeapp_send_atcmd("DB", NULL, 0, 0, update_power_level, NULL);
81 /* send a hello message */
82 // XXX iovec for xbee ?
83 int8_t rc_proto_send_hello(uint64_t addr, void *data, uint8_t data_len)
86 struct rc_proto_echo_req hdr;
87 char buf[XBEE_MAX_FRAME_LEN - sizeof(struct rc_proto_echo_req)];
90 if (data_len > sizeof(frame.buf))
93 frame.hdr.type = RC_PROTO_HELLO;
94 frame.hdr.datalen = data_len;
95 memcpy(frame.buf, data, data_len);
96 return xbeeapp_send_msg(addr, &frame,
97 data_len + sizeof(struct rc_proto_echo_req), 1);
103 #define SERVO_NBITS 10
104 uint16_t servos[N_SERVO] = { 0x123, 0x234, 0x321, 0x123, 0x234, 0x321 };
106 int8_t servo2buf(uint8_t *buf, uint8_t len, uint8_t servo_mask,
107 uint8_t pow, uint8_t seq)
111 uint8_t servo_bits = 0;
119 buf[i++] = servo_mask;
120 buf[i++] = ((seq & 0x1f) << 3) | (pow & 0x7);
123 for (num = 0; num < N_SERVO; num++) {
124 if (!(servo_mask & (1 << num)))
126 servo_val = servos[num];
127 servo_bits = SERVO_NBITS;
129 tmp = (servo_val >> (servo_bits - remain_bits));
130 tmp &= ((1 << remain_bits) - 1);
135 servo_bits = 10 - remain_bits;
136 tmp = servo_val & ((1 << servo_bits) - 1);
137 tmp <<= (8 - servo_bits);
142 if (servo_bits == 8) {
147 remain_bits = 8 - servo_bits;
150 if (remain_bits != 8)
156 int8_t buf2servo(uint8_t *buf, uint8_t len)
158 uint8_t mask, count = 0;
172 for (num = 0; num < N_SERVO; num++) {
177 case 1: if (len != 4) return -1; break;
178 case 2: if (len != 5) return -1; break;
179 case 3: if (len != 6) return -1; break;
180 case 4: if (len != 7) return -1; break;
181 case 5: if (len != 9) return -1; break;
182 case 6: if (len != 10) return -1; break;
186 for (num = 0; ((1<<num) & mask) == 0; num++) {
193 val |= (buf[3] >> 6);
195 for (num++; ((1<<num) & mask) == 0; num++) {
202 val |= (buf[4] >> 4);
204 for (num++; ((1<<num) & mask) == 0; num++) {
211 val |= (buf[5] >> 2);
213 for (num++; ((1<<num) & mask) == 0; num++) {
222 for (num++; ((1<<num) & mask) == 0; num++) {
229 val |= (buf[8] >> 6);
231 for (num++; ((1<<num) & mask) == 0; num++) {
238 val |= (buf[9] >> 4);