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

ArRobotPacket.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 "ArRobotPacket.h"
00030 #include "stdio.h"
00031 
00038 ArRobotPacket::ArRobotPacket(unsigned char sync1,
00039                                       unsigned char sync2) :
00040     ArBasePacket(265, 4, NULL, 2)
00041 {
00042   mySync1 = sync1;
00043   mySync2 = sync2;
00044 }
00045 
00046 ArRobotPacket::~ArRobotPacket()
00047 {
00048 }
00049 
00050 ArTypes::UByte ArRobotPacket::getID(void)
00051 {
00052   if (myLength >= 4)
00053     return myBuf[3];
00054   else
00055     return 0;
00056 }
00057 
00058 void ArRobotPacket::setID(ArTypes::UByte id)
00059 {
00060   myBuf[3] = id;
00061 }
00062 
00063 void ArRobotPacket::finalizePacket(void)
00064 {
00065   int len = myLength;
00066   int chkSum;
00067 
00068   myLength = 0;
00069   uByteToBuf(mySync1);
00070   uByteToBuf(mySync2);
00071   uByteToBuf(len - getHeaderLength() + 3);
00072   myLength = len;
00073 
00074   chkSum = calcCheckSum();
00075   byteToBuf((chkSum >> 8) & 0xff );
00076   byteToBuf(chkSum & 0xff );
00077   /* Put this in if you want to see the packets being outputted 
00078      printf("Output(%3d) ", getID());
00079      printHex();
00080   */
00081   // or put this in if you just want to see the type
00082   //printf("Output %d\n", getID());
00083 }
00084 
00085 ArTypes::Byte2 ArRobotPacket::calcCheckSum(void)
00086 {
00087   int i;
00088   unsigned char n;
00089   int c = 0;
00090 
00091   i = 3;
00092   n = myBuf[2] - 2;
00093   while (n > 1) {
00094     c += ((unsigned char)myBuf[i]<<8) | (unsigned char)myBuf[i+1];
00095     c = c & 0xffff;
00096     n -= 2;
00097     i += 2;
00098   }
00099   if (n > 0) 
00100     c = c ^ (int)((unsigned char) myBuf[i]);
00101   return c;
00102 }
00103 
00104 bool ArRobotPacket::verifyCheckSum(void) 
00105 {
00106   ArTypes::Byte2 chksum;
00107   unsigned char c1, c2;
00108 
00109   if (myLength - 2 < myHeaderLength)
00110     return false;
00111 
00112   c2 = myBuf[myLength - 2];
00113   c1 = myBuf[myLength - 1];
00114   chksum = (c1 & 0xff) | (c2 << 8);
00115 
00116   if (chksum == calcCheckSum()) {
00117     return true;
00118   } else {
00119     return false;
00120   }
00121   
00122 }
00123 
00124 ArTime ArRobotPacket::getTimeReceived(void)
00125 {
00126   return myTimeReceived;
00127 }
00128 
00129 void ArRobotPacket::setTimeReceived(ArTime timeReceived)
00130 {
00131   myTimeReceived = timeReceived;
00132 }

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