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:
Wed Jan 08 07:32:30 2014 +0000
Revision:
0:26a151d2c6db
Child:
1:4f53de75bc96
Create two serial instance for Rx and Tx

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 0:26a151d2c6db 22 /*********************** com( PinName, PinName ) *************************/
oprospero 0:26a151d2c6db 23 /* */
oprospero 0:26a151d2c6db 24 /*************************************************************************/
oprospero 0:26a151d2c6db 25
oprospero 0:26a151d2c6db 26 com::com( PinName tx, PinName rx , PinName rssipin) : xbeeRx( tx, rx), xbeeTx( tx, rx), rssi(rssipin)
oprospero 0:26a151d2c6db 27 {
oprospero 0:26a151d2c6db 28 bLength = 0; // How many bytes are in the buffer.
oprospero 0:26a151d2c6db 29 xbeeRx.baud(BAUDRATE); // Setup the serial baud rate.
oprospero 0:26a151d2c6db 30 xbeeTx.baud(BAUDRATE); // Setup the serial baud rate.
oprospero 0:26a151d2c6db 31 rxBuffer = new queue(); // Point to the rxQueue.
oprospero 0:26a151d2c6db 32 signalStrength = 0;
oprospero 0:26a151d2c6db 33 xbeeRx.attach( this, &com::callback ); // Set callback as the interrupt handler.
oprospero 0:26a151d2c6db 34 xbeeTx.printf("Communication.....Done\n\r");
oprospero 0:26a151d2c6db 35 }
oprospero 0:26a151d2c6db 36
oprospero 0:26a151d2c6db 37 /************************* bool isData() ********************************/
oprospero 0:26a151d2c6db 38 /* */
oprospero 0:26a151d2c6db 39 /*************************************************************************/
oprospero 0:26a151d2c6db 40
oprospero 0:26a151d2c6db 41 bool com::isData()
oprospero 0:26a151d2c6db 42 {
oprospero 0:26a151d2c6db 43 if( rxBuffer->isEmpty() )
oprospero 0:26a151d2c6db 44 return false;
oprospero 0:26a151d2c6db 45
oprospero 0:26a151d2c6db 46 return true;
oprospero 0:26a151d2c6db 47 }
oprospero 0:26a151d2c6db 48
oprospero 0:26a151d2c6db 49 /************************ void write( char ) *****************************/
oprospero 0:26a151d2c6db 50 /* Write a packet out the xbee com port. */
oprospero 0:26a151d2c6db 51 /* */
oprospero 0:26a151d2c6db 52 /* Data format byte[] */
oprospero 0:26a151d2c6db 53 /* byte[0] = command. */
oprospero 0:26a151d2c6db 54 /* byte[1] = upper 8 bits of value. */
oprospero 0:26a151d2c6db 55 /* byte[2] = lower 8 bits of value. */
oprospero 0:26a151d2c6db 56 /* byte[3] = Checksum byte[0] + byte[2]. */
oprospero 0:26a151d2c6db 57 /* byte[4] = Sequence Number. */
oprospero 0:26a151d2c6db 58 /* byte[5] = 255 End of message. */
oprospero 0:26a151d2c6db 59 /*************************************************************************/
oprospero 0:26a151d2c6db 60
oprospero 0:26a151d2c6db 61 void com::write( short command, short value )
oprospero 0:26a151d2c6db 62 {
oprospero 0:26a151d2c6db 63 xbeeTx.putc( (char)command ); // Command
oprospero 0:26a151d2c6db 64 xbeeTx.putc( 0 ); // First 8 bits in array.
oprospero 0:26a151d2c6db 65 xbeeTx.putc( (char)value ); // Second 8 bits in array.
oprospero 0:26a151d2c6db 66 xbeeTx.putc( command + value ); // Checksum array[0] + array[1].
oprospero 0:26a151d2c6db 67 xbeeTx.putc( 0 ); // Sequence number.
oprospero 0:26a151d2c6db 68 xbeeTx.putc( 255 ); // End of message.
oprospero 0:26a151d2c6db 69 }
oprospero 0:26a151d2c6db 70
oprospero 0:26a151d2c6db 71 /*************************** char read() ********************************/
oprospero 0:26a151d2c6db 72 /* */
oprospero 0:26a151d2c6db 73 /*************************************************************************/
oprospero 0:26a151d2c6db 74
oprospero 0:26a151d2c6db 75 short * com::read()
oprospero 0:26a151d2c6db 76 {
oprospero 0:26a151d2c6db 77 if( !rxBuffer->isEmpty())
oprospero 0:26a151d2c6db 78 return rxBuffer->pop();
oprospero 0:26a151d2c6db 79
oprospero 0:26a151d2c6db 80 return NULL;
oprospero 0:26a151d2c6db 81 }
oprospero 0:26a151d2c6db 82
oprospero 0:26a151d2c6db 83 /************************ void callback() ********************************/
oprospero 0:26a151d2c6db 84 /* */
oprospero 0:26a151d2c6db 85 /*************************************************************************/
oprospero 0:26a151d2c6db 86
oprospero 0:26a151d2c6db 87 void com::callback()
oprospero 0:26a151d2c6db 88 {
oprospero 0:26a151d2c6db 89 while( xbeeRx.readable() )
oprospero 0:26a151d2c6db 90 {
oprospero 0:26a151d2c6db 91 char data = xbeeRx.getc();
oprospero 0:26a151d2c6db 92 // xbeeTx.printf("data: %d\n\r", data);
oprospero 0:26a151d2c6db 93 if( bLength++ < BUFFERSIZE )
oprospero 0:26a151d2c6db 94 buffer[bLength] = data;
oprospero 0:26a151d2c6db 95
oprospero 0:26a151d2c6db 96 if( data == 255 )
oprospero 0:26a151d2c6db 97 packetBuilder();
oprospero 0:26a151d2c6db 98 }
oprospero 0:26a151d2c6db 99 }
oprospero 0:26a151d2c6db 100
oprospero 0:26a151d2c6db 101 /********************** void packetBuilder() *****************************/
oprospero 0:26a151d2c6db 102 /* Creates a packet from the buffered data and places it in the rxBuffer */
oprospero 0:26a151d2c6db 103 /* queue to be read whenever convenient. Max value of +/- 8063. */
oprospero 0:26a151d2c6db 104 /*************************************************************************/
oprospero 0:26a151d2c6db 105
oprospero 0:26a151d2c6db 106 void com::packetBuilder()
oprospero 0:26a151d2c6db 107 {
oprospero 0:26a151d2c6db 108 char * commandData = new char[bLength];
oprospero 0:26a151d2c6db 109 commandData[4] = buffer[--bLength]; // Sequence Number.
oprospero 0:26a151d2c6db 110 commandData[3] = buffer[--bLength]; // CheckSum value.
oprospero 0:26a151d2c6db 111 commandData[2] = buffer[--bLength]; // Second 7 bits.
oprospero 0:26a151d2c6db 112 commandData[1] = buffer[--bLength]; // Fisrt 7 bits.
oprospero 0:26a151d2c6db 113 commandData[0] = buffer[--bLength]; // Command.
oprospero 0:26a151d2c6db 114
oprospero 0:26a151d2c6db 115
oprospero 0:26a151d2c6db 116 if( commandData[0] + commandData[2] == commandData[3] ) // Validate checksum.
oprospero 0:26a151d2c6db 117 {
oprospero 0:26a151d2c6db 118 short * array = new short[2];
oprospero 0:26a151d2c6db 119
oprospero 0:26a151d2c6db 120 array[0] = (short)commandData[0];
oprospero 0:26a151d2c6db 121
oprospero 0:26a151d2c6db 122 short value = (short)(commandData[1] * 128 + commandData[2]);
oprospero 0:26a151d2c6db 123
oprospero 0:26a151d2c6db 124 if( value > 8062 )
oprospero 0:26a151d2c6db 125 value = (short)value + 57344;
oprospero 0:26a151d2c6db 126
oprospero 0:26a151d2c6db 127 array[1] = value;
oprospero 0:26a151d2c6db 128 // xbeeTx.printf("Cmd: %d,\tVal: %d\n\r,",array[0],array[1]);
oprospero 0:26a151d2c6db 129 rxBuffer->add( array ); // Add to read buffer.
oprospero 0:26a151d2c6db 130 write( (short)commandData[0], (short)commandData[4]); // Ack the packet with sequence nuber.
oprospero 0:26a151d2c6db 131 }
oprospero 0:26a151d2c6db 132 delete[] commandData;
oprospero 0:26a151d2c6db 133 bLength = 0; // Reset the buffer length.
oprospero 0:26a151d2c6db 134 }
oprospero 0:26a151d2c6db 135
oprospero 0:26a151d2c6db 136
oprospero 0:26a151d2c6db 137 /********************** bool isSignalGood() ******************************/
oprospero 0:26a151d2c6db 138 /* For future use */
oprospero 0:26a151d2c6db 139 /*************************************************************************/
oprospero 0:26a151d2c6db 140 bool com::isSignalGood()
oprospero 0:26a151d2c6db 141 {
oprospero 0:26a151d2c6db 142 signalStrength = rssi.dutycycle();
oprospero 0:26a151d2c6db 143 if (signalStrength > RSSI_THRES) return true;
oprospero 0:26a151d2c6db 144 else return false;
oprospero 0:26a151d2c6db 145 }
oprospero 0:26a151d2c6db 146