First drive: a square
The robot drives a square with 30 cm sides and comes back to the start. On the way you learn the two main commands: drive a distance and turn by an angle.
Work through Getting Started first: the library is installed and the Start example uploads. You need about a metre by a metre of flat floor.
What you will build
Steps
-
Create the robot
#include <UNI.h> UniBase robot("UNI"); void setup() { robot.begin(); } void loop() { }The
robotobject is the whole platform: motors, encoders and screen. The name in quotes appears on the screen.robot.begin()switches all of it on; you may leave it out — the very first command does it by itself. -
Drive straight
robot.moveDist(50, 300); // power 50, distance 300 mmThe first number is power in percent, from −100 to 100; the second is distance in millimetres. The command is blocking: the next line runs only once the robot has arrived.
Power is the speed as a percentage of the maximum: 100 is about 0.3 m/s. The robot holds this speed with the encoders, so a small power simply drives slowly.
-
Turn
robot.rotate(50, 90); // power 50, turn by 90 degreesA positive angle turns right, a negative one turns left. The robot turns on the spot: the wheels spin in opposite directions.
-
Repeat four times
void setup() { robot.begin(); for (int i = 0; i < 4; i++) { robot.moveDist(50, 300); robot.rotate(50, 90); } }Upload the sketch and put the robot on the floor.
That is the whole sketch: the loop goes into setup() of the sketch from the first step. A square
with turns is also driven by the
example; its code is on the Examples page.
Try changing it
- Make the side 500 mm. Compare power 30 and 80: how does stopping accuracy change?
- Drive the square counter-clockwise:
robot.rotate(50, -90). - Draw a triangle: three sides and three 120° turns.
- Add driving backwards and by time:
Negative power is reverse.robot.moveDist(-50, 200); // back 200 mm robot.moveTime(40, 1000); // forward for one secondmoveTime()drives for a set time rather than a distance; the robot slows down smoothly so that it stops when the time is up.
If it does not work
- The screen works but the robot does not move. Check the battery switch and the charge — see the FAQ.
- The square does not close; the corners are clearly more or less than 90°. Check the wheels and the floor — see the FAQ.
- The robot drifts on a slippery floor. Odometry counts wheel rotation and cannot see slipping — see Accuracy and Odometry.
- The robot hit something and moved on to the next command about a second later. The library noticed that the wheels had stopped and ended the command — see Movement and Commands.