Location Sensor

Retrieves the robot’s position in the field

This page is under construction.

Basics

The location sensor, also called the GPS, is used to retrieve the absolute x, y, and z position of the robot, useful for localizing the robot within the field. The units returned are in meters.

There are 4 basic steps to using this sensor:

  1. Start by importing or including the appropriate class for the location 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.

Note: The horizontal plane of the sensor's coordinate system is defined by the X and Z axes. The Y axis is therefore the vertical axis:

   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from controller import Robot, GPS # Step 1: Import GPS

robot = Robot() 

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

timestep = int(robot.getBasicTimeStep())

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

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

    x = gps.getValues()[0] # Step 4: Use the getValues() function to get the sensor readings
    y = gps.getValues()[1] # Note that the gps returns a list of 3 values for x, y, z, position
    z = gps.getValues()[2]
    
    print("x: " + str(x) + " y: " + str(y) + " z: " + str(z))
 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
#include <iostream>

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

using namespace webots;
using namespace std;

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

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

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

  while (robot->step(timeStep) != -1) {
  
    double x = gps->getValues()[0]; // Step 4: use the getValues() function to get the sensor reading
    double y = gps->getValues()[1]; // Note that the gps returns an array of 3 values for x, y, z, position 
    double z = gps->getValues()[2]; 
    
    cout << "x: " << x << " y: " << y << " z: " << z << endl; // Print out the values of the sensor
  }

  delete robot;
  return 0;
}

Example

This sample gives an example application of the location sensor. It continuously prints the distance, calculated using the distance formula, from the starting position of the robot.

   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from controller import Robot, GPS
import math

robot = Robot()

gps = robot.getDevice("gps")

timestep = int(robot.getBasicTimeStep())

gps.enable(timestep)

robot.step(timestep) # Must step once to update sensor reading
startX = gps.getValues()[0] # Store initial robot position 
startZ = gps.getValues()[2]

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

    x = gps.getValues()[0]
    z = gps.getValues()[2]
    
    dist = math.sqrt((x-startX)**2 + (z-startZ)**2); # Use the distance formula to calculate distance to starting position
    
    print("Distance from starting location: " + str(dist))
        
 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
#include <iostream>
#include <math.h>

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

using namespace webots;
using namespace std;

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

  Robot *robot = new Robot();
  
  GPS* gps = robot->getGPS("gps");

  int timeStep = (int)robot->getBasicTimeStep();
  
  gps->enable(timeStep);

  robot->step(timeStep); // Must step once to update sensor reading
  double startX = gps->getValues()[0]; // Store initial robot position
  double startZ = gps->getValues()[2];

  while (robot->step(timeStep) != -1) {
  
    double x = gps->getValues()[0]; 
    double z = gps->getValues()[2]; 
    
    double dist = sqrt(pow(x-startX, 2) + pow(z-startZ, 2)); // Use the distance formula to calculate distance to starting position
    
    cout << "Distance from starting location: " << dist << endl;
  }

  delete robot;
  return 0;
}


Last modified April 17, 2021: Added info about sensors (bba725b)