Pixy2 sending incorrect packets for vectors

I’m part of the FRC Team 7157 I’ve been trying to get the Pixy2 to work with the RoboRIO. I’ve got it connected via I2C custom cable and have confirmed that data can be sent back and forth between the rio and pixy. Using the protocol documentation on the official pixy website, I’ve been able to make use of the LEDs on the front of the camera, but am unable to get useful data from the line tracking program. My program sends a byte array request to the pixy for getMainComponents, and prints out the return buffer in hex format in the console. Although other commands seem to work using this method, the data I get back from this method doesn’t match what’s displayed on the website. Here’s what the output looks like:

  • beginning transfer…

  • Pixy Data:

  • [B@a86356

  • a4

  • 0

  • 80

  • 80

  • 80

  • 80

  • 80

  • 80

Changing which vector the camera is focused on only changes the first two numbers. The next 6 numbers are always 80.

I’ve tried configuring the settings in PixyMon, using the setMode command, and looking through the source code to find some hints. I’ve tried calling changeProg and setting the default mode to line tracing, but the output remains the same.

I’ve tried to contact Pixy support on this but they’re taking a long time to get back to me, so I was wondering if anyone else had it all figured out already.
Any suggestions?

Link to code (look at Robot.java and Pixy2Handler.java):


1 Like

When you use pixymon can you point the pixy at the line you are trying to track? Does pixymon show a vector being detected? You should be able to configure everything in pixymon and view the vector. That will at least make sure the line you are pointing at is being detected. If you do not see line being detected tweak the settings in pixymon until you do. After you do that you can just plug pixy2 into i2c on your robot and try same thing and see if you get vector data back.

Another thing I noticed is that the code is always trying to read 12 bytes in response to getMainFeatures: https://github.com/Mubotics7157/2019-Robot-Code/blob/master/DriveBot/src/main/java/frc/robot/Pixy2Handler.java#L156
The protocol spec(https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:protocol_reference#getmainfeatures-features-wait) says that only 6 bytes will be returned for sure. More bytes will be returned if a line is detected. The code should read 6 bytes first, then decide how much more to read based on length of payload.

I see the code does check for payload length on line 158 and tries to read length bytes more on line 173. Too late by then… the code has already read 12 bytes into initBuffer on line 156. If there was any vector data it was already read off the wire into the buffer.

Think about what data pixy2 returns if it doesn’t see a line. What would the value of byte 3 be? What happens if you try to use that value to more data over i2c from pixy2?

Line 187 there is some constant 8? What is that for? If it is the size of a vector then I think it should be 6, not 8.

A suggestion, maybe print all the bytes being read off the wire. Every time bytes are read into buffer print the buffer out. Might I suggest a method similar to this to print byes out? https://github.com/BHSRobotix/PowerUp2019/blob/pixy2_port/src/main/java/org/usfirst/frc2876/PowerUp2018/Pixy2/Pixy2.java#L17
Or learn to use java debugger and set breakpoints after every read and see what data is returned.

Another suggestion, allocate buffers exact size needed. For example on line 154 make the buffer 6 bytes and use initBuffer.length everywhere the number of bytes is needed.

It looks like you are making pretty good progress getting pixy2 to work… keep going… good luck!

1 Like

Thanks! I’ll try that today during our meeting.

Another suggestion… you can take advantage of other people’s work getting pixy2 protocol working in Java. See this thread: Using pixy @dilanpace1496 has suggested using https://github.com/PseudoResonance/Pixy2JavaAPI
The thread I linked also has link to a github search you can do to find other team’s code for pixy2 java. There’s nothing wrong with looking at other peoples/teams code to give you ideas. And you can even use their code in your code. But if you enjoy (and have time) writing your own version of pixy2 java code then keep on going. My team started writing our own code and decided to keep going down that path. We are learning a lot about protocols, reading specs, byte vs int, arrays, etc. It is fun! Whatever you decide to do remember you can always ask for help here.

1 Like

Were you able to get the data from line tracking mode (vector data)?

I’ve read you can’t run the Pixy2 in compatibility mode and access the line tracking feature.