reading US sensors in single shot mode (ping mode; original code modified):
Code: Select all
//multi US single shot
// 1 US sensor at port 0=S1
// 1 US sensor at port 1=S2
#define printf2( _x, _y, _format1, _format2, _value1, _value2) { \
string sval1 = FormatNum(_format1, _value1); \
string sval2 = FormatNum(_format2, _value2); \
string s =sval1+sval2; \
TextOut(_x, _y, s); \
}
void USSingleShot(byte port, int &echo)
{
byte ping_cmnd[] = {0x2, 0x41, 0x1}; // sensor one-shot-mode command
byte register_select[] = {0x2, 0x42}; // sensor data register
byte data_size=4;
byte data[4]; // =data_size, up to 4 echoes
I2CWrite(port, 0, ping_cmnd);
Wait(40); // waiting time shortened!
if (I2CBytes(port, register_select, data_size, data)) {
echo=data[0]; } // 1st echo
}
task main()
{
int
e_left, // US-Echodaten von US Sensor an port 0 (S1)
e_right; // US-Echodaten von US Sensor an port 1 (S2)
SetSensorLowspeed(0);
SetSensorLowspeed(1);
while(true)
{
USSingleShot(0, e_left);
Wait(40);
USSingleShot(1, e_right);
printf2( 0, 0, "%4d", " %4d", e_left, e_right);
Wait(40);
}
}