joyd.cc

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <unistd.h>
00004 #include <fcntl.h>
00005 #include <sys/ioctl.h>
00006 #include <linux/joystick.h>
00007 #include <time.h>
00008 #include <math.h>
00009 #include "robomon_orte.h"
00010 
00011 #define JOY_DEV "/dev/input/js0"
00012 #define VMAX 16000
00013 
00014 int main(int argc, char *argv[]) 
00015 {
00016         struct timespec timer;
00017         int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
00018         char *button=NULL, joy_name[80];
00019         double r=0.15,angle,lenght,v, omega;    
00020         
00021         struct js_event js;
00022         
00023         orte_function(NULL);
00024         
00025         if((joy_fd=open(JOY_DEV, O_NONBLOCK))==-1)
00026         {
00027                 printf("Unable to open joystick!\n");
00028                 return -1;
00029         }
00030         
00031         ioctl(joy_fd, JSIOCGAXES, &num_of_axis);
00032         ioctl(joy_fd, JSIOCGBUTTONS, &num_of_buttons);
00033         ioctl(joy_fd, JSIOCGNAME(80), &joy_name);
00034         
00035         printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n", joy_name, num_of_axis, num_of_buttons);
00036         
00037         axis = (int *) calloc( num_of_axis, sizeof( int ) );
00038         button = (char *) calloc( num_of_buttons, sizeof( char ) );
00039         
00040         while(1) 
00041         {
00042                 timer.tv_sec = 0;
00043                 timer.tv_nsec = 2000000;
00044                 
00045                 while(read(joy_fd, &js, sizeof(struct js_event))>0) {           
00046                         switch (js.type & ~JS_EVENT_INIT)
00047                         {
00048                                 case JS_EVENT_AXIS:
00049                                         axis   [ js.number ] = js.value;
00050                                         break;
00051                                 case JS_EVENT_BUTTON:
00052                                         button [ js.number ] = js.value;
00053                                         break;
00054                         }
00055                         joy.axisX = axis[0];
00056                         joy.axisY = axis[1];
00057                         joy.axisZ = axis[2];
00058                         joy.axisR = axis[3];
00059                         joy.axisS1 = axis[4];
00060                         joy.axisS2 = axis[5];
00061                         
00062                         joy.button1 = button[0];
00063                         joy.button2 = button[1];
00064                         joy.button3 = button[2];
00065                         joy.button4 = button[3];
00066                         joy.button5 = button[4];
00067                         joy.button6 = button[5];
00068                         joy.button7 = button[6];
00069                         joy.button8 = button[7];
00070                         joy.button9 = button[8];
00071                         joy.button10 = button[9];
00072                         joy.button11 = button[10];
00073                         joy.button12 = button[11];
00074                         
00075 /*                      v = (double)(axis[0]/32768.0);// * 10000;
00076                         omega = (double)(axis[1]/32768.0);// * 10000;
00077                         mspeed.right = -(v + r*omega);
00078                         mspeed.left = v - r*omega;*/
00079                         v = (double)axis[1]/32768.0;
00080                         omega = (double)axis[0]/32768.0;
00081                         lenght = sqrt( pow(v,2) + pow(omega,2));
00082                         if (lenght >= 1)                // If lenght si more than 1 is used unit circle
00083                         {
00084                                 angle = atan2(axis[1], axis[0]);        // computing new speed and omega
00085                                 v = sin(angle);
00086                                 omega = cos(angle);
00087                         }
00088                         omega *= 2;
00089                         mspeed.right = (int)(-((v + r*omega))*VMAX);
00090                         mspeed.left = (int)(-(v - r*omega)*VMAX);
00091                         printf("mspeed.left=%d, mspeed.right=%d\n",mspeed.left,mspeed.right);
00092                         printf("v %f, omega %f\n",v, omega);//#endif
00093                         printf("length %f, angle %f\n",lenght, angle);//#endif
00094                         printf("axisX %d, axisY %d\n",axis[0], axis[1]);//#endif
00095                         ORTEPublicationSend(publisherMotor);                    
00096                         ORTEPublicationSend(publisherJoy);
00097                         
00098                 }
00099                 ORTEPublicationSend(publisherMotor);
00100                 ORTEPublicationSend(publisherJoy);
00101                 nanosleep(&timer,NULL);
00102                 
00103 //              printf( "X: %6d  Y: %6d  ", axis[0], axis[1] );
00104                 
00105 /*              if( num_of_axis > 2 )
00106                         printf("Z: %6d  ", axis[2] );
00107                         
00108                 if( num_of_axis > 3 )
00109                         printf("R: %6d  ", axis[3] );
00110                 
00111                 if( num_of_axis > 4 )
00112                         printf("S1: %6d  ", axis[4] );
00113                 
00114                 if( num_of_axis > 5 )
00115                         printf("S2: %6d  ", axis[5] );*/
00116                 
00117                 
00118                         
00119 //              for( x=0 ; x<num_of_buttons ; ++x )
00120 //                      printf("B%d: %d  ", x, button[x] );
00121 
00122                 printf("  \r");
00123                 fflush(stdout);
00124         }
00125         
00126         close(joy_fd);
00127         
00128         return 0;
00129 }

Generated on Thu Sep 13 11:28:28 2007 for DCE-Eurobot by  doxygen 1.5.3