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

ArSickPacket.cpp

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

Generated on Wed Oct 19 12:56:37 2005 for Aria by  doxygen 1.4.0