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