AI with ZOOM:BIT: Object Tracking

AI with ZOOM:BIT: Object Tracking

In this tutorial, we're going to explore a new exciting project: Object Tracking! We'll teach ZOOM:BIT to track and follow an object using its servo motor. Let's get started.

 

Video Tutorial

 

 

How Does This Project Work?

 

By pressing Button A, ZOOM:BIT will learn and memorize the object you want it to track.

 

 

Once ZOOM:BIT has learned the object, it will start tracking it. The Huskylens connected to the servo motor will turn to follow the object. If the object moves to the right, the servo motor will turn right to keep it centered.

 

 

If the object moves to the left, the servo motor will turn left to recenter it. This way, ZOOM:BIT can keep the object in its view, no matter where it moves.

 

 

Pressing Button B will clear ZOOM:BIT's memory and prepare it to learn a new object.

 

Building the Code

 

Firstly, we'll need to add ZOOM:BIT and Huskylens extensions in MakeCode Editor. Once they are ready, we can jump into building the code.

You can view and download the complete code from here.

 

 

What the Code Does?

 

The code starts by initializing Huskylens to use the object tracking algorithm. We created a variable called current_X to keep track of the servo motor position, and we set the initial position of the servo motor to 90 degrees.

 

 

When Button A is pressed, ZOOM:BIT will learn and remember the object you want it to track. This is done by capturing the object’s image and assigning it an ID.

 

 

Pressing Button B will clear this memory, allowing you to teach ZOOM:BIT a new object.

 

 

Inside the forever loop, we created a new variable called X_center to simplify understanding the code. This variable will hold the horizontal position of the object we're tracking.

 

 

X_center refers to the horizontal position of the object being tracked on the camera's screen. Imagine the camera screen as a graph with a horizontal x-axis and a vertical y-axis. The x-axis represents the horizontal position, with the left side being 0 and the right side 320. The center of the screen horizontally would be 160. The code retrieves a value from Huskylens that represents the object's horizontal position on the screen. This value, referred to as X_center, falls between 0 (far left) and 320 (far right).

 

 

Then, we use "show custom text" block to display the X_center values of the object on the screen at (0,0) coordinates.

 

 

Note that, if the X_center value is displayed as -1, this indicates the object is not detected. 

 

 

If the object's X_center is greater than 190, the servo motor moves right by decreasing the current_X position by 3 degrees to recenter the object.

 

 

If the X_center is between 0 and 130, the servo motor moves left by increasing the current_X position by 3 degrees to recenter the object.

 

 

The servo motor’s position is constrained between 0 and 180 degrees to ensure it rotates within its range.

 

 

Project Complete

Well done completing the second project of the ZOOM:BIT & Huskylens series. In the next tutorial, we'll see how ZOOM:BIT can recognize and react to colors.

Hardware Components

ZOOM:BIT Robot Car Kit for micro:bit -22% Best Seller
x 1 unit(s)