This is basically just a demo of how to use the sony, but made fancy so you can drive around the robot and look at stuff with the camera. Press down button 1 to drive the robot, button 2 to move the camera, and both buttons to zoom the camera.
00001 #include "Aria.h" 00002 00003 00013 /* 00014 This class is the core of this demo, it adds itself to the robot given 00015 as a user task, then drives the robot and camera from the joystick 00016 */ 00017 class Joydrive 00018 { 00019 public: 00020 // constructor 00021 Joydrive(ArRobot *robot, int LRAmount = 15, int UDAmount = 10, 00022 int zoomAmount = 50); 00023 // destructor, its just empty 00024 ~Joydrive(void) {} 00025 00026 // the callback function 00027 void drive(void); 00028 00029 // callbacks for key presses 00030 void up(void); 00031 void down(void); 00032 void left(void); 00033 void right(void); 00034 void in(void); 00035 void out(void); 00036 void center(void); 00037 protected: 00038 // the rotational max for the robot 00039 int myRotValRobot; 00040 // the translational max for the robot 00041 int myTransValRobot; 00042 // the pan max for the camera 00043 int myPanValCamera; 00044 // the tilt max for the camera 00045 int myTiltValCamera; 00046 // the zoom max for the camera 00047 int myZoomValCamera; 00048 00049 // joystick handler 00050 ArJoyHandler myJoyHandler; 00051 00052 // the camera 00053 ArSonyPTZ myCam; 00054 // the robot pointer 00055 ArRobot *myRobot; 00056 // callback for the drive function 00057 ArFunctorC<Joydrive> myDriveCB; 00058 ArFunctorC<Joydrive> myUpCB; 00059 ArFunctorC<Joydrive> myDownCB; 00060 ArFunctorC<Joydrive> myLeftCB; 00061 ArFunctorC<Joydrive> myRightCB; 00062 ArFunctorC<Joydrive> myInCB; 00063 ArFunctorC<Joydrive> myOutCB; 00064 ArFunctorC<Joydrive> myCenterCB; 00065 int myLRAmount; 00066 int myUDAmount; 00067 int myZoomAmount; 00068 }; 00069 00070 /* 00071 Constructor, sets the robot pointer, and some initial values, also note the 00072 use of constructor chaining on myCam and myDriveCB. 00073 */ 00074 Joydrive::Joydrive(ArRobot *robot, int LRAmount, int UDAmount, int zoomAmount) : 00075 myCam(robot), 00076 myDriveCB(this, &Joydrive::drive), 00077 myUpCB(this, &Joydrive::up), 00078 myDownCB(this, &Joydrive::down), 00079 myLeftCB(this, &Joydrive::left), 00080 myRightCB(this, &Joydrive::right), 00081 myInCB(this, &Joydrive::in), 00082 myOutCB(this, &Joydrive::out), 00083 myCenterCB(this, &Joydrive::center) 00084 00085 { 00086 // set the robot pointer and add the joydrive as a user task 00087 myRobot = robot; 00088 myRobot->addUserTask("joydrive", 50, &myDriveCB); 00089 00090 00091 00092 00093 // initalize some variables 00094 myRotValRobot = 100; 00095 myTransValRobot = 700; 00096 myPanValCamera = 8; 00097 myTiltValCamera = 3; 00098 myZoomValCamera = 50; 00099 myLRAmount = LRAmount; 00100 myUDAmount = UDAmount; 00101 myZoomAmount = zoomAmount; 00102 00103 ArKeyHandler *keyHandler; 00104 myRobot = robot; 00105 // see if there is already a keyhandler, if not make one for ourselves 00106 if ((keyHandler = Aria::getKeyHandler()) == NULL) 00107 { 00108 keyHandler = new ArKeyHandler; 00109 Aria::setKeyHandler(keyHandler); 00110 myRobot->attachKeyHandler(keyHandler); 00111 } 00112 // now that we have one, add our keys as callbacks, print out big 00113 // warning messages if they fail 00114 if (!keyHandler->addKeyHandler(ArKeyHandler::UP, &myUpCB)) 00115 ArLog::log(ArLog::Terse, "The key handler already has a key for up, keydrive will not work correctly."); 00116 if (!keyHandler->addKeyHandler(ArKeyHandler::DOWN, &myDownCB)) 00117 ArLog::log(ArLog::Terse, "The key handler already has a key for down, keydrive will not work correctly."); 00118 if (!keyHandler->addKeyHandler(ArKeyHandler::LEFT, &myLeftCB)) 00119 ArLog::log(ArLog::Terse, 00120 "The key handler already has a key for left, keydrive will not work correctly."); 00121 if (!keyHandler->addKeyHandler(ArKeyHandler::RIGHT, &myRightCB)) 00122 ArLog::log(ArLog::Terse, 00123 "The key handler already has a key for right, keydrive will not work correctly."); 00124 if (!keyHandler->addKeyHandler('z', &myInCB)) 00125 ArLog::log(ArLog::Terse, 00126 "The key handler already has a key for 'z', keydrive will not work correctly."); 00127 if (!keyHandler->addKeyHandler('Z', &myInCB)) 00128 ArLog::log(ArLog::Terse, 00129 "The key handler already has a key for 'Z', keydrive will not work correctly."); 00130 if (!keyHandler->addKeyHandler('x', &myOutCB)) 00131 ArLog::log(ArLog::Terse, 00132 "The key handler already has a key for 'x', keydrive will not work correctly."); 00133 if (!keyHandler->addKeyHandler('X', &myOutCB)) 00134 ArLog::log(ArLog::Terse, 00135 "The key handler already has a key for 'X', keydrive will not work correctly."); 00136 if (!keyHandler->addKeyHandler('c', &myCenterCB)) 00137 ArLog::log(ArLog::Terse, 00138 "The key handler already has a key for 'c', keydrive will not work correctly."); 00139 if (!keyHandler->addKeyHandler('C', &myCenterCB)) 00140 ArLog::log(ArLog::Terse, 00141 "The key handler already has a key for 'C', keydrive will not work correctly."); 00142 00143 00144 // initialize the joystick 00145 myJoyHandler.init(); 00146 // see if we have a joystick, and let the usre know the results 00147 if (myJoyHandler.haveJoystick()) 00148 { 00149 printf("Have a joystick\n\n"); 00150 } 00151 // we don't have a joystic, so get out 00152 else 00153 { 00154 printf("Do not have a joystick, only the keyboard will work.\n"); 00155 } 00156 } 00157 00158 void Joydrive::left(void) 00159 { 00160 myCam.panTiltRel(-myLRAmount, 0); 00161 } 00162 00163 void Joydrive::right(void) 00164 { 00165 myCam.panTiltRel(myLRAmount, 0); 00166 } 00167 00168 void Joydrive::up(void) 00169 { 00170 myCam.panTiltRel(0, myUDAmount); 00171 } 00172 00173 void Joydrive::down(void) 00174 { 00175 myCam.panTiltRel(0, -myUDAmount); 00176 } 00177 00178 void Joydrive::in(void) 00179 { 00180 myCam.zoomRel(myZoomAmount); 00181 } 00182 00183 void Joydrive::out(void) 00184 { 00185 myCam.zoomRel(-myZoomAmount); 00186 } 00187 00188 void Joydrive::center(void) 00189 { 00190 myCam.panTilt(0,0); 00191 myCam.zoom(0); 00192 } 00193 00194 void Joydrive::drive(void) 00195 { 00196 int trans, rot; 00197 int pan, tilt; 00198 int zoom, nothing; 00199 00200 // if both buttons are pushed, zoom the joystick 00201 if (myJoyHandler.haveJoystick() && myJoyHandler.getButton(1) && 00202 myJoyHandler.getButton(2)) 00203 { 00204 // set its speed so we get desired value range, we only care about y 00205 myJoyHandler.setSpeeds(0, myZoomValCamera); 00206 // get the values 00207 myJoyHandler.getAdjusted(¬hing, &zoom); 00208 // zoom the camera 00209 myCam.zoomRel(zoom); 00210 } 00211 // if both buttons aren't pushed, see if one button is pushed 00212 else 00213 { 00214 // if button one is pushed, drive the robot 00215 if (myJoyHandler.haveJoystick() && myJoyHandler.getButton(1)) 00216 { 00217 // set the speed on the joystick so we get the values we want 00218 myJoyHandler.setSpeeds(myRotValRobot, myTransValRobot); 00219 // get the values 00220 myJoyHandler.getAdjusted(&rot, &trans); 00221 // set the robots speed 00222 myRobot->setVel(trans); 00223 myRobot->setRotVel(-rot); 00224 } 00225 // if button one isn't pushed, stop the robot 00226 else 00227 { 00228 myRobot->setVel(0); 00229 myRobot->setRotVel(0); 00230 } 00231 // if button two is pushed, move the camera 00232 if (myJoyHandler.haveJoystick() && myJoyHandler.getButton(2)) 00233 { 00234 // set the speeds on the joystick so we get desired value range 00235 myJoyHandler.setSpeeds(myPanValCamera, myTiltValCamera); 00236 // get the values 00237 myJoyHandler.getAdjusted(&pan, &tilt); 00238 // drive the camera 00239 myCam.panTiltRel(pan, tilt); 00240 } 00241 } 00242 00243 } 00244 00245 int main(int argc, char **argv) 00246 { 00247 // just some stuff for returns 00248 std::string str; 00249 int ret; 00250 00251 // robots connection 00252 ArSerialConnection con; 00253 // the robot, this turns state reflection off 00254 ArRobot robot(NULL, false); 00255 // the joydrive as defined above, this also adds itself as a user task 00256 Joydrive joyd(&robot); 00257 00258 // mandatory init 00259 Aria::init(); 00260 00261 // open the connection, if it fails, exit 00262 if ((ret = con.open()) != 0) 00263 { 00264 str = con.getOpenMessage(ret); 00265 printf("Open failed: %s\n", str.c_str()); 00266 Aria::shutdown(); 00267 return 1; 00268 } 00269 00270 // set the connection o nthe robot 00271 robot.setDeviceConnection(&con); 00272 // connect, if we fail, exit 00273 if (!robot.blockingConnect()) 00274 { 00275 printf("Could not connect to robot... exiting\n"); 00276 Aria::shutdown(); 00277 return 1; 00278 } 00279 00280 // turn off the sonar, enable the motors, turn off amigobot sounds 00281 robot.comInt(ArCommands::SONAR, 0); 00282 robot.comInt(ArCommands::ENABLE, 1); 00283 robot.comInt(ArCommands::SOUNDTOG, 0); 00284 00285 // run, if we lose connection to the robot, exit 00286 robot.run(true); 00287 00288 // shutdown and go away 00289 Aria::shutdown(); 00290 return 0; 00291 } 00292
1.4.7