00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "ariaOSDef.h"
00027 #include "ArExport.h"
00028 #include "ArRobotConfigPacketReader.h"
00029 #include "ArRobot.h"
00030 #include "ArRobotPacket.h"
00031 #include "ArCommands.h"
00032
00043 AREXPORT ArRobotConfigPacketReader::ArRobotConfigPacketReader(
00044 ArRobot *robot, bool onlyOneRequest, ArFunctor *packetArrivedCB) :
00045 myPacketHandlerCB(this, &ArRobotConfigPacketReader::packetHandler),
00046 myConnectedCB(this, &ArRobotConfigPacketReader::connected)
00047 {
00048 myRobot = robot;
00049 myPacketHandlerCB.setName("ArRobotConfigPacketReader");
00050 myRobot->addPacketHandler(&myPacketHandlerCB);
00051 myRobot->addConnectCB(&myConnectedCB);
00052 myOnlyOneRequest = onlyOneRequest;
00053 myPacketRequested = false;
00054 myPacketArrived = false;
00055 myPacketArrivedCB = packetArrivedCB;
00056 }
00057
00058 AREXPORT ArRobotConfigPacketReader::~ArRobotConfigPacketReader(void)
00059 {
00060 myRobot->remPacketHandler(&myPacketHandlerCB);
00061 myRobot->remConnectCB(&myConnectedCB);
00062 }
00063
00064 AREXPORT bool ArRobotConfigPacketReader::requestPacket(void)
00065 {
00066
00067 if (myOnlyOneRequest && myPacketArrived)
00068 return false;
00069
00070 if (myPacketRequested && myLastPacketRequest.mSecSince() < 200)
00071 return true;
00072
00073 myPacketArrived = false;
00074 myPacketRequested = true;
00075 myLastPacketRequest.setToNow();
00076 myRobot->comInt(ArCommands::CONFIG, 1);
00077 return true;
00078 }
00079
00080 AREXPORT void ArRobotConfigPacketReader::connected(void)
00081 {
00082 if (myPacketRequested)
00083 myRobot->comInt(ArCommands::CONFIG, 1);
00084 }
00085
00086 AREXPORT bool ArRobotConfigPacketReader::packetHandler(ArRobotPacket *packet)
00087 {
00088 char buf[256];
00089
00090
00091 if (packet->getID() != 0x20)
00092 return false;
00093
00094 if (myPacketArrived)
00095 return false;
00096
00097 if (!myPacketRequested)
00098 return false;
00099
00100 myPacketRequested = false;
00101 myPacketArrived = true;
00102
00103 packet->bufToStr(buf, sizeof(buf));
00104 myType = buf;
00105 packet->bufToStr(buf, sizeof(buf));
00106 mySubType = buf;
00107 packet->bufToStr(buf, sizeof(buf));
00108 mySerialNumber = buf;
00109 packet->bufToUByte();
00110 myRotVelTop = packet->bufToUByte2();
00111 myTransVelTop = packet->bufToUByte2();
00112 myRotAccelTop = packet->bufToUByte2();
00113 myTransAccelTop = packet->bufToUByte2();
00114 myPwmMax = packet->bufToUByte2();
00115 packet->bufToStr(buf, sizeof(buf));
00116 myName = buf;
00117 mySipCycleTime = packet->bufToUByte();
00118 myHostBaud = packet->bufToUByte();
00119 myAux1Baud = packet->bufToUByte();
00120 myHasGripper = (bool)packet->bufToUByte2();
00121 myFrontSonar = (bool) packet->bufToUByte2();
00122 myRearSonar = (bool) packet->bufToUByte();
00123 myLowBattery = packet->bufToUByte2();
00124 myRevCount = packet->bufToUByte2();
00125 myWatchdog = packet->bufToUByte2();
00126 myNormalMPacs = (bool) packet->bufToUByte();
00127 myStallVal = packet->bufToUByte2();
00128 myStallCount = packet->bufToUByte2();
00129 myJoyVel = packet->bufToUByte2();
00130 myJoyRotVel = packet->bufToUByte2();
00131 myRotVelMax = packet->bufToUByte2();
00132 myTransVelMax = packet->bufToUByte2();
00133 myRotAccel = packet->bufToUByte2();
00134 myRotDecel = packet->bufToUByte2();
00135 myRotKP = packet->bufToUByte2();
00136 myRotKV = packet->bufToUByte2();
00137 myRotKI = packet->bufToUByte2();
00138 myTransAccel = packet->bufToUByte2();
00139 myTransDecel = packet->bufToUByte2();
00140 myTransKP = packet->bufToUByte2();
00141 myTransKV = packet->bufToUByte2();
00142 myTransKI = packet->bufToUByte2();
00143 myFrontBumps = packet->bufToUByte();
00144 myRearBumps = packet->bufToUByte();
00145 myHasCharger = packet->bufToUByte();
00146 mySonarCycle = packet->bufToUByte();
00147 if (packet->bufToUByte() == 2)
00148 myResetBaud = true;
00149 else
00150 myResetBaud = false;
00151 myGyroType = packet->bufToUByte();
00152 if (myGyroType == 1)
00153 myHasGyro = true;
00154 else
00155 myHasGyro = false;
00156 myDriftFactor = packet->bufToUByte2();
00157 myAux2Baud = packet->bufToUByte();
00158 myAux3Baud = packet->bufToUByte();
00159 myTicksMM = packet->bufToUByte2();
00160 myShutdownVoltage = packet->bufToUByte2();
00161
00162 if (myPacketArrivedCB != NULL)
00163 {
00164 myPacketArrivedCB->invoke();
00165 }
00166 return true;
00167 }
00168
00169 AREXPORT void ArRobotConfigPacketReader::log(void) const
00170 {
00171 std::string str;
00172 str = buildString();
00173 ArLog::log(ArLog::Terse, str.c_str());
00174 }
00175
00180 AREXPORT std::string ArRobotConfigPacketReader::buildString(void) const
00181 {
00182 std::string ret;
00183
00184 char line[32000];
00185 sprintf(line, "General information:\n");
00186 ret += line;
00187 sprintf(line, "Robot is type '%s' subtype '%s'\n",
00188 getType(), getSubType());
00189 ret += line;
00190 sprintf(line, "serial number '%s' name '%s'\n",
00191 getSerialNumber(), getName());
00192 ret += line;
00193 sprintf(line, "Intrinsic properties and unsettable maxes:\n");
00194 ret += line;
00195 sprintf(line, "RotVelTop %d RotAccelTop %d\n",
00196 getRotVelTop(), getRotAccelTop());
00197 ret += line;
00198 sprintf(line, "TransVelTop %d TransAccelTop %d\n",
00199 getTransVelTop(), getTransAccelTop());
00200 ret += line;
00201 sprintf(line, "PWMMax %d ResetBaud %s\n", getPwmMax(),
00202 ArUtil::convertBool(getResetBaud()));
00203 ret += line;
00204 sprintf(line, "Current values:\n");
00205 ret += line;
00206 sprintf(line, "RotVelMax %d RotAccel %d RotDecel %d\n",
00207 getRotVelMax(), getRotAccel(), getRotDecel());
00208 ret += line;
00209 sprintf(line, "TransVelMax %d TransAccel %d TransDecel %d\n",
00210 getTransVelMax(), getTransAccel(), getTransDecel());
00211 ret += line;
00212 sprintf(line, "Accessories:\n");
00213 ret += line;
00214 sprintf(line,
00215 "Gripper %s FrontSonar %s RearSonar %s Charger %d Gyro %s\n",
00216 ArUtil::convertBool(getHasGripper()),
00217 ArUtil::convertBool(getFrontSonar()),
00218 ArUtil::convertBool(getRearSonar()),
00219 getHasCharger(),
00220 ArUtil::convertBool(getHasGyro()));
00221 ret += line;
00222 sprintf(line, "FrontBumps %d RearBumps %d\n",
00223 getFrontBumps(), getRearBumps());
00224 ret += line;
00225 sprintf(line, "Settings:\n");
00226 ret += line;
00227 sprintf(line, "SipCycle %d SonarCycle %d HostBaud %d Aux1Baud %d\n", getSipCycleTime(), getSonarCycle(), getHostBaud(), getAux1Baud());
00228 ret += line;
00229 sprintf(line, "StallVal %d StallCount %d RevCount %d Watchdog %d\n",
00230 getStallVal(), getStallCount(), getRevCount(), getWatchdog());
00231 ret += line;
00232 sprintf(line, "JoyVel %d JoyRVel %d NormalMotorPackets %s\n", getJoyVel(), getJoyRotVel(), ArUtil::convertBool(getNormalMPacs()));
00233 ret += line;
00234 sprintf(line, "PID Settings:\n");
00235 ret += line;
00236 sprintf(line, "Rot kp %d kv %d ki %d\n", getRotKP(), getRotKV(),
00237 getRotKI());
00238 ret += line;
00239 sprintf(line, "Trans kp %d kv %d ki %d\n", getTransKP(),
00240 getTransKV(), getTransKI());
00241 ret += line;
00242 sprintf(line, "DriftFactor %d\n", getDriftFactor());
00243 ret += line;
00244 sprintf(line, "Aux2Baud setting %d, Aux3Baud setting %d\n", getAux2Baud(), getAux3Baud());
00245 ret += line;
00246 sprintf(line, "TicksMM: %d\n", getTicksMM());
00247 ret += line;
00248 sprintf(line, "Shutdown Voltage: %d\n", getShutdownVoltage());
00249 ret += line;
00250
00251 return ret;
00252 }