mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[examples] DigitalCommunication, I2CCommunication: Add tests (#4865)
This commit is contained in:
@@ -2,45 +2,28 @@
|
||||
// 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 "Robot.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);
|
||||
void Robot::RobotPeriodic() {
|
||||
// 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 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 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};
|
||||
};
|
||||
// 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);
|
||||
}
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() {
|
||||
return frc::StartRobot<Robot>();
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <frc/DigitalOutput.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:
|
||||
// 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;
|
||||
|
||||
void RobotPeriodic() override;
|
||||
|
||||
private:
|
||||
frc::DigitalOutput m_allianceOutput{kAlliancePort};
|
||||
frc::DigitalOutput m_enabledOutput{kEnabledPort};
|
||||
frc::DigitalOutput m_autonomousOutput{kAutonomousPort};
|
||||
frc::DigitalOutput m_alertOutput{kAlertPort};
|
||||
};
|
||||
@@ -2,45 +2,34 @@
|
||||
// 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 "Robot.h"
|
||||
|
||||
#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()));
|
||||
void Robot::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.
|
||||
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};
|
||||
};
|
||||
arduino.WriteBulk(reinterpret_cast<uint8_t*>(string.data()), string.size());
|
||||
}
|
||||
|
||||
#ifndef RUNNING_FRC_TESTS
|
||||
int main() {
|
||||
|
||||
@@ -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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
|
||||
#include <frc/I2C.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 I2C port.
|
||||
*/
|
||||
class Robot : public frc::TimedRobot {
|
||||
public:
|
||||
void RobotPeriodic() override;
|
||||
|
||||
static constexpr frc::I2C::Port kPort = frc::I2C::Port::kOnboard;
|
||||
|
||||
private:
|
||||
static constexpr int deviceAddress = 4;
|
||||
frc::I2C arduino{kPort, deviceAddress};
|
||||
};
|
||||
Reference in New Issue
Block a user