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 "ArRatioInputJoydrive.h"
00030 #include "ArRobot.h"
00031 #include "ariaInternal.h"
00032
00055 ArRatioInputJoydrive::ArRatioInputJoydrive(
00056 ArRobot *robot,
00057 ArActionRatioInput *input,
00058 int priority,
00059 bool stopIfNoButtonPressed,
00060 bool useOSCalForJoystick) :
00061 myFireCB(this, &ArRatioInputJoydrive::fireCallback)
00062 {
00063 myRobot = robot;
00064 myInput = input;
00065 myInput->addFireCallback(priority, &myFireCB);
00066 myFireCB.setName("Joydrive");
00067 if ((myJoyHandler = Aria::getJoyHandler()) == NULL)
00068 {
00069 myJoyHandler = new ArJoyHandler;
00070 myJoyHandler->init();
00071 Aria::setJoyHandler(myJoyHandler);
00072 }
00073
00074 myUseOSCal = useOSCalForJoystick;
00075 myPreviousUseOSCal = myUseOSCal;
00076 myStopIfNoButtonPressed = stopIfNoButtonPressed;
00077 myFiredLast = false;
00078 myPrinting = false;
00079 }
00080
00081 ArRatioInputJoydrive::~ArRatioInputJoydrive()
00082 {
00083 myInput->remFireCallback(&myFireCB);
00084 }
00085
00086 void ArRatioInputJoydrive::setStopIfNoButtonPressed(
00087 bool stopIfNoButtonPressed)
00088 {
00089 myStopIfNoButtonPressed = stopIfNoButtonPressed;
00090 }
00091
00092 bool ArRatioInputJoydrive::getStopIfNoButtonPressed(void)
00093 {
00094 return myStopIfNoButtonPressed;
00095 }
00096
00097 bool ArRatioInputJoydrive::joystickInited(void)
00098 {
00099 return myJoyHandler->haveJoystick();
00100 }
00101
00105 void ArRatioInputJoydrive::setUseOSCal(bool useOSCal)
00106 {
00107 myUseOSCal = useOSCal;
00108 myPreviousUseOSCal = useOSCal;
00109 myJoyHandler->setUseOSCal(useOSCal);
00110 }
00111
00115 bool ArRatioInputJoydrive::getUseOSCal(void)
00116 {
00117 return myUseOSCal;
00118 }
00119
00120
00121 void ArRatioInputJoydrive::fireCallback(void)
00122 {
00123 double rot, trans, throttle;
00124
00125 if (myPreviousUseOSCal != myUseOSCal)
00126 {
00127 myJoyHandler->setUseOSCal(myUseOSCal);
00128 myPreviousUseOSCal = myUseOSCal;
00129 }
00130
00131 if (myJoyHandler->haveJoystick() && myJoyHandler->getButton(1))
00132 {
00133
00134 myJoyHandler->getDoubles(&rot, &trans);
00135
00136 if (!myJoyHandler->haveZAxis())
00137 {
00138 throttle = 1;
00139 }
00140
00141
00142 else
00143 {
00144 throttle = myJoyHandler->getAxis(3);
00145 throttle += 1.0;
00146 throttle /= 2.0;
00147 }
00148 myInput->setRatios(trans * 100, -rot * 100, throttle * 100);
00149 myFiredLast = true;
00150 if (myPrinting)
00151 printf("joy %g %g %g\n", trans * 100, -rot * 100, throttle * 100);
00152 }
00153 else if (myJoyHandler->haveJoystick() && (myStopIfNoButtonPressed ||
00154 myFiredLast))
00155 {
00156 if (myPrinting)
00157 printf("joy nothing\n");
00158 myFiredLast = false;
00159 myInput->setRatios(0, 0, myInput->getThrottleRatio());
00160 }
00161
00162 }