Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArSickPacketReceiver.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArSickPacketReceiver.h"
00029 #include "ArLog.h"
00030 #include "ariaUtil.h"
00031 
00032 
00039 AREXPORT ArSickPacketReceiver::ArSickPacketReceiver(
00040     unsigned char receivingAddress, bool allocatePackets,
00041     bool useBase0Address) 
00042 {
00043   myAllocatePackets = allocatePackets;
00044   myReceivingAddress = receivingAddress;
00045   myDeviceConn = NULL;
00046   myUseBase0Address = useBase0Address;
00047 }
00048 
00056 AREXPORT ArSickPacketReceiver::ArSickPacketReceiver(
00057     ArDeviceConnection *deviceConnection, 
00058     unsigned char receivingAddress, bool allocatePackets,
00059     bool useBase0Address)
00060 {
00061   myDeviceConn = deviceConnection;
00062   myAllocatePackets = allocatePackets;
00063   myReceivingAddress = receivingAddress;
00064   myUseBase0Address = useBase0Address;
00065 }
00066 
00067 AREXPORT ArSickPacketReceiver::~ArSickPacketReceiver() 
00068 {
00069   
00070 }
00071 
00072 AREXPORT void ArSickPacketReceiver::setDeviceConnection(
00073     ArDeviceConnection *deviceConnection)
00074 {
00075   myDeviceConn = deviceConnection;
00076 }
00077 
00078 AREXPORT ArDeviceConnection *ArSickPacketReceiver::getDeviceConnection(void)
00079 {
00080   return myDeviceConn;
00081 }
00082 
00092 AREXPORT ArSickPacket *ArSickPacketReceiver::receivePacket(
00093     unsigned int msWait)
00094 {
00095   ArSickPacket *packet;
00096   unsigned char c;
00097   char buf[2048];
00098   long count = 0;
00099   // state can be one of the STATE_ enums in the class
00100   int state = STATE_START;
00101   //unsigned int timeDone;
00102   //unsigned int curTime;
00103   long timeToRunFor;
00104   long packetLength;
00105   ArTime timeDone;
00106   ArTime lastDataRead;
00107   ArTime packetReceived;
00108   int numRead;
00109 
00110 
00111   if (myDeviceConn == NULL || 
00112       myDeviceConn->getStatus() != ArDeviceConnection::STATUS_OPEN)
00113   {
00114     return NULL;
00115   }
00116   
00117   timeDone.setToNow();
00118   timeDone.addMSec(msWait);
00119   do
00120   {
00121     timeToRunFor = timeDone.mSecTo();
00122     if (timeToRunFor < 0)
00123       timeToRunFor = 0;
00124 
00125     if (myDeviceConn->read((char *)&c, 1, timeToRunFor) == 0) 
00126     {
00127       if (state == STATE_START)
00128       {
00129     return NULL;
00130       }
00131       else
00132       {
00133     //ArUtil::sleep(1);
00134     continue;
00135       }
00136     }
00137 
00138     //printf("%x\n", c);
00139     switch (state) {
00140     case STATE_START:
00141       if (c == 0x02) // move on, resetting packet
00142       {
00143     //printf("###############\n");
00144     state = STATE_ADDR;
00145     myPacket.empty();
00146     myPacket.setLength(0);
00147     myPacket.uByteToBuf(c);
00148     packetReceived = myDeviceConn->getTimeRead(0);
00149     myPacket.setTimeReceived(packetReceived);
00150       }
00151       /*else
00152       {
00153     //printf(" BAD\n");
00154     }*/
00155       break;
00156     case STATE_ADDR:
00157       // if this is correct move on, adding this byte... this is taken
00158       // out in favor of a more inclusive approach, if someone ever
00159       // wnats to drive multiple robots off of one serial port just
00160       // put this back in, or I don't know, punt
00161       //if (c == ((unsigned char)0x80 + myReceivingAddress)) 
00162       if (!myUseBase0Address && c >= 0x80 && c <= 0x84)
00163       {
00164     state = STATE_START_COUNT;
00165     myPacket.uByteToBuf(c);
00166       }
00167       // c will always be >= 0 since its unsigned
00168       else if (myUseBase0Address && c <= 0x4)
00169       {
00170     state = STATE_START_COUNT;
00171     myPacket.uByteToBuf(c);
00172       }
00173       else // go back to beginning, packet hosed
00174       {
00175     ArLog::log(ArLog::Terse, 
00176            "ArSickPacketReceiver::receivePacket: wrong address (0x%x instead of 0x%x)", c, (unsigned) 0x80 + myReceivingAddress);
00177     state = STATE_START;
00178       }
00179       break;
00180     case STATE_START_COUNT:
00181       packetLength = c;
00182       myPacket.uByteToBuf(c);
00183       state = STATE_ACQUIRE_DATA;
00184       break;
00185     case STATE_ACQUIRE_DATA:
00186       // the character c is high ordre byte of the packet length count 
00187       // so we'll just build the length of the packet then get the 
00188       //rest of the data
00189       myPacket.uByteToBuf(c);
00190       packetLength = packetLength | (c << 8);
00191       count = 0;
00192       // make sure the length isn't longer than the maximum packet length...
00193       // getting some wierd 25k or 44k long packets (um, no)
00194       if (packetLength > ((long)myPacket.getMaxLength() -
00195               (long)myPacket.getHeaderLength()))
00196       {
00197     ArLog::log(ArLog::Normal, 
00198        "ArSickPacketReceiver::receivePacket: packet too long, it is %d long while the maximum is %d.", packetLength, myPacket.getMaxLength());
00199     state = STATE_START;
00200     //myPacket.log();
00201     break;
00202       }
00203       // here we read until we get as much as we want, OR until
00204       // we go 100 ms without data... its arbitrary but it doesn't happen often
00205       // and it'll mean a bad packet anyways
00206       lastDataRead.setToNow();
00207       while (count < packetLength + 2)
00208       {
00209     numRead = myDeviceConn->read(buf + count, packetLength + 2- count, 1);
00210     if (numRead > 0)
00211       lastDataRead.setToNow();
00212     if (lastDataRead.mSecTo() < -100)
00213     {
00214       return NULL;
00215     }
00216     count += numRead;
00217       }
00218       myPacket.dataToBuf(buf, packetLength + 2);
00219       if (myPacket.verifyCRC()) 
00220       {
00221     myPacket.resetRead();
00222     //printf("Received ");
00223     //myPacket.log();
00224     if (myAllocatePackets)
00225     {
00226       packet = new ArSickPacket;
00227       packet->duplicatePacket(&myPacket);
00228       return packet;
00229     }
00230     else
00231       return &myPacket;
00232       }
00233       else 
00234       {
00235     ArLog::log(ArLog::Normal, 
00236        "ArSickPacketReceiver::receivePacket: bad packet, bad checksum");
00237     state = STATE_START;
00238     //myPacket.log();
00239     break;
00240       }
00241       break;
00242     default:
00243       break;
00244     }
00245   } while (timeDone.mSecTo() >= 0 || state != STATE_START);
00246 
00247   //printf("finished the loop...\n");
00248   return NULL;
00249 
00250 }
00251 
00252 

Generated on Tue Feb 20 10:51:41 2007 for Aria by  doxygen 1.4.0