RobotAR Tutorial

Getting Started

Before you start, you need to print the AR markers from this link.

Steps to get started:

  1. Open the app.

  2. The app will ask you to scan a flat surface for the robot to move around. Tap to select the surface.

  3. Next, place the four boundary markers to form something like a rectangle. It doesn't have to be a rectangle. Use the iPad screen to scan the boundary markers.

  4. Lastly, place the robot marker to indicate the starting position of the robot.

  5. Now you are ready to write your Python code. A text editor should appear.

Moving the Robot Straight for Three Seconds

In this first tutorial, we will move the robot straight for three seconds and stop. Write the following Python code into the editor.

from robotar import RobotAR

robot = RobotAR()

robot.wheels(50, 50)

robot.sleep(3)

robot.halt()

Explanation of the code:

  • The first line import the class RobotAR that is used to create the object robot in line two.

  • The second line instantiate the object RobotAR.

  • The third line calls the wheels(left, right) method of the robot to move the wheels at 50 for the left wheel and 50 for the right wheel.

  • The sleep method is used to delay the execution of the next command for three seconds.

  • The last line is to stop the wheels of the robot. This is the same as calling robot.wheels(0, 0).


Blinking on LEDs

In this second tutorial, we will look on how we can use the LEDs. Write the following Python code into the editor.

from robotar import RobotAR

robot = RobotAR()

for step in range(10):
robot.leds_top(32, 0, 0)
robot.sleep(1)
robot.leds_top(0, 0, 0)
robot.sleep(1)

Explanation of the code:

  • We use for step in range(10) to repeat 10 times the body of the loop.

  • The code that is being repeated 10 times are the indented code. The first line in this indented code is to turn the Top LED to red, i.e. R=32, G=0, B=0, using robot.leds_top(32, 0, 0).

  • The next line is to pause for 1 second before executing the next line.

  • The line after robot.sleep(1) is to turn off the Top LED.

  • The last line in the indented block is to pause for another 1 second before going back to the top of the loop.

Stop at an Obstacle

In this third tutorial you will need an "Obstacle" card that gives you a vertical block. Write the following code and see the setup of the obstacle card in the video.

from robotar import RobotAR

robot = RobotAR()

while True:

obs = robot.prox_horizontal[2]
print(obs)
if obs > 2000:
break
robot.wheels(50, 50)
robot.sleep(0.5)
robot.halt()

Explanation of the code:

  • This code contains an infinite loop using while True. Since the condition of the while statement is always true, it will continue looping.

  • The first line in the body of the while-loop stores the proximity sensor result into the variable obs.

  • The second line print this value into the console.

  • The third line contains if-else statement. The value when it detects an obstacle will be greater than zero and the closer it is the higher the value will be. So when this value is greater than 2000, it will break from the infinite loop.

  • If it does not break, it will continue to move forward with speed 50 for both left and right wheels.

  • If it breaks from the loop it will stop the robot by calling robot.halt().

See the video for the setup of the card and the expected behaviour.


Using Buttons to Turn on LEDs

In this tutorial, we will show how you can use button as input to control the robot's light. Type in the following code:

from robotar import RobotAR

robot = RobotAR()

while True:

robot.leds_top(0, 0, 0)

if robot.button_forward == 1:

robot.leds_top(32, 0, 0)

elif robot.button_left == 1:

robot.leds_top(0, 32, 0)

elif robot.button_right == 1:

robot.leds_top(0, 0, 32)

elif robot.button_backward == 1:

break

robot.sleep(0.5)

Explanation of the code:

  • The first two lines are the usual code to import and to instantiate the robot object.

  • We have an infinite loop with while True: condition. The loop is only broken when break statement is called.

  • The first line inside the loop body is to switch off the top LEDs.

  • The second line onwards present different conditions when different button is pressed:

    • if forward button is pressed, the top LED is turned on with RED light (32, 0, 0).

    • if the left button is pressed, the top LED is turned on with GREEN light (0, 32, 0).

    • if the right button is pressed, the top LED is turned on with BLUE light (0, 0, 32).

    • if the backward button is pressed, the loop is terminated and the program will stop

  • There is one statement robot.sleep(0.5) to delay the execution of the code so that the buttons are checked only at about 0.5 seconds approximately.

See videos below for expected behaviour. Notice the bottom right for the button presses.

Challenge 1

Try to write the code to move in a square. Hint: you can turn the robot by adjusting the speed of the left and the right wheels. See video below for the expected output.