Hi,

I used the SRF02 some time ago. I attach the code I used at that time, dont 
know if it is working (I dont work on the project anymore) and its 
not "clean", but maybe it can serve as a starting point! You will need to 
open the i2c before calling us_ranging() and close it if you dont need it 
anymore.

Have fun!

-Cornelius



Am Dienstag, 14. Oktober 2008 schrieb Andrea Sassetti:
> I'm an absolute beginner in working with the fox-board.
> someone has interfaced with the fox-borad sensors srf04 or sfr08(i2c)?
> please kindly post a sourcecode. please!
>
> Andrea



-- 
Cornelius Claussen
opentek GbR
Mobile: +49 (0)175 6944518
E-Mail: [EMAIL PROTECTED]
Internet: http://www.opentek.de
USt.-IdNr.: DE242952925
#include "globals.h"


int us_ranging()
{
int ret = 0;
#ifndef BUILD_FOR_HOST
// is it ready for ranging?
       I2C_DATA i2cdata_us;

       i2cdata_us.slave     = 0xE0;
       i2cdata_us.wbuf[ 0 ] = 0;
       i2cdata_us.wlen      = 1;
       i2cdata_us.rlen      = 1;
       if ( EI2CNOERRORS != ioctl( fd_i2c, _IO( ETRAXI2C_IOCTYPE, I2C_WRITEREAD ), &i2cdata_us ) )
         {
          // ranging not ready, no new position
          return (0);
         }
       if (i2cdata_us.rbuf[0] == 255) {return 0;}
      // printf("%d, read ranging...\n",i2cdata_us.rbuf[0]);

// if it is idle, read ranging data
       i2cdata_us.wbuf[ 0 ] = 2; // high byte
       if ( EI2CNOERRORS != ioctl( fd_i2c, _IO( ETRAXI2C_IOCTYPE, I2C_WRITEREAD ), &i2cdata_us ) )
         {
#ifdef DEBUG
            printf("ERROR: I2C writeread error #%i\n");
#endif
//          I2C_Close();
          return (0);
         }
       unsigned char hi = i2cdata_us.rbuf[0];

       i2cdata_us.wbuf[ 0 ] = 3; // lo byte
       if ( EI2CNOERRORS != ioctl( fd_i2c, _IO( ETRAXI2C_IOCTYPE, I2C_WRITEREAD ), &i2cdata_us ) )
         {
#ifdef DEBUG
            printf("ERROR: I2C writeread error #%i\n");
#endif
//          I2C_Close();
          return (0);
         }
       unsigned char lo = i2cdata_us.rbuf[0];
// printf ("DEBUG: Ultrasonic: hi: %d, lo: %d",hi,lo);      

       unsigned short range = 0;
       range = (unsigned short)(hi << 8) | lo;


       // The distance is now in "range" 

       /*if (range >20 ) {
         us_alt = range;
         time (&us_timestamp);
         ret = -1;
       }*/

       // emit next sonar burst
 //      printf("emit next burst...\n");
       i2cdata_us.slave     = 0xE0;
       i2cdata_us.wbuf[ 0 ] = 0;
       i2cdata_us.wbuf[ 1 ] = 0x51;
       i2cdata_us.wlen      = 2;
       i2cdata_us.rlen      = 1;
       int err = ioctl( fd_i2c, _IO( ETRAXI2C_IOCTYPE, I2C_WRITE ), &i2cdata_us ); 
        if ( EI2CNOERRORS != err )
        {
            printf("ERROR: I2C write error #%i\n", err);
            return 0;
        }
#endif
 return ret;
#ifdef DEBUG
 printf ("DEBUG: Ultrasonic: RANGING");      
#endif
}

#include "globals.h"

int I2C_Open() 
{
#ifndef BUILD_FOR_HOST
    int RetVal = 0;
    
    fd_i2c = open( "/dev/i2c", O_RDWR );
    
    if ( fd_i2c <= 0 ) 
    {
        printf( "I2C: open error on /dev/i2c\n" );
        RetVal = -1;
    }

    return( RetVal );
#else
    return 0;
#endif

}

void I2C_Close() 
{
#ifndef BUILD_FOR_HOST
    close( fd_i2c );
#endif
}

Reply via email to