Page 3 of 3

Re: US sensors in single shot mode (ping mode)

Posted: 12 Nov 2010, 23:20
by muntoo
doc-helmut wrote:but thx anyway - you seem to have a very good understanding of all about IIC reading and writing
I wish. :lol:

One thing I don't get is what are all those data[1] to data[7] values represent...

Code: Select all

ReadSensorUSEx(port, data);
(data[1] is read from 0x43, and data[7] is read from 0x49)

Re: US sensors in single shot mode (ping mode)

Posted: 13 Nov 2010, 09:25
by HaWe
ok, now I see more clearly!
Thx to all :)

Re: US sensors in single shot mode (ping mode)

Posted: 13 Nov 2010, 15:51
by HaWe
I compared Muntoo's code to mine, and now it's clear that it actually is quite the same except that he uses the SensorUS() cmd to poll the values and in code I got a specific I²C read cmd:

Code: Select all

// 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);
   }
}
edit:
now I understand:
the I"C write cmd
ping_cmnd[] = {0x2, 0x41, 0x1};
does not switch the sensor constantly into the ping mode but it causes just 1 ping - the wait() is need to listen for echoes before the reflections can be read - correct?