Skip to content

Commit 269cf03

Browse files
[examples] Add communication examples (e.g. arduino) (#2500)
Co-authored-by: Andrew Dassonville <[email protected]>
1 parent 5ccfc4a commit 269cf03

File tree

8 files changed

+287
-0
lines changed

8 files changed

+287
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#include <frc/DigitalOutput.h>
6+
#include <frc/DriverStation.h>
7+
#include <frc/TimedRobot.h>
8+
9+
/**
10+
* This is a sample program demonstrating how to communicate to a light
11+
* controller from the robot code using the roboRIO's DIO ports.
12+
*/
13+
class Robot : public frc::TimedRobot {
14+
public:
15+
void RobotPeriodic() override {
16+
// pull alliance port high if on red alliance, pull low if on blue alliance
17+
m_allianceOutput.Set(frc::DriverStation::GetAlliance() ==
18+
frc::DriverStation::kRed);
19+
20+
// pull enabled port high if enabled, low if disabled
21+
m_enabledOutput.Set(frc::DriverStation::IsEnabled());
22+
23+
// pull auto port high if in autonomous, low if in teleop (or disabled)
24+
m_autonomousOutput.Set(frc::DriverStation::IsAutonomous());
25+
26+
// pull alert port high if match time remaining is between 30 and 25 seconds
27+
auto matchTime = frc::DriverStation::GetMatchTime();
28+
m_alertOutput.Set(matchTime <= 30 && matchTime >= 25);
29+
}
30+
31+
private:
32+
// define ports for communication with light controller
33+
static constexpr int kAlliancePort = 0;
34+
static constexpr int kEnabledPort = 1;
35+
static constexpr int kAutonomousPort = 2;
36+
static constexpr int kAlertPort = 3;
37+
38+
frc::DigitalOutput m_allianceOutput{kAlliancePort};
39+
frc::DigitalOutput m_enabledOutput{kEnabledPort};
40+
frc::DigitalOutput m_autonomousOutput{kAutonomousPort};
41+
frc::DigitalOutput m_alertOutput{kAlertPort};
42+
};
43+
44+
int main() {
45+
return frc::StartRobot<Robot>();
46+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#include <fmt/format.h>
6+
7+
#include <frc/DriverStation.h>
8+
#include <frc/I2C.h>
9+
#include <frc/TimedRobot.h>
10+
#include <frc/Timer.h>
11+
12+
/**
13+
* This is a sample program demonstrating how to communicate to a light
14+
* controller from the robot code using the roboRIO's I2C port.
15+
*/
16+
class Robot : public frc::TimedRobot {
17+
public:
18+
void RobotPeriodic() override {
19+
// Creates a string to hold current robot state information, including
20+
// alliance, enabled state, operation mode, and match time. The message
21+
// is sent in format "AEM###" where A is the alliance color, (R)ed or
22+
// (B)lue, E is the enabled state, (E)nabled or (D)isabled, M is the
23+
// operation mode, (A)utonomous or (T)eleop, and ### is the zero-padded
24+
// time remaining in the match.
25+
//
26+
// For example, "RET043" would indicate that the robot is on the red
27+
// alliance, enabled in teleop mode, with 43 seconds left in the match.
28+
auto string = fmt::format(
29+
"{}{}{}{:03}",
30+
frc::DriverStation::GetAlliance() == frc::DriverStation::Alliance::kRed
31+
? "R"
32+
: "B",
33+
frc::DriverStation::IsEnabled() ? "E" : "D",
34+
frc::DriverStation::IsAutonomous() ? "A" : "T",
35+
static_cast<int>(frc::Timer::GetMatchTime().value()));
36+
37+
arduino.WriteBulk(reinterpret_cast<uint8_t*>(string.data()), string.size());
38+
}
39+
40+
private:
41+
static constexpr int deviceAddress = 4;
42+
frc::I2C arduino{frc::I2C::Port::kOnboard, deviceAddress};
43+
};
44+
45+
#ifndef RUNNING_FRC_TESTS
46+
int main() {
47+
return frc::StartRobot<Robot>();
48+
}
49+
#endif

wpilibcExamples/src/main/cpp/examples/examples.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,27 @@
260260
"gradlebase": "cpp",
261261
"commandversion": 2
262262
},
263+
{
264+
"name": "I2C Communication",
265+
"description": "An example program that communicates with external devices (such as an Arduino) using the roboRIO's I2C port",
266+
"tags": [
267+
"I2C"
268+
],
269+
"foldername": "I2CCommunication",
270+
"gradlebase": "cpp",
271+
"commandversion": 2
272+
},
273+
{
274+
"name": "Digital Communication Sample",
275+
"description": "An example program that communicates with external devices (such as an Arduino) using the roboRIO's DIO",
276+
"tags": [
277+
"Digital"
278+
],
279+
"foldername": "DigitalCommunication",
280+
"gradlebase": "cpp",
281+
"commandversion": 2
282+
},
283+
263284
{
264285
"name": "Axis Camera Sample",
265286
"description": "An example program that acquires images from an Axis network camera and adds some annotation to the image as you might do for showing operators the result of some image recognition, and sends it to the dashboard for display. This demonstrates the use of the AxisCamera class.",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.wpilibj.examples.digitalcommunication;
6+
7+
import edu.wpi.first.wpilibj.RobotBase;
8+
9+
/**
10+
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what
11+
* you are doing, do not modify this file except to change the parameter class to the startRobot
12+
* call.
13+
*/
14+
public final class Main {
15+
private Main() {}
16+
17+
/**
18+
* Main initialization function. Do not perform any initialization here.
19+
*
20+
* <p>If you change your main robot class, change the parameter type.
21+
*/
22+
public static void main(String... args) {
23+
RobotBase.startRobot(Robot::new);
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.wpilibj.examples.digitalcommunication;
6+
7+
import edu.wpi.first.wpilibj.DigitalOutput;
8+
import edu.wpi.first.wpilibj.DriverStation;
9+
import edu.wpi.first.wpilibj.TimedRobot;
10+
11+
/**
12+
* This is a sample program demonstrating how to communicate to a light controller from the robot
13+
* code using the roboRIO's DIO ports.
14+
*/
15+
public class Robot extends TimedRobot {
16+
// define ports for digitalcommunication with light controller
17+
private static final int kAlliancePort = 0;
18+
private static final int kEnabledPort = 1;
19+
private static final int kAutonomousPort = 2;
20+
private static final int kAlertPort = 3;
21+
22+
private final DigitalOutput m_allianceOutput = new DigitalOutput(kAlliancePort);
23+
private final DigitalOutput m_enabledOutput = new DigitalOutput(kEnabledPort);
24+
private final DigitalOutput m_autonomousOutput = new DigitalOutput(kAutonomousPort);
25+
private final DigitalOutput m_alertOutput = new DigitalOutput(kAlertPort);
26+
27+
@Override
28+
public void robotPeriodic() {
29+
// pull alliance port high if on red alliance, pull low if on blue alliance
30+
m_allianceOutput.set(DriverStation.getAlliance() == DriverStation.Alliance.Red);
31+
32+
// pull enabled port high if enabled, low if disabled
33+
m_enabledOutput.set(DriverStation.isEnabled());
34+
35+
// pull auto port high if in autonomous, low if in teleop (or disabled)
36+
m_autonomousOutput.set(DriverStation.isAutonomous());
37+
38+
// pull alert port high if match time remaining is between 30 and 25 seconds
39+
var matchTime = DriverStation.getMatchTime();
40+
m_alertOutput.set(matchTime <= 30 && matchTime >= 25);
41+
}
42+
}

wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/examples.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,5 +770,27 @@
770770
"gradlebase": "javaromi",
771771
"mainclass": "Main",
772772
"commandversion": 2
773+
},
774+
{
775+
"name": "Digital Communication Sample",
776+
"description": "An example program that communicates with external devices (such as an Arduino) using the roboRIO's DIO",
777+
"tags": [
778+
"Digital"
779+
],
780+
"foldername": "digitalcommunication",
781+
"gradlebase": "java",
782+
"commandversion": 2,
783+
"mainclass": "Main"
784+
},
785+
{
786+
"name": "I2C Communication Sample",
787+
"description": "An example program that communicates with external devices (such as an Arduino) using the roboRIO's I2C port",
788+
"tags": [
789+
"I2C"
790+
],
791+
"foldername": "i2ccommunication",
792+
"gradlebase": "java",
793+
"commandversion": 2,
794+
"mainclass": "Main"
773795
}
774796
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.wpilibj.examples.i2ccommunication;
6+
7+
import edu.wpi.first.wpilibj.RobotBase;
8+
9+
/**
10+
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what
11+
* you are doing, do not modify this file except to change the parameter class to the startRobot
12+
* call.
13+
*/
14+
public final class Main {
15+
private Main() {}
16+
17+
/**
18+
* Main initialization function. Do not perform any initialization here.
19+
*
20+
* <p>If you change your main robot class, change the parameter type.
21+
*/
22+
public static void main(String... args) {
23+
RobotBase.startRobot(Robot::new);
24+
}
25+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.wpilibj.examples.i2ccommunication;
6+
7+
import edu.wpi.first.wpilibj.DriverStation;
8+
import edu.wpi.first.wpilibj.I2C;
9+
import edu.wpi.first.wpilibj.TimedRobot;
10+
11+
/**
12+
* This is a sample program demonstrating how to communicate to a light controller from the robot
13+
* code using the roboRIO's I2C port.
14+
*/
15+
public class Robot extends TimedRobot {
16+
private static int kDeviceAddress = 4;
17+
18+
private static I2C m_arduino = new I2C(I2C.Port.kOnboard, kDeviceAddress);
19+
20+
private void writeString(String input) {
21+
// Creates a char array from the input string
22+
char[] chars = input.toCharArray();
23+
24+
// Creates a byte array from the char array
25+
byte[] data = new byte[chars.length];
26+
27+
// Adds each character
28+
for (int i = 0; i < chars.length; i++) {
29+
data[i] = (byte) chars[i];
30+
}
31+
32+
// Writes bytes over I2C
33+
m_arduino.transaction(data, data.length, null, 0);
34+
}
35+
36+
@Override
37+
public void robotPeriodic() {
38+
// Creates a string to hold current robot state information, including
39+
// alliance, enabled state, operation mode, and match time. The message
40+
// is sent in format "AEM###" where A is the alliance color, (R)ed or
41+
// (B)lue, E is the enabled state, (E)nabled or (D)isabled, M is the
42+
// operation mode, (A)utonomous or (T)eleop, and ### is the zero-padded
43+
// time remaining in the match.
44+
//
45+
// For example, "RET043" would indicate that the robot is on the red
46+
// alliance, enabled in teleop mode, with 43 seconds left in the match.
47+
StringBuilder stateMessage = new StringBuilder(6);
48+
49+
stateMessage
50+
.append(DriverStation.getAlliance() == DriverStation.Alliance.Red ? "R" : "B")
51+
.append(DriverStation.isEnabled() ? "E" : "D")
52+
.append(DriverStation.isAutonomous() ? "A" : "T")
53+
.append(String.format("%03d", (int) DriverStation.getMatchTime()));
54+
55+
writeString(stateMessage.toString());
56+
}
57+
}

0 commit comments

Comments
 (0)