6 * Copyright: (c) 2007 CTU Dragons
7 * CTU FEE - Department of Control Engineering
16 #include <sys/ioctl.h>
17 #include <linux/joystick.h>
21 #include <roboorte_robottype.h>
22 #include <actuators.h>
25 #define JOY_DEV "/dev/input/js0"
27 #define JOY_NAME "Logitech Logitech Extreme 3D"
29 struct robottype_orte_data orte;
31 static int *axis_prev, *axis;
32 static char *buttons_prev, *buttons;
34 static void set_robot_speed(void)
36 double r=0.15; //FIXME use radius from robodim?
37 double angle = 0, lenght, v, omega;
39 // printf("axis x = %d, axis y = %d\n",axis[AXIS_X], axis[AXIS_Y]);
40 v = (double)axis[AXIS_Y]/32768.0;
41 omega = (double)axis[AXIS_X]/32768.0;
42 lenght = sqrt( pow(v,2) + pow(omega,2));
43 /* if lenght si more than 1 is used unit circle */
45 /* computing new speed and omega */
46 angle = atan2(axis[AXIS_Y], axis[AXIS_X]);
52 orte.motion_speed.right = (int)(-((v + r*omega))*VMAX);
53 orte.motion_speed.left = (int)(-(v - r*omega)*VMAX);
54 ORTEPublicationSend(orte.publication_motion_speed);
55 printf("Publishing commands: right %d left %d\n", orte.motion_speed.right, orte.motion_speed.left);
58 /* ************************************************** */
60 static void button_act(char state, int id)
67 printf("jaws CATCH\n");
142 printf("Unknown button pressed!\n");
147 static void process_axis(int value, int id)
162 printf("jaws CLOSE\n");
166 printf("jaws OPEN\n");
169 printf("jaws stop\n");
179 act_lift(DOWN, 0, 0);
180 printf("lift DOWN\n");
187 printf("lift stop\n");
195 printf("Unknown axis changed!\n");
200 static void process_button(char state, int id)
203 if(buttons_prev[id] != 1) {
205 buttons_prev[id] = 1;
207 button_act(state, id);
210 buttons_prev[id] = 0;
211 button_act(state, id);
215 static void int_handler(int sig)
217 printf("joyd stopping.\n");
219 /* orte domain destroy */
220 robottype_roboorte_destroy(&orte);
222 /* close file descriptor */
228 int open_joystick(char *name, int *num_of_axis, int *num_of_buttons)
234 if((joy_fd=open("/dev/input/js0", O_NONBLOCK))==-1) {
235 printf("No joystick detected!\n");
239 ioctl(joy_fd, JSIOCGNAME(80), name);
240 ret = strcmp(JOY_NAME, name);
242 printf("Logitech joystick not present as js0\n");
244 ioctl(joy_fd, JSIOCGAXES, num_of_axis);
245 ioctl(joy_fd, JSIOCGBUTTONS, num_of_buttons);
249 if((joy_fd=open("/dev/input/js1", O_NONBLOCK))==-1) {
250 printf("No joystick detected!\n");
254 ioctl(joy_fd, JSIOCGNAME(80), name);
255 ret = strcmp(JOY_NAME, name);
257 printf("Logitech joystick not present as js1\n");
259 ioctl(joy_fd, JSIOCGAXES, num_of_axis);
260 ioctl(joy_fd, JSIOCGBUTTONS, num_of_buttons);
268 void send_dummy_cb(const ORTESendInfo *info, void *vinstance,
269 void *sendCallBackParam)
273 int main(int argc, char *argv[])
275 struct timespec timer;
276 int num_of_axis=0, num_of_buttons=0;
280 signal(SIGINT, int_handler);
284 /* orte initialization */
285 if (robottype_roboorte_init(&orte)) {
286 printf("Unable to initialize ORTE.\n");
292 /* creating publishers */
293 robottype_publisher_motion_speed_create(&orte, send_dummy_cb, &orte);
294 robottype_publisher_lift_cmd_create(&orte, send_dummy_cb, &orte);
295 robottype_publisher_jaws_cmd_create(&orte, send_dummy_cb, &orte);
297 if(open_joystick(joy_name, &num_of_axis, &num_of_buttons) == -1) {
298 printf("Joystick not found, exiting...\n");
302 printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n",
303 joy_name, num_of_axis, num_of_buttons);
305 axis = (int *) calloc( num_of_axis, sizeof( int ) );
306 buttons = (char *) calloc( num_of_buttons, sizeof( char ) );
307 axis_prev = (int *) calloc( num_of_axis, sizeof( int ) );
308 buttons_prev = (char *) calloc( num_of_buttons, sizeof( char ) );
312 timer.tv_nsec = 100000000;
314 while(read(joy_fd, &js, sizeof(struct js_event))>0) {
315 switch (js.type & ~JS_EVENT_INIT) {
317 axis [ js.number ] = js.value;
318 process_axis(js.value, js.number);
320 case JS_EVENT_BUTTON:
321 process_button(js.value, js.number);
322 buttons [ js.number ] = js.value;
329 nanosleep(&timer,NULL);