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:
Sat Apr 26 02:38:08 2014 +0000
Revision:
12:bf4642578682
Parent:
11:d1f488302d06
Child:
13:2fb7b19dcd70
initial changed from orginal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oprospero 0:26a151d2c6db 1 /****************************** com.cpp **********************************/
oprospero 0:26a151d2c6db 2 /* Version: 1.0 */
oprospero 0:26a151d2c6db 3 /* Last Updated: June 1, 2013 */
oprospero 0:26a151d2c6db 4 /* */
oprospero 0:26a151d2c6db 5 /* The com class implements reliable data transfer between two nodes */
oprospero 0:26a151d2c6db 6 /*using a checksum and a sequence number for guaranteed message delivery */
oprospero 0:26a151d2c6db 7 /*over an xbee modem connected to the passed in tx and rx pins. Messages */
oprospero 0:26a151d2c6db 8 /*are received and placed in the rxBuffer to be read when convenient. */
oprospero 0:26a151d2c6db 9 /*Messages are encoded by sending a byte with the value of the command */
oprospero 0:26a151d2c6db 10 /*then and int of the command. */
oprospero 0:26a151d2c6db 11 /* */
oprospero 0:26a151d2c6db 12 /* Commands: 0 -> Ack, does not get placed in rxQueue. */
oprospero 0:26a151d2c6db 13 /* 1 -> Throttle */
oprospero 0:26a151d2c6db 14 /* 2 -> Pitch */
oprospero 0:26a151d2c6db 15 /* 3 -> Roll */
oprospero 0:26a151d2c6db 16 /* 4 -> Yaw */
oprospero 0:26a151d2c6db 17 /*************************************************************************/
oprospero 0:26a151d2c6db 18
oprospero 0:26a151d2c6db 19 #include "mbed.h"
oprospero 0:26a151d2c6db 20 #include "com.h"
oprospero 0:26a151d2c6db 21
oprospero 1:4f53de75bc96 22
oprospero 0:26a151d2c6db 23 /*********************** com( PinName, PinName ) *************************/
oprospero 0:26a151d2c6db 24 /* */
oprospero 0:26a151d2c6db 25 /*************************************************************************/
oprospero 0:26a151d2c6db 26
oprospero 2:bd1621102cad 27 com::com( PinName tx, PinName rx , PinName rssipin) : xbeeTx( tx, NC), xbeeRx( NC, rx), rssi(rssipin)
oprospero 0:26a151d2c6db 28 {
oprospero 12:bf4642578682 29 index1 = 0; // How many bytes are in the buffer.
oprospero 12:bf4642578682 30 index2 = 0; // How many bytes are in the buffer.
oprospero 12:bf4642578682 31 pindex = 0; // How many bytes are in the buffer.
oprospero 12:bf4642578682 32 rdy2build = false; // Xbee is in transparent mode.
oprospero 1:4f53de75bc96 33 xbeeTx.baud(BAUDRATE); // Setup the serial baud rate.
oprospero 0:26a151d2c6db 34 xbeeRx.baud(BAUDRATE); // Setup the serial baud rate.
oprospero 5:7cdf299ea7a4 35 txBuffer = new queue();
oprospero 0:26a151d2c6db 36 signalStrength = 0;
oprospero 0:26a151d2c6db 37 xbeeRx.attach( this, &com::callback ); // Set callback as the interrupt handler.
oprospero 1:4f53de75bc96 38 #ifdef DEBUG
oprospero 0:26a151d2c6db 39 xbeeTx.printf("Communication.....Done\n\r");
oprospero 1:4f53de75bc96 40 #endif
oprospero 0:26a151d2c6db 41 }
oprospero 0:26a151d2c6db 42
oprospero 0:26a151d2c6db 43 /************************* bool isData() ********************************/
oprospero 0:26a151d2c6db 44 /* */
oprospero 0:26a151d2c6db 45 /*************************************************************************/
oprospero 0:26a151d2c6db 46
oprospero 0:26a151d2c6db 47 bool com::isData()
oprospero 0:26a151d2c6db 48 {
oprospero 2:bd1621102cad 49
oprospero 12:bf4642578682 50 return rdy2build;
oprospero 0:26a151d2c6db 51 }
oprospero 0:26a151d2c6db 52
oprospero 0:26a151d2c6db 53 /************************ void write( char ) *****************************/
oprospero 0:26a151d2c6db 54 /* Write a packet out the xbee com port. */
oprospero 0:26a151d2c6db 55 /* */
oprospero 0:26a151d2c6db 56 /* Data format byte[] */
oprospero 0:26a151d2c6db 57 /* byte[0] = command. */
oprospero 0:26a151d2c6db 58 /* byte[1] = upper 8 bits of value. */
oprospero 0:26a151d2c6db 59 /* byte[2] = lower 8 bits of value. */
oprospero 0:26a151d2c6db 60 /* byte[3] = Checksum byte[0] + byte[2]. */
oprospero 0:26a151d2c6db 61 /* byte[4] = Sequence Number. */
oprospero 0:26a151d2c6db 62 /* byte[5] = 255 End of message. */
oprospero 0:26a151d2c6db 63 /*************************************************************************/
oprospero 0:26a151d2c6db 64
oprospero 0:26a151d2c6db 65 void com::write( short command, short value )
oprospero 12:bf4642578682 66 {
oprospero 12:bf4642578682 67 short lvalue = (value % 128);
oprospero 12:bf4642578682 68 xbeeTx.putc( (char)command ); // Command
oprospero 12:bf4642578682 69 xbeeTx.putc( (char) value / 128 ); // First 8 bits in array.
oprospero 12:bf4642578682 70 xbeeTx.putc( (char) lvalue); // Second 8 bits in array.
oprospero 12:bf4642578682 71 xbeeTx.putc( command + lvalue ); // Checksum array[0] + array[1].
oprospero 12:bf4642578682 72 xbeeTx.putc( (char)value ); // Sequence number.
oprospero 12:bf4642578682 73 xbeeTx.putc( 255 ); // End of message.
oprospero 0:26a151d2c6db 74 }
oprospero 0:26a151d2c6db 75
oprospero 5:7cdf299ea7a4 76 /*************************** char ackCheck() ********************************/
oprospero 0:26a151d2c6db 77 /* */
oprospero 0:26a151d2c6db 78 /*************************************************************************/
oprospero 0:26a151d2c6db 79
oprospero 4:f8e953201251 80 void com::ackCheck()
oprospero 4:f8e953201251 81 {
oprospero 5:7cdf299ea7a4 82 if( !txBuffer->isEmpty() && xbeeTx.writeable())
oprospero 4:f8e953201251 83 {
oprospero 11:d1f488302d06 84 __disable_irq();
oprospero 5:7cdf299ea7a4 85 short * pkt = txBuffer->pop();
oprospero 5:7cdf299ea7a4 86 write(pkt[0],pkt[1]); //may need to disable interrupt
oprospero 5:7cdf299ea7a4 87 delete[] pkt;
oprospero 11:d1f488302d06 88 __enable_irq();
oprospero 4:f8e953201251 89 }
oprospero 4:f8e953201251 90 }
oprospero 4:f8e953201251 91
oprospero 5:7cdf299ea7a4 92 bool com::rdy2ack()
oprospero 5:7cdf299ea7a4 93 {
oprospero 5:7cdf299ea7a4 94 return !txBuffer->isEmpty();
oprospero 5:7cdf299ea7a4 95 }
oprospero 4:f8e953201251 96
oprospero 4:f8e953201251 97
oprospero 0:26a151d2c6db 98
oprospero 0:26a151d2c6db 99 /************************ void callback() ********************************/
oprospero 0:26a151d2c6db 100 /* */
oprospero 0:26a151d2c6db 101 /*************************************************************************/
oprospero 0:26a151d2c6db 102
oprospero 0:26a151d2c6db 103 void com::callback()
oprospero 0:26a151d2c6db 104 {
oprospero 4:f8e953201251 105 __disable_irq();
oprospero 0:26a151d2c6db 106 while( xbeeRx.readable() )
oprospero 0:26a151d2c6db 107 {
oprospero 0:26a151d2c6db 108 char data = xbeeRx.getc();
oprospero 12:bf4642578682 109
oprospero 12:bf4642578682 110 if (isA1)
oprospero 12:bf4642578682 111 {
oprospero 12:bf4642578682 112 if ( data == 255 && index1 > 4 )
oprospero 12:bf4642578682 113 {
oprospero 12:bf4642578682 114 rdy2build = true;
oprospero 12:bf4642578682 115 pindex = index1;
oprospero 12:bf4642578682 116 index1 = 0;
oprospero 12:bf4642578682 117 isA1 = false;
oprospero 12:bf4642578682 118 }
oprospero 12:bf4642578682 119 else if ( index1 < BUFFERSIZE )
oprospero 12:bf4642578682 120 {
oprospero 12:bf4642578682 121 buffer1[index1++] = data;
oprospero 12:bf4642578682 122 }
oprospero 12:bf4642578682 123 }
oprospero 12:bf4642578682 124 else
oprospero 12:bf4642578682 125 {
oprospero 12:bf4642578682 126 if ( data == 255 && index2 > 4 )
oprospero 12:bf4642578682 127 {
oprospero 12:bf4642578682 128 rdy2build = true;
oprospero 12:bf4642578682 129 pindex = index2;
oprospero 12:bf4642578682 130 index2 = 0;
oprospero 12:bf4642578682 131 isA1 = true;
oprospero 12:bf4642578682 132 }
oprospero 12:bf4642578682 133 else if ( index2 < BUFFERSIZE )
oprospero 12:bf4642578682 134 {
oprospero 12:bf4642578682 135 buffer2[index2++] = data;
oprospero 12:bf4642578682 136 }
oprospero 12:bf4642578682 137 }
oprospero 0:26a151d2c6db 138 }
oprospero 4:f8e953201251 139 __enable_irq();
oprospero 0:26a151d2c6db 140 }
oprospero 0:26a151d2c6db 141
oprospero 2:bd1621102cad 142
oprospero 0:26a151d2c6db 143 /********************** void packetBuilder() *****************************/
oprospero 0:26a151d2c6db 144 /* Creates a packet from the buffered data and places it in the rxBuffer */
oprospero 0:26a151d2c6db 145 /* queue to be read whenever convenient. Max value of +/- 8063. */
oprospero 0:26a151d2c6db 146 /*************************************************************************/
oprospero 0:26a151d2c6db 147
oprospero 12:bf4642578682 148 short * com::read()
oprospero 0:26a151d2c6db 149 {
oprospero 12:bf4642578682 150 if (rdy2build)
oprospero 0:26a151d2c6db 151 {
oprospero 12:bf4642578682 152 char * commandData = new char[pindex];
oprospero 12:bf4642578682 153 if (!isA1)
oprospero 12:bf4642578682 154 {
oprospero 12:bf4642578682 155 commandData[4] = buffer1[--pindex]; // Sequence Number.
oprospero 12:bf4642578682 156 commandData[3] = buffer1[--pindex]; // CheckSum value.
oprospero 12:bf4642578682 157 commandData[2] = buffer1[--pindex]; // Second 7 bits.
oprospero 12:bf4642578682 158 commandData[1] = buffer1[--pindex]; // Fisrt 7 bits.
oprospero 12:bf4642578682 159 commandData[0] = buffer1[--pindex]; // Command.
oprospero 12:bf4642578682 160 }
oprospero 12:bf4642578682 161 else
oprospero 12:bf4642578682 162 {
oprospero 12:bf4642578682 163 commandData[4] = buffer2[--pindex]; // Sequence Number.
oprospero 12:bf4642578682 164 commandData[3] = buffer2[--pindex]; // CheckSum value.
oprospero 12:bf4642578682 165 commandData[2] = buffer2[--pindex]; // Second 7 bits.
oprospero 12:bf4642578682 166 commandData[1] = buffer2[--pindex]; // Fisrt 7 bits.
oprospero 12:bf4642578682 167 commandData[0] = buffer2[--pindex]; // Command.
oprospero 12:bf4642578682 168 }
oprospero 0:26a151d2c6db 169
oprospero 12:bf4642578682 170
oprospero 12:bf4642578682 171 if( commandData[0] + commandData[2] == commandData[3] ) // Validate checksum.
oprospero 4:f8e953201251 172 {
oprospero 12:bf4642578682 173 short * array = new short[2];
oprospero 12:bf4642578682 174 array[0] = (short)commandData[0];
oprospero 12:bf4642578682 175
oprospero 12:bf4642578682 176 short value = (short)(commandData[1] * 128 + commandData[2]);
oprospero 12:bf4642578682 177
oprospero 12:bf4642578682 178 if( value > 8062 )
oprospero 12:bf4642578682 179 value = (short)value + 57344;
oprospero 12:bf4642578682 180
oprospero 12:bf4642578682 181 array[1] = value;
oprospero 12:bf4642578682 182 if ( commandData[0] != 0)
oprospero 12:bf4642578682 183 {
oprospero 12:bf4642578682 184 short * ackPacket = new short[2];
oprospero 12:bf4642578682 185 ackPacket[0] = commandData[0];
oprospero 12:bf4642578682 186 ackPacket[1] = commandData[4];
oprospero 12:bf4642578682 187 txBuffer->add( ackPacket ); // Ack the packet with sequence nuber.
oprospero 12:bf4642578682 188 }
oprospero 12:bf4642578682 189
oprospero 12:bf4642578682 190 delete[] commandData;
oprospero 12:bf4642578682 191 return array;
oprospero 12:bf4642578682 192 }
oprospero 12:bf4642578682 193 delete[] commandData;
oprospero 12:bf4642578682 194 }
oprospero 12:bf4642578682 195 return NULL;
oprospero 0:26a151d2c6db 196 }
oprospero 12:bf4642578682 197
oprospero 0:26a151d2c6db 198
oprospero 0:26a151d2c6db 199
oprospero 0:26a151d2c6db 200 /********************** bool isSignalGood() ******************************/
oprospero 0:26a151d2c6db 201 /* For future use */
oprospero 0:26a151d2c6db 202 /*************************************************************************/
oprospero 0:26a151d2c6db 203 bool com::isSignalGood()
oprospero 0:26a151d2c6db 204 {
oprospero 0:26a151d2c6db 205 signalStrength = rssi.dutycycle();
oprospero 0:26a151d2c6db 206 if (signalStrength > RSSI_THRES) return true;
oprospero 0:26a151d2c6db 207 else return false;
oprospero 0:26a151d2c6db 208 }