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 Prosper Van

Committer:
oprospero
Date:
Mon Oct 06 04:59:20 2014 +0000
Revision:
17:acef0fb07510
Parent:
16:89695823d407
Child:
18:19bcb2dbf3c8
Working ; 255

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oprospero 0:26a151d2c6db 1
oprospero 0:26a151d2c6db 2 #include "com.h"
oprospero 17:acef0fb07510 3
oprospero 17:acef0fb07510 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 17:acef0fb07510 27 com::com(PinName tx, PinName rx): xbee(tx,rx)
oprospero 0:26a151d2c6db 28 {
oprospero 17:acef0fb07510 29 xbee.baud(BAUDRATE);
oprospero 17:acef0fb07510 30 xbee.attach(this,&com::callback);
oprospero 0:26a151d2c6db 31 }
oprospero 0:26a151d2c6db 32
oprospero 17:acef0fb07510 33 com::~com()
oprospero 17:acef0fb07510 34 {
oprospero 0:26a151d2c6db 35 }
oprospero 0:26a151d2c6db 36
oprospero 17:acef0fb07510 37 void com::callback()
oprospero 4:f8e953201251 38 {
oprospero 17:acef0fb07510 39 while(xbee.readable())
oprospero 4:f8e953201251 40 {
oprospero 17:acef0fb07510 41 char data = xbee.getc();
oprospero 17:acef0fb07510 42 // xbee.putc(data);
oprospero 17:acef0fb07510 43 rxBuffer.add(data);
oprospero 4:f8e953201251 44 }
oprospero 4:f8e953201251 45 }
oprospero 4:f8e953201251 46
oprospero 16:89695823d407 47 bool com::isData()
oprospero 16:89695823d407 48 {
oprospero 17:acef0fb07510 49 static char packetIndex = 0;
oprospero 17:acef0fb07510 50 static char pack_cmd = 0;
oprospero 17:acef0fb07510 51 static char pack_value[2] = {0,0};
oprospero 17:acef0fb07510 52 static char pack_seq = 0;
oprospero 17:acef0fb07510 53 static char pack_checksum = 0;
oprospero 16:89695823d407 54
oprospero 16:89695823d407 55 __disable_irq();
oprospero 17:acef0fb07510 56 while ( !rxBuffer.isEmpty() )
oprospero 16:89695823d407 57 {
oprospero 17:acef0fb07510 58 char data = rxBuffer.pop();
oprospero 16:89695823d407 59 __enable_irq();
oprospero 17:acef0fb07510 60 // PRINTF("d: %d \n\r", data);
oprospero 16:89695823d407 61 switch (packetIndex)
oprospero 16:89695823d407 62 {
oprospero 16:89695823d407 63 case 0:
oprospero 16:89695823d407 64 {
oprospero 17:acef0fb07510 65 if ( data == 255 )
oprospero 16:89695823d407 66 packetIndex++;
oprospero 16:89695823d407 67 break;
oprospero 16:89695823d407 68 }
oprospero 16:89695823d407 69 case 1:
oprospero 16:89695823d407 70 {
oprospero 17:acef0fb07510 71 if ( data < 13 )
oprospero 16:89695823d407 72 {
oprospero 17:acef0fb07510 73 pack_cmd = data;
oprospero 16:89695823d407 74 packetIndex++;
oprospero 16:89695823d407 75 }
oprospero 16:89695823d407 76 else
oprospero 16:89695823d407 77 packetIndex = 0;
oprospero 16:89695823d407 78 break;
oprospero 16:89695823d407 79 }
oprospero 16:89695823d407 80 case 2:
oprospero 16:89695823d407 81 {
oprospero 17:acef0fb07510 82 pack_value[1] = data;
oprospero 16:89695823d407 83 packetIndex++;
oprospero 16:89695823d407 84 break;
oprospero 16:89695823d407 85 }
oprospero 16:89695823d407 86 case 3:
oprospero 16:89695823d407 87 {
oprospero 17:acef0fb07510 88 if ( data < 128 )
oprospero 16:89695823d407 89 {
oprospero 17:acef0fb07510 90 pack_value[0] = data;
oprospero 16:89695823d407 91 packetIndex++;
oprospero 16:89695823d407 92 }
oprospero 16:89695823d407 93 else
oprospero 16:89695823d407 94 packetIndex = 0;
oprospero 16:89695823d407 95 break;
oprospero 16:89695823d407 96 }
oprospero 16:89695823d407 97 case 4:
oprospero 16:89695823d407 98 {
oprospero 17:acef0fb07510 99 pack_seq = data;
oprospero 16:89695823d407 100 packetIndex++;
oprospero 16:89695823d407 101 break;
oprospero 16:89695823d407 102 }
oprospero 16:89695823d407 103 case 5:
oprospero 16:89695823d407 104 {
oprospero 16:89695823d407 105 short temp = pack_value[0] + pack_cmd;
oprospero 17:acef0fb07510 106 pack_checksum = data;
oprospero 16:89695823d407 107 if ( temp == pack_checksum )
oprospero 16:89695823d407 108 {
oprospero 16:89695823d407 109 short * ackPacket = new short[2];
oprospero 16:89695823d407 110 ackPacket[0] = pack_cmd;
oprospero 16:89695823d407 111 ackPacket[1] = pack_seq;
oprospero 17:acef0fb07510 112 txBuffer.add( ackPacket ); // Ack the packet with sequence nuber.
oprospero 16:89695823d407 113
oprospero 16:89695823d407 114 short * array = new short[2];
oprospero 16:89695823d407 115 array[0] = pack_cmd;
oprospero 16:89695823d407 116 array[1] = pack_value[1] * 128 + pack_value[0];
oprospero 17:acef0fb07510 117 cmdBuffer.add( array );
oprospero 16:89695823d407 118 }
oprospero 16:89695823d407 119
oprospero 16:89695823d407 120 packetIndex = 0;
oprospero 16:89695823d407 121 break;
oprospero 16:89695823d407 122 }
oprospero 16:89695823d407 123 default:
oprospero 16:89695823d407 124 packetIndex = 0;
oprospero 16:89695823d407 125 break;
oprospero 16:89695823d407 126 }
oprospero 16:89695823d407 127
oprospero 16:89695823d407 128 __disable_irq();
oprospero 16:89695823d407 129
oprospero 16:89695823d407 130 }
oprospero 16:89695823d407 131 __enable_irq();
oprospero 17:acef0fb07510 132 return !cmdBuffer.isEmpty();
oprospero 17:acef0fb07510 133 }
oprospero 17:acef0fb07510 134
oprospero 17:acef0fb07510 135 short * com::read()
oprospero 17:acef0fb07510 136 {
oprospero 17:acef0fb07510 137 if ( !cmdBuffer.isEmpty() )
oprospero 17:acef0fb07510 138 return cmdBuffer.pop();
oprospero 17:acef0fb07510 139 else
oprospero 17:acef0fb07510 140 return NULL;
oprospero 16:89695823d407 141 }
oprospero 16:89695823d407 142
oprospero 16:89695823d407 143
oprospero 17:acef0fb07510 144 bool com::rdy2ack()
oprospero 17:acef0fb07510 145 {
oprospero 17:acef0fb07510 146 return !txBuffer.isEmpty();
oprospero 17:acef0fb07510 147 }
oprospero 17:acef0fb07510 148
oprospero 17:acef0fb07510 149 void com::sendACK()
oprospero 0:26a151d2c6db 150 {
oprospero 17:acef0fb07510 151 if( !txBuffer.isEmpty())
oprospero 17:acef0fb07510 152 {
oprospero 17:acef0fb07510 153 short * pkt = txBuffer.pop();
oprospero 17:acef0fb07510 154 __disable_irq();
oprospero 17:acef0fb07510 155 write(pkt[0],pkt[1]); //may need to disable interrupt
oprospero 17:acef0fb07510 156 __enable_irq();
oprospero 17:acef0fb07510 157 delete[] pkt;
oprospero 17:acef0fb07510 158
oprospero 17:acef0fb07510 159 }
oprospero 0:26a151d2c6db 160 }
oprospero 17:acef0fb07510 161
oprospero 17:acef0fb07510 162 void com::write( short command, short seq )
oprospero 17:acef0fb07510 163 {
oprospero 17:acef0fb07510 164 xbee.putc( 255 ); // End of message.
oprospero 17:acef0fb07510 165 xbee.putc( (char)command ); // Command
oprospero 17:acef0fb07510 166 xbee.putc( (char) seq); // Second 8 bits in array.
oprospero 17:acef0fb07510 167 xbee.putc( (char) command + seq ); // Checksum array[0] + array[1].
oprospero 17:acef0fb07510 168 }
oprospero 17:acef0fb07510 169