Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of com by
Revision 0:26a151d2c6db, committed 2014-01-08
- Comitter:
- oprospero
- Date:
- Wed Jan 08 07:32:30 2014 +0000
- Child:
- 1:4f53de75bc96
- Commit message:
- Create two serial instance for Rx and Tx
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PwmIn.lib Wed Jan 08 07:32:30 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/PwmIn/#6d68eb9b6bbb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/com.cpp Wed Jan 08 07:32:30 2014 +0000 @@ -0,0 +1,146 @@ +/****************************** com.cpp **********************************/ +/* Version: 1.0 */ +/* Last Updated: June 1, 2013 */ +/* */ +/* The com class implements reliable data transfer between two nodes */ +/*using a checksum and a sequence number for guaranteed message delivery */ +/*over an xbee modem connected to the passed in tx and rx pins. Messages */ +/*are received and placed in the rxBuffer to be read when convenient. */ +/*Messages are encoded by sending a byte with the value of the command */ +/*then and int of the command. */ +/* */ +/* Commands: 0 -> Ack, does not get placed in rxQueue. */ +/* 1 -> Throttle */ +/* 2 -> Pitch */ +/* 3 -> Roll */ +/* 4 -> Yaw */ +/*************************************************************************/ + +#include "mbed.h" +#include "com.h" + +/*********************** com( PinName, PinName ) *************************/ +/* */ +/*************************************************************************/ + +com::com( PinName tx, PinName rx , PinName rssipin) : xbeeRx( tx, rx), xbeeTx( tx, rx), rssi(rssipin) +{ + bLength = 0; // How many bytes are in the buffer. + xbeeRx.baud(BAUDRATE); // Setup the serial baud rate. + xbeeTx.baud(BAUDRATE); // Setup the serial baud rate. + rxBuffer = new queue(); // Point to the rxQueue. + signalStrength = 0; + xbeeRx.attach( this, &com::callback ); // Set callback as the interrupt handler. + xbeeTx.printf("Communication.....Done\n\r"); +} + +/************************* bool isData() ********************************/ +/* */ +/*************************************************************************/ + +bool com::isData() +{ + if( rxBuffer->isEmpty() ) + return false; + + return true; +} + +/************************ void write( char ) *****************************/ +/* Write a packet out the xbee com port. */ +/* */ +/* Data format byte[] */ +/* byte[0] = command. */ +/* byte[1] = upper 8 bits of value. */ +/* byte[2] = lower 8 bits of value. */ +/* byte[3] = Checksum byte[0] + byte[2]. */ +/* byte[4] = Sequence Number. */ +/* byte[5] = 255 End of message. */ +/*************************************************************************/ + +void com::write( short command, short value ) +{ + xbeeTx.putc( (char)command ); // Command + xbeeTx.putc( 0 ); // First 8 bits in array. + xbeeTx.putc( (char)value ); // Second 8 bits in array. + xbeeTx.putc( command + value ); // Checksum array[0] + array[1]. + xbeeTx.putc( 0 ); // Sequence number. + xbeeTx.putc( 255 ); // End of message. +} + +/*************************** char read() ********************************/ +/* */ +/*************************************************************************/ + +short * com::read() +{ + if( !rxBuffer->isEmpty()) + return rxBuffer->pop(); + + return NULL; +} + +/************************ void callback() ********************************/ +/* */ +/*************************************************************************/ + +void com::callback() +{ + while( xbeeRx.readable() ) + { + char data = xbeeRx.getc(); +// xbeeTx.printf("data: %d\n\r", data); + if( bLength++ < BUFFERSIZE ) + buffer[bLength] = data; + + if( data == 255 ) + packetBuilder(); + } +} + +/********************** 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. */ +/*************************************************************************/ + +void com::packetBuilder() +{ + char * commandData = new char[bLength]; + commandData[4] = buffer[--bLength]; // Sequence Number. + commandData[3] = buffer[--bLength]; // CheckSum value. + commandData[2] = buffer[--bLength]; // Second 7 bits. + commandData[1] = buffer[--bLength]; // Fisrt 7 bits. + commandData[0] = buffer[--bLength]; // Command. + + + if( commandData[0] + commandData[2] == commandData[3] ) // Validate checksum. + { + short * array = new short[2]; + + array[0] = (short)commandData[0]; + + short value = (short)(commandData[1] * 128 + commandData[2]); + + if( value > 8062 ) + value = (short)value + 57344; + + array[1] = value; +// xbeeTx.printf("Cmd: %d,\tVal: %d\n\r,",array[0],array[1]); + rxBuffer->add( array ); // Add to read buffer. + write( (short)commandData[0], (short)commandData[4]); // Ack the packet with sequence nuber. + } + delete[] commandData; + bLength = 0; // Reset the buffer length. +} + + +/********************** bool isSignalGood() ******************************/ +/* For future use */ +/*************************************************************************/ +bool com::isSignalGood() +{ + signalStrength = rssi.dutycycle(); + if (signalStrength > RSSI_THRES) return true; + else return false; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/com.h Wed Jan 08 07:32:30 2014 +0000 @@ -0,0 +1,48 @@ +/******************************* com.h ***********************************/ +/* Version: 1.0 */ +/* Last Updated: June 1, 2013 */ +/* */ +/* The com class implements reliable data transfer between two nodes */ +/*using a checksum and a sequence number for guaranteed message delivery */ +/*over an xbee modem connected to the passed in tx and rx pins. Messages */ +/*are received and placed in the rxBuffer to be read when convenient. */ +/*Messages are encoded by sending a byte with the value of the command */ +/*then and int of the command. */ +/* Alternative Pins RX = PTA1, TX PTA2 */ +/*************************************************************************/ + +#ifndef COM_H +#define COM_H +#define RSSI_THRES 0.8 + +#include "mbed.h" +#include "queue.h" +#include "PwmIn.h" + +const int BAUDRATE = 38400; +const int BUFFERSIZE = 10; + +class com +{ + public: + com( PinName, PinName, PinName ); // Setup the com serial port. (tx, rx) + bool isData(); // Is there data to be read? + bool isSignalGood(); + void write( short, short ); // Write to the port. + short * read(); // Read from the queue. + + private: + void callback(); // Handle the interrupts. + void packetBuilder(); // Called by callback to place commandes into the queue. + + char buffer[BUFFERSIZE]; // Buffer for holding serial data. + int bLength; // Location in the buffer to place next data. + int signalStrength; + + Serial xbeeRx; // tx - DIN, rx - DOUT + Serial xbeeTx; // tx - DIN, rx - DOUT + queue *rxBuffer; // queue of commands ready to be read. + PwmIn rssi; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/queue/queue.cpp Wed Jan 08 07:32:30 2014 +0000 @@ -0,0 +1,148 @@ +/*************************** queue.cpp ***************************************/ +/* */ +/* Authers: Oanh Tran, Ling Lei, Sihao Xie, Greg Abdo. */ +/* Date: February 23, 2013 */ +/* Version: 1.0 */ +/* */ +/* The queue is used to stack StructureItem in order with a FILO */ +/*arrangement. */ +/*****************************************************************************/ + +#include "queue.h" + +/***************************** constructor ***********************************/ +/* Description: */ +/*****************************************************************************/ + +queue::queue() +{ + front = NULL; // Set front to NULL at the start. +} + +/******************************* distructor **********************************/ +/* Description: */ +/*****************************************************************************/ + +queue::~queue() +{ + clear(); // Clear the entire queue. +} + +/*****************************************************************************/ +/* Description: */ +/* Accepts: */ +/* Returns: */ +/*****************************************************************************/ + +bool queue::isEmpty() +{ + // Is the queue empty? + if( front == NULL ) // Check the front pointer. + return true; // Queue is empty, return true. + + return false; // There is atleast one item, not empty. +} + +/*****************************************************************************/ +/* Description: */ +/* Accepts: */ +/* Returns: */ +/*****************************************************************************/ + +void queue::clear() +{ + // Is the list already empty? + if( isEmpty() ) // Check for an empty list. + return; // List is empty, don't need to do anything. + + queueNode * current = front; // Create node to help step through list. + queueNode * placeHold = front; // Create node to keep place of delete. + + // As long as were not at the end, keep stepping to the next node. + while( current != NULL) + { + placeHold = current->next; // Hold where we have to go. + delete current; // Delete the node. + current = placeHold; // Set current to the next node. + } + + front = NULL; // Reset the front to NULL; + length = 0; +} + +/*****************************************************************************/ +/* Description: */ +/* Accepts: */ +/* Returns: */ +/*****************************************************************************/ + +void queue::add( short * item ) +{ + // Were we passed an invalid object somehow? + if( item == NULL ) // Check for NULL + return; // If so, return. + + if( queueLength() > MAXQUEUELENGTH ) + clear(); + + queueNode * newNode = new queueNode( item ); // Create the new node. + + if( isEmpty() ) + front = newNode; // Set front to the new node. + + else + { + queueNode *temp = front; + while( temp->next != NULL ) + temp = temp->next; + + temp->next = newNode; + } + length++; +} + +/*****************************************************************************/ +/* Description: */ +/* Accepts: */ +/* Returns: */ +/*****************************************************************************/ + +short * queue::pop() +{ + // Is the list already empty? + if( isEmpty() ) // Check for an empty list. + return NULL; // List is empty, don't need to do anything. + + short* dataHold = front->data; // Keep track of what were returning. + queueNode * oldNode = front; // Save the old node to be deleted + front = front->next; // Set front to next object. + + delete oldNode; // Delete the front node. + length--; // Remove one from the length. + return dataHold; // return the stuctureItem. +} + +/*****************************************************************************/ +/* Description: */ +/* Accepts: */ +/* Returns: */ +/*****************************************************************************/ + +short * queue::peek() +{ + if( front != NULL ) + return front->data; + + return NULL; +} + +/*****************************************************************************/ +/* Description: */ +/* Accepts: */ +/* Returns: */ +/*****************************************************************************/ + +short queue::queueLength() +{ + return length; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/queue/queue.h Wed Jan 08 07:32:30 2014 +0000 @@ -0,0 +1,53 @@ +/**************************** queue.h ****************************************/ +/* */ +/* Authers: Greg Abdo. */ +/* Date: February 23, 2013 */ +/* Version: 1.0 */ +/* */ +/* The queue is used to stack StructureItem in order with a FILO arrangement.*/ +/*****************************************************************************/ + +#ifndef QUEUE_H +#define QUEUE_H + +#include "mbed.h" + +using namespace std; + +const int MAXQUEUELENGTH = 4; + +class queue +{ +public: + queue(); // Queue constructor + ~queue(); // Queue destructor + + bool isEmpty(); // Check for an empty queue. + void clear(); // Clears the entire queue. + void add( short* ); // Push commandData into the queue. + short* peek(); // Look at the last item in the queue. + short* pop(); // Pop the top item off the queue. + short queueLength(); // Return how many objects are in the queue. + +private: + int length; + + struct queueNode // Node object for the queue. + { + queueNode( short* array ) + { + data = array; + next = NULL; + } + + ~queueNode() + {} + + short* data; // Pointer to the StructureItem object. + queueNode * next; // Next node in the queue. + }; + + queueNode * front; // Root of the queue. +}; + +#endif