xbee communication for UWB quadcopter project Originally by Greg Abdo Forking to reduce impact of interrupt by moving packetbuilder out of the interrupt and letting be handled in the main loop
Fork of com by
com.cpp@20:95244d4f47f6, 2014-10-22 (annotated)
- Committer:
- oprospero
- Date:
- Wed Oct 22 16:09:56 2014 +0000
- Revision:
- 20:95244d4f47f6
- Parent:
- 19:dc9d18037565
- Child:
- 21:117e01fc0373
currently debuging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oprospero | 0:26a151d2c6db | 1 | |
oprospero | 0:26a151d2c6db | 2 | #include "com.h" |
oprospero | 17:acef0fb07510 | 3 | |
oprospero | 18:19bcb2dbf3c8 | 4 | //#define DEBUG_COM |
oprospero | 13:2fb7b19dcd70 | 5 | #ifdef DEBUG_COM |
oprospero | 13:2fb7b19dcd70 | 6 | #define NL "\n\r" |
oprospero | 17:acef0fb07510 | 7 | #define PRINT(x) xbee.printf(x) //Serial.print(x) |
oprospero | 17:acef0fb07510 | 8 | #define PRINTF xbee.printf //Serial.print(x, y) |
oprospero | 16:89695823d407 | 9 | #define PRINTLN(x) PRINT(x);PRINT(NL) |
oprospero | 16:89695823d407 | 10 | #define START timerCom.start(); |
oprospero | 16:89695823d407 | 11 | #define STOP timerCom.stop() |
oprospero | 16:89695823d407 | 12 | #define RESET timerCom.reset() |
oprospero | 16:89695823d407 | 13 | #define READ timerCom.read_us() |
oprospero | 16:89695823d407 | 14 | #define GET(x) x = READ |
oprospero | 13:2fb7b19dcd70 | 15 | #else |
oprospero | 13:2fb7b19dcd70 | 16 | #define PRINT(x) |
oprospero | 13:2fb7b19dcd70 | 17 | #define PRINTF(x, y) |
oprospero | 13:2fb7b19dcd70 | 18 | #define PRINTLN(x) |
oprospero | 13:2fb7b19dcd70 | 19 | #define PRINTLNF(x, y) |
oprospero | 13:2fb7b19dcd70 | 20 | #define START |
oprospero | 13:2fb7b19dcd70 | 21 | #define STOP |
oprospero | 13:2fb7b19dcd70 | 22 | #define RESET |
oprospero | 13:2fb7b19dcd70 | 23 | #define READ |
oprospero | 13:2fb7b19dcd70 | 24 | #define GET(x) |
oprospero | 13:2fb7b19dcd70 | 25 | #endif |
oprospero | 0:26a151d2c6db | 26 | |
oprospero | 19:dc9d18037565 | 27 | com::com(PinName tx, PinName rx): xbee(NC,rx), xbtx(tx,NC) |
oprospero | 0:26a151d2c6db | 28 | { |
oprospero | 20:95244d4f47f6 | 29 | xbtx.baud(BAUDRATE); |
oprospero | 17:acef0fb07510 | 30 | xbee.baud(BAUDRATE); |
oprospero | 17:acef0fb07510 | 31 | xbee.attach(this,&com::callback); |
oprospero | 0:26a151d2c6db | 32 | } |
oprospero | 0:26a151d2c6db | 33 | |
oprospero | 17:acef0fb07510 | 34 | com::~com() |
oprospero | 17:acef0fb07510 | 35 | { |
oprospero | 0:26a151d2c6db | 36 | } |
oprospero | 0:26a151d2c6db | 37 | |
oprospero | 17:acef0fb07510 | 38 | void com::callback() |
oprospero | 4:f8e953201251 | 39 | { |
oprospero | 19:dc9d18037565 | 40 | __disable_irq(); |
oprospero | 17:acef0fb07510 | 41 | while(xbee.readable()) |
oprospero | 4:f8e953201251 | 42 | { |
oprospero | 17:acef0fb07510 | 43 | char data = xbee.getc(); |
oprospero | 20:95244d4f47f6 | 44 | xbtx.putc(data); |
oprospero | 17:acef0fb07510 | 45 | rxBuffer.add(data); |
oprospero | 4:f8e953201251 | 46 | } |
oprospero | 19:dc9d18037565 | 47 | __enable_irq(); |
oprospero | 4:f8e953201251 | 48 | } |
oprospero | 4:f8e953201251 | 49 | |
oprospero | 16:89695823d407 | 50 | bool com::isData() |
oprospero | 16:89695823d407 | 51 | { |
oprospero | 17:acef0fb07510 | 52 | static char packetIndex = 0; |
oprospero | 17:acef0fb07510 | 53 | static char pack_cmd = 0; |
oprospero | 17:acef0fb07510 | 54 | static char pack_value[2] = {0,0}; |
oprospero | 17:acef0fb07510 | 55 | static char pack_seq = 0; |
oprospero | 17:acef0fb07510 | 56 | static char pack_checksum = 0; |
oprospero | 16:89695823d407 | 57 | |
oprospero | 16:89695823d407 | 58 | __disable_irq(); |
oprospero | 17:acef0fb07510 | 59 | while ( !rxBuffer.isEmpty() ) |
oprospero | 16:89695823d407 | 60 | { |
oprospero | 17:acef0fb07510 | 61 | char data = rxBuffer.pop(); |
oprospero | 20:95244d4f47f6 | 62 | // __enable_irq(); |
oprospero | 17:acef0fb07510 | 63 | // PRINTF("d: %d \n\r", data); |
oprospero | 16:89695823d407 | 64 | switch (packetIndex) |
oprospero | 16:89695823d407 | 65 | { |
oprospero | 16:89695823d407 | 66 | case 0: |
oprospero | 16:89695823d407 | 67 | { |
oprospero | 17:acef0fb07510 | 68 | if ( data == 255 ) |
oprospero | 16:89695823d407 | 69 | packetIndex++; |
oprospero | 16:89695823d407 | 70 | break; |
oprospero | 16:89695823d407 | 71 | } |
oprospero | 16:89695823d407 | 72 | case 1: |
oprospero | 16:89695823d407 | 73 | { |
oprospero | 17:acef0fb07510 | 74 | if ( data < 13 ) |
oprospero | 16:89695823d407 | 75 | { |
oprospero | 17:acef0fb07510 | 76 | pack_cmd = data; |
oprospero | 16:89695823d407 | 77 | packetIndex++; |
oprospero | 16:89695823d407 | 78 | } |
oprospero | 16:89695823d407 | 79 | else |
oprospero | 16:89695823d407 | 80 | packetIndex = 0; |
oprospero | 16:89695823d407 | 81 | break; |
oprospero | 16:89695823d407 | 82 | } |
oprospero | 16:89695823d407 | 83 | case 2: |
oprospero | 16:89695823d407 | 84 | { |
oprospero | 17:acef0fb07510 | 85 | pack_value[1] = data; |
oprospero | 16:89695823d407 | 86 | packetIndex++; |
oprospero | 16:89695823d407 | 87 | break; |
oprospero | 16:89695823d407 | 88 | } |
oprospero | 16:89695823d407 | 89 | case 3: |
oprospero | 16:89695823d407 | 90 | { |
oprospero | 17:acef0fb07510 | 91 | if ( data < 128 ) |
oprospero | 16:89695823d407 | 92 | { |
oprospero | 17:acef0fb07510 | 93 | pack_value[0] = data; |
oprospero | 16:89695823d407 | 94 | packetIndex++; |
oprospero | 16:89695823d407 | 95 | } |
oprospero | 16:89695823d407 | 96 | else |
oprospero | 16:89695823d407 | 97 | packetIndex = 0; |
oprospero | 16:89695823d407 | 98 | break; |
oprospero | 16:89695823d407 | 99 | } |
oprospero | 16:89695823d407 | 100 | case 4: |
oprospero | 16:89695823d407 | 101 | { |
oprospero | 17:acef0fb07510 | 102 | pack_seq = data; |
oprospero | 16:89695823d407 | 103 | packetIndex++; |
oprospero | 16:89695823d407 | 104 | break; |
oprospero | 16:89695823d407 | 105 | } |
oprospero | 16:89695823d407 | 106 | case 5: |
oprospero | 16:89695823d407 | 107 | { |
oprospero | 16:89695823d407 | 108 | short temp = pack_value[0] + pack_cmd; |
oprospero | 17:acef0fb07510 | 109 | pack_checksum = data; |
oprospero | 16:89695823d407 | 110 | if ( temp == pack_checksum ) |
oprospero | 16:89695823d407 | 111 | { |
oprospero | 20:95244d4f47f6 | 112 | // __disable_irq(); |
oprospero | 16:89695823d407 | 113 | short * ackPacket = new short[2]; |
oprospero | 16:89695823d407 | 114 | ackPacket[0] = pack_cmd; |
oprospero | 16:89695823d407 | 115 | ackPacket[1] = pack_seq; |
oprospero | 17:acef0fb07510 | 116 | txBuffer.add( ackPacket ); // Ack the packet with sequence nuber. |
oprospero | 16:89695823d407 | 117 | |
oprospero | 16:89695823d407 | 118 | short * array = new short[2]; |
oprospero | 16:89695823d407 | 119 | array[0] = pack_cmd; |
oprospero | 16:89695823d407 | 120 | array[1] = pack_value[1] * 128 + pack_value[0]; |
oprospero | 17:acef0fb07510 | 121 | cmdBuffer.add( array ); |
oprospero | 20:95244d4f47f6 | 122 | // __enable_irq(); |
oprospero | 16:89695823d407 | 123 | } |
oprospero | 16:89695823d407 | 124 | |
oprospero | 16:89695823d407 | 125 | packetIndex = 0; |
oprospero | 16:89695823d407 | 126 | break; |
oprospero | 16:89695823d407 | 127 | } |
oprospero | 16:89695823d407 | 128 | default: |
oprospero | 16:89695823d407 | 129 | packetIndex = 0; |
oprospero | 16:89695823d407 | 130 | break; |
oprospero | 16:89695823d407 | 131 | } |
oprospero | 16:89695823d407 | 132 | |
oprospero | 20:95244d4f47f6 | 133 | // __disable_irq(); |
oprospero | 16:89695823d407 | 134 | |
oprospero | 16:89695823d407 | 135 | } |
oprospero | 16:89695823d407 | 136 | __enable_irq(); |
oprospero | 17:acef0fb07510 | 137 | return !cmdBuffer.isEmpty(); |
oprospero | 17:acef0fb07510 | 138 | } |
oprospero | 17:acef0fb07510 | 139 | |
oprospero | 17:acef0fb07510 | 140 | short * com::read() |
oprospero | 17:acef0fb07510 | 141 | { |
oprospero | 17:acef0fb07510 | 142 | if ( !cmdBuffer.isEmpty() ) |
oprospero | 17:acef0fb07510 | 143 | return cmdBuffer.pop(); |
oprospero | 17:acef0fb07510 | 144 | else |
oprospero | 17:acef0fb07510 | 145 | return NULL; |
oprospero | 16:89695823d407 | 146 | } |
oprospero | 16:89695823d407 | 147 | |
oprospero | 16:89695823d407 | 148 | |
oprospero | 17:acef0fb07510 | 149 | bool com::rdy2ack() |
oprospero | 17:acef0fb07510 | 150 | { |
oprospero | 17:acef0fb07510 | 151 | return !txBuffer.isEmpty(); |
oprospero | 17:acef0fb07510 | 152 | } |
oprospero | 17:acef0fb07510 | 153 | |
oprospero | 17:acef0fb07510 | 154 | void com::sendACK() |
oprospero | 0:26a151d2c6db | 155 | { |
oprospero | 19:dc9d18037565 | 156 | __disable_irq(); |
oprospero | 20:95244d4f47f6 | 157 | if( !txBuffer.isEmpty() && xbtx.writeable() ) |
oprospero | 17:acef0fb07510 | 158 | { |
oprospero | 17:acef0fb07510 | 159 | short * pkt = txBuffer.pop(); |
oprospero | 17:acef0fb07510 | 160 | write(pkt[0],pkt[1]); //may need to disable interrupt |
oprospero | 17:acef0fb07510 | 161 | delete[] pkt; |
oprospero | 17:acef0fb07510 | 162 | |
oprospero | 17:acef0fb07510 | 163 | } |
oprospero | 19:dc9d18037565 | 164 | __enable_irq(); |
oprospero | 0:26a151d2c6db | 165 | } |
oprospero | 17:acef0fb07510 | 166 | |
oprospero | 17:acef0fb07510 | 167 | void com::write( short command, short seq ) |
oprospero | 17:acef0fb07510 | 168 | { |
oprospero | 19:dc9d18037565 | 169 | xbtx.putc( 255 ); // End of message. |
oprospero | 19:dc9d18037565 | 170 | xbtx.putc( (char)command ); // Command |
oprospero | 19:dc9d18037565 | 171 | xbtx.putc( (char) seq); // Second 8 bits in array. |
oprospero | 19:dc9d18037565 | 172 | xbtx.putc( (char) command + seq ); // Checksum array[0] + array[1]. |
oprospero | 17:acef0fb07510 | 173 | } |
oprospero | 17:acef0fb07510 | 174 | |
oprospero | 19:dc9d18037565 | 175 | void com::print(char* str) |
oprospero | 19:dc9d18037565 | 176 | { |
oprospero | 19:dc9d18037565 | 177 | __disable_irq(); |
oprospero | 19:dc9d18037565 | 178 | xbee.printf(str); |
oprospero | 19:dc9d18037565 | 179 | __enable_irq(); |
oprospero | 19:dc9d18037565 | 180 | } |