Microbit - Visual Object Tracking With Huskylens
by Thorsgaard Efterskole in Circuits > Microcontrollers
7611 Views, 7 Favorites, 0 Comments
Microbit - Visual Object Tracking With Huskylens
I have made a number of instructables about the MU vision sensor, but earlier this year I got a class set of Huskylens thanks to the Svend Nielsen og Herdis Myginds Foundation.
It have taken me some time to learn this new sensor and what it can do compared to the MU vision sensor. It is in fact very interresting because the two vision sensors works so opposite each other. Anyway. Now that I have learned the new sensor I have a few project I want to share on instructables to help other people learn how to use the huskylens with their micro:bit.
Supplies
Materials:
1 x Micro:bit
1 x Motor:bit
1 x Huskylens (I am using the pro version)
1 x 9 volt battery + battery holder
Something to track. I use a dog snack.
Connect the Battery Holder to the Motor:bit
Red line to Vin and black to GND. You can also put the battery on now, just remember to switch the motor:bit to off.
Connect the Huskylens
First we connect the huskylens to the motor:bit like shown on the first picture.
Green -> Pin 20
Blue -> Pin 19
Black -> GND
Red -> 5 volt
Be sure that the motor:bit is switched to 5 volt and that you draw power from the volt and ground pins by pin 13 to 20.
Getting the Extension
First we go to the Makecode editor and start a new project. We then go to "Advanced" and select "Extensions". Be aware that since I am danish, these buttons have slightly different names in the pictures. In extensions we search for "Huskylens" and select the first result.
Initializing Connection and Enabling the Algorithm
The first orange part of the code initialize the I2C connection.
The second orange part of the code enables the object tracking algorithms.
The huskylens is a bit limited with what other sensors you can combine it with, since it needs pin 19&20 for its I2C connection. That means you can't combine it with another thing that need those two pins, like an OLED screen.
Programming
The Huskylens uses a coordinate system to place the objects that it tracks. You can see the coordinate system in the first picture just above. The importent thing to know is that the top left corner is 0;0 and that the X value goes from 0 to 320 and the Y value from 0 to 280.
For this program I just want the micro:but to tell us, which of the rectangles that you can see on picture 2, the object we are tracking is in.
I can't show the entire program in one screenshot and it is kind of repetitive, so I have only shown par of the program in the third picture above. The first orange block make this huskylens take a temporary and use its algorithm on it.
The second orange block check if the object we are tracking is in the picture. If the object isn't then there is no reason for us to ask where it is.
Third orange block asks for X value of the object we are tracking and the math checks if it is less than 140. If it is, then we know what we are tracking is to our left.
The next orange blocks ask for the Y value and the math check to see if it is either less than a 100 or more than 140. If the Y value is neither then we know that the Y value is betwen 100 and 140 and the object we are tracking must be in the middle rectangle to the left, so we mark that with a middle-left dot on the micro:bit.
We do that for all 9 combinations of X and Y values. In that way the program is simple, but rather long. You can see or download it all here.
Learn Object
First you use the large triple button on the top left to select object tracking. Then you get the object you want to track inside the small square on the screen and then you push the smaller button on the top right for the huskylens to learn it.
Test It
That is it. Now test your project.