Movement

Explanation of the commands used to control the robot’s movement

This page is under construction.

Basics

Note: The Webots API accepts and returns angles in radians, a unit of measure for angles (similar to degrees). If you want to work in degrees, note:

  • number_of_radians = number_of_degrees * PI / 180
  • number_of_degrees = number_of_radians * 180 / PI

To use motors in Webots:

  1. Create a motor object for each motor you want to use. These motor objects will be used to call motor related methods to control the motors.
  2. Set the position target of each motor to infinity so the motors will never stop on their own.
  3. Set the motors to rotate at a set angular velocity (in radians per second).
   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from controller import Motor
...

wheel_left = robot.getDevice("wheel1 motor")     #Step 1
wheel_right = robot.getDevice("wheel2 motor")

wheel_left.setPosition(float('inf'))    #Step 2
wheel_right.setPosition(float('inf'))

wheel_left.setVelocity(5.0)     #Step 3
wheel_right.setVelocity(5.0)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <webots/Motor.hpp>
...

//Inside main:
Motor *wheel_left = robot->getMotor("wheel1 motor");    //Step 1
Motor *wheel_right = robot->getMotor("wheel2 motor");

wheel_left->setPosition(INFINITY);   //Step 2
wheel_right->setPosition(INFINITY);

wheel_left->setVelocity(5.0);    //Step 3
wheel_right->setVelocity(5.0);

Tracking Wheel Revolutions / Motor Encoders

Webots provides a PositionSensor that allows users to track how much a wheel has rotated - some may know this as motor encoders. To use the PositionSensor:

  1. Create a PositionSensor object for each motor you want to track.
  2. Enable the PositionSensor using the enable method. The parameter that you pass in is the number of milliseconds the program should wait before updating the PositionSensor’s value.
  3. Retrieve the amount of radians your motor has rotated so far. Note that rotating a motor backward decreases this count.
   
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from controller import PositionSensor
...

leftEncoder = wheel_left.getPositionSensor()    #Step 1
rightEncoder = wheel_right.getPositionSensor()

leftEncoder.enable(timeStep)    #Step 2
rightEncoder.enable(timeStep)

print("Left motor has spun " + str(leftEncoder.getValue()) + " radians")    #Step 3
print("Right motor has spun " + str(rightEncoder.getValue()) + " radians")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <webots/PositionSensor.hpp>
...

//Inside main:
PositionSensor *leftEncoders = wheel_left->getPositionSensor();     //Step 1
PositionSensor *rightEncoders = wheel_right->getPositionSensor();

leftEncoders->enable(timeStep);    //Step 2
rightEncoders->enable(timeStep);

printf("Left motor has spun %f radians", leftEncoders->getValue());     //Step 3
printf("Right motor has spun %f radians", rightEncoders->getValue());

Full Example Program

   
 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
from controller import Robot
from controller import Motor
from controller import PositionSensor

robot = Robot() # Create robot object
timeStep = 32   # timeStep = number of milliseconds between world updates

wheel_left = robot.getDevice("wheel1 motor")    # Motor initialization
wheel_right = robot.getDevice("wheel2 motor")
wheel_left.setPosition(float('inf'))
wheel_right.setPosition(float('inf'))

leftEncoder = wheel_left.getPositionSensor()    # Encoder initialization
rightEncoder = wheel_right.getPositionSensor()
leftEncoder.enable(timeStep)
rightEncoder.enable(timeStep)

wheel_left.setVelocity(5.0)
wheel_right.setVelocity(5.0)

while robot.step(timeStep) != -1:
    if leftEncoder.getValue() > 20.0:   # If left motor has spun more than 20 radians
        break

wheel_left.setVelocity(0.0)
wheel_right.setVelocity(0.0)
 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
#include <webots/Robot.hpp>
#include <webots/Motor.hpp>
#include <webots/PositionSensor.hpp>

using namespace webots;

int main() {	
    Robot *robot = new Robot(); // Create robot object
    int timeStep = (int)robot->getBasicTimeStep();  // timeStep = number of milliseconds between world updates

    Motor *wheel_left = robot->getMotor("wheel1 motor");    // Motor initialization
    Motor *wheel_right = robot->getMotor("wheel2 motor");
    wheel_left->setPosition(INFINITY);
    wheel_right->setPosition(INFINITY);

    PositionSensor *leftEncoders = wheel_left->getPositionSensor();     // Encoder initialization
    PositionSensor *rightEncoders = wheel_right->getPositionSensor();
    leftEncoders->enable(timeStep);
    rightEncoders->enable(timeStep);

    wheel_left->setVelocity(5.0);
    wheel_right->setVelocity(5.0);

    while (robot->step(timeStep) != -1) {
        if (leftEncoders->getValue() > 20.0) // If left motor has spun more than 20 radians
            break;
    };

    wheel_left->setVelocity(0.0);
    wheel_right->setVelocity(0.0);
    
    delete robot;
    return 0;
}