Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

poc switching arduino comms over to i2c #77

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions arduino/onrobotlights/onrobotlights.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// Create a variable that will store the colors for the LEDs.
CRGB leds[NUM_LEDS];

//I2C address of the arduino
#define I2C_ADDR 4

int pattern = 0;

void setup() {
Expand All @@ -28,6 +31,8 @@ void setup() {
// * leds - the variable that is storing all the colors
// * NUM_LEDS - the number of LEDs to control
Serial.begin(9600);
Wire.begin(I2C_ADDR);
Wire.onReceive(receiveEvent);

// Ensures that the onboard led LOW == off.
pinMode(13, OUTPUT);
Expand Down Expand Up @@ -63,9 +68,12 @@ void setup() {
FastLED.setMaxPowerInVoltsAndMilliamps(5, 400);
}

void loop() {
if (Serial.available() > 0) {
byte value = Serial.read();
void receiveEvent(int howMany){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a count of events received?

int addr = Wire.read();

//the robot wants to send some data
if (addr == 0x10) {
byte value = Wire.read();

// These byte values should match with values in
// ArduinoSubsystem.enumToByte()
Expand All @@ -80,7 +88,9 @@ void loop() {
pattern = 2;
}
}
}

void loop() {
if (pattern == 0) {
off();
} else if (pattern == 1) {
Expand All @@ -96,6 +106,9 @@ void loop() {
}

FastLED.show();

// Roughly match the roborio loop timing.
delay(20);
}

void bluewash() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class RobotContainer {
private final StopGripperCmd stopGripperCmd = new StopGripperCmd(gripperSubsystem);

// Display
// private final ArduinoSubsystem arduino = new ArduinoSubsystem(SerialPort.Port.kUSB2);
private final ArduinoSubsystem arduino = new ArduinoSubsystem();

public void setbrake() {
drivetrain.setBrake();
Expand Down
54 changes: 14 additions & 40 deletions src/main/java/frc/robot/subsystems/ArduinoSubsystem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package frc.robot.subsystems;

import edu.wpi.first.wpilibj.SerialPort;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.I2C.Port;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj2.command.SubsystemBase;

Expand All @@ -12,16 +13,17 @@ private enum PATTERN {
YELLOW
}

private SerialPort.Port port;
private SerialPort handler;
private Timer timer;
private Timer colorChange;
private PATTERN state;
private boolean initialized = false;
private I2C arduino;
private final int ARDUINO_ADDRESS = 4;
private final Port PORT = Port.kOnboard;
private final int WRITE_ADDRESS = 0x10;

/** Creates a new Arduino controller. */
public ArduinoSubsystem(SerialPort.Port port) {
this.port = port;
public ArduinoSubsystem() {
arduino = new I2C(PORT, ARDUINO_ADDRESS);
this.state = PATTERN.PURPLE;
timer = new Timer();
colorChange = new Timer();
Expand All @@ -30,17 +32,6 @@ public ArduinoSubsystem(SerialPort.Port port) {
colorChange.start();
}

private void connect() {
System.out.println("ArduinoSubsystem: connecting on port " + this.port);
try {
this.handler = new SerialPort(9600, this.port);
} catch (Exception e) {
e.printStackTrace();
return;
}
this.initialized = true;
}

/** Call this function constantly sends the desired state to the arduino. */
@Override
public void periodic() {
Expand All @@ -57,39 +48,22 @@ public void periodic() {

// Send the current state on a timer.
if (timer.get() > 1) {

if (!this.initialized) {
connect();
return;
}

System.out.println("Sending lighting state: " + state + " to " + handler);
int n = handler.write(enumToByte(state), 1);
if (n == 0) {
System.out.println("ARDUINO: closing connection to arduino.");
this.handler.close();
this.initialized = false;
return;
}

if (handler.getBytesReceived() > 0) {
System.out.print(handler.readString());
}
arduino.write(WRITE_ADDRESS, enumToByte(state));
timer.reset();
}
}

/** These byte values should match with values in arduino/onrobotlights.ino */
private byte[] enumToByte(PATTERN p) {
private byte enumToByte(PATTERN p) {
if (p == PATTERN.OFF) {
return new byte[] {0x40};
return 0x40;
} else if (p == PATTERN.YELLOW) {
return new byte[] {0x41};
return 0x41;
} else if (p == PATTERN.PURPLE) {
return new byte[] {0x42};
return 0x42;
}
// If unknown state, return a 0 byte.
return new byte[] {0x00};
return 0x00;
}

public void ledOff() {
Expand Down