This page contains all of Pixy2's functionality available through the serial interfaces.
Pixy2's general API can be called through the base Pixy2 object, for example:
Pixy2 pixy; pixy.init(); // call init() member function
See also the Color Connected Components API, Line tracking API, and Video API for other functionality.
For a byte-level reference to the protocol, check out the new Pixy2 Serial Protocol - Packet Reference
init() should be called before your program communicates with Pixy. init() returns an error value (<0) if it fails and 0 (PIXY_RESULT_OK) if it succeeds.
getVersion() queries and receives the firmware and hardware version of Pixy2., which is put in the version member variable. It returns an error value (<0) if it fails and 0 (PIXY_RESULT_OK) if it succeeds. It is called automatically as part of init().
changeProg() instructs Pixy2 to switch programs. It takes a string argument that indicates the name of the program. The argument can be a partial string of the program name as long as it is unique with respect to the other program names. It returns an error value (<0) if it fails and 0 (PIXY_RESULT_OK) if it succeeds.
setServos() sets the servo positions of servos plugged into Pixy2's two RC servo connectors. The servo values range between PIXY_RCS_MIN_POS (0) and PIXY_RCS_MAX_POS (1000). It returns an error value (<0) if it fails and 0 (PIXY_RESULT_OK) if it succeeds.
setCameraBrightness() sets the relative exposure level of Pixy2's image sensor. Higher values result in a brighter (more exposed) image. It returns an error value (<0) if it fails and 0 (PIXY_RESULT_OK) if it succeeds.
setLED() sets Pixy2's RGB LED value. The three arguments sets the brightness of the red, green and blue sections of the LED. It will override Pixy2's own setting of the RGB LED. It returns an error value (<0) if it fails and 0 (PIXY_RESULT_OK) if it succeeds.
setLamp() turns on/off Pixy2's integrated light source. The upper argument controls the two white LEDs along the top edge of Pixy2's PCB. The lower argument sets the RGB LED, causing it to turn on all three color channels at full brightness, resulting in white light. Both arguments are binary, zero or non-zero. It returns an error value (<0) if it fails and 0 (PIXY_RESULT_OK) if it succeeds.
getResolution() gets the width and height of the frames used by the current program. After calling this function, the width and height can be found in the frameWidth and frameHeight member variables. Note, calling changeProg() will call getResolution() automatically and update frameWidth and frameHeight.
Pixy2 will automatically change programs if, for example, you call ccc.getBlocks() from the color connected components program followed by line.getMainFeatures() from the line tracking program. These “automatic program changes” will not update frameWidth and frameHeight member variables.
getFPS() gets Pixy2's framerate. The framerate can range between 2 and 62 frames per second depending on the amount of light in the environment and the min frames per second setting in the Camera configuration tab. This function can also serve as a simple indicator of the amount of light in the environment. That is, low framerates necessarily imply lower lighting levels.
This contains the Pixy2's version information as a result of getVersion(). Inside the Version struct:
Calling this prints the version information to the console.
This variable contains the hardware version number.
This variable contains the firmware major version number.
This variable contains the firmware minor version number.
This variable contains the firmware build version number.
This variable contains the a string description of the firmware type.
This variable contains the width of the frame in pixels. This field is updated upon calling init() and upon calling changeProg() as each program may have a unique frame size.
This variable contains the height of the frame in pixels. This field is updated upon calling init() and upon calling changeProg() as each program may have a unique frame size.
This is an instance of the Color Connected Components object associated with the color_connected_components program.
This is an instance of the Line Tracking object associated with the line_tracking program.
General error result
This is returned when Pixy has no new data. It is used in polling mode, as opposed to blocking mode.
This is returned when the data packet received contains a checksum error.
This is returned when Pixy2 has taken too long to return its result.
This is returned when the user is interacting with the button (e.g. teaching Pixy a signature) and can't perform the requested action.
This API can be called through the ccc member of the Pixy2 object, for example:
Pixy2 pixy; pixy.ccc.getBlocks();
See also the Pixy2 General API, Line tracking API, and Video API for other functionality.
For a byte-level reference to the protocol, check out the new Pixy2 Serial Protocol - Packet Reference
Firmware versions 3.0.11 and greater will automatically switch to the color_connected_components program when making requests through the color connected components API. Older firmware versions will return errors when attempting to call these functions while the color connected components program is not running.
A good program to run to familiarize yourself the CCC algorithm is the ccc_hello_world example found in the Arduino library.
getBlocks() gets all detected blocks in the most recent frame. The new data is then available in the blocks member variable. The returned blocks are sorted by area, with the largest blocks appearing first in the blocks array. It returns an error value (<0) if it fails and the number of detected blocks (>=0) if it succeeds.
Setting wait to false causes getBlocks() to return immediately if no new data is available (polling mode). Setting wait to true (default) causes getBlocks() to block (wait) until the next frame of block data is available. Note, there may be no block data if no objects have been detected.
sigmap is a bitmap of all 7 signatures from which you wish to receive block data. For example, if you are only interested in block data from signature 1, you would pass in a value of 1. If you are interested in block data from both signatures 1 and 2, you would pass in a value of 3. If you are interested in block data from signatures 1, 2, and 3, you would pass a value of 7, and so on. The most-significant-bit (128 or 0x80) is used for color-codes. A value of 255 (default) indicates that you are interested in all block data.
maxblocks indicates the maximum number of blocks you wish to receive. For example, passing in a value of 1 would return at most 1 block. A value of 255 (default) indicates that you are interested in all blocks.
The number of blocks contained in the blocks member variable.
This array contains all of the block data as a result of getBlocks(). The blocks in this array are sorted by area, with the largest blocks appearing first in the blocks array.
Inside the Block struct:
Calling this prints the block information to the console.
This variable contains the signature number or color-code number.
This variable contains the x location of the center of the block. The value ranges between 0 and frameWidth (315).
This variable contains the y location of the center of the block. The value ranges between 0 and frameHeight (207).
This variable contains the width of the block. The value ranges between 0 and frameWidth (316).
This variable contains the height of the block. The value ranges between 0 and frameHeight (208).
This variable contains the angle of color-code in degrees. The value ranges between -180 and 180. If the block is a regular signature (not a color-code), the angle value will be 0.
This variable contains the tracking index of the block. When Pixy2 detects a new block, it will add it to a table of blocks that it is currently tracking and assign it a tracking index. It will then attempt to find the block (and every block in the table) in the next frame by finding its best match. Each block index will be kept for that block until it either leaves Pixy2's field-of-view, or Pixy2 can no longer find the object in subsequent frames (because of occlusion, lack of lighting, etc.)
This variable contains the number of frames a given block has been tracked. When the age reaches 255, it remains at 255.
This API can be called through the line member of the Pixy2 object, for example:
Pixy2 pixy; pixy.line.getMainFeatures();
See also the Pixy2 General API, Color Connected Components API, and Video API for other functionality.
For a byte-level reference to the protocol, check out the new Pixy2 Serial Protocol - Packet Reference
Firmware versions 3.0.11 and greater will automatically switch to the line_tracking program when making requests through the line tracking API.
A good program to run to familiarize yourself the Line Tracking algorithm is the line_hello_world example found in the Arduino Pixy2 library. This quickstart guide will get you off to a good start.
The line-tracking algorithm is really easy to use, especially if you stick to getMainFeatures(), which does it's best to keep your program simple, and it does a pretty good job. If you haven't stepped through this quickstart guide, do it!
The getAllFeatures() function is for more advanced programs and applications. Steer clear of it if you're just getting started!
This function gets the latest features including the Vector, any intersection that connects to the Vector, and barcodes. The results are returned in the variables vectors, intersections, and barcodes, respectively.
The optional features argument is a bitwise-ORing of LINE_VECTOR (1), LINE_INTERSECTION (2), and LINE_BARCODE (4), depending on what features you are interested in. All features, if present, are returned by default.
Setting wait to false causes getMainFeatures() to return immediately if no new data is available (polling mode). Setting wait to true (default) causes getMainFeatures() to block (wait) until the next frame of line feature data is available, unless the current frame of features hasn't been returned before, in which case, it will return immediately with the current frame's features. Note, there may be no feature data if no features have been detected.
If successful, the result is a bitwise-ORing of LINE_VECTOR (1), LINE_INTERSECTION (2), and LINE_BARCODE (4), depending on the data returned. If unsuccessful it returns an error value (<0).
getMainFeatures() tries to send only the most relevant information. Some notes:
This function returns all lines, intersections and barcodes that the line tracking algorithm detects. The results are returned in the variables vectors, intersections, and barcodes, respectively.
The optional features argument is a bitwise-ORing of LINE_VECTOR (1), LINE_INTERSECTION (2), and LINE_BARCODE (4), depending on what features you are interested in. All detected features are returned by default.
Setting wait to false causes getMainFeatures() to return immediately if no new data is available (polling mode). Setting wait to true (default) causes getMainFeatures() to block (wait) until the next frame of line feature data is available. Note, there may be no feature data if no features have been detected.
If successful, the result is a bitwise-ORing of LINE_VECTOR (1), LINE_INTERSECTION (2), and LINE_BARCODE (4), depending on the data returned. If unsuccessful it returns an error value (<0).
This function sets various modes in the line tracking algorithm. The mode argument consists of a bitwise-ORing of the following bits:
Normally, the line tracking algorithm will choose the straightest path (branch) when encountering an intersection. 2) Setting LINE_MODE_TURN_DELAYED will prevent the line tracking algorithm from choosing the path automatically. This is useful if your program doesn't know beforehand which path it wants to take at the next intersection.
Normally, the line tracking algorithm will choose what it thinks is the best Vector line automatically. Setting LINE_MODE_MANUAL_SELECT_VECTOR will prevent the line tracking algorithm from choosing the Vector automatically. Instead, your program will need to set the Vector by calling setVector().
Normally, the line tracking algorithm will try to find dark lines on a light background (black lines). Setting LINE_MODE_WHITE_LINE will instruct the line tracking algorithm to look for light lines on a dark background (white lines).
This function tells the line tracking algorithm which path it should take at the next intersection.
Turn angles are specified in degrees, with 0 being straight ahead, left being 90 and right being -90 (for example), although any valid angle value can be used. Valid angles are between -180 and 180.
setNextTurn() will remember the turn angle you give it, and execute it at the next intersection. The line tracking algorithm will then go back to the default turn angle for subsequent intersections.
Upon encountering an intersection, the line tracking algorithm will find the path in the intersection that matches the turn angle most closely.
This function tells the line tracking algorithm which path to choose by default upon encountering an intersection. The line tracking algorithm will find the path in the intersection that matches the default turn angle most closely. A call to setNextTurn() will supercede the default turn angle for the next intersection.
Turn angles are specified in degrees, with 0 being straight ahead, left being 90 and right being -90 (for example), although any valid angle value can be used. Valid angles are between -180 and 180.
If the LINE_MODE_MANUAL_SELECT_VECTOR mode bit is set, the line tracking algorithm will no longer choose the Vector automatically. Instead, setVector() will set the Vector by providing the index of the line.
The Vector has a direction. It normally points up, from the bottom of the camera frame to the top of the camera frame for a forward-moving robot. Calling reverseVector() will invert the vector. This will typically cause your robot to back-up and change directions.
The number of lines in the vectors variable.
This array contains either all of the detected lines if getAllFeatures() is called or the Vector if getMainFeatures() is called.
Inside the Vector struct:
Calling this prints the vector information to the console.
This variable contains the x location of the tail of the Vector or line. The value ranges between 0 and frameWidth (79) 3)
This variable contains the y location of the tail of the Vector or line. The value ranges between 0 and frameWidth (52).
This variable contains the x location of the head (arrow end) of the Vector or line. The value ranges between 0 and frameWidth (79).
This variable contains the y location of the head (arrow end) of the Vector or line. The value ranges between 0 and frameWidth (52).
This variable contains the tracking index of the Vector or line. When Pixy2 detects a new line, it will add it to a table of lines that it is currently tracking. It will then attempt to find the line (and every line in the table) in the next frame by finding its best match. Each line index will be kept for that line until the line either leaves Pixy2's field-of-view, or Pixy2 can no longer find the line in subsequent frames (because of occlusion, lack of lighting, etc.)
This variable contains various flags that might be useful.
LINE_FLAG_INTERSECTION_PRESENT: This flag is only available if getMainFeatures() is called and the Vector is provided. This flag indicates that an intersection was detected but may not have met the filtering constraint.
LINE_FLAG_INVALID: This flag is only available if getAllFeatures() is called. This flag indicates that the line has been detected but has not met the filtering constraint.
The number of intersections in the intersections variable.
This array contains either all of the detected intersections if getAllFeatures() is called or a single intersection that connects to the Vector if present and if getMainFeatures() is called.
Inside the intersection struct:
Calling this prints the intersection information to the console.
This variable contains the x location of the intersection. The value ranges between 0 and frameWidth (79).
This variable contains the y location of the intersection. The value ranges between 0 and frameHeight (52).
This variable contains the number of lines (branches) in the intersection and in the m_intLines array.
This array contains the lines in the intersection.
Inside the IntersectionLine struct:
This variable contains the tracking index of the line.
This variable contains the angle in degrees of the line.
The number of barcodes in the barcodes variable.
This array contains the detected barcodes.
Inside the Barcode struct:
Calling this prints the barcode information to the console.
This variable contains the x location of the barcode. The value ranges between 0 and frameWidth (79).
This variable contains the y location of the barcode. The value ranges between 0 and frameHeight (52).
This variable contains various flags that might be useful.
LINE_FLAG_INVALID: This flag is only available if getAllFeatures() is called. This flag indicates that the barcode has been detected but has not met the filtering constraint.
This variable contains the value of the code, which can range between 0 and 15.