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
00076
00077
00078
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)
00083 {
00084 angle = atan2(axis[1], axis[0]);
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);
00093 printf("length %f, angle %f\n",lenght, angle);
00094 printf("axisX %d, axisY %d\n",axis[0], axis[1]);
00095 ORTEPublicationSend(publisherMotor);
00096 ORTEPublicationSend(publisherJoy);
00097
00098 }
00099 ORTEPublicationSend(publisherMotor);
00100 ORTEPublicationSend(publisherJoy);
00101 nanosleep(&timer,NULL);
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 printf(" \r");
00123 fflush(stdout);
00124 }
00125
00126 close(joy_fd);
00127
00128 return 0;
00129 }