Log in

View Full Version : Need help with arduino to i2c on roborio!


SoftwareRachel
10-01-2015, 15:03
Hi everyone!
This season, our team will be using an led strip with an arduino so we can control individual lights :) . We then want to connect our arduino to our roborio so we can control the arduino from our java code. We've done a lot of research but we can't figure out how to write the code for this. :mad: :confused:

For example, if we want the strip of led lights to be rainbow in autonomous, we know how to do that with the arduino code, but once we connect the arduino to the roborio (i2c port, right? :confused: ), what code would we write in eclipse to trigger the arduino? Everything we've looked at has used DigitalModule which seems to not be used this year. Thanks for the help! :)

Owen Busler
10-01-2015, 15:20
Instead of using the i2c you could use an analog output from the roboRio into the arduino. On the roboRio you would output 5v during auto and 0v during teleop. Then the arudino would output rainbow when it has a 5v input and it could change when it receives 0v.

dyanoshak
10-01-2015, 15:39
I realize that this example is in LabVIEW but similar APIs should be available in Java. Hopefully this will give you a push in the right direction.

AndyMark released some example code that drives a touch-screen shield on the RIOduino (http://www.revrobotics.com/product/rioduino/) (the REV Robotics Arduino-compatible MXP board). The screen code won't be of interest, but the last part of their example shows how they communicate between the roboRIO and the RIOduino through the I2C port.

Take a look at the example under the "Files & Documents" tab on the RIOduino with 2.8" Touchscreen (am-2999) (http://www.andymark.com/product-p/am-2999.htm) bundle page.

Again, their example is in LabVIEW, but the same APIs should exist in Java.

Hope this helps!

-David

choffman123
10-01-2015, 19:26
I'm a lazy coder but, for a super quick turnaround, you might use a few digital IO on the roborio to interact with the arduino and have a few pins on the arduino set to input. when the arduino reads a pin is high it would change the LED modes.

on the arduino, if pin 2 is High, Blue LED's blink... Etc.

you could use 4 DIO's and then use them like binary to multiply your LED modes.

or use 1 pwm and use pulsein(); on the arduino and you could have 180 modes... we did something similar with a pot as input for our driver-station board last year.

ThomasClark
12-01-2015, 02:07
This I2C tutorial (http://wpilib.screenstepslive.com/s/3120/m/7912/l/175524-sending-data-from-the-crio-to-an-arduino) will still work on a roboRIO, if you make a couple changes. Mainly, your robotInit() method just needs to have this:

public void robotInit() {
i2c = new I2C(I2C.Port.kOnboard, 168);
}

Another easy way would be to just use USB. Use the SerialPort class in your Java code to send data to the Arduino, and use Serial.read() on the Arduino to get the data. This has the advantage that it's really easy to test using the Serial Console in the Arduino IDE.

SoftwareRachel
13-01-2015, 11:34
This I2C tutorial (http://wpilib.screenstepslive.com/s/3120/m/7912/l/175524-sending-data-from-the-crio-to-an-arduino) will still work on a roboRIO, if you make a couple changes. Mainly, your robotInit() method just needs to have this:

public void robotInit() {
i2c = new I2C(I2C.Port.kOnboard, 168);
}

Another easy way would be to just use USB. Use the SerialPort class in your Java code to send data to the Arduino, and use Serial.read() on the Arduino to get the data. This has the advantage that it's really easy to test using the Serial Console in the Arduino IDE.

Thank you! That helps out a lot! :)

seg9585
13-01-2015, 11:38
This I2C tutorial (http://wpilib.screenstepslive.com/s/3120/m/7912/l/175524-sending-data-from-the-crio-to-an-arduino) will still work on a roboRIO, if you make a couple changes. Mainly, your robotInit() method just needs to have this:

public void robotInit() {
i2c = new I2C(I2C.Port.kOnboard, 168);
}

Another easy way would be to just use USB. Use the SerialPort class in your Java code to send data to the Arduino, and use Serial.read() on the Arduino to get the data. This has the advantage that it's really easy to test using the Serial Console in the Arduino IDE.

Let me know if the I2C method actually works, so far we have been unable to get communication working on the I2C OnBoard port, only the MXP. We suspect it's an issue specific to Java (See thread about the LIDAR sensor in the Technical forum)

Tom Line
17-01-2015, 16:16
Let me know if the I2C method actually works, so far we have been unable to get communication working on the I2C OnBoard port, only the MXP. We suspect it's an issue specific to Java (See thread about the LIDAR sensor in the Technical forum)

FYI - we are unable to get the Lidar working on the I2C onboard board with LabVIEW - it returns read and write errors. It is working with the I2C MXP port though.

cad321
25-01-2015, 20:16
Another easy way would be to just use USB. Use the SerialPort class in your Java code to send data to the Arduino, and use Serial.read() on the Arduino to get the data. This has the advantage that it's really easy to test using the Serial Console in the Arduino IDE.

How might one go about implementing this? Would I just plug the arduino into the usb port, set my baud rate and my port (not sure which port), and then finally use the Serial.read() command to receive what I had output over the arduino serial?

ThomasClark
26-01-2015, 18:45
How might one go about implementing this? Would I just plug the arduino into the usb port, set my baud rate and my port (not sure which port), and then finally use the Serial.read() command to receive what I had output over the arduino serial?

That's right. I haven't tried it, but you might be able to see the name of the serial port from the web interface.

Sn3akyP3t3
08-02-2015, 23:46
I saw this post awhile back and had come across a forum in the past that ensured that USB serial communication between the Raspberry Pi and Arduino is possible so I thought that it would be easy to setup a serial connection via USB. I looked at the online documentation for the WPILib SerialPort implementation and saw that documentation mentions "Driver for the RS-232 serial port on the RoboRIO".

Not being discouraged I tried some open source USB serial libraries and met failure with both of them. I figured I'll share my experience. RXTX, 64 bit precompiled binaries for Windows, was attempted, compiled, and transferred to the RoboRio, but once there it ran into runtime failure as something about the library was not available for some reason. The next library tried was java-simple-serial-connector or jSSC. This library looked promising as something quite simple with only one single jar file to add as a library in Eclipse. Unfortunately, as a WPILib project it fails to build, but has no problem as a Java application. Not sure what the issue is with that.

I returned to searching forums for some means of reading serial from USB and came across this post again. I decided to look at the code for SerialPort and I'm seeing that the option to read from USB appears to indeed exist as an option which I'm writing code for right now to test with, but ran out of time for the day. I'm hoping this is the solution and that it helps someone else. "usbSerialPort = new SerialPort(9600, Port.kUSB);" Some code example to work with for communicating between Arduino and a Pi, modify for Java, is http://www.bitsharr.com/rtxu6LvK

Sn3akyP3t3
09-02-2015, 20:28
Tested out the code today with high hopes, but it ended up throwing an error at the point that the call to SerialPort's USB device was made. The catch of the error proved to be fruitless as the returned error was the empty string. The code to inspect will be available in today's Github commit if someone wishes to take a look on their own. https://github.com/FRC-3277/2015Recycle/commits/2015CommandBasedRefactor

For now I'm going to test with I2C to read and as a last resort I believe RS-232 is available, but if that depends on the SerialPort library it may be a better idea to re-write the Arduino code library in Java and avoid that class if its buggy.

Rakusan2
10-02-2015, 13:12
My team got the Arduino communicating with the roborio over I2C using the following code on the roborio

static I2C Wire = new I2C(Port.kOnboard, 4);

if (Global.driver.Buttons.Back.changedDown) {
String WriteString = "go";
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);
}

And this code on the Arduino
#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);


}
}

The Doctor
04-02-2016, 17:02
Another easy way would be to just use USB. Use the SerialPort class in your Java code to send data to the Arduino, and use Serial.read() on the Arduino to get the data. This has the advantage that it's really easy to test using the Serial Console in the Arduino IDE.

This has never worked for me...

The Doctor
04-02-2016, 17:15
Hi everyone!
This season, our team will be using an led strip with an arduino so we can control individual lights :) .

We're doing the same thing! (we did last year as well)