Ultrasonic Sensor - QuirkyCort/gears GitHub Wiki

This simulates an ultrasonic sensor, not unlike those found on a Lego EV3 robot.

Working Principles (Simulator)

It works by sending out multiple rays (21 by default) in a fan shape and checking if any of them hit objects. If more than one ray hits objects, only the shortest distance will be used. The rays looks like this...

A real sensor works by detecting the returned echo, and is not effective when the angle of incident is too large as most of the sound will be reflected away from the sensor. The simulated sensor mimics this by checking the angle of incident where it hits an object, and ignore those where the angle of incident is greater than a configurable limit.

Differences from a real sensor

  • An object that is small enough to fit between the rays may be missed by the simulated ultrasonic sensor. This is not entirely unlike a real sensor, as a real sensor tends to miss out small objects.

  • The simulated sensor can detect even a tiny object, as long as one of the ray hits it. A real sensor requires sufficient echo to be returned and will not detect very small objects.

  • For a real sensor, detection depends on the returned echo. So soft objects that absorbs rather than reflect sound may not be detected. Within the simulator, there is no concept of a hard or soft object, so all objects are equally detectable.

Configurations

Default values are shown below.

"type": "UltrasonicSensor",
"options": {
  "rayLength": 255,
  "rayIncidentLimit": 0.698132
}

rayLength: Maximum distance detectable by the ultrasonic sensor.

rayIncidentLimit: If the angle of incident is greater than "PI - rayIncidentLimit", the ray will be ignored.