Which function returns millisecond values ​​in the UltrasonicSensor service?
I need to write a script using two HC-SR04 sensors, as in this example for Arduino:

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
GroG's picture

Helllo alexstarter, Check the

Helllo alexstarter,

Check the UltrasonicSensor service page :


At the bottom of a service page is an example script

alexstarter's picture

  This returns me the same

This returns me the same values ​​in cm.
GroG's picture

Ha !   Thanks. I looked into

Ha !   Thanks.

I looked into the details to see what was going on.

NewPing was used, and instead of the time being sent back to the arduino the ping_cm() was.
The reason we switched to NewPing, is the previous Ping in Arduino blocked communication until the the time came back or there was a timeout.

I'll fix this in Nixie, for Manticore version of MRL you can just multiply it to get microseconds or milliseconds

Therefore echoTime = distance_cm * 57 in micro-seconds.

Hope this helps.

alexstarter's picture

Thank You, GroG! I will

Thank You, GroG! I will definitely try it tomorrow.

alexstarter's picture

Hi GroG! I understand that

Hi GroG!
I understand that these settings are made in MrlComm for Arduino. Please tell me which variable is responsible for the polling rate of the HC-SR04. And yet, I need to do a synchronous launch of two HC-SR04. Because one of them will only listen.
GroG's picture

The details are in

The details are in MrlUltrasonicSensor.cpp

Within MrlComm devices - update() is called in a processing loop which has a high as possible frequency rate.  MrlUltrasonicSensor.cpp uses NewPing to process the ping.  calling ping_cm() before the echo has processed, is a noop I believe.

If you want to synchronize multiple Sensors I would suggest that you NOT do it in MrlComm.  Rather use the sensor startRanging() stopRanging() in Python.


s1 = Runtime.start("s1","UltrasonicSensor")
s2 = Runtime.start("s2","UltrasonicSensor")
current_sensor = "s1"

def onRange(rang):
  .. do something ..

while (True):

   if current_sensor == "s1"
        current_sensor = "s2"
        current_sensor = "s1"


this alternates 3 seconds for each sensor



alexstarter's picture

Oh, GroG ! It's great that

Oh, GroG ! It's great that you took the time to give me an example. Thanks! I will definitely try it!

alexstarter's picture

GroG. Your example

Your example synchronizes the sensors alternately.
I need to request both sensors at the same time.
GroG's picture

Hmm... I would think "at the

Hmm... I would think "at the same time"  is not a good idea - 
If you have the sensor placed anywhere near one another, you'll start getting interference from one to the other.

I'd recommend you just reduce the sleep time between the sensors but keep them alternating.  You can probably reduce the time so much the "appear" to be working simultaneously.


alexstarter's picture

Thank GroG I understood

Thank GroG
I understood your idea.
I need to request sensors alternately. And then mathematically determine the movement of an object from side to side.
This is necessary so that the platform for the robot can follow me.
I slightly modified and printed out the proposed Marcus - Mecanum_wheels_for_Robyn_inmoov.
Now I need to learn how to manage it.
GroG's picture

Looks Great Alextstarter !

Looks Great Alextstarter !