Arduino and roboRIO comms

I am currently trying to drive LED Strips using data from the rio. I am having issues with code, and I also want to know what the best method would be for comms between the two devices. I tried serial over USB but I couldn’t get a response from the arduino, I can post both sections of code if wanted, and I am currently trying to see how I2C would work.

Anyone have wisdom concerning this topic? I am using addressable LEDs right now, so that makes coding it a little different.

Recently, I was tasked with the same problem. I tried USB Serial comm to see how reliable it is, and it actually worked pretty well. Below is the code I used. There may be an advantage to I2C that I’m not aware of, but we’ve used Serial for two way communication and it worked for us great.

private SerialPort arduino;

try {
	arduino = new SerialPort(9600, SerialPort.Port.kUSB);
	arduino.setTimeout(0.001);
}
catch (UncleanStatusException e){
	Logger.log("ERROR: Unable to connect to Arduino");
}

arduino.writeString("C");
String read = arduino.readString(3);
1 Like

If you post your code (for both Arduino and NIRoborio) I’d be happy to try and help it work for you as well.

1 Like

You’re the man. Java code first, I will post all relevant code out of our driving program.

SPI sp;

sp = new SerialPort(9600, SerialPort.Port.kUSB);
ba[0] = (byte)trgb;
ba[1] = (byte)-trgb;
sp.write(ba,2);

trgb is just a joystick *255 so I could theoretically control colors using the joystick for testing.

#include <PololuLedStrip.h>

// Create an ledStrip object and specify the pin it will use.
PololuLedStrip<6> strip;

// Create a buffer for holding the colors (3 bytes per color).
#define LED_COUNT 3
rgb_color colors[LED_COUNT];

void setup()
{
Serial.begin(9600);
rgb_color colors[LED_COUNT];

}

void loop()
{
for(uint16_t i = 0; i < LED_COUNT; i++)
{
colors[i] = rgb_color(Serial.read(),Serial.read(),Serial.read());
}

    strip.write(colors,3); 

  }

The problem may be not in the Serial communication itself but rather in the way you’re storing/interpreting the data. To isolate the USB serial variable, I recommend you try the following:

  1. Simplify the roborio code to just have it send over one of two simple things (i.e. either ‘a’ or ‘b’) in a loop. You should be able to control what it sends through a joystick or something simple.

  2. Simplify the arduino code so that all it’s doing is reading from Serial and acting accordingly in a way you can spot (i.e. if ‘a’ is received then turn on an LED, if ‘b’ is receieved then turn it off).

Once you run this experiment, you should have further insight as to whether the problem is in the communication path or just the surrounding code (in which case you should debug each variable to make sure it’s what you want it to be).

2 Likes

Just ran it by sending the String “a” over the port, and Serial.readString - ing on the arduino and got no change. I think it’s a comms issue.

2 Likes

If you still are running into issues and want to give I2C a try, we may be able to help. We are using I2C to pass pixy data from the Arduino to the RoboRIO and it’s been working pretty well. We have a GitHub repo that is just code to talk between the RoboRIO and Arduino, with a few pixy lines in there. The only thing is we never actually had a need to write data to the Arduino but the functions are there and should work. They’ve just never been tested.

Personally, I prefer to use the Analog Out function of the RoboRIO (hidden in the MXP). It interfaces with the Arduino extremely well and doesn’t require the complexity of the SPI port or proper Serial. Additionally, It can be set up in code as easily as

AnalogOut analogOut = new AnalogOut(0);
analogOut.setVoltage(  Voltage between 0 and 5  );
1 Like

Just to verify - are you using a Leonardo or other ATmega 32U4 Arduino? Most non 32U4 boards have Serial mapped to communication over a pair of digital pins.

The last time my team did this we had trouble using i2c and didn’t have the time to figure out the issue (it was right before a regional). With all of the DIO and PWM ports on our RoboRio already taken up for sensors and ESCs, we found out you could use the relay ports (roborio) to send signals to the DIO ports (arduino) with essentially 4 bits of data (4 ports). This allowed us to run 16 different modes on the LEDs that year and provided a quick and easy fix.

1 Like

I am trying to use a generic Arduino Uno

It’s been a while since I’ve used a UNO, but IIRC, the Serial object on the UNO is only on pins 0 and 1 (RX/TX), not USB.

I am pretty sureSerial.println() on the Uno will push data out to the Serial-USB converter as well as the hardware serial on 0/1. In other words, I think ports 0 and 1 are connected directly to the Serial-USB converter. If you need additional Serial ports, they can be simulated with SoftwareSerial.