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)
138 printf("Unknown button pressed!\n");
143 static void process_axis(int value, int id)
157 printf("jaws CLOSE\n");
160 printf("jaws OPEN\n");
163 printf("jaws stop\n");
174 printf("lift DOWN\n");
181 printf("lift stop\n");
189 printf("Unknown axis changed!\n");
194 static void process_button(char state, int id)
197 if(buttons_prev[id] != 1) {
199 buttons_prev[id] = 1;
201 button_act(state, id);
204 buttons_prev[id] = 0;
205 button_act(state, id);
209 static void int_handler(int sig)
211 printf("joyd stopping.\n");
213 /* orte domain destroy */
214 robottype_roboorte_destroy(&orte);
216 /* close file descriptor */
222 int open_joystick(char *name, int *num_of_axis, int *num_of_buttons)
228 if((joy_fd=open("/dev/input/js0", O_NONBLOCK))==-1) {
229 printf("No joystick detected!\n");
233 ioctl(joy_fd, JSIOCGNAME(80), name);
234 ret = strcmp(JOY_NAME, name);
236 printf("Logitech joystick not present as js0\n");
238 ioctl(joy_fd, JSIOCGAXES, num_of_axis);
239 ioctl(joy_fd, JSIOCGBUTTONS, num_of_buttons);
243 if((joy_fd=open("/dev/input/js1", O_NONBLOCK))==-1) {
244 printf("No joystick detected!\n");
248 ioctl(joy_fd, JSIOCGNAME(80), name);
249 ret = strcmp(JOY_NAME, name);
251 printf("Logitech joystick not present as js1\n");
253 ioctl(joy_fd, JSIOCGAXES, num_of_axis);
254 ioctl(joy_fd, JSIOCGBUTTONS, num_of_buttons);
262 void send_dummy_cb(const ORTESendInfo *info, void *vinstance,
263 void *sendCallBackParam)
267 int main(int argc, char *argv[])
269 struct timespec timer;
270 int num_of_axis=0, num_of_buttons=0;
274 signal(SIGINT, int_handler);
278 /* orte initialization */
279 if (robottype_roboorte_init(&orte)) {
280 printf("Unable to initialize ORTE.\n");
286 /* creating publishers */
287 robottype_publisher_motion_speed_create(&orte, send_dummy_cb, &orte);
288 robottype_publisher_lift_cmd_create(&orte, send_dummy_cb, &orte);
290 if(open_joystick(joy_name, &num_of_axis, &num_of_buttons) == -1) {
291 printf("Joystick not found, exiting...\n");
295 printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n",
296 joy_name, num_of_axis, num_of_buttons);
298 axis = (int *) calloc( num_of_axis, sizeof( int ) );
299 buttons = (char *) calloc( num_of_buttons, sizeof( char ) );
300 axis_prev = (int *) calloc( num_of_axis, sizeof( int ) );
301 buttons_prev = (char *) calloc( num_of_buttons, sizeof( char ) );
305 timer.tv_nsec = 100000000;
307 while(read(joy_fd, &js, sizeof(struct js_event))>0) {
308 switch (js.type & ~JS_EVENT_INIT) {
310 axis [ js.number ] = js.value;
311 process_axis(js.value, js.number);
313 case JS_EVENT_BUTTON:
314 process_button(js.value, js.number);
315 buttons [ js.number ] = js.value;
322 nanosleep(&timer,NULL);