Cutebot - Object Detection Image by Freepik

Cutebot - Object Detection

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 instructed the Smart CuteBot robot car to automatically track and follow a black line. In this tutorial, we will instruct the Smart CuteBot to detect and automatically avoid objects.

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.

Object Detection

The Smart CuteBot is equipped with an ultrasonic sensor that enables it to calculate the distance to objects. The ultrasonic sensor works by emitting high-frequency sound waves and measuring the time it takes for the sound to travel to an object, reflect off its surface, and return as an echo to the sensor. In this tutorial, we will instruct the Smart CuteBot to detect and automatically avoid objects, as demonstrated in the following video.

Object Avoidance

In this section, we will develop a Python program that instructs the Smart CuteBot to detect an object and automatically stop if it is within a certain distance.

# Global variable that can be accessed by any function.
sonar = 0

# Move forwards at a constant speed.
cuteBot.motors(20, 20)


def on_forever():
    global sonar

    # Get the distance to the closest object in cm using the ultrasonic sensor.
    sonar = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)

    # If the distance to the closest object is less than a certain distance
    # e.g. 15cm, then stop the car.
    if sonar < 15 and sonar > 1:
        cuteBot.stopcar()
        basic.pause(200)

    else:

        # Otherwise move forwards.
        cuteBot.motors(20, 20)


# Keep running the 'on_forever' function in the background in a forever loop.
basic.forever(on_forever)

We begin the Python code above by defining a variable called sonar and initialising it with the value of 0. This variable will continuously keep track of the distance to the closest object. Next, we instruct the Smart CuteBot to move forwards at a constant speed. We then define a custom function called on_forever. This custom Python function instructs the Smart CuteBot to do the following:

  • The statement global sonar instructs the function to use the sonar variable that has been initialised outside of the function body. In other words, the sonar variable defined on line 2 has global scope, meaning that it can be referenced by any function.
  • The function measures the distance, in centimeters, to the closest object, and stores this distance in the globally-scoped sonar variable. This is achieved by calling the ultrasonic(unit: SonarUnit) method of the cuteBot object, and passing the unit of measurement as the first parameter. To return the distance in centimeters, we use the cuteBot.SonarUnit.CENTIMETERS sonar unit of measurement.
  • The function instructs the Smart CuteBot to stop if the measured distance to the closest object is closer than a certain distance (15cm in our example). This is achieved by testing the value of sonar in a conditional if statement, and calling the stopcar() method of the cuteBot object if the conditional statement resolves to True.
  • If the conditional statement resolves to False, then the else branch is followed and the function instructs the car to continue moving forwards at a constant speed by setting the speeds of the left and right wheels respectively to the same positive number (20 in our example).

We specify that our custom on_forever function should run continuously in the background. This is achieved by calling the basic.forever(body: ()) method, with the first and only parameter set as the name of our custom Python function i.e. on_forever that will be executed continuously in the background within a forever event-based loop.

To learn more about the basic functions and actions integrated with the Micro:bit, please refer to the Micro:bit Basic API documentation.

Jillur Quddus
Written by

Jillur Quddus

Computational Mathematician @ HyperLearning AI