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
Diff: com.cpp
- Revision:
- 2:bd1621102cad
- Parent:
- 1:4f53de75bc96
- Child:
- 3:42e37cdab039
- Child:
- 4:f8e953201251
--- a/com.cpp Sat Feb 15 19:54:45 2014 +0000 +++ b/com.cpp Sun Apr 20 02:27:39 2014 +0000 @@ -24,9 +24,10 @@ /* */ /*************************************************************************/ -com::com( PinName tx, PinName rx , PinName rssipin) : xbeeTx( tx, rx), xbeeRx( tx, rx), rssi(rssipin) +com::com( PinName tx, PinName rx , PinName rssipin) : xbeeTx( tx, NC), xbeeRx( NC, rx), rssi(rssipin) { bLength = 0; // How many bytes are in the buffer. + api_mode = false; // Xbee is in transparent mode. xbeeTx.baud(BAUDRATE); // Setup the serial baud rate. xbeeRx.baud(BAUDRATE); // Setup the serial baud rate. rxBuffer = new queue(); // Point to the rxQueue. @@ -43,6 +44,7 @@ bool com::isData() { + return !rxBuffer->isEmpty(); } @@ -60,13 +62,37 @@ void com::write( short command, short value ) { - short lvalue = (value % 128); - xbeeTx.putc( (char)command ); // Command - xbeeTx.putc( (char) value / 128 ); // First 8 bits in array. - xbeeTx.putc( (char) lvalue); // Second 8 bits in array. - xbeeTx.putc( command + lvalue ); // Checksum array[0] + array[1]. - xbeeTx.putc( (char)value ); // Sequence number. - xbeeTx.putc( 255 ); // End of message. + if (api_mode) + { + short hvalue = value / 128; + short lvalue = (value % 128); + short sum = (2*command + hvalue + 2*lvalue + value); + short check = sum % 256; + xbeeTx.putc( (char) 127); //Start Delimiter + + xbeeTx.putc( (char) 0); //Frame Data Length MSB + xbeeTx.putc( (char) 6); //Frame Data Length LSB + //Frame Data + xbeeTx.putc( (char) command); // Command + xbeeTx.putc( (char) hvalue); // First 8 bits in array. + xbeeTx.putc( (char) lvalue); // Second 8 bits in array. + xbeeTx.putc( command + lvalue ); // Checksum array[0] + array[1]. + xbeeTx.putc( (char) value); // Sequence number. + xbeeTx.putc( 255 ); // End of message. + + xbeeTx.putc( (char) 255 - check); //Checksum + + } + else + { + short lvalue = (value % 128); + xbeeTx.putc( (char)command ); // Command + xbeeTx.putc( (char) value / 128 ); // First 8 bits in array. + xbeeTx.putc( (char) lvalue); // Second 8 bits in array. + xbeeTx.putc( command + lvalue ); // Checksum array[0] + array[1]. + xbeeTx.putc( (char)value ); // Sequence number. + xbeeTx.putc( 255 ); // End of message. + } } /*************************** char read() ********************************/ @@ -91,6 +117,8 @@ { char data = xbeeRx.getc(); // xbeeTx.printf("data: %d\n\r", data); +// xbeeTx.printf("%d %d", data,(char) 255); +// xbeeTx.putc( data ); if( bLength++ < BUFFERSIZE ) buffer[bLength] = data; @@ -99,6 +127,15 @@ } } +//void com::api_callback() +//{ +// while( xbeeRx.readable() ) +// { +// buffer[bLength++] = xbeeRx.getc(); +// } +//} + + /********************** void packetBuilder() *****************************/ /* Creates a packet from the buffered data and places it in the rxBuffer */ /* queue to be read whenever convenient. Max value of +/- 8063. */ @@ -128,11 +165,18 @@ array[1] = value; // xbeeTx.printf("Cmd: %d,\tVal: %d\n\r,",array[0],array[1]); rxBuffer->add( array ); // Add to read buffer. +// xbeeTx.putc( 255 ); +// xbeeTx.putc( 255 ); write( (short)commandData[0], (short)commandData[4]); // Ack the packet with sequence nuber. } delete[] commandData; bLength = 0; // Reset the buffer length. } +// +//void com::api_packetBuilder() +//{ +// +//} /********************** bool isSignalGood() ******************************/ @@ -145,3 +189,8 @@ else return false; } +void com::setAPImode(bool mode) +{ + api_mode = mode; +} +