Prosper Van / xbeeCom

Fork of com by Prosper Van

Files at this revision

API Documentation at this revision

Comitter:
oprospero
Date:
Mon Oct 06 04:59:20 2014 +0000
Parent:
16:89695823d407
Child:
18:19bcb2dbf3c8
Commit message:
Working ; 255

Changed in this revision

PwmIn.lib Show diff for this revision Revisions of this file
com.cpp Show annotated file Show diff for this revision Revisions of this file
com.h Show annotated file Show diff for this revision Revisions of this file
queueChar/queueChar.cpp Show annotated file Show diff for this revision Revisions of this file
queueChar/queueChar.h Show annotated file Show diff for this revision Revisions of this file
--- a/PwmIn.lib	Thu Sep 25 02:30:25 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/oprospero/code/PwmIn/#f87e8836a87b
--- a/com.cpp	Thu Sep 25 02:30:25 2014 +0000
+++ b/com.cpp	Mon Oct 06 04:59:20 2014 +0000
@@ -1,27 +1,11 @@
-/****************************** 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 "com.h"
+
+#define DEBUG_COM
 #ifdef DEBUG_COM
-    Timer timerCom;
     #define NL "\n\r"
-    #define PRINT(x)        xbeeTx.printf(x)   //Serial.print(x)
-    #define PRINTF          xbeeTx.printf   //Serial.print(x, y)
+    #define PRINT(x)        xbee.printf(x)   //Serial.print(x)
+    #define PRINTF          xbee.printf   //Serial.print(x, y)
     #define PRINTLN(x)      PRINT(x);PRINT(NL)
     #define START           timerCom.start(); 
     #define STOP            timerCom.stop()
@@ -40,238 +24,53 @@
     #define GET(x)
 #endif
 
-
-/*********************** com( PinName, PinName ) *************************/
-/*                                                                       */
-/*************************************************************************/
-
-com::com( PinName tx, PinName rx , PinName rssipin) : xbeeTx( tx, NC), xbeeRx( NC, rx), rssi(rssipin)
+com::com(PinName tx, PinName rx): xbee(tx,rx)
 {
-    index1 = 0;                             // How many bytes are in the buffer.
-    index2 = 0;                             // How many bytes are in the buffer.
-    pindex = 0;                             // How many bytes are in the buffer.
-    rdy2build = false;                      // Xbee is in transparent mode.
-    xbeeTx.baud(BAUDRATE);                  // Setup the serial baud rate.
-    xbeeRx.baud(BAUDRATE);                  // Setup the serial baud rate.
-    txBuffer = new queue();
-    signalStrength = 0;
-    xbeeRx.attach( this, &com::callback );    // Set callback as the interrupt handler. 
-    #ifdef DEBUG_COM
-    xbeeTx.printf("Communication.....Done\n\r");
-    #endif
-    START;
+    xbee.baud(BAUDRATE);
+    xbee.attach(this,&com::callback);
 }
 
-
-/************************ 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( 255 );                     // End of message.
-    xbeeTx.putc( (char)command );           // Command
-    xbeeTx.putc( (char) value);            // Second 8 bits in array.
-    xbeeTx.putc( command + value );        // Checksum array[0] + array[1].
+com::~com()
+{
 }
 
-/*************************** char ackCheck()  ********************************/
-/*                                                                       */
-/*************************************************************************/
-
-void com::sendACK()
+void com::callback()
 {
-    if( !txBuffer->isEmpty())
+    while(xbee.readable())
     {
-    __disable_irq();
-        short * pkt = txBuffer->pop();
-        write(pkt[0],pkt[1]); //may need to disable interrupt
-    __enable_irq();
-//        #ifdef DEBUG_COM
-        if(pkt[1] % 5 == 0) 
-        {
-            PRINTF("len: %d\n\r",txBuffer->queueLength());
-        }
-//        #endif
-        delete[] pkt;
-    
+        char data = xbee.getc();
+//        xbee.putc(data);
+        rxBuffer.add(data);
     }
 }
 
