I'm trying to write some code to use the AXON2 and the SRF02-SRF08 ultrasonic rangers ( they both have the same i2C address and commands).
I have writen some code for the Wiring microcontroller ( the father of Arduino - they use the same "language") and it works great.
I used the same algorithms and steps in AXON2 using the Webbotlib, but I have a couple of issues:
1. The command to start a reading is not actually working, although it seems that code-wise it's accepted. I don't know what seems to be the problem, but in some point
I need to stop the I2C transmission and start it again so I can read the registers that hold the actuall reading.
I'm not very familiar with the inner works of Webbotlib since i'm new to AXON, but I think there must be a "start"-"stop" commands for I2C interface.
2. The reading results come in two bytes ( high-low) , I overpassed this by using unsigned char types, since there's no "byte" type, but there's nothing to read there
since the I2C reading command is not working.
A couple of months ago, I interfaced the TPA81 and submitted the code here, and everything works fine with this sensor.
But the SRF02 gives me a real headache.
Below is the code.
Any help will be appreciated!!
// SRF02 - SRF08 Test
#include "sys/axon2.h"
#include "rprintf.h" //use for UART
#include "i2c_master.h" //use for I2C
#include "myhardware.h" // user includes
uint8_t address=0xE0; // I2C address for SRF02 - same as the SRF08
uint8_t command[1]={0x00}; // command to start ranging
unsigned char rangebyte_high[1]={0x02}; //ranging data high byte
unsigned char rangebyte_low[1]={0x03}; // ranging data low byte
uint8_t unitbyte[1]={0x51}; // ranging unit in cm -use 0x50 for inches -and 0x52 for ms
unsigned int range;
unsigned char result[2]={0,0};
void appInitHardware(void)
{
uartInit(USB_UART, USB_BAUD);//USB
rprintfInit(USB_ACTIVATE);
}
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
rprintf("\nAxon Initiated.\n\n");
return 0;
}
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart)
{
i2cMasterSend(address,1,command); //command to initialize module
i2cMasterSend(address,1,unitbyte);// start ranging in cm
delay_ms(70); // wait to complete ranging
if(i2cMasterReceive(address,1,unitbyte)) // if it's ok
{
rprintf("Command for cm 0x51 sent\n");
i2cMasterSend(address,1,rangebyte_high); // request high byte first
i2cMasterSend(address,1,rangebyte_low); // request low byte
if(i2cMasterReceive(address,1,result)&&(i2cMasterReceive(address,1,result)
{
rprintf("range: ");
range=result[0]<<8; // shift high byte left to make room for low byte
range+=range+result[1]; // and add low byte
rprintf("%d\n", range);
delay_ms(400);
}
}
else
{
rprintf("No responce\n");
}
return(1000);
}