Turns and waypoints
The robot keeps track of where it is at all times. In this lesson you learn to use that: turn to an exact heading and drive by coordinates.
How the robot knows where it is
From the wheel encoders the robot keeps recomputing three numbers — the same ones shown on the screen:
X and Y in millimetres and Angle in degrees. This is called
odometry.
- At power-on the robot assumes it stands at (0, 0) facing along the X axis.
- The current values are returned by
getAbsX(),getAbsY()andgetAbsAngle(), and all three at once bygetOdometry().
Steps
-
Compare the two turns
robot.rotate(50, 90); // another 90 degrees to the right of where the robot faces now robot.rotateTo(50, 90); // face heading 90 degrees, wherever it faces nowrotate()turns by an angle counted from the previous target, so in a series of moves the errors do not pile up.rotateTo()turns to a heading of the odometry and does not depend on what came before: it also works aftermotors(),stop()or after the robot was pushed. -
Drive a square on absolute headings
robot.setPosition(0, 0, 0); for (int i = 0; i < 4; i++) { robot.moveDist(50, 300); robot.rotateTo(50, (i + 1) * 90); // headings 90, 180, 270, 360 } robot.displayPrint("Angle", robot.getAngle());At the end the screen shows the accumulated angle. The closer it is to 360, the more exactly the robot has returned to its original direction.
-
Tie the coordinates to the field
robot.setPosition(0, 0, 0); // x, y in millimetres and heading in degreessetPosition()tells the robot where it stands. You can give the real coordinates of the start cell — for example,robot.setPosition(150, 150, 90). -
Describe a route as points
const float route[][2] = { {400, 0}, {400, 400}, {0, 400}, {0, 0}, }; void setup() { robot.begin(); robot.setPosition(0, 0, 0); for (int i = 0; i < 4; i++) { robot.moveTo(50, route[i][0], route[i][1]); delay(300); } robot.rotateTo(50, 0); // restore the original heading at the end }A route of four points. moveTo() turns the robot towards the target by itself and drives to it in a straight line. -
Watch the position from the computer
void loop() { robot.printOdometry(); delay(1000); }Open the Serial Monitor at 115200 baud. Once a second it prints a line with the encoder ticks, the coordinates and the heading.
Finished sketches: and . The code is on the Examples page.
What should happen
Try changing it
- Describe a triangle or a U shape with points.
- Run the PrecisionTurns example: it drives a square first with
rotate(), then withrotateTo(), and shows the angle after each run. Compare the two numbers. - Stretch the route to ten points and watch how the return error grows.
If it does not work
- The robot drove the wrong way. Check the axes figure: +Y is to the right. Check the
argument order:
moveTo(power, x, y). - The robot does not return exactly to the start. Odometry error grows with distance and with wheel slip. The reasons are explained in Accuracy and Odometry.
- The coordinates did not reset after
resetAngle().resetDistance()andresetAngle()only clear the distance and angle counters. The pose is set bysetPosition().