joystick.cpp
Go to the documentation of this file.
00001 #include "joystick.h"
00002 
00003 #ifdef Q_WS_WIN
00004 #include "windows.h"
00005 #include "mmsystem.h"
00006 #else
00007 #include "linux/joystick.h"
00008 #include <unistd.h>
00009 #include <fcntl.h>
00010 #endif
00011 
00013 const char* JOY_DEV = "/dev/input/js0";
00015 const int MS_50 = 50000;
00016 
00018 const int DRIVE_X_AXIS = 0;
00020 const int DRIVE_Y_AXIS = 1;
00021 
00022 const int ARM_X_AXIS = 4;
00023 const int ARM_Y_AXIS = 5;
00024 const int WRIST_AXIS = 2;
00025 const int GRIPPER_AXIS = 3;
00026 
00028 const int DRIVE_ENABLE = 0;
00029 
00030 Joystick::Joystick(QWidget *parent){
00031 
00032     p = parent;
00033 }
00034 
00035 double Joystick::normalizeAxis(int value) //normalize the joystick axis
00036 {
00037     if (value > 0)
00038     {
00039         if (value < 100)
00040         {
00041             return 0.0;
00042         }
00043         else
00044         {
00045             return -(value - 100) / 32668.0;
00046         }
00047     }
00048     else
00049     {
00050         if (value > -100)
00051         {
00052             return 0.0;
00053         }
00054         else
00055         {
00056             return -(value + 100) / 32668.0;
00057         }
00058     }
00059 }
00060 
00061 
00062 void Joystick::run(){
00063 
00064 double *axis=NULL;
00065 int num_of_axis=0;
00066 
00067 #ifndef Q_WS_WIN
00068     int joy_fd;
00069 
00070 
00071     int x;
00072     struct js_event js;
00073 
00074     // keep looping until we open the joystick
00075     while ((joy_fd = open( JOY_DEV , O_RDONLY)) == -1)
00076     {
00077         usleep(MS_50);
00078     }
00079 
00080     // determine what the capabilities are supported
00081     ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
00082 
00083     // allocate memory to support what we have
00084     axis = (double *) calloc( num_of_axis, sizeof( double ) );
00085 
00086     fcntl( joy_fd, F_SETFL, O_NONBLOCK );       /* use non-blocking mode */
00087 
00088 
00089 
00090 #else
00091 
00092     UINT wNumDevs;
00093     JOYINFO joyinfo;
00094     JOYCAPS joycaps;
00095     if((wNumDevs = joyGetNumDevs()) != 0)
00096     {
00097         // keep looping until we open the joystick
00098         while (joyGetPos(JOYSTICKID1,&joyinfo) != JOYERR_UNPLUGGED)
00099         {
00100             usleep(MS_50);
00101         }
00102         joyGetDevCaps(JOYSTICKID1,&joycaps,sizeof(joycaps));
00103 
00104 
00105         joySetCapture(p->winId(),JOYSTICKID1, 0, TRUE);
00106         joySetCapture(p->winId(),JOYSTICKID2, 0, TRUE);
00107     }
00108 
00109 #endif
00110 #ifndef Q_WS_WIN
00111 
00112     // clear the read queue
00113     while (read(joy_fd, &js, sizeof(struct js_event)) > 0)
00114     {
00115     }
00116 
00117     // hang here forever reading from the joystick
00118     while (1)
00119     {
00120         // Process the current joystick state and read the next one
00121         while (read(joy_fd, &js, sizeof(struct js_event)) <= 0)
00122         {
00123             if (axis[ARM_X_AXIS] > 0.5)
00124             {
00125                 arm->moveArmLeft();
00126             }
00127             else if (axis[ARM_X_AXIS] < -0.5)
00128             {
00129                 arm->moveArmRight();
00130             }
00131             if (axis[ARM_Y_AXIS] > 0.5)
00132             {
00133                 arm->moveArmUp();
00134             }
00135             else if (axis[ARM_Y_AXIS] < -0.5)
00136             {
00137                  arm->moveArmDown();
00138             }
00139             if (axis[GRIPPER_AXIS] < -0.5)
00140             {
00141                  ros->openGripper();
00142             }
00143             else if (axis[GRIPPER_AXIS] > 0.5)
00144             {
00145                 ros->closeGripper();
00146             }
00147             if (axis[WRIST_AXIS] < -0.5)
00148             {
00149                 wrist->turnCounterClockwise();
00150             }
00151             else if (axis[WRIST_AXIS] > 0.5)
00152             {
00153                 wrist->turnClockwise();
00154             }
00155             usleep(MS_50);
00156         }
00157 
00158         // see what to do with the event
00159         switch (js.type & ~JS_EVENT_INIT)
00160         {
00161         case JS_EVENT_AXIS:
00162             axis[js.number] = normalizeAxis(js.value);
00163             // is this a drive axis change? If so inform the drive system
00164             if ((js.number == DRIVE_X_AXIS) || (js.number == DRIVE_Y_AXIS))
00165             {
00166                 JoystickAxisChange(axis[DRIVE_X_AXIS], axis[DRIVE_Y_AXIS]);
00167             }
00168             break;
00169         case JS_EVENT_BUTTON:
00170             if (js.number == DRIVE_ENABLE)
00171             {
00172                 // inform the drive service of a button status change
00173                 DriveEnableChange(js.value);
00174             }
00175             break;
00176         }
00177     }
00178     close( joy_fd );    // too bad we never get here
00179 
00180 #endif
00181 }
00182 
00183 void Joystick::JoystickAxisChange(double x, double y)
00184 //computes the new speed of the robot
00185 {
00186         double r=sqrt(x*x+y*y);
00187         if (r>1.0)
00188                 r=1.0;
00189         double radTheta=atan2(-y,x)-M_PI/2.0;
00190 
00191         float translation=-r*cos(radTheta);
00192         float rotation=-r*sin(radTheta);
00193 
00194         if (x==0)
00195                 {
00196                         if (y==0)
00197                                 {
00198                                         translation=0.0;
00199                                         rotation=0.0;
00200                                 }
00201                         else
00202                                 {
00203                                         rotation=0.0;
00204                                 }
00205                 }
00206         else
00207                 {
00208                         if (y==0)
00209                                 {
00210                                         translation=0;
00211                                         rotation=x;
00212                                 }
00213                         else
00214                                 {
00215                                         // neither is zero, do nothing
00216                                 }
00217                 }
00218 /*
00219         if (driveEnabled)
00220     {
00221             ros->setSpeed(translation,rotation);
00222     }
00223     else
00224     {
00225         ros->setSpeed(0.0, 0.0);
00226         }*/
00227 }
00228 
00229 void Joystick::DriveEnableChange(const int value)
00230 //if value is true, the robot can moves thanks to the joystick
00231 {
00232         if (value != 0)
00233         {
00234                 driveEnabled = true;
00235         }
00236         else
00237         {
00238                 driveEnabled = false;
00239         }
00240 }
00241 
00242 void Joystick::setArmWidget(ArmWidget * arm_)
00243 //gives pointer to the arm widget
00244 {
00245     arm = arm_;
00246 }
00247 
00248 void Joystick::setRos(Ros * ros_)
00249 //gives pointer to the ros thread
00250 {
00251     ros = ros_;
00252 }
00253 
00254 void Joystick::setWristWidget(WristWidget * wrist_)
00255 //gives pointer to the wrist widget
00256 {
00257     wrist = wrist_;
00258 }


corobot_teleop
Author(s): Morgan Cormier/Gang Li/mcormier@coroware.com
autogenerated on Tue Jan 7 2014 11:39:41