Get Started with Pixy2 Vision Sensor for Robotics Applications Using Arduino Hardware and Simulink
This example shows how to use Simulink® Support Package for Arduino™ Hardware and a Arduino hardware board to get started with interfacing the Pixy2 vision sensor for robotics applications.
Simulink Support Package for Arduino™ Hardware enables you to interface Pixy2 vision sensor with Arduino hardware board for robotics applications. The Pixy2 vision sensor detects simple lines, intersections, barcodes, and you can teach the sensor to track colored moving objects. Hence, the Pixy2 vision sensor is useful in developing object tracking and path following robots. This example shows you the line and colour tracking modes of the model.
Complete the Getting Started with Arduino Hardware and Communicating with Arduino Hardware examples.
Simulink Support Package for Arduino Hardware
To run this example, you will need the following hardware.
Arduino hardware board
Pixy2 vision sensor
1. Build a two-wheeled robot with a Pixy2 vision sensor mounted on it.
2. Connect Pixy2 vision sensor to the Arduino hardware board through the supplied Arduino cable. If you do not have the cable, refer to Pin Connections to Pixy2 for customized connections.
3. Set up Pixy2 vision sensor through PixyMon Utility
PixyMon is an application that acts as an interface to help you view what the Pixy2 vision sensor sees as either unprocessed or processed video.
Configure I2C Address: Open the PixyMon utility and navigate to File > Configure > Pixy Parameters (saved on Pixy) > Interface. Configure the I2C address between 0x54 and 0x57.
Configure Color Tracking Mode: You can use the PixyMon utility to configure the sensor for seven unique colored objects. For more information on how to train the sensor for Color Signature 1, refer to Train Pixy2.
Configure Line Tracking Mode: Open the PixyMon utility and navigate to File > Configure > Pixy Parameters (saved on Pixy) > Expert. Check Delayed turn box to enable intersection detection.
PixyMon Utility and the preconfigured simulink model cannot run simultaneously. Close the PixyMon Utility app after the configuration is complete and before coming back to the simulink model.
This example uses a preconfigured Simulink model from the Simulink Support Package for Arduino Hardware. To open the Simulink model, run this command in the MATLAB® Command Window.
The model is divided into two areas based on the functionality in each area.
You can choose the color tracking subsystem and comment out the other if you choose to use color tracking mode of the Pixy2 vision sensor with the Arduino Hardware. The readily available Pixy2 Vision Sensor block from the Simulink Support Package for Arduino™ Hardware is used in this model to represent the connection of pixy2 sensor to the Arduino Hardware board. The block outputs the coordinates (X,Y), width, height and object count of the biggest object of the selected colour signature.
You can choose the line tracking subsystem and comment out the other if you choose to use line tracking mode of the Pixy2 vision sensor with the Arduino Hardware. The block outputs the start (X0,Y0) and end (X1,Y1) coordinates of the most significant line and a flag (IntFlag) that detects an intersection along the line.
Run Simulink Model in Connected IO Mode
You can run the model in connected IO mode where the model communicates with the hardware without any code generation in the hardware.
Follow these steps to run the Simulink model in connected IO mode.
1. Open arduino_robotics_pixy2_gettingstarted model.
2. On the Modeling tab of the model, click Model Settings. In the Configuration Parameters dialog box, click Hardware Implementation on the left pane and set the Hardware board parameter to Arduino.
3. On the Hardware tab of the model, in the Mode section, click Run with IO.
4. Place a colored object, a path or an intersection anywhere in the field of view of the Pixy2 vision sensor. Observe the captured data of the line or colour tracking modes in the display blocks of the subsystem block.
Run Model on Monitor & Tune Mode
1. On the Hardware tab of your Simulink model, click Monitor & Tune to run the model on the Arduino hardware board.
2. Place a colored object, a path or an intersection anywhere in the field of view of the Pixy2 vision sensor. Observe the real time values of the objects in line or colour tracking modes in the display blocks of the subsystem block.
Other Things to Try
1. Try training the sensor with objects of different color signatures. You can observe the results in real time.
2. The default Color Tracking mode for Pixy2 vision sensor is
Color Signature. You can use other tracking options like
Color Code Signature,
Color and Color Code Signature (Mixed).
3. The default Color Signature is
1. You can use
ALL to track all the color signatures.
4. Try deploying the model on the Arduino hardware board by clicking Build, Deploy & Start in the Deploy section of the Hardware tab of your Simulink model.
5. Try different interfacing options such as serial (SPI, I2C and UART), USB, analog or digital interfacing of Pixy2 vision sensor. For more information on interfacing options, refer to Pin Connections to Pixy2.