-
Notifications
You must be signed in to change notification settings - Fork 611
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Add communication examples (e.g. arduino) (#2500)
Co-authored-by: Andrew Dassonville <[email protected]>
- Loading branch information
1 parent
5ccfc4a
commit 269cf03
Showing
8 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
wpilibcExamples/src/main/cpp/examples/DigitalCommunication/cpp/Robot.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#include <frc/DigitalOutput.h> | ||
#include <frc/DriverStation.h> | ||
#include <frc/TimedRobot.h> | ||
|
||
/** | ||
* This is a sample program demonstrating how to communicate to a light | ||
* controller from the robot code using the roboRIO's DIO ports. | ||
*/ | ||
class Robot : public frc::TimedRobot { | ||
public: | ||
void RobotPeriodic() override { | ||
// pull alliance port high if on red alliance, pull low if on blue alliance | ||
m_allianceOutput.Set(frc::DriverStation::GetAlliance() == | ||
frc::DriverStation::kRed); | ||
|
||
// pull enabled port high if enabled, low if disabled | ||
m_enabledOutput.Set(frc::DriverStation::IsEnabled()); | ||
|
||
// pull auto port high if in autonomous, low if in teleop (or disabled) | ||
m_autonomousOutput.Set(frc::DriverStation::IsAutonomous()); | ||
|
||
// pull alert port high if match time remaining is between 30 and 25 seconds | ||
auto matchTime = frc::DriverStation::GetMatchTime(); | ||
m_alertOutput.Set(matchTime <= 30 && matchTime >= 25); | ||
} | ||
|
||
private: | ||
// define ports for communication with light controller | ||
static constexpr int kAlliancePort = 0; | ||
static constexpr int kEnabledPort = 1; | ||
static constexpr int kAutonomousPort = 2; | ||
static constexpr int kAlertPort = 3; | ||
|
||
frc::DigitalOutput m_allianceOutput{kAlliancePort}; | ||
frc::DigitalOutput m_enabledOutput{kEnabledPort}; | ||
frc::DigitalOutput m_autonomousOutput{kAutonomousPort}; | ||
frc::DigitalOutput m_alertOutput{kAlertPort}; | ||
}; | ||
|
||
int main() { | ||
return frc::StartRobot<Robot>(); | ||
} |
49 changes: 49 additions & 0 deletions
49
wpilibcExamples/src/main/cpp/examples/I2CCommunication/cpp/Robot.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <frc/DriverStation.h> | ||
#include <frc/I2C.h> | ||
#include <frc/TimedRobot.h> | ||
#include <frc/Timer.h> | ||
|
||
/** | ||
* This is a sample program demonstrating how to communicate to a light | ||
* controller from the robot code using the roboRIO's I2C port. | ||
*/ | ||
class Robot : public frc::TimedRobot { | ||
public: | ||
void RobotPeriodic() override { | ||
// Creates a string to hold current robot state information, including | ||
// alliance, enabled state, operation mode, and match time. The message | ||
// is sent in format "AEM###" where A is the alliance color, (R)ed or | ||
// (B)lue, E is the enabled state, (E)nabled or (D)isabled, M is the | ||
// operation mode, (A)utonomous or (T)eleop, and ### is the zero-padded | ||
// time remaining in the match. | ||
// | ||
// For example, "RET043" would indicate that the robot is on the red | ||
// alliance, enabled in teleop mode, with 43 seconds left in the match. | ||
auto string = fmt::format( | ||
"{}{}{}{:03}", | ||
frc::DriverStation::GetAlliance() == frc::DriverStation::Alliance::kRed | ||
? "R" | ||
: "B", | ||
frc::DriverStation::IsEnabled() ? "E" : "D", | ||
frc::DriverStation::IsAutonomous() ? "A" : "T", | ||
static_cast<int>(frc::Timer::GetMatchTime().value())); | ||
|
||
arduino.WriteBulk(reinterpret_cast<uint8_t*>(string.data()), string.size()); | ||
} | ||
|
||
private: | ||
static constexpr int deviceAddress = 4; | ||
frc::I2C arduino{frc::I2C::Port::kOnboard, deviceAddress}; | ||
}; | ||
|
||
#ifndef RUNNING_FRC_TESTS | ||
int main() { | ||
return frc::StartRobot<Robot>(); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/digitalcommunication/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.wpilibj.examples.digitalcommunication; | ||
|
||
import edu.wpi.first.wpilibj.RobotBase; | ||
|
||
/** | ||
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what | ||
* you are doing, do not modify this file except to change the parameter class to the startRobot | ||
* call. | ||
*/ | ||
public final class Main { | ||
private Main() {} | ||
|
||
/** | ||
* Main initialization function. Do not perform any initialization here. | ||
* | ||
* <p>If you change your main robot class, change the parameter type. | ||
*/ | ||
public static void main(String... args) { | ||
RobotBase.startRobot(Robot::new); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/digitalcommunication/Robot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.wpilibj.examples.digitalcommunication; | ||
|
||
import edu.wpi.first.wpilibj.DigitalOutput; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
import edu.wpi.first.wpilibj.TimedRobot; | ||
|
||
/** | ||
* This is a sample program demonstrating how to communicate to a light controller from the robot | ||
* code using the roboRIO's DIO ports. | ||
*/ | ||
public class Robot extends TimedRobot { | ||
// define ports for digitalcommunication with light controller | ||
private static final int kAlliancePort = 0; | ||
private static final int kEnabledPort = 1; | ||
private static final int kAutonomousPort = 2; | ||
private static final int kAlertPort = 3; | ||
|
||
private final DigitalOutput m_allianceOutput = new DigitalOutput(kAlliancePort); | ||
private final DigitalOutput m_enabledOutput = new DigitalOutput(kEnabledPort); | ||
private final DigitalOutput m_autonomousOutput = new DigitalOutput(kAutonomousPort); | ||
private final DigitalOutput m_alertOutput = new DigitalOutput(kAlertPort); | ||
|
||
@Override | ||
public void robotPeriodic() { | ||
// pull alliance port high if on red alliance, pull low if on blue alliance | ||
m_allianceOutput.set(DriverStation.getAlliance() == DriverStation.Alliance.Red); | ||
|
||
// pull enabled port high if enabled, low if disabled | ||
m_enabledOutput.set(DriverStation.isEnabled()); | ||
|
||
// pull auto port high if in autonomous, low if in teleop (or disabled) | ||
m_autonomousOutput.set(DriverStation.isAutonomous()); | ||
|
||
// pull alert port high if match time remaining is between 30 and 25 seconds | ||
var matchTime = DriverStation.getMatchTime(); | ||
m_alertOutput.set(matchTime <= 30 && matchTime >= 25); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/i2ccommunication/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.wpilibj.examples.i2ccommunication; | ||
|
||
import edu.wpi.first.wpilibj.RobotBase; | ||
|
||
/** | ||
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what | ||
* you are doing, do not modify this file except to change the parameter class to the startRobot | ||
* call. | ||
*/ | ||
public final class Main { | ||
private Main() {} | ||
|
||
/** | ||
* Main initialization function. Do not perform any initialization here. | ||
* | ||
* <p>If you change your main robot class, change the parameter type. | ||
*/ | ||
public static void main(String... args) { | ||
RobotBase.startRobot(Robot::new); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/i2ccommunication/Robot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.wpilibj.examples.i2ccommunication; | ||
|
||
import edu.wpi.first.wpilibj.DriverStation; | ||
import edu.wpi.first.wpilibj.I2C; | ||
import edu.wpi.first.wpilibj.TimedRobot; | ||
|
||
/** | ||
* This is a sample program demonstrating how to communicate to a light controller from the robot | ||
* code using the roboRIO's I2C port. | ||
*/ | ||
public class Robot extends TimedRobot { | ||
private static int kDeviceAddress = 4; | ||
|
||
private static I2C m_arduino = new I2C(I2C.Port.kOnboard, kDeviceAddress); | ||
|
||
private void writeString(String input) { | ||
// Creates a char array from the input string | ||
char[] chars = input.toCharArray(); | ||
|
||
// Creates a byte array from the char array | ||
byte[] data = new byte[chars.length]; | ||
|
||
// Adds each character | ||
for (int i = 0; i < chars.length; i++) { | ||
data[i] = (byte) chars[i]; | ||
} | ||
|
||
// Writes bytes over I2C | ||
m_arduino.transaction(data, data.length, null, 0); | ||
} | ||
|
||
@Override | ||
public void robotPeriodic() { | ||
// Creates a string to hold current robot state information, including | ||
// alliance, enabled state, operation mode, and match time. The message | ||
// is sent in format "AEM###" where A is the alliance color, (R)ed or | ||
// (B)lue, E is the enabled state, (E)nabled or (D)isabled, M is the | ||
// operation mode, (A)utonomous or (T)eleop, and ### is the zero-padded | ||
// time remaining in the match. | ||
// | ||
// For example, "RET043" would indicate that the robot is on the red | ||
// alliance, enabled in teleop mode, with 43 seconds left in the match. | ||
StringBuilder stateMessage = new StringBuilder(6); | ||
|
||
stateMessage | ||
.append(DriverStation.getAlliance() == DriverStation.Alliance.Red ? "R" : "B") | ||
.append(DriverStation.isEnabled() ? "E" : "D") | ||
.append(DriverStation.isAutonomous() ? "A" : "T") | ||
.append(String.format("%03d", (int) DriverStation.getMatchTime())); | ||
|
||
writeString(stateMessage.toString()); | ||
} | ||
} |