Axon Commands for Blackfin (and Examples)
Blackfin Initialization
Before using any Blackfin commands, you first need to initialize the camera.
All this function does is accept firmware version info, sets the resolution,
and inserts a short delay so that the camera has time to adjust to the settings.
Blackfin_Initialize();
Echo Command
Skip this command unless you plan to write your own functions -
you will not normally need to use this command.
Whenever you send a command to the camera, it returns a response of some time. What
the Echo command does is waits for the response, records the response into the array
response[counter] character by character, and returns once the response is completed by the camera.
//intercept camera response
//response_counter is the number of expected characters,
//as defined by the Blackfin documentation
Blackfin_Echo(response_counter);
Mean Color
The mean color algorithm finds the average color within an image. This is useful
in determining the dominating color, typically background noise, for use with other
algorithms.
To get a current reading on mean color, just call this function:
//get mean colors
Blackfin_YUV_Mean();
It will then update three int variables that you can use as you like:
Y_mean, U_mean, V_mean
I will give examples of using these values later in this tutorial.
Define a Color Blob to Track
The middle mass algorithm
allows your robot to track the center location of a color blob. So logically, the first
step would be to first define the colors in the blob.
Up to 16 separate colors can be simultaneously tracked on the Blackfin.
To declare a color to track, you first need to choose a bin number.
Then you need to define the Y, U, and V minimum and maximum allowable colors in a blob. This
is the format you should use:
//define color blob
Blackfin_Bin_Range(bin, ymin, ymax, umin, umax, vmin, vmax);
Now fill in the values as you like.
We will use bin #0 for the first color (because I said so, silly). Then, using
the YUV chart, I selected YUV values to track the color blue.
//define BLUE color blob
Blackfin_Bin_Range(0, 60, 255, 160, 255, 0, 120);
If you wanted to also track the color red, you should also send this command
(using bin #1):
//define RED color blob
Blackfin_Bin_Range(1, 60, 255, 0, 120, 120, 255);
Auto-Calibration Methods
Although hardcoding colors for your robot to track works, its actually
a really crude unreliable method. Its very sensitive to environmental changes,
isn't as robust, and requires a decent amount of tweaking.
The better way to do it is make use of the Mean Color algorithm. Here are three
code examples done for you:
//track blue LED auto-calibrate
Blackfin_Bin_Range(0, (Y_mean+40), 255, (U_mean+40), 255, 0, 120);
//track flame auto-calibrate
Blackfin_Bin_Range(1, (Y_mean+15), 255, 0, 120, (V_mean+15), 255);
//track white line auto-calibrate
Blackfin_Bin_Range(2, (Y_mean+50), 255, 0, 255, 0, (V_mean-5));
Looking at the code, you will see that it is setting the mean value as a threshold.
If you think about it for a bit, knowing that the mean color is basically the environment
color, you will realize that by doing this you are effectively eliminating half the environment.
Now, to eliminate the rest of the environment, you need to add some hard coded tweak values to
the mean values.
How do you know how big the tweak should be? Well, keep decreasing the color range until the returned number
of pixels is small, like less than 200-ish. It greatly depends on the colors of the environment and
the tracked object.
Blob Detection and Middle Mass
Now that you have defined blobs to track, you just need to query the Blackfin camera
for calculated information on each blob. To do so, call this function with your
desired bin number:
//return blob location data for bin#
Blackfin_Blob_Detect(bin);
By calling this function, it will then store useful information into these
seven values that you can use in your own functions:
X_left - The far left edge location of the blob
X_right - The far right edge location of the blob
Y_top - The top edge location
Y_bottom - The bottom edge location
pixels - Total pixels contained within the blob. This is useful to know in that really small pixel counts
could be noise, and really large pixel counts could signal an ill-defined blob.
X_centroid - Middle mass X coordinate
Y_centroid - Middle mass Y coordinate
Camera Pan Tilt Commands for Servos
If you have pan/tilt servos moving your camera, the HeadTrack function
can be very useful for you:
//move robot head
//confidence is minimum number of pixels to track
//high damping ratio values slow camera down,
//but also reduces harmful oscillations
Blackfin_HeadTrack(confidence, damping_ratio);
You want the confidence higher than expected noise. I got decent results using
a value of 20. As for damping_ratio, I got good results using a value of 13
using the 16MHz Axon microcontroller. The
damping ratio is highly dependent on clock speed - faster microcontrollers require
higher damping ratios.
I calibrated the rotation values using two 180 degree Hitec HS-311 on my Axon microcontroller,
so you may need to modify the function code in Blackfin_Axon.c a bit to get it to properly work.
I used fuzzy logic as the control algorithm.
Auto Constrast
Auto Constrast uses a histogram method to make sure the colors in your images
fill the entire spectrum. You typically want to turn it on,
let it auto-configure to the robot surroundings, then turn it off again so the settings
remain fixed. For example:
//pre-configure auto contrast
auto_contrast_on();
delay_ms(65500);//delay a bit
auto_contrast_off();
Auto White Balance
Auto White Balance determines how bright your image is. You typically want to turn it on,
let it auto-configure to the robot surroundings, then turn it off again so the settings
remain fixed. For example:
//pre-configure auto white balance
auto_white_on();
delay_ms(65500);//delay a bit
auto_white_off();
Other features like edge detection or face detection are being worked on.
YUV: What is it?
Like RBG, YUV is a method of representing colors.
The Y channel describes Luma (slightly different than Luminance), the range of value between light and dark.
Luma is the signal seen by black and white televisions. The U (Cb) and V (Cr) channels subtract the Luminance
values from Red (U) and Blue (V) to reduce the color information.
Use this chart to better understand how U and V define color:
where -.5 to +.5 is 0 to 255 for the Blackfin camera.
But that graph only shows U and V. The third component (shown over time
in this below animation) is Y - basically brightness.
To learn more, check out YUV at Wikipedia.