Gyro.C

Go to the documentation of this file.
00001 /*!@file Devices/Gyro.C calculates angle of mouse with respect to floor */
00002 
00003 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Devices/Gyro.C $
00004 // $Id: Gyro.C 6003 2005-11-29 17:22:45Z rjpeters $
00005 
00006 //note: "g++ Gyro2.C TestGyro2.C -lpthread"
00007 //to compile
00008 #include<iostream>
00009 #include<fstream>
00010 #include<string.h>
00011 #include<errno.h>
00012 #include<stdio.h>
00013 #include<sys/types.h>
00014 #include<sys/stat.h>
00015 #include<fcntl.h>
00016 #include<unistd.h>
00017 #include"Gyro.H"
00018 
00019 //degrees
00020 #define XANGLE_CONVERSION .0432
00021 #define YANGLE_CONVERSION .0405
00022 
00023 void* Gyro_run(void *c0);  // will live in a separate thread
00024 void* Gyro_run(void *c0)
00025 {
00026   //a wrapper so that we call the member function go() on
00027   //the Gyro1 object passed as argument:
00028   Gyro *c = (Gyro *)c0;
00029   c->go(); return NULL;
00030 }
00031 
00032 //#####################################################################
00033 //Gyro constructor
00034 Gyro::Gyro()
00035 {
00036   xpos = 0;
00037   ypos = 0;
00038   xdpos = 0;
00039   ydpos = 0;
00040   // start thread for run():
00041   pthread_create(&runner, NULL, &Gyro_run, (void *)this);
00042 }
00043 
00044 //#####################################################################
00045 //continuously updates position
00046 void Gyro::go()
00047 {
00048   int g1, fileno, nbytes = -1, g2, g3;
00049   fileno = open("/dev/mouse" , O_NONBLOCK);
00050   while( 1 ) //conditional break?
00051     {
00052       g1 = 999;
00053       g2 = 0;
00054       g3 = 0;
00055       nbytes = read(fileno , &g1 , 1);
00056       //testing
00057       //std::cout<<"( "<<nbytes<<" )"<<std::endl;
00058 
00059       if(g1 != 999) g1 = g1 % 256;
00060       if(g1 == 8 || g1 == 24 || g1 == 56 || g1 == 40)
00061         {
00062           nbytes = -1;
00063           while(nbytes == -1)
00064             {
00065               nbytes = read(fileno , &g2 , 1);
00066               if(nbytes == 1)
00067                 {
00068                   nbytes = -1;
00069                   while(nbytes == -1)
00070                     {
00071                       nbytes = read(fileno , &g3 , 1);
00072                     }
00073                 }
00074             }
00075           g2 = g2 % 256;
00076           g3 = g3 % 256;
00077           if(g2 > 128) g2 = g2 - 256;
00078           if(g3 > 128) g3 = g3 - 256;
00079           xdpos = xdpos + g2;
00080           ydpos = ydpos + g3;
00081         }
00082       if(nbytes == -1) usleep(3000);
00083     }
00084   close(fileno);
00085   return;
00086 }
00087 
00088 //#####################################################################
00089 //returns position
00090 void Gyro::getAngle( int &x , int &y )
00091 {
00092   xpos += xdpos;
00093   ypos += ydpos;
00094   xdpos = 0;
00095   ydpos = 0;
00096   x = int(xpos * XANGLE_CONVERSION);
00097   y = int(ypos * YANGLE_CONVERSION);
00098 }
00099 
00100 //#####################################################################
00101 //sets position to x,y
00102 void Gyro::setAngle( int x , int y )
00103 {
00104   xpos = x;
00105   ypos = y;
00106 }
00107 
00108 // ######################################################################
00109 /* So things look consistent in everyone's emacs... */
00110 /* Local Variables: */
00111 /* indent-tabs-mode: nil */
00112 /* End: */
Generated on Sun May 8 08:04:45 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3