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
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArActionRobotJoydrive.h"
00030 #include "ArRobot.h"
00031 #include "ariaInternal.h"
00032 #include "ArCommands.h"
00033
00040 ArActionRobotJoydrive::ArActionRobotJoydrive(
00041 const char *name, bool requireDeadmanPushed) :
00042 ArAction(name, "This action reads the joystick on the robot and sets the translational and rotational velocities based on this."),
00043 myHandleJoystickPacketCB(this, &ArActionRobotJoydrive::handleJoystickPacket),
00044 myConnectCB(this, &ArActionRobotJoydrive::connectCallback)
00045 {
00046 myRequireDeadmanPushed = requireDeadmanPushed;
00047 setNextArgument(ArArg("whether to require the deadman to be pushed or not", &myRequireDeadmanPushed, "If this is true then deadman will need to be pushed to drive, if false we'll drive based on the joystick all the time"));
00048 myDeadZoneLast = false;
00049 }
00050
00051 ArActionRobotJoydrive::~ArActionRobotJoydrive()
00052 {
00053
00054 }
00055
00056 void ArActionRobotJoydrive::setRobot(ArRobot *robot)
00057 {
00058 ArAction::setRobot(robot);
00059 if (myRobot != NULL)
00060 {
00061 myRobot->addConnectCB(&myConnectCB);
00062 myRobot->addPacketHandler(&myHandleJoystickPacketCB);
00063 if (robot->isConnected())
00064 connectCallback();
00065 }
00066 }
00067
00068 void ArActionRobotJoydrive::connectCallback(void)
00069 {
00070 myRobot->comInt(ArCommands::JOYINFO, 2);
00071 }
00072
00073 bool ArActionRobotJoydrive::handleJoystickPacket(
00074 ArRobotPacket *packet)
00075 {
00076 if (packet->getID() != 0xF8)
00077 return false;
00078
00079 myPacketReceivedTime.setToNow();
00080
00081 myButton1 = packet->bufToUByte();
00082 myButton2 = packet->bufToUByte();
00083 myJoyX = packet->bufToUByte2();
00084 myJoyY = packet->bufToUByte2();
00085 myThrottle = packet->bufToUByte2();
00086
00087
00088 return true;
00089 }
00090
00091 ArActionDesired *ArActionRobotJoydrive::fire(ArActionDesired currentDesired)
00092 {
00093 bool printing = false;
00094 myDesired.reset();
00095
00096 if (myRequireDeadmanPushed && !myButton1)
00097 {
00098 if (printing)
00099 printf("ArActionRobotJoydrive: Nothing\n");
00100 myDeadZoneLast = false;
00101 return NULL;
00102 }
00103
00104
00105 double ratioRot = -(myJoyX - 512) / 512.0;
00106 double ratioTrans = (myJoyY - 512) / 512.0;
00107 double ratioThrottle = myThrottle / 1024.0;
00108
00109 bool doTrans = ArMath::fabs(ratioTrans) > .33;
00110 bool doRot = ArMath::fabs(ratioRot) > .33;
00111
00112 if (0)
00113 printf("%.0f %.0f (x %.3f y %.3f throttle %.3f)\n", ratioTrans * ratioThrottle * 1000,
00114 ratioRot * ratioThrottle * 50, ratioTrans, ratioRot, ratioThrottle);
00115 if (!doTrans && !doRot)
00116 {
00117
00118
00119 if (myDeadZoneLast && !myRequireDeadmanPushed)
00120 {
00121 if (printing)
00122 printf("ArActionRobotJoydrive: deadzone Nothing\n");
00123 return NULL;
00124 }
00125
00126 if (printing)
00127 printf("ArActionRobotJoydrive: deadzone\n");
00128 myDesired.setVel(0);
00129 myDesired.setDeltaHeading(0);
00130 myDeadZoneLast = true;
00131 return &myDesired;
00132 }
00133
00134 myDeadZoneLast = false;
00135
00136
00137 if (doTrans && ((myRobot->getVel() > 0 && ratioTrans < -0.5) ||
00138 (myRobot->getVel() < 0 && ratioTrans > 0.5)))
00139 {
00140 if (printing)
00141 printf("ArActionRobotJoydrive: Decelerating trans more\n");
00142 myDesired.setTransDecel(myRobot->getTransDecel() * 3);
00143 }
00144
00145
00146
00147 if (doRot && ((myRobot->getRotVel() > 0 && ratioRot < -0.5) ||
00148 (myRobot->getRotVel() < 0 && ratioRot > 0.5)))
00149 {
00150 if (printing)
00151 printf("ArActionRobotJoydrive: Decelerating rot more\n");
00152 myDesired.setRotDecel(myRobot->getRotDecel() * 3);
00153 }
00154
00155 if (doTrans)
00156 myDesired.setVel(ratioTrans * ratioThrottle * myRobot->getTransVelMax());
00157 else
00158 myDesired.setVel(0);
00159
00160 printf("%.0f %.0f\n", ratioTrans * ratioThrottle * myRobot->getTransVelMax(),
00161 ratioRot * ratioThrottle * myRobot->getRotVelMax());
00162
00163
00164 if (doRot)
00165 myDesired.setRotVel(ratioRot * ratioThrottle * myRobot->getRotVelMax());
00166 else
00167 myDesired.setRotVel(0);
00168
00169 if(printing)
00170 printf("ArActionRobotJoydrive: (%ld ms ago) we got %d %d %.2f %.2f %.2f (speed %.0f %.0f)\n",
00171 myPacketReceivedTime.mSecSince(),
00172 myButton1, myButton2, ratioTrans, ratioRot, ratioThrottle,
00173 myRobot->getVel(), myRobot->getRotVel());
00174 return &myDesired;
00175 }