Society of Robots - Robot Forum

Software => Software => Topic started by: nexusnoesis on January 12, 2009, 05:58:12 PM

Title: optical mouse for motion measurement in a linux driven robot?
Post by: nexusnoesis on January 12, 2009, 05:58:12 PM
hi there,

as i'm building a simple robot that uses a linux netbook as controller (http://nexno.blogspot.com/ (http://nexno.blogspot.com/)), I thought about using an optical mouse as a sensor for motion, to be able to regulate the motors if i'm detecting unwanted drifts for example.

does anybody know how to read an usb mouse unter linux so, that it has no effect in the cursor in my x-environment? or think of it as if there is no window manager running at all.

wild guessings welcome :D

thanks!
Title: Re: optical mouse for motion measurement in a linux driven robot?
Post by: hgordon on January 12, 2009, 08:21:36 PM
take a look at
    http://home.roadrunner.com/~maccody/robotics/croms-1/croms-1.html
Title: Re: optical mouse for motion measurement in a linux driven robot?
Post by: nexusnoesis on January 13, 2009, 03:24:47 AM
Well thanks very much, it's a nice article and I learned something new, but it involves a lot of disassembling, and soldering etc... mainly a hardware project. I just want to simply read the mouse position x/y absolute or as delta value under linux :)
cmon, can't be that hard guys ...  :P ??? ;D
Title: Re: optical mouse for motion measurement in a linux driven robot?
Post by: cosminprund on January 13, 2009, 03:39:33 AM
This turns your question into a very specific Linux programming question. You'll need to do your own digging and if that fails try asking on some Linux programming forum.

Here are a few pointers those: As allmost everything that's interesting in Linux you can get access to the mouse using a character device. Boot your Linux desktop/laptop in text mode (or kill X if starts automatically) and do something like this:

cat /dev/mice

While this command runs, move your mouse! Voila, you're getting data from the mice and you should be able to do something similar using your embedded Linux board or mini PC. Now for the complicated stuff: Your device might have a different name! Googleing indicated dev/mouse, but for me it was /dev/mice - this is probably both distribution dependent, driver dependent, kernel dependent. As with other things Linux it might also depend on Moon Phase :)

P.S: There's probably an other way of accesing the mouse without using the pseudodevice, with some API. "man -k" and "man -K" for that!
Title: Re: optical mouse for motion measurement in a linux driven robot?
Post by: dunk on January 13, 2009, 03:57:02 AM
where your mouse shows up under /dev/ will depend what kind of mouse it is. (USB/serial/PS2 etc).
look for it in dmesg when you plug/unplug it. (presuming you have hotplug enabled.)

alternatively, if you are running X, look for the Section "InputDevice" in xorg.conf (or Xorg.conf depending on which X server you are running).

you should be able to treat /dev/*mousename as a regular file to get input from it to whatever programing language you are using.


dunk.
Title: Re: optical mouse for motion measurement in a linux driven robot?
Post by: cosminprund on January 13, 2009, 04:43:42 AM
Take a look at the "GDM" library if you have that. I've seen some code on the net using GDM to read the mouse and it's really simple, but on an embedded platform there's no guessing what you'll find!
Title: Re: optical mouse for motion measurement in a linux driven robot?
Post by: nexusnoesis on January 13, 2009, 06:38:10 AM
I figured it out:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
main(){
FILE *fmouse;
char b[3];
fmouse = fopen("/dev/input/mice","r");
int xd=0,yd=0; //x/y movement delta
int xo=0,yo=0; //x/y overflow (out of range -255 to +255)
int lb=0,mb=0,rb=0; //left/middle/right mousebutton
while(!mb){
fread(b,sizeof(char),3,fmouse);
lb=(b[0]&1)>0;
rb=(b[0]&2)>0;
mb=(b[0]&4)>0;
xo=(b[0]&64)>0;
yo=(b[0]&128)>0;
xd=b[1];
yd=b[2];
printf("lb=%d rm=%d mb=%d xo=%d yo=%d xd=%d yd=%d\n",lb,rb,mb,xo,yo,xd,yd);
}
fclose(fmouse);
}

PS: you have to call it with sudo or else you just get a segfault (at least I did).
Title: Re: optical mouse for motion measurement in a linux driven robot?
Post by: jka on January 13, 2009, 10:03:26 AM
You probably got the segafult because you don't have rights to read /dev/input/mice. You can avoid it by setting the right permissions and also, you should check the result from the call to fopen().
Title: Re: optical mouse for motion measurement in a linux driven robot?
Post by: dunk on January 13, 2009, 12:27:20 PM
Quote
PS: you have to call it with sudo or else you just get a segfault (at least I did).
does your system use udev?
if it does then you can set your device startup permissions there so you don't have to sudo every time.

i can't remember how to do it without udev just now...
i'm sure i used to know that.


dunk.