Cutebot - Basic Movements
In this series of tutorials we will be using the Python programming language to instruct an Elecfreaks Smart CuteBot robot car to perform a range of tasks including basic movements, line tracking, and light & object detection. In the previous tutorial, we provided a tour of the Smart CuteBot robot car and instructions to connect it to the Microsoft MakeCode browser-based code editor. In this tutorial, we will instruct the Smart CuteBot to perform basic movements including moving forwards, reversing and stopping.
Requirements
Please ensure that you have read and followed the instructions described in our CuteBot - Getting Started tutorial in order to prepare your Micro:bit, and hence your Smart CuteBot, for programming.
Basic Movements
We will instruct the Smart CuteBot to perform basic movements when specified buttons on the Micro:bit board have been pressed, as demonstrated in the animation below.
Move Forwards
In this section, we will develop a Python program that instructs the Smart CuteBot to move forwards at a constant speed when the "A" button is pressed on the Micro:bit board.
def on_button_pressed_a():
# Move forwards (speed of left wheel: 0-100, speed of right wheel: 0-100).
cuteBot.motors(20, 20)
# When the A button is pressed, run the 'on_button_pressed_a' function.
input.on_button_pressed(Button.A, on_button_pressed_a)
We begin the Python code above by defining a custom function called on_button_pressed_a. This custom Python function instructs the Smart CuteBot to set the speed of the left wheel to 20, where the range of possible speeds start from 0 (no movement) up to 100 (maximum speed), and the speed of the right wheel to 20 as well. This is achieved by calling the motors(lspeed: number, rspeed: number) method of the cuteBot object. By setting the left wheel and the right wheel to the same speed, this has the effect of moving the car forwards at a constant speed.
Finally, we want our custom on_button_pressed_a function to run only when the "A" button is pressed on the Micro:bit board. This is achieved by calling the input.on_button_pressed(button: Button, body: ()) event handler, with the 1st parameter set as Button.A and the 2nd parameter set as the name of our custom Python function i.e. on_button_pressed_a that will be invoked when the specified button is pressed.
To learn more about the
inputevents and data from sensors integrated with the Micro:bit, please refer to the Micro:bit Input API documentation.
Reverse
In this section, we will develop a Python program that instructs the Smart CuteBot to reverse at a constant speed when the "B" button is pressed on the Micro:bit board.
def on_button_pressed_b():
# Reverse (speed of left wheel: 0-100, speed of right wheel: 0-100).
cuteBot.motors(-20, -20)
# When the B button is pressed, run the 'on_button_pressed_b' function.
input.on_button_pressed(Button.B, on_button_pressed_b)
Similar to when we instructed the Smart CuteBot to move forwards, we begin our Python code by defining a custom function called on_button_pressed_b. This custom Python function instructs the Smart CuteBot to set the speed of the left wheel to -20, where the range of possible speeds start from 0 (no movement) up to 100 (maximum speed), and the speed of the right wheel to -20 as well. By defining a negative value, this has the effect of instructing the motor, and hence the wheels, to run in the opposite direction. And by setting the left wheel and the right wheel to the same negative speed, this has the effect of reversing the car at a constant speed.
And finally, as before, we want our custom on_button_pressed_b function to run only when the "B" button is pressed on the Micro:bit board. This is achieved by calling the input.on_button_pressed(button: Button, body: ()) event handler, with the 1st parameter set as Button.B and the 2nd parameter set as the name of our custom Python function i.e. on_button_pressed_b that will be invoked when the specified button is pressed.
Stop
In this section, we will develop a Python program that instructs the Smart CuteBot to stop when both the "A" and "B" buttons are pressed together on the Micro:bit board.
def on_button_pressed_ab():
# Stop the car.
cuteBot.stopcar()
# When the A and B buttons are pressed together, run the 'on_button_pressed_ab' function.
input.on_button_pressed(Button.AB, on_button_pressed_ab)
As before, we begin our Python code by defining a custom function, but this time called on_button_pressed_ab. This custom Python function instructs the Smart CuteBot to stop. This is achieved by calling the stopcar() method of the cuteBot object. And we want our custom on_button_pressed_ab function to run only when both the "A" and "B" buttons together are pressed on the Micro:bit board. This is achieved by calling the input.on_button_pressed(button: Button, body: ()) event handler, with the 1st parameter set as Button.AB and the 2nd parameter set as the name of our custom Python function i.e. on_button_pressed_ab that will be invoked when the specified buttons are pressed.
Complete Code
The complete Python code, which instructs our Smart CuteBot to perform basic movements when specified buttons on the Micro:bit are pressed, is provided below for ease of reference.
def on_button_pressed_a():
# Move forwards (lspeed: 0-100, rspeed: 0-100).
cuteBot.motors(20, 20)
def on_button_pressed_b():
# Reverse (lspeed: 0-100, rspeed: 0-100).
cuteBot.motors(-20, -20)
def on_button_pressed_ab():
# Stop the car.
cuteBot.stopcar()
# When the A button is pressed, run the 'on_button_pressed_a' function.
input.on_button_pressed(Button.A, on_button_pressed_a)
# When the B button is pressed, run the 'on_button_pressed_b' function.
input.on_button_pressed(Button.B, on_button_pressed_b)
# When the A and B buttons are pressed together, run the 'on_button_pressed_ab' function.
input.on_button_pressed(Button.AB, on_button_pressed_ab)