Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArRobotJoyHandler.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 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 "ArRobotJoyHandler.h"
00029 #include "ArRobot.h"
00030 #include "ArCommands.h"
00031 
00032 AREXPORT ArRobotJoyHandler::ArRobotJoyHandler(ArRobot *robot) : 
00033     myHandleJoystickPacketCB(this, &ArRobotJoyHandler::handleJoystickPacket),
00034     myConnectCB(this, &ArRobotJoyHandler::connectCallback)
00035 {
00036   myRobot = robot;
00037 
00038   myHandleJoystickPacketCB.setName("ArRobotJoyHandler");
00039   myRobot->addConnectCB(&myConnectCB);
00040   myRobot->addPacketHandler(&myHandleJoystickPacketCB, ArListPos::FIRST);
00041   if (myRobot->isConnected())
00042     connectCallback();
00043 
00044   myStarted.setToNow();
00045   myButton1 = 0;
00046   myButton2 = 0;
00047   myJoyX = 0;
00048   myJoyY = 0;
00049   myThrottle = 1;
00050   myGotData = false;
00051 }
00052 
00053 AREXPORT ArRobotJoyHandler::~ArRobotJoyHandler()
00054 {
00055   myRobot->remConnectCB(&myConnectCB);
00056   myRobot->remPacketHandler(&myHandleJoystickPacketCB);
00057 }
00058 
00059 
00060 AREXPORT void ArRobotJoyHandler::connectCallback(void)
00061 {
00062   myRobot->comInt(ArCommands::JOYINFO, 2);
00063 }
00064 
00065 AREXPORT bool ArRobotJoyHandler::handleJoystickPacket(ArRobotPacket *packet)
00066 {
00067 
00068   if (packet->getID() != 0xF8)
00069     return false;
00070   
00071   myDataReceived.setToNow();
00072 
00073   if (packet->bufToUByte() != 0)
00074     myButton1 = true;
00075   else
00076     myButton1 = false;
00077 
00078   if (packet->bufToUByte() != 0)
00079     myButton2 = true;
00080   else
00081     myButton2  = false;
00082 
00083   // these should vary between 1 and -1
00084   myJoyX = -((double)packet->bufToUByte2() - 512.0) / 512.0;
00085   myJoyY = ((double)packet->bufToUByte2() - 512.0) / 512.0;
00086   // these should vary between 1 and 0
00087   myThrottle = (double)packet->bufToUByte2() / 1024.0;
00088 
00089   /*
00090   ArLog::log(ArLog::Normal, 
00091          "%10d.%03d ago %5.3f %5.3f %5.3f %d %d vel %4.0f %4.0f", 
00092          myStarted.secSince(), myStarted.mSecSince() % 1000, 
00093          myJoyX, myJoyY, myThrottle, myButton1, myButton2, 
00094          myRobot->getVel(), myRobot->getRotVel());
00095   */
00096   //  printf("%d %d %g %g %g\n", myButton1, myButton2, myJoyX, myJoyY, myThrottle);
00097   if (!myGotData)
00098   {
00099     ArLog::log(ArLog::Verbose, "Received joystick information from the robot");
00100     myGotData = true;
00101   }
00102   return true;
00103 }
00104 
00105 AREXPORT void ArRobotJoyHandler::getDoubles(double *x, double *y, double *z)
00106 {
00107   if (x != NULL)
00108     *x = myJoyX;
00109   if (y != NULL)
00110     *y = myJoyY;
00111   if (z != NULL)
00112     *z = myThrottle;
00113 }

Generated on Tue Feb 20 10:51:41 2007 for Aria by  doxygen 1.4.0