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

ArSickPacket.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 "ArSickPacket.h"
00029 #include "stdio.h"
00030 
00031 AREXPORT ArSickPacket::ArSickPacket(unsigned char sendingAddress) :
00032   ArBasePacket(2048, 4)
00033 {
00034   mySendingAddress = sendingAddress;
00035 }
00036 
00037 AREXPORT ArSickPacket::~ArSickPacket()
00038 {
00039 }
00040 
00047 AREXPORT void ArSickPacket::setSendingAddress(unsigned char address)
00048 {
00049   mySendingAddress = address;
00050 }
00051 
00058 AREXPORT unsigned char ArSickPacket::getSendingAddress(void)
00059 {
00060   return mySendingAddress;
00061 }
00062 
00069 AREXPORT unsigned char ArSickPacket::getReceivedAddress(void)
00070 {
00071   int len = myReadLength;
00072   unsigned char address;
00073   
00074   // toss it into the second byte of the packet
00075   myReadLength = 1;
00076   address = bufToUByte();
00077   myLength = len;
00078   return address;
00079 }
00080 
00081 AREXPORT ArTypes::UByte ArSickPacket::getID(void)
00082 {
00083  if (myLength >= 5)
00084     return myBuf[4];
00085   else
00086     return 0;
00087 }
00088 
00089 AREXPORT void ArSickPacket::resetRead(void)
00090 {
00091   myReadLength = myHeaderLength + 1;
00092 }
00093 
00094 AREXPORT void ArSickPacket::finalizePacket(void)
00095 {
00096   int len = myLength;
00097   int chkSum;
00098 
00099   // put in the start of the packet
00100   myLength = 0;
00101   // toss in the header 
00102   uByteToBuf(0x02);
00103   // now the laser we want to talk to
00104   uByteToBuf(mySendingAddress);
00105   // dump in the length
00106   uByte2ToBuf(len - myHeaderLength);
00107   myLength = len;
00108 
00109   // that lovely CRC
00110   chkSum = calcCRC();
00111   byteToBuf(chkSum & 0xff );
00112   byteToBuf((chkSum >> 8) & 0xff );
00113 
00114   //printf("Sending ");
00115   //log();
00116 }
00117 
00123 AREXPORT void ArSickPacket::duplicatePacket(ArSickPacket *packet)
00124 {
00125   myLength = packet->getLength();
00126   myReadLength = packet->getReadLength();
00127   myTimeReceived = packet->getTimeReceived();
00128   mySendingAddress = packet->getSendingAddress();
00129   memcpy(myBuf, packet->getBuf(), myLength);
00130   
00131 }
00132 
00133 AREXPORT ArTypes::Byte2 ArSickPacket::calcCRC(void)
00134 {
00135   unsigned short uCrc16;
00136   unsigned char abData[2];
00137   unsigned int uLen = myLength;
00138   unsigned char * commData = (unsigned char *)myBuf;
00139 
00140   uCrc16 = 0;
00141   abData[0] = 0;
00142   while (uLen--)
00143   {
00144     abData[1] = abData[0];
00145     abData[0] = *commData++;
00146     if (uCrc16 & 0x8000)
00147     {
00148       uCrc16 = (uCrc16 & 0x7fff) << 1;
00149       uCrc16 ^= 0x8005;
00150     }
00151     else
00152     {
00153       uCrc16 <<= 1;
00154     }
00155     uCrc16 ^= ((unsigned short) abData[0] | 
00156            ((unsigned short)(abData[1]) << 8));
00157   }
00158   return uCrc16;
00159 }
00160 
00161 AREXPORT bool ArSickPacket::verifyCRC(void) 
00162 {
00163   int readLen = myReadLength;
00164   int len = myLength;
00165   ArTypes::Byte2 chksum;
00166   unsigned char c1, c2;
00167 
00168   myReadLength = myLength - 2;
00169   
00170   if (myReadLength < myHeaderLength)
00171     return false;
00172 
00173   c1 = bufToByte();
00174   c2 = bufToByte();
00175   myReadLength = readLen;
00176   chksum = (c1 & 0xff) | (c2 << 8);
00177 
00178   myLength = myLength - 2;
00179   if (chksum == calcCRC()) {
00180     myLength = len;
00181     return true;
00182   } else {
00183     myLength = len;
00184     return false;
00185   }
00186   
00187 }
00188 
00189 AREXPORT ArTime ArSickPacket::getTimeReceived(void)
00190 {
00191   return myTimeReceived;
00192 }
00193 
00194 AREXPORT void ArSickPacket::setTimeReceived(ArTime timeReceived)
00195 {
00196   myTimeReceived = timeReceived;
00197 }

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