-bool com::rdy2ack()
-{
-    return !txBuffer->isEmpty();
-}
-
-
-/************************ void callback() ********************************/
-/*                                                                       */
-/*************************************************************************/
-
-//void com::callback()
-//{   
-//    __disable_irq();
-//    while( xbeeRx.readable() )
-//    {
-//        char data = xbeeRx.getc(); 
-////        xbeeRx.putc(data);
-//        if (isA1)
-//        {
-//            if ( data == 255 && index1 > 4 )
-//            {
-//                rdy2build = true;
-//                pindex = index1;
-//                index1 = 0;
-//                isA1 = false;
-//            }
-//            else if ( index1 < BUFFERSIZE )
-//            {
-//                
-//                buffer1[index1++] = data;
-//            }
-//        }
-//        else
-//        {
-//            if ( data == 255 && index2 > 4 )
-//            {
-//                rdy2build = true;
-//                pindex = index2;
-//                index2 = 0;
-//                isA1 = true;
-//            }
-//            else if ( index2 < BUFFERSIZE )
-//            {
-//                buffer2[index2++] = data;
-//            }
-//        }
-//    }
-//    __enable_irq();
-//}
-
-void com::callback()
-{   
-    __disable_irq();
-    PRINTF("Reading\n\r");
-    while( xbeeRx.readable() )
-    {
-        short *data = new short;
-        data[0] = xbeeRx.getc(); 
-        PRINTF("d: %d\n\r", data[0]);
-        rxBuffer->add( data );
-    }
-    __enable_irq();
-}
-
-/********************** 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.          */
-/*************************************************************************/
-
-//short * com::read()
-//{
-//    if (rdy2build)
-//    {
-//        rdy2build = false;
-//        char * commandData = new char[5];
-//        if (!isA1)
-//        {
-//            commandData[4] = buffer1[--pindex];     // Sequence Number.
-//            commandData[3] = buffer1[--pindex];     // CheckSum value.
-//            commandData[2] = buffer1[--pindex];     // Second 7 bits.
-//            commandData[1] = buffer1[--pindex];     // Fisrt 7 bits.
-//            commandData[0] = buffer1[--pindex];     // Command.
-//        }
-//        else 
-//        {
-//            commandData[4] = buffer2[--pindex];     // Sequence Number.
-//            commandData[3] = buffer2[--pindex];     // CheckSum value.
-//            commandData[2] = buffer2[--pindex];     // Second 7 bits.
-//            commandData[1] = buffer2[--pindex];     // Fisrt 7 bits.
-//            commandData[0] = buffer2[--pindex];     // Command.
-//        }
-//        
-////        xbeeTx.printf("Copied: %d %d %d %d %d\n\r",commandData[0],commandData[1],commandData[2],commandData[3],commandData[4]);
-//          
-//        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;
-//            if ( commandData[0] != 0)
-//            {
-//                short * ackPacket = new short[2];
-//                ackPacket[0] = commandData[0];
-//                ackPacket[1] = commandData[4];
-//                txBuffer->add( ackPacket ); // Ack the packet with sequence nuber.
-//            }
-//            
-//            delete[] commandData;
-//            return array;
-//        } 
-//        delete[] commandData; 
-//    }        
-//    return NULL;                                 
-//}
-
-short * com::read()
-{
-    if ( !cmdBuffer->isEmpty() )
-        return cmdBuffer->pop();
-    else
-        return NULL;    
-}
-
-
-/************************* bool isData()  ********************************/
-/*                                                                       */
-/*************************************************************************/
-
-//bool com::isData()
-//{
-//    return rdy2build;
-//}
-
 bool com::isData()
 {
-    static short packetIndex = 0;
-    static short pack_cmd = 0;
-    static short pack_value[2] = {0,0};
-    static short pack_seq = 0;
-    static short pack_checksum = 0;
+    static char packetIndex = 0;
+    static char pack_cmd = 0;
+    static char pack_value[2] = {0,0};
+    static char pack_seq = 0;
+    static char pack_checksum = 0;
     
     __disable_irq();
-    while ( !rxBuffer->isEmpty() )
+    while ( !rxBuffer.isEmpty() )
     {
-        short * data = rxBuffer->pop();
+        char data = rxBuffer.pop();
         __enable_irq();
-        PRINTF("d: %d \n\r", *data);
+//        PRINTF("d: %d \n\r", data);
         switch (packetIndex)
         {
             case 0:
             {
-                if ( *data == 255 )
+                if ( data == 255 )
                 packetIndex++;
                 break;
             }
             case 1:
             {
-                if ( *data < 13 )
+                if ( data < 13 )
                 {
-                    pack_cmd = *data;
+                    pack_cmd = data;
                     packetIndex++;
                 }
                 else
@@ -280,15 +79,15 @@
             }
             case 2:
             {
-                pack_value[1] = *data;
+                pack_value[1] = data;
                 packetIndex++;
                 break;
             }
             case 3:
             {
-                if ( *data < 128 )
+                if ( data < 128 )
                 {
-                    pack_value[0] = *data;
+                    pack_value[0] = data;
                     packetIndex++;
                 }
                 else
@@ -297,25 +96,25 @@
             }
             case 4:
             {
-                pack_seq = *data;
+                pack_seq = data;
                 packetIndex++;
                 break;   
             }
             case 5:
             {
                 short temp = pack_value[0] + pack_cmd;
-                pack_checksum = *data;
+                pack_checksum = data;
                 if ( temp == pack_checksum )
                 {
                     short * ackPacket = new short[2];
                     ackPacket[0] = pack_cmd;
                     ackPacket[1] = pack_seq;
-                    txBuffer->add( ackPacket ); // Ack the packet with sequence nuber.
+                    txBuffer.add( ackPacket ); // Ack the packet with sequence nuber.
                     
                     short * array = new short[2];
                     array[0] = pack_cmd;
                     array[1] = pack_value[1] * 128 + pack_value[0];
-                    cmdBuffer->add( array );
+                    cmdBuffer.add( array );
                 }
                 
                 packetIndex = 0;
@@ -327,20 +126,44 @@
         }
         
         __disable_irq();
-        delete[] data;
         
     }
     __enable_irq();
-    return !cmdBuffer->isEmpty();
+    return !cmdBuffer.isEmpty();
+}
+
+short * com::read()
+{
+    if ( !cmdBuffer.isEmpty() )
+        return cmdBuffer.pop();
+    else
+        return NULL;    
 }
 
 
-/********************** bool isSignalGood() ******************************/
-/* For future use   */
-/*************************************************************************/
-bool com::isSignalGood()
+bool com::rdy2ack()
+{
+    return !txBuffer.isEmpty();
+}
+
+void com::sendACK()
 {
-    signalStrength = rssi.dutycycle();
-    if (signalStrength > RSSI_THRES) return true;
-    else return false;
+    if( !txBuffer.isEmpty())
+    {
+        short * pkt = txBuffer.pop();
+        __disable_irq();
+        write(pkt[0],pkt[1]); //may need to disable interrupt
+        __enable_irq();
+        delete[] pkt;
+    
+    }
 }
+
+void com::write( short command, short seq  )
+{  
+    xbee.putc( 255 );                     // End of message.
+    xbee.putc( (char)command );           // Command
+    xbee.putc( (char) seq);            // Second 8 bits in array.
+    xbee.putc( (char) command + seq );        // Checksum array[0] + array[1].
+}
+
--- a/com.h	Thu Sep 25 02:30:25 2014 +0000
+++ b/com.h	Mon Oct 06 04:59:20 2014 +0000
@@ -1,59 +1,31 @@
-/******************************* 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"
-
-#define DEBUG_COM
+#include "queueChar.h"
 
-
-const int BAUDRATE      = 38400;
-const int BUFFERSIZE    = 16;
+int const BAUDRATE = 38400;
 
 class com
-{  
-    public:
-        com( PinName, PinName, PinName );   // Setup the com serial port. (tx, rx)
-        bool isData();              // Is there data to be read?
-        bool isSignalGood();
-        short * read();             // Read from the queue.
-        void sendACK();
-        bool rdy2ack();
-        
-    private:         
-        void write( short, short ); // Write to the port.
-        RawSerial xbeeTx;                // tx - DIN, rx - DOUT
-        RawSerial xbeeRx;                // tx - DIN, rx - DOUT
-        queue *txBuffer;
-        queue *rxBuffer;
-        queue *cmdBuffer;
-        PwmIn rssi;
-          
-        bool rdy2build;
-        bool isA1;
-        void callback();            // Handle the interrupts.
-        
-        char buffer1[BUFFERSIZE];    // Buffer for holding serial data.
-        char buffer2[BUFFERSIZE];    // Buffer for holding serial data.
-        int index1;                // Location in the buffer to place next data.
-        int index2;                // Location in the buffer to place next data.
-        int pindex;                // Location in the buffer to place next data.
-        int signalStrength;
+{
+public:
+    com(PinName, PinName);
+    ~com();
+    short * read();
+    bool isData();
+    bool rdy2ack();
+    void sendACK();
+    void write(short,short);
+    
+    
+private:
+    Serial xbee;
+    queue cmdBuffer;
+    queue txBuffer;
+    queueChar rxBuffer;
+    void callback();
 
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/queueChar/queueChar.cpp	Mon Oct 06 04:59:20 2014 +0000
@@ -0,0 +1,132 @@
+/*************************** queue.cpp ***************************************/
+/*                                                                           */
+/*****************************************************************************/
+
+#include "queueChar.h"
+
+/***************************** constructor ***********************************/
+/* Description:                                                              */
+/*****************************************************************************/
+
+queueChar::queueChar()
+{
+    front = 0;
+    end = 0;
+    lengthVar = 0;
+    for (int i = 0; i < MAXQUEUECHARLENGTH; i++)
+    {
+        buffer[i] = 0;
+    }
+}
+
+/******************************* distructor **********************************/
+/* Description:                                                              */
+/*****************************************************************************/
+
+queueChar::~queueChar()
+{
+    clear();        // Clear the entire queue.
+}
+
+/*****************************************************************************/
+/* Description:                                                              */
+/* Accepts:                                                                  */
+/* Returns:                                                                  */
+/*****************************************************************************/
+
+bool queueChar::isEmpty()
+{
+    bool empty = front == end;
+    if (empty) lengthVar = 0;
+    return empty;
+}
+
+/*****************************************************************************/
+/* Description:                                                              */
+/* Accepts:                                                                  */
+/* Returns:                                                                  */
+/*****************************************************************************/
+
+void queueChar::clear()
+{
+    lengthVar = 0;
+    front = 0;
+    end = 0;
+}
+
+/*****************************************************************************/
+/* Description:                                                              */
+/* Accepts:                                                                  */
+/* Returns:                                                                  */
+/*****************************************************************************/
+
+void queueChar::add( char data )
+{
+    buffer[front] = data;
+    front = nextIndex(front);
+    if (front == end)
+        end = nextIndex(end);
+    else
+        lengthVar++;
+}
+
+/*****************************************************************************/
+/* Description:                                                              */
+/* Accepts:                                                                  */
+/* Returns:                                                                  */
+/*****************************************************************************/
+
+char queueChar::pop()
+{
+    if (isEmpty())
+        return 255;
+    else
+    {
+        lengthVar--;
+        char data = buffer[end];
+        end = nextIndex(end);
+        return data;
+    }
+}
+
+/*****************************************************************************/
+/* Description:                                                              */
+/* Accepts:                                                                  */
+/* Returns:                                                                  */
+/*****************************************************************************/
+
+char queueChar::peek()
+{
+//    return front;
+    char data = buffer[end];
+    return data;
+}
+
+/*****************************************************************************/
+/* Description:                                                              */
+/* Accepts:                                                                  */
+/* Returns:                                                                  */
+/*****************************************************************************/
+
+char queueChar::length()
+{
+    short diff = front - end;
+    if (diff < 0)
+        diff += MAXQUEUECHARLENGTH;
+//    diff = diff % MAXQUEUECHARLENGTH;
+    return diff;
+//    if (diff == lengthVar)
+//        return lengthVar;
+//    else
+//        lengthVar = diff;
+//        return 255;
+}
+
+
+char queueChar::nextIndex(char index)
+{
+    index++;
+    if (index >= MAXQUEUECHARLENGTH)
+        index = 0;
+    return index;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/queueChar/queueChar.h	Mon Oct 06 04:59:20 2014 +0000
@@ -0,0 +1,42 @@
+/**************************** 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 QUEUECHAR_H
+#define QUEUECHAR_H
+
+#include "mbed.h"
+
+using namespace std;
+
+const int MAXQUEUECHARLENGTH = 12;
+
+class queueChar
+{
+public: 
+    queueChar();                        // Queue constructor
+    ~queueChar();                       // Queue destructor
+
+    bool isEmpty();                 // Check for an empty queue.
+    void clear();                   // Clears the entire queue.
+    void add( char );              // Push commandData into the queue.
+    char peek();                   // Look at the last item in the queue.
+    char pop();                    // Pop the top item off the queue.
+    char length();              // Return how many objects are in the queue.
+
+private:
+    char buffer[MAXQUEUECHARLENGTH];
+    char front;
+    char end;
+    char lengthVar;
+    char nextIndex(char);
+    
+
+};
+
+#endif