Pages: [1]   Go Down
Author Topic: Arduino -> SRF02 (Fake-Mode Problems)  (Read 622 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hi everyone,
I have been trying to measure the distance between two SRF02 sensors (in Fake Mode).
I have connected two SRF02 sensors on my Arduino by I2C Bus system.

The idea is that one SRF02 should send a signal and the second SRF02 should be waiting for the signal.

The problem is that I get wrong distances as result.
Here is my Code....



#include <Wire.h>

#define srfAddress1 0x70                               // Address of the SRF02
#define srfAddress2 0x72                              // Address of the SRF02

#define cmdByte 0x00                                 // Command byte
#define rangeByte 0x02                             // Byte for start of ranging data

byte highByte = 0x00;                                // Stores high byte from ranging
byte lowByte = 0x00;                                // Stored low byte from ranging
int range = 0;
int rangeData;


void setup()
{
   Serial.begin(9600);
   Wire.begin();                               
 }


void loop()
{
  rangeData=getRange(); 
}


int getRange()
{                                               
 
 
//Sensor 1 –Sender-

  Wire.beginTransmission(srfAddress1);             // Start communticating with SRF02
  Wire.write((uint8_t)cmdByte);                         // Send Command Byte
  Wire.write((uint8_t)0x5C);        // Transmit an 8 cycle 40khz burst - no ranging takes place
  Wire.endTransmission(); 
 
   
  //Sensor 2 –Empfänger-
 
  Wire.beginTransmission(srfAddress2);             // Start communticating with SRF02
  Wire.write((uint8_t)cmdByte);                         // Send Command Byte
  Wire.write((uint8_t)0x57);                               // Fake Ranging Mode - Result in centimeters
  Wire.endTransmission(); 
 
  delay(100);                                                           
 
  Wire.beginTransmission(srfAddress2);       // start communicating with SRFmodule
  Wire.write((uint8_t)rangeByte);                 // Call the register for start of ranging data
  Wire.endTransmission();
 
  Wire.requestFrom(srfAddress2, 2);            // Request 2 bytes from SRF module
 
  while(Wire.available() < 2);                    // Wait for data to arrive
  highByte = Wire.read();                           // Get high byte
  lowByte = Wire.read();                           // Get low byte
 
  range = (highByte << 8 ) + lowByte;              // Put them together
 
  //print it
  Serial.print("Entfernung zwischen 1&2: ");
  Serial.println(range, DEC);   
  delay(1500);


  return(range);                                                    // Returns Range

}
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First: use code tags for your code parts, else the tag parsing on the server may omit some code.

Quote
The problem is that I get wrong distances as result.

May I guess: your result is about 10-15cm wrong?
The transmission of the range command takes approximately 300us which translates to about 10cm. If there's another delay in the communication that I've missed it may be more. The datasheet explicitly states that you are responsible for the synchronization of the two sensors if used this way.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"use code tags for your code parts, else the tag parsing on the server may omit some code."
-> Ok next time i will use it.

"May I guess: your result is about 10-15cm wrong?
The transmission of the range command takes approximately 300us which translates to about 10cm. If there's another delay in the communication that I've missed it may be more. The datasheet explicitly states that you are responsible for the synchronization of the two sensors if used this way."

-> Ok thanks. I will check if the result is constant wrong. (for example every time about 10cm) Then i will reduce every time the result about 10cm. So i guess i get the right result. 

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tested it and the problem is that the result is not constant wrong.

for example: the right length is 37cm -> result of srf02: 27cm
                                                   27cm -> result of srf02: 25cm
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed a bit the code. I call "Sensor2" (receive the signal) before "Sensor1" (transmit the signal).

If i call Sensor2 after Sensor1 it could be that the Signal is just gone. That was my intention.


Code:
#include <Wire.h>

#define srfAddress1 0x70                               // Address of the SRF02
#define srfAddress2 0x72                              // Address of the SRF02

#define cmdByte 0x00                                 // Command byte
#define rangeByte 0x02                             // Byte for start of ranging data

byte highByte = 0x00;                                // Stores high byte from ranging
byte lowByte = 0x00;                                // Stored low byte from ranging
int range = 0;
int rangeData;


void setup()
{
   Serial.begin(9600);
   Wire.begin();                              
 }


void loop()
{
  rangeData=getRange();  
}


int getRange()                                                      // This function gets a ranging from the SRF02
{                                                
  //Sensor 2 –Empfänger-

  Wire.beginTransmission(srfAddress2);             // Start communticating with SRF02
  Wire.write((uint8_t)cmdByte);                         // Send Command Byte
  Wire.write((uint8_t)0x57);                               // Fake Ranging Mode - Result in centimeters
  Wire.endTransmission();

  
//Sensor 1 –Sender-

  Wire.beginTransmission(srfAddress1);             // Start communticating with SRF02
  Wire.write((uint8_t)cmdByte);                         // Send Command Byte
  Wire.write((uint8_t)0x5C);        // Transmit an 8 cycle 40khz burst - no ranging takes place
  Wire.endTransmission();  
  
    
  //Sensor 2 –Empfänger-
 
  delay(100);                                                           // Wait for ranging to be complete
  
  Wire.beginTransmission(srfAddress2);       // start communicating with SRFmodule
  Wire.write((uint8_t)rangeByte);                 // Call the register for start of ranging data
  Wire.endTransmission();
  
  Wire.requestFrom(srfAddress2, 2);            // Request 2 bytes from SRF module
  
  while(Wire.available() < 2);                    // Wait for data to arrive
  highByte = Wire.read();                           // Get high byte
  lowByte = Wire.read();                           // Get low byte
  
  range = (highByte << 8 ) + lowByte;              // Put them together
  
  //print it
  Serial.print("Entfernung zwischen 1&2: ");
  Serial.println(range, DEC);  
  delay(1500);


  return(range);                                                    // Returns Range

}


Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What's the result of that change? Did you get better (more constant) readings?
Logged

Pages: [1]   Go Up
Jump to: