LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > ActivityBot 360° and Pixy2 for Color Object Tracking

ActivityBot 360° and Pixy2 for Color Object Tracking

]

What it’s about

The Pixy2 CMUcam can be trained to identify objects by their colors and send their locations and sizes to the ActivityBot 360. This step-by-step tutorial shows how to make the ActivityBot 360° use the data it receives from the Pixy2 to find objects of certain colors and track them in BlocklyProp. Code examples in C and Spin are also available on the Pixy2 product page under “downloads”.

IMPORTANT: Follow each step; don’t skip any. You’ll have to carefully follow all the steps in this tutorial for this robot color tracking application to work.  Then, you’ll see a totally new personality in your robot and be very proud of the results!

Before you start

You will need:

  • Assembled and calibrated ActivityBot 360° Robot (#32600)
  • Pixy2 with Cable + Mounting Hardware (#30028)
  • Cylinders, blocks or painted objects of different colors. 3D printed objects work well. In this tutorial we’re using 1″ square wooden blocks.

You should have completed:

  • Add a Pixy2 CMUcam to an ActivityBot 360°. This tutorial shows you how to mount the Pixy2 CMUcam on the ActivityBot 360° using the cable and mounting hardware.
  • BlocklyProp Robotics with the ActivityBot 360 Tutorials. Experience with the servo speed blocks and managing large programs (collapsing blocks, using terminal, etc.) in BlocklyProp will be especially useful.

Pixy Power from USB or Propeller Activity Board, not Both!

It’s OK to use either power setup (A) or (B) shown below, but do not connect both at the same time.  For more info, see Make the Pixy2 Connections.

Power Supply Setup (A)

In Power Supply Setup (A), Pixy2 is powered through USB during configuration. Ground, Sin and Sout are connected to Propeller Activity Board. The Pixy2’s power cable is not connected to the Propeller Activity Board WX 5V supply.

Power Supply Setup (B)

In Power Supply Setup (B), the USB cable is removed. The Pixy2’s Ground (Vss), Sin and Sout are connected to Propeller Activity Board. The Pixy2’s power cable is connected to the Propeller Activity Board WX 5V supply. Pixy2 is powered through the Propeller Activity Board’s 5V connection. The Propeller Activity Board WX must be in switch position 2 for this arrangement (position 2 provides power to the servo header 5V ports). You may also use switch position 1 if you connect the Pixy2 5V header above the breadboard.

Configure Pixy2 with PixyMon Software

The PixyMon software is used to teach the Pixy2 to recognize up to seven color signatures. PixyMon also provides a view from the Pixy2’s camera. Settings for communication baud rate, color correction and other modes of operation need to be configured. The Pixy2 can also be taught color signatures without PixyMon using the white button, but we’ve found this method to be difficult to verify and less reliable in general.  

Download and Install PixyMon 

Visit the Pixy2 Downloads and install PixyMon for your operating system. You can also find FAQs, troubleshooting guides, and much more are available on the Pixy2 Wiki.  

Connect Pixy2 to Computer and Run PixyMon Software

The following steps are also shown in this video. 

]

  • Connect the Pixy2 to your computer’s USB port using Power Supply Setup (A) pictured earlier. 
  • Make sure the Propeller Activity Board’s 5V is NOT CONNECTED to the Pixy2.
  • Place some colored objects in front of your ActivityBot 360 and run PixyMon. 

You’ll see your colored objects through the PixyMon viewer:

  • Click File and select Configure and the Interface tab.  
  • Change the Data out port setting from “Arduino” to “UART”. 
  • Set the UART baudrate to 19200. Click Apply and then Ok.

Restore default parameters (under File menu). As you become familiar with PixyMon software you will find yourself tuning colors for your environment’s lighting and background, sometimes moving your robot from place to place – especially to demonstrate to others. It can be fastest to restore the default parameters and reteach the color signatures in your new environment. When do this, you’ll also have to configure the Data out port and UART baudrate settings again, as shown above. These settings are necessary for our example code. 

  • Set signatures 1, 2, and 3 for the colored blocks. With three different colored blocks, set three signatures (for three objects of the same color, set one signature and verify the remaining two are “seen” with the same signature). 
    • Click Action / Set Signature 1. Select the area you wish to identify as Signature 1. Signature 1 is the green block in this example.  
    • Click Action / Set Signature 2. Select the area you wish to identify as Signature 2. Signature 2 is the yellow block in this example. 
    • Click Action / Set Signature 3. Select the area you wish to identify as Signature 3. Signature 3 is the red block in this example.

PixyMon should now show the color signature number of each unique block color with an outline and a label:

Getting the perfect color signature matches isn’t easy but you can modify the environment to improve your ability to get quality color signature matches. Try these things: 

  • Select Action / Clear All Signatures to start over
  • Select Action / Toggle Lamp if you need to turn the LED light on. Natural light and white backgrounds will also affect your success, changing the color hues.
  • Clear background objects you don’t wish to attach to a signature, else they’ll be seen by the ActivityBot 360° and make coding far more difficult.

The Toggle lamp feature is perhaps the single most important option for setting color signatures. If you set your color signatures with the LED lamp ON, then you will also need to turn it ON in the BlocklyProp example Pixy2TerminalDisplay.svg code. The default code example has the LED in the OFF state. 

Through experimentation we’ve found the LED lamp should be set ON when there’s no natural light or the room is dark. The LED lamp can usually be OFF when natural lighting exists. Experiment!  

Additional controls including white balance and color ranges are also accessible through PixyMon’s configure menu, documented on the Teach Pixy an Object wiki. We’ve found that while the many configurations allow for more precise results they are also not necessary for basic success and functionality. Also see the Tips on Improving Detection Accuracy wiki. 

Using BlocklyProp Terminal to Show Color Signatures

You’ve taught the Pixy2 which color signatures to recognize, and assigned each color to a number. In this tutorial, we will load the Pixy2TerminalDisplay.svg BlocklyProp code into the Propeller Activity Board and see each object’s position and size using the Terminal. The BlocklyProp program will receive and parse that data from the Pixy2, and then decide what to do with it.

  • Disconnect USB cable from the Pixy2. 
  • Connect the Activity Board’s 5V supply to the Pixy2 as shown in Power Supply Setup (B).
    • If you use the 5V supply from the black row of pins on top of the breadboard, put the Propeller Activity Board’s swich in Position 1 or 2.
    • If you use the 5V supply on the servo headers, put the Propeller Activity Board’s switch in Position 2.   
  • Download Pixy2TerminalDisplay.svg
  • Start BlocklyProp Launcher and solo.blocklyprop.com.
  • Click the Open Project / Choose File and open the Pixy2TerminalDisplay.svg you just downloaded.

This program has many functions to manage the serial data parsing from the Pixy2. These functions are shown in “collapsed” view (right-click a function if you want to expand it). You will not need to use most of these function blocks; it is convenient to keep their view collapsed.   

  • Click the Load and run (Save code to EEPROM) button. 

After loading code into the Propeller Activity Board, BlocklyProp Solo will open its Terminal and display information about colored objects that you have trained the Pixy2 to detect.  

  • Try placing some of those colored objects in the Pixy2’s view, and verify that its signature number, position and size are displayed.  

Congratulations! Your BlocklyProp program is now ready to be modified and used on the ActivityBot 360 to track objects!  

If you find BlocklyProp’s Terminal too slow to refresh, you can use Parallax Serial Terminal (PST) instead, which only runs on Windows operating systems. PST’s display may be paused or cleared and also supports multiple font sizes. To use PST: 

  • Download and unzip Parallax Serial Terminal. 
  • Double click Parallax-Serial-Terminal.exe to run it.  (No installation required.)
  • Press Close on the BlocklyProp Terminal.

  • Set Baud Rate to 115200. 
  • Click Enable. 
  • Press/release the Propeller Activity Board’s RST button.
  • Click into the light yellow transmit pane, and press any key.

A Tiny Tutorial video on using PST instead of BlocklyProp Terminal is also available.

What the Numbers Mean

The Pixy2TerminalDisplay.svg program translated the detected color signatures into numbers and displayed them on the Terminal. In the prior tutorial we configured the Pixy2 to see these blocks:

We loaded a program into the Propeller Activity Board WX and observed the following in the terminal window:

How do the two relate? As an X,Y grid with the 0,0 origin in the upper left:

 

The “show_signatures” Code Block in Pixy2TerminalDisplay.svg

The “show_signatures” Code Block in Pixy2TerminalDisplay.svg

The Pixy2TerminalDisplay.svg program translated the color signatures into numbers and displayed them in the Terminal. This example BlocklyProp code has many functions, but one of them — the show_signatures function — steps through all detected color signatures and displays their signature numbers, sizes, and position.

The key to using the Pixy2TerminalDisplay.svg code example is to be able to use the variables in the show_signatures function. 

Vision is complex! Amazon, Google, Uber, Softbank and GM are spending billions of dollars to effectively use vision and sensors for autonomous vehicles. Pixy2 simplifies what your robot sees, yet there’s still a significant amount of data to evaluate when color signatures are detected. Attributes such as quantity of each signature, the signature number, size and location may quickly complicate simple robotic code!Before we use the show_signatures function, let’s get a look at what’s inside:

The repeat blockIdx loop reports the maximum number of color signatures detected, which you taught the Pixy2 with the PixyMon software.  The variables within this function describe the properties of each colored block that was detected: blockIdx, blocksDetected, bSignature, bXpos, bYpos, bWidth, and bHeight.  Also, note maxBlocks = 3; it’s the 3rd block in the function.  You will have to adjust that if you want the program to report more than three blocks.  

For each of the color signatures that were detected, the loop stores the signature number, x/y location, width and height in their respective variables. These variables are also overwritten with each loop iteration. For example, if you taught the Pixy2 to recognize three signatures and are only interested in the data from signature 1, you’ll need to add code that uses the data before it gets overwritten by the next loop iteration. To do that, you could add an if block that checks if bSignature = 1 that contains a routine that takes some kind of action in response to the bXpos and bYpos values.   

 

Single-Axis Colored Object Tracking with ActivityBot 360°

]

Complex projects are always built in small steps. With microcontrollers, these challenges are best being divided into small pieces with consideration to the outcome. This means you’ll be programming and testing in small steps, saving your programs along the way in case you need to roll back to a prior version. Therefore, let’s start by simply pointing the ActivityBot 360° towards a single color signature which centers the colored object in the middle of the display using its X or Y position.

These are the steps:

  • Connect the Pixy2 to the USB cable. Use power setup (A).
  • Make sure the 5V wire is disconnected from the Pixy2.
  • Run the PixyMon software and clear all signatures (Action / Clear Signatures).
  • Choose the colored object you would like to track and set it as signature 1 (Action / Set signature 1). Take note of whether you have the LED lamp ON or OFF when setting the signature.

Here is an example with a single red block and LED lamp ON.

  • Keeping the ActivityBot 360° in place with the colored object in front, disconnect the USB cable from the Pixy2, close the PixyMon software and change over to power setup (B). 
  • Make sure the USB cable is disconnected from the Pixy2.
  • Connect the ActivityBot 360° to your computer with the USB cable. 
  • Turn the ActivityBot 360° power switch to position 1. 
  • Start your Launcher and BlocklyProp Solo.
  • Run the Pixy2TerminalDisplay.svg code in BlocklyProp Solo. You should now see the  values for Signature = 1 in the Terminal window.

Programming the ActivityBot 360° To Track Color Signatures

With a single colored block which has an associated signature assigned in PixyMon, we can program the ActivityBot 360° to point towards the block. Suppose the red block is positioned either left, center or right in the Pixy2’s view. Three simple cases could mean three different programming actions!

The variable bXpos in the “show_signatures” function of Pixy2DisplayTerminal.svg shows where the detected color signature is from the Pixy2’s point of view. The values of bXpos will range from 1 (far left) to 316 (far right). 

One easy coding approach would be an IF-ELSE block that evaluates bXpos and either turns left or right towards the block at full speed. 

But this is a very clunky control method. A simple full-speed, on-or-off control loop that quickly “startles” the ActivityBot 360° towards the red block, possibly over-shooting the goal of centering the block. 

Proportional Control: Smoothly Turn Towards Color Signature 

Proportional control is a much more suitable approach. With proportional control, the desired drive speed depends on the distance of the red block from the middle of the Pixy2’s view. If the red block is far left, the robot would set the servo speeds to fast, to move toward the red block aggressively. But if the block is only a few pixels left of center, the robot could respond with a slow-speed driving command to the left. Once the block is centered in the camera’s view, the robot will calm down and remain still, making only small adjustments to the speed until it stays still. 

We can use the bXpos value to determine how to affect the robot’s drive speed. If the red block is far left or right in the camera view, the ActivityBot 360° will rotate quickly to center the block. If the red block is just left or right of the center, the ActivityBot 360° could respond more slowly, avoiding an overshoot.   

Let’s review the numbers we’re working with before we look at the code modifications:

  • ActivityBot 360° servo speed values range -100 to +100
  • Left speed = 0 and right speed = 0 causes the robot to stay put. 
    • Left speed = -100 and right speed = 100 is a left turn at full speed.
    • Left speed = 100 and right speed = -100 is a right turn at full speed.
  • When the detected color signature is on the left, we want to turn left; if detected on the right side, let’s turn right.
  • X Position of the color signature 1 will be between 0 (far left) and 316 (far right).
  • Half of 316 is 158; this occurs when the block is centered in front of the ActivityBot 360.
  • If we subtract 158 from any measured X position, then x position can be used in the following way:
    • 0 means the detected color signature is directly in front of the ActivityBot 360.
    • -158 means the object is far left of the robot, but still in the camera view.
    • 158 means the object is far right, but still in the camera view .
  • If there is no color signature detected, we should stop (speeds of 0,0).

We can use the value of bXpos to calculate our servo motor drive speeds. If the red block is on the far left (Example #1 above) and we subtract 158 from the bXpos variable, bXpos s now -157. This is a large number to use as a servo speed, so we’ll divide it by 3 and it’s now -52, a number we can use to drive a servo. This is very convenient, so we’ll use -52 on the left servo motor (to turn it backwards) and the positive value (multiply by -1) of 52 on the right servo motor. This will cause the robot to turn left, towards the object. 

If the red block is on the far right (xPosition = 316) and we subtract 158 from this variable, it is now 158. Again, divide it by three and it’s 52. Drive the left motor with 52 and the right motor with -52 and turn right, towards the object.  

Program the ActivityBot 360° To Point Towards Color Signature 1 on X-Axis

Download Pixy2_X-Axis_Following.svg

The example code Pixy2_X-Axis_Following.svg is a modified version of the Pixy2TerminalDisplay.svg code, both produced on BlocklyProp Solo. The following changes were made to Pixy2TerminalDisplay.svg:

  • Added an IF-DO statement in the navigate_by_signatures function to do the following:
    • Check for presence of color signature 1 
    • Calculate a left and right wheel speed based on the position of the detected signature
    • Added an additional function call to display the servo speeds on the Terminal
  • Disabled all terminal blocks in the main program loop in order to increase the sampling/motor drive frequency

Your Turn: Program the ActivityBot 360° To Point Towards Color Signature 1 on Y-Axis

Using the same approach, you can modify the BlocklyProp example code to drive near or away the colored object, centering it in the middle of the Y-axis display of the Pixy2. 

X and Y Axis Colored Object Tracking with ActivityBot 360°

]

The ActivityBot 360° can also be programmed to track a color signature on both the X and Y axis. This sample code allows the ActivityBot to center the object in the middle of the Pixy2 camera by driving left, right, forward and backward as shown in the video. 

  • Just download the new example code and give it a try!

Download Pixy2_X-Y_Axis_Roaming.svg

The Pixy2_X-Y_Axis_Roaming.svg project is a modified version of the original Pixy2_Terminal_Display.svg. The following changes were made:

  • Added an IF-DO statement in the navigate_by_signatures function to do the following:
    • Check for presence of color signature 1 
    • Calculate a left and right wheel speed based on the position of the X and Y position of detected color signatures
      • Left and right wheel speeds account for changes in bXpos and bYpos
    • Added an additional function call to display the servo speeds on Terminal
  • Disabled all terminal blocks in the main program loop in order to increase the sampling/motor drive frequency

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/activitybot-360-and-pixy2-for-color-object-tracking/
Links