Hi,
I'm encountering yet another strange problem while using a Devantech SRF08 (firmware version 8 ) with WebBot Lib 2.09 (C version).
The upper code outputs the correct distance while the original version always outputs 0 (zero):
This is the code I am using:
//This part of the code works!
i2cMasterWriteRegister(&sonar.i2cInfo, 0, 0x51); // Send command to register 0
DISTANCE_TYPE val=4;
uint8_t response[4];
// Read registers 0,1,2,3
delay_ms(80);
if(!i2cMasterReadRegisters(&(sonar.i2cInfo), 0, 4, response)){
rprintf("Error reading SRF!\r\n");
}
while(i2cMasterReadRegisters(&(sonar.i2cInfo), 0, 4, response)){
if(response[0]!=255){
// Ranging has finished
val = response[2]; // get high byte
val = (val << 8) + response[3]; // put in low byte
break;
}
}
rprintf("sonar(CUSTOM): %u, %d, %d\r\n", val, val, sonar.i2cInfo.addr);
//But this part (library code) fails...
// -------- Start Devantech SRF08 sonar-------
// Read the Devantech SRF08 sonar and store the result in sonar.distance.cm
distanceRead(sonar);
// The value can be printed using %u eg rprintf("Distance=%u",sonar.distance.cm);
// or dumped using:
rprintf("sonar(LIB): ");
distanceDump(sonar);
rprintfCRLF();
// -------- End Devantech SRF08 sonar-------
I believe that I found out which part of the library's code causes this problem (excerpt from 'SRF08.c'):
static void getReading(Devantech_SRF08* device){
if(device){
uint8_t response[4];
const I2C_DEVICE* i2c = &(device->i2cInfo);
// Read registers 0,1,2,3
while(i2cMasterReadRegisters(i2c, 0, sizeof(response), response)){ //I think it fails over here!
if(response[0]!=255){
// Ranging has finished
DISTANCE_TYPE val = response[2]; // get high byte
val = (val << 8) | response[3]; // put in low byte
device->distance.cm = val;
device->ldr = response[1]; // Store the light level
break;
}
}
}
}
Because the sensor works perfectly while using the upper code (with the delay included), I think it does not react to any I2C requests while it's measuring...
My SRF's firmware version is 8. Am I right with my suspicion or am I using the library in a wrong way? Is my sensor broken?
How could this issue be fixed in a better way than duplicating the SRF's code?
Regards,
Max Web