|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools |
Rating:
|
Display Modes |
|
#1
|
|||
|
|||
|
Pixy I2C
I am trying to figure out how to interface a Pixy Camera with our Roborio using the i2C port. Does anyone have experience with the Pixy or i2C communications?
Also, can we instead use an Arduino as a mediator, we already wrote the code for the Arduino and It would be easier to do it via the Arduino to Roborio if possible. |
|
#2
|
|||
|
|||
|
Re: Pixy I2C
My team got the Arduino communicating with the roborio over I2C using the following code on the roborio
Code:
//this is outside a method
I2C Wire = new I2C(Port.kOnboard, 4);
//this is inside a method
if (Global.driver.Buttons.Back.changedDown) {
String WriteString = "go"; //this is being sent to the Arduino
char[] CharArray = WriteString.toCharArray();
byte[] WriteData = new byte[CharArray.length];
for (int i = 0; i < CharArray.length; i++) {
WriteData[i] = (byte) CharArray[i];
}
Wire.transaction(WriteData, WriteData.length, null, 0);
}
Code:
#include <Wire.h>
void setup()
{
pinMode (13, OUTPUT);
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
}
void loop()
{
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
String LED = "";
while ( Wire.available() > 0 )
{
char n=(char)Wire.read();
if(((int)n)>((int)(' ')))
LED += n;
}
if (LED == "go")
{
digitalWrite (13, HIGH);
}
}
Last edited by Rakusan2 : 11-02-2015 at 19:19. Reason: spelling mistake |
|
#3
|
|||
|
|||
|
Re: Pixy I2C
We are using pixy to lock onto yellow totes in AUTO. My team found it easier to decode packets via the serial port (connected to roboRIOC) than the I2C. We can share code if you need it.
|
|
#4
|
||||
|
||||
|
Re: Pixy I2C
I'd be interested in the code.
|
|
#5
|
|||
|
|||
|
Re: Pixy I2C
That would be great, if serial is possible it would be a lot easier.
|
|
#6
|
|||
|
|||
|
Re: Pixy I2C
I would also like to see said code.
|
|
#7
|
||||
|
||||
|
Re: Pixy I2C
Hi there. Here is what out team is using to pry information out of the pixy. I hope this helps! I'm not sure why the code block is anti-tabbing...
(the code gpetilli mentioned) Code:
import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.SerialPort.Port;
//Warning: if the pixy is plugged in through mini usb, this code WILL NOT WORK b/c the pixy is smart and detects where it should send data
public class Pixy {
SerialPort pixy;
Port port = Port.kMXP;
PixyPacket[] packets;
PixyException pExc;
String print;
public Pixy() {
pixy = new SerialPort(19200, port);
pixy.setReadBufferSize(14);
packets = new PixyPacket[7];
pExc = new PixyException(print);
}
//This method parses raw data from the pixy into readable integers
public int cvt(byte upper, byte lower) {
return (((int)upper & 0xff) << 8) | ((int)lower & 0xff);
}
public void pixyReset(){
pixy.reset();
}
//This method gathers data, then parses that data, and assigns the ints to global variables
public PixyPacket readPacket(int Signature) throws PixyException {
int Checksum;
int Sig;
byte[] rawData = new byte[32];
try{
rawData = pixy.read(32);
} catch (RuntimeException e){
}
if(rawData.length < 32){
System.out.println("byte array length is broken");
return null;
}
for (int i = 0; i <= 16; i++) {
int syncWord = cvt(rawData[i+1], rawData[i+0]); //Parse first 2 bytes
if (syncWord == 0xaa55) { //Check is first 2 bytes equal a "sync word", which indicates the start of a packet of valid data
syncWord = cvt(rawData[i+3], rawData[i+2]); //Parse the next 2 bytes
if (syncWord != 0xaa55){ //Shifts everything in the case that one syncword is sent
i -= 2;
}
//This next block parses the rest of the data
Checksum = cvt(rawData[i+5], rawData[i+4]);
Sig = cvt(rawData[i+7], rawData[i+6]);
if(Sig <= 0 || Sig > packets.length){
break;
}
packets[Sig - 1] = new PixyPacket();
packets[Sig - 1].X = cvt(rawData[i+9], rawData[i+8]);
packets[Sig - 1].Y = cvt(rawData[i+11], rawData[i+10]);
packets[Sig - 1].Width = cvt(rawData[i+13], rawData[i+12]);
packets[Sig - 1].Height = cvt(rawData[i+15], rawData[i+14]);
//Checks whether the data is valid using the checksum *This if block should never be entered*
if (Checksum != Sig + packets[Sig - 1].X + packets[Sig - 1].Y + packets[Sig - 1].Width + packets[Sig - 1].Height) {
packets[Sig - 1] = null;
throw pExc;
}
break;
}
}
//Assigns our packet to a temp packet, then deletes data so that we dont return old data
PixyPacket pkt = packets[Signature - 1];
packets[Signature - 1] = null;
return pkt;
}
}
Code:
public class PixyException extends Exception {
public PixyException(String message){
super(message);
}
}
Code:
public class PixyPacket {
public int X;
public int Y;
public int Width;
public int Height;
}
Last edited by 2B || !2B : 13-02-2015 at 17:01. Reason: I forgot to mention something. |
|
#8
|
|||
|
|||
|
Re: Pixy I2C
2B || !2B ,
I am the lead programmer on Mike's team, thank you so much for your code, We are cutting it a little close to the deadline and this definitely helps. Also, awesome name. Thanks, gsampel |
|
#9
|
|||
|
|||
|
Re: Pixy I2C
How exactly do we wire the Pixy, the stock cable does not seem like the correct one to connect directly to the RoboRio
|
|
#10
|
|||
|
|||
|
Re: Pixy I2C
Quote:
Look at the Pixy Docs - i dont remember if they call it rs232 or serial port. roboRIO also has a serial port. I will ask our electrical team to post more details as to what they did. |
|
#11
|
|||
|
|||
|
Re: Pixy I2C
Is this the sort of thing were we can put together our own cable, or will we need to use the actual serial cable? I don't think we have one, so we would have to make one, probably out of a few scrapped PWM cables. I would find it really helpful to see a picture of your connection or what cable you used.
thanks, gsampel |
|
#12
|
|||
|
|||
|
Re: Pixy I2C
Quote:
|
|
#13
|
|||
|
|||
|
Re: Pixy I2C
Does anyone have a picture or instructions on how to connect the pixy to the Roborio using the ribbon cables?
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|