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:
Create a PositionSensor object for each motor you want to track.
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.
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 3print("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());
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 radiansbreak
wheel_left.setVelocity(0.0)
wheel_right.setVelocity(0.0)
#include<webots/Robot.hpp>#include<webots/Motor.hpp>#include<webots/PositionSensor.hpp>usingnamespace webots;
intmain() {
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;
return0;
}