[examples] Adding XRP java and cpp examples for Timed Robot (#8599)

Adding an example (one C++ and one Java) for using TimedRobot with the
XRP.
This commit is contained in:
jpokornyiii
2026-02-07 00:36:35 -05:00
committed by GitHub
parent 6b225bb1f1
commit 0a37317467
6 changed files with 218 additions and 0 deletions

View File

@@ -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.
#include "Robot.h"
Robot::Robot() {
wpi::SendableRegistry::AddChild(&m_drive, &m_leftMotor);
wpi::SendableRegistry::AddChild(&m_drive, &m_rightMotor);
// We need to invert one side of the drivetrain so that positive voltages
// result in both sides moving forward. Depending on how your robot's
// gearbox is constructed, you might have to invert the left side instead.
m_rightMotor.SetInverted(true);
}
/**
* This function is called every 20 ms, no matter the mode. Use
* this for items like diagnostics that you want ran during disabled,
* autonomous, teleoperated and test.
*
* <p> This runs after the mode specific periodic functions, but before
* LiveWindow and SmartDashboard integrated updating.
*/
void Robot::RobotPeriodic() {}
// This function is only once each time Autonomous is enabled
void Robot::AutonomousInit() {
m_timer.Restart();
}
// This function is called periodically during autonomous mode
void Robot::AutonomousPeriodic() {
// Drive for 2 seconds
if (m_timer.Get() < 2_s) {
// Drive forwards half speed, make sure to turn input squaring off
m_drive.ArcadeDrive(0.5, 0.0, false);
} else {
// Stop robot
m_drive.ArcadeDrive(0.0, 0.0, false);
}
}
// This function is only once each time telop is enabled
void Robot::TeleopInit() {}
// This function is called periodically during teleop mode
void Robot::TeleopPeriodic() {
m_drive.ArcadeDrive(-m_XboxController.GetLeftY(),
-m_XboxController.GetRightX());
}
#ifndef RUNNING_FRC_TESTS
int main() {
return frc::StartRobot<Robot>();
}
#endif

View File

@@ -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 <frc/TimedRobot.h>
#include <frc/Timer.h>
#include <frc/XboxController.h>
#include <frc/drive/DifferentialDrive.h>
#include <frc/xrp/XRPMotor.h>
class Robot : public frc::TimedRobot {
public:
Robot();
void RobotPeriodic() override;
void AutonomousInit() override;
void AutonomousPeriodic() override;
void TeleopInit() override;
void TeleopPeriodic() override;
private:
frc::XRPMotor m_leftMotor{0};
frc::XRPMotor m_rightMotor{1};
frc::XboxController m_XboxController{0};
frc::Timer m_timer;
frc::DifferentialDrive m_drive{
[&](double output) { m_leftMotor.Set(output); },
[&](double output) { m_rightMotor.Set(output); }};
};

View File

@@ -609,6 +609,22 @@
"xrp"
]
},
{
"name": "XRP Timed",
"description": "An very basic timed robot program that can be used with the XRP reference robot design.",
"tags": [
"XRP",
"Differential Drive",
"XboxController"
],
"foldername": "Xrptimed",
"gradlebase": "cppxrp",
"mainclass": "Main",
"commandversion": 2,
"extravendordeps": [
"xrp"
]
},
{
"name": "StateSpaceFlywheel",
"description": "Control a flywheel using a state-space model (based on values from CAD), with a Kalman Filter and LQR.",