Distance Sensor

Gets the distance to objects in the environment

This page is under construction.

Basics

The distance sensors in Erebus are configured to have a range between 0 to 0.8 meters. They will return the distance to the first object blocking their line of sight.

There are 4 basic steps to using this sensor:

  1. Start by importing or including the appropriate class for the distance sensor.
  2. Retrieve the sensor by name from the robot.
  3. Enable the sensor by passing in the update rate of the sensor. (Usually the timestep)
  4. Finally, retrieve values from the sensor.
   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from controller import Robot, DistanceSensor # Step 1: Import DistanceSensor

robot = Robot()

distanceSensor = robot.getDevice("ps0") # Step 2: Retrieve the sensor, named "ps0", from the robot. Note that the sensor name may differ between robots

timestep = int(robot.getBasicTimeStep())

distanceSensor.enable(timestep) # Step 3: Enable the sensor, using the timestep as the update rate

while robot.step(timestep) != -1:
    
    distance = distanceSensor.getValue() # Step 4: Use the getValue() function to get the sensor reading
    print("Distance: " + str(distance)) # Print out the value of the sensor
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

#include <webots/Robot.hpp>
#include <webots/DistanceSensor.hpp> // Step 1: Include DistanceSensor

using namespace webots;
using namespace std;

int main(int argc, char **argv) {

  Robot *robot = new Robot();
  
  DistanceSensor* distanceSensor = robot->getDistanceSensor("ps0"); // Step 2: Retrieve the sensor, named "ps0", from the robot. Note that the sensor name may differ between robots.

  int timeStep = (int)robot->getBasicTimeStep();
  
  distanceSensor->enable(timeStep); // Step 3: Enable the sensor, using the timestep as the update rate

  while (robot->step(timeStep) != -1) {
  
    double distance = distanceSensor->getValue(); // Step 4: use the getValue() function to get the sensor reading
    cout << "Distance: " << distance << endl; // Print out the value of the sensor
  }

  delete robot;
  return 0;
}

Example

This example shows how to enable, poll, and print values from multiple distance sensors using a list or array. This is useful for managing multiple distance sensors:

   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from controller import Robot, DistanceSensor

robot = Robot()

distanceSensors = [] # Create list to store the distance sensors

timestep = int(robot.getBasicTimeStep())

# Use a for loop to retrieve and enable the distance sensors
for i in range(8):
    distanceSensors.append(robot.getDevice("ps" + str(i)))
    distanceSensors[i].enable(timestep)

while robot.step(timestep) != -1:

    # Print the values of the 8 distance sensors    
    for distanceSensor in distanceSensors:        
        distance = distanceSensor.getValue()
        print("Distance: " + str(distance))
        
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string> // Use the C++ string library for string manipulation

#include <webots/Robot.hpp>
#include <webots/DistanceSensor.hpp>

using namespace webots;
using namespace std;

int main(int argc, char **argv) {

  Robot *robot = new Robot();
  
  DistanceSensor* distanceSensors[8]; // Create an array containing 8 distance sensors

  int timeStep = (int)robot->getBasicTimeStep();
  
  for(int i = 0; i < 8; i++) {
  
    // Retrieve an enable the distance sensors named ps0, ps1, ps2, etc...
    distanceSensors[i] = robot->getDistanceSensor("ps" + to_string(i));
    distanceSensors[i]->enable(timeStep);  
  }

  while (robot->step(timeStep) != -1) {

    // Print the values of the 8 distance sensors
    for(int i = 0; i < 8; i++) {
      
      double distance = distanceSensors[i]->getValue();
      cout << "Distance " << i << ": " << distance << endl;
    }
  }

  delete robot;
  return 0;
}