US sensors in single shot mode (ping mode)

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

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

Post 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)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post by HaWe »

ok, now I see more clearly!
Thx to all :)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post 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?
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 7 guests