Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

access to mouseXY in ROS on raspberry-pi-2 w/ Ubuntu14.04

Hi,

I'm learning how to use ROS on Raspberry-pi2. I was able to follow your tutorial and run talker/listener. I also made simple nodes which would blink LED or move servo in response to a message sent to a dedicated topic.

But I am stuck on my node which is meant to broadcast mouseXY coordinates.

1) I started with the example (attached at the bottom), which compiles and runs as non-sudo after I changed sudo chmod a+r /dev/input/event3

So I can read mouse XY as non-sudo on R-pi using my compiled C++ stand alone code.

time1480312402.421372   x 484   y 662
time1480312409.158280   x 483   y 662
time1480312409.174276   x 484   y 662

2) I have modified your talker.cpp to access X,Y mouse data the same way, for now just want to print the values on the screen. The code compiles and executes as ROS node, but never passes this line:

balewski@rpi2blue:~/catkin_rpi$ rosrun basic_jan streamMouseXY

now will print mouse XY to stdout - stuck

 printf("now will print mouse XY to stdout - stuck\n");
 while(read(fd, &ie, sizeof(struct input_event))) {
    if (ie.type == 2) { ....

The value of ie.type is stuck on some large integer, like: ie.type=11672

With my limited understanding how ROS works I am not able to figure out why the same sequence of C++ code embedded in to a ROS node is not working.

My full ROS code in bitbucket, public repo:

https://balewski@bitbucket.org/balewski/catkin_rpi.git

It can be compiled on Ubuntu14.04 as:

cd  ~/catkin_rpi
source ./devel/setup.bash
catkin_make
source ./devel/setup.bash

The code in question is : ~/catkin_rpi/src/basic_jan/src/streamMouseXY.cpp

It executes as follows (assuming mouse is seen as event3)

sudo chmod a+r /dev/input/event3
rosrun basic_jan streamMouseXY 3

Can you perhaps advice me how to proceed?

Thanks

Jan

Below is C++ code working on R-Pi2.

Jans-MBP-3:optMouse balewski$ cat  readMouse1.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/input.h>
#include <fcntl.h>
#include <X11/Xlib.h>

/* 
Tested on R-pi2 w/ Ubuntu14.04

To compile:
  gcc readMouse1.cpp -lX11

To list all  connected devices:
 cat /proc/bus/input/devices |grep mouse

To enable none-sudo user to access mouse data
 sudo chmod a+r /dev/input/event3

Results with
crw-r--r-- 1 root root 13, 67 Nov 27 19:03 /dev/input/event3

*/

#define MOUSEFILE "/dev/input/event3"

int main(){
  int fd;
  struct input_event ie;
  Display *dpy;
  Window root, child;
  int rootX, rootY, winX, winY;
  unsigned int mask;

  dpy = XOpenDisplay(NULL);
  XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
              &rootX,&rootY,&winX,&winY,&mask); 

  if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
    perror("opening device");
    exit(EXIT_FAILURE);
  }

  while(read(fd, &ie, sizeof(struct input_event))) {
    if (ie.type == 2) {
      if (ie.code == 0) { rootX += ie.value; }
      else if (ie.code == 1) { rootY += ie.value; }
      printf("time%ld.%06ld\tx %d\ty %d\n", 
         ie.time.tv_sec, ie.time.tv_usec, rootX, rootY);
    } else if (ie.type == 1) {
      if (ie.code == 272 ) { 
        printf("Mouse button ");
        if (ie.value == 0)  
          printf("released!!\n");
        if (ie.value == 1)  
          printf("pressed!!\n");
      } else {
        printf("time %ld.%06ld\ttype %d\tcode %d\tvalue %d\n",
           ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);
      }
    }
  }
  return 0;
}

access to mouseXY in ROS on raspberry-pi-2 w/ Ubuntu14.04

Hi,

I'm learning how to use ROS on Raspberry-pi2. I was able to follow your tutorial and run talker/listener. I also made simple nodes which would blink LED or move servo in response to a message sent to a dedicated topic.

But I am stuck on my node which is meant to broadcast mouseXY coordinates.

1) I started with the example (attached at the bottom), which compiles and runs as non-sudo after I changed sudo chmod a+r /dev/input/event3

So I can read mouse XY as non-sudo on R-pi using my compiled C++ stand alone code.

time1480312402.421372   x 484   y 662
time1480312409.158280   x 483   y 662
time1480312409.174276   x 484   y 662

2) I have modified your talker.cpp to access X,Y mouse data the same way, for now just want to print the values on the screen. The code compiles and executes as ROS node, but never passes this line:

balewski@rpi2blue:~/catkin_rpi$ rosrun basic_jan streamMouseXY

streamMouseXY [ INFO] [1480313171.898452068]: started streaming mouse XY to topic mouseXY3

now will print mouse XY to stdout - stuck

 printf("now will print mouse XY to stdout - stuck\n");
 while(read(fd, &ie, sizeof(struct input_event))) {
    if (ie.type == 2) { ....

The value of ie.type is stuck on some large integer, like: ie.type=11672

With my limited understanding how ROS works I am not able to figure out why the same sequence of C++ code embedded in to a ROS node is not working.

My full ROS code in bitbucket, public repo:

https://balewski@bitbucket.org/balewski/catkin_rpi.gitrepo.

It can be compiled on Ubuntu14.04 as:

cd  ~/catkin_rpi
source ./devel/setup.bash
catkin_make
source ./devel/setup.bash

The code in question is : ~/catkin_rpi/src/basic_jan/src/streamMouseXY.cpp

It executes as follows (assuming mouse is seen as event3)

sudo chmod a+r /dev/input/event3
rosrun basic_jan streamMouseXY 3

Can you perhaps advice me how to proceed?

Thanks

Jan

Below is C++ code working on R-Pi2.

Jans-MBP-3:optMouse balewski$ cat  readMouse1.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <linux/input.h>
#include <fcntl.h>
#include <X11/Xlib.h>

/* 
Tested on R-pi2 w/ Ubuntu14.04

To compile:
  gcc readMouse1.cpp -lX11

To list all  connected devices:
 cat /proc/bus/input/devices |grep mouse

To enable none-sudo user to access mouse data
 sudo chmod a+r /dev/input/event3

Results with
crw-r--r-- 1 root root 13, 67 Nov 27 19:03 /dev/input/event3

*/

#define MOUSEFILE "/dev/input/event3"

int main(){
  int fd;
  struct input_event ie;
  Display *dpy;
  Window root, child;
  int rootX, rootY, winX, winY;
  unsigned int mask;

  dpy = XOpenDisplay(NULL);
  XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,
              &rootX,&rootY,&winX,&winY,&mask); 

  if((fd = open(MOUSEFILE, O_RDONLY)) == -1) {
    perror("opening device");
    exit(EXIT_FAILURE);
  }

  while(read(fd, &ie, sizeof(struct input_event))) {
    if (ie.type == 2) {
      if (ie.code == 0) { rootX += ie.value; }
      else if (ie.code == 1) { rootY += ie.value; }
      printf("time%ld.%06ld\tx %d\ty %d\n", 
         ie.time.tv_sec, ie.time.tv_usec, rootX, rootY);
    } else if (ie.type == 1) {
      if (ie.code == 272 ) { 
        printf("Mouse button ");
        if (ie.value == 0)  
          printf("released!!\n");
        if (ie.value == 1)  
          printf("pressed!!\n");
      } else {
        printf("time %ld.%06ld\ttype %d\tcode %d\tvalue %d\n",
           ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value);
      }
    }
  }
  return 0;
}