[templates] Add Java Romi Educational template (#3837)

This is a combination of a Romi Gradle project and Educational robot (added in #3309)
This commit is contained in:
Austin Shalit
2021-12-26 15:46:22 -08:00
committed by GitHub
parent c0da9d2d35
commit 84b15f0883
5 changed files with 197 additions and 4 deletions

View File

@@ -0,0 +1,77 @@
// 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.templates.romieducational;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.RobotBase;
/** Educational robot base class. */
public class EducationalRobot extends RobotBase {
public void robotInit() {}
public void disabled() {}
public void run() {}
public void autonomous() {
run();
}
public void teleop() {
run();
}
public void test() {
run();
}
private volatile boolean m_exit;
@Override
public void startCompetition() {
robotInit();
// Tell the DS that the robot is ready to be enabled
HAL.observeUserProgramStarting();
while (!Thread.currentThread().isInterrupted() && !m_exit) {
if (isDisabled()) {
DriverStation.inDisabled(true);
disabled();
DriverStation.inDisabled(false);
while (isDisabled()) {
DriverStation.waitForData();
}
} else if (isAutonomous()) {
DriverStation.inAutonomous(true);
autonomous();
DriverStation.inAutonomous(false);
while (isAutonomousEnabled()) {
DriverStation.waitForData();
}
} else if (isTest()) {
DriverStation.inTest(true);
test();
DriverStation.inTest(false);
while (isTest() && isEnabled()) {
DriverStation.waitForData();
}
} else {
DriverStation.inTeleop(true);
teleop();
DriverStation.inTeleop(false);
while (isTeleopEnabled()) {
DriverStation.waitForData();
}
}
}
}
@Override
public void endCompetition() {
m_exit = true;
}
}

View File

@@ -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.templates.romieducational;
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);
}
}

View File

@@ -0,0 +1,23 @@
// 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.templates.romieducational;
/**
* The VM is configured to automatically run this class, and to call the run() function when the
* robot is enabled. If you change the name of this class or the package after creating this
* project, you must also update the build.gradle file in the project.
*/
public class Robot extends EducationalRobot {
/**
* This function is run when the robot is first started up and should be used for any
* initialization code.
*/
@Override
public void robotInit() {}
/** This function is run when the robot is enabled. */
@Override
public void run() {}
}

View File

@@ -0,0 +1,52 @@
// 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.templates.romieducational;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.motorcontrol.Spark;
public class RomiDrivetrain {
private static final double kCountsPerRevolution = 1440.0;
private static final double kWheelDiameterInch = 2.75591; // 70 mm
// The Romi has the left and right motors set to
// PWM channels 0 and 1 respectively
private final Spark m_leftMotor = new Spark(0);
private final Spark m_rightMotor = new Spark(1);
// The Romi has onboard encoders that are hardcoded
// to use DIO pins 4/5 and 6/7 for the left and right
private final Encoder m_leftEncoder = new Encoder(4, 5);
private final Encoder m_rightEncoder = new Encoder(6, 7);
// Set up the differential drive controller
private final DifferentialDrive m_diffDrive = new DifferentialDrive(m_leftMotor, m_rightMotor);
/** Creates a new RomiDrivetrain. */
public RomiDrivetrain() {
// Use inches as unit for encoder distances
m_leftEncoder.setDistancePerPulse((Math.PI * kWheelDiameterInch) / kCountsPerRevolution);
m_rightEncoder.setDistancePerPulse((Math.PI * kWheelDiameterInch) / kCountsPerRevolution);
resetEncoders();
}
public void arcadeDrive(double xaxisSpeed, double zaxisRotate) {
m_diffDrive.arcadeDrive(xaxisSpeed, zaxisRotate);
}
public void resetEncoders() {
m_leftEncoder.reset();
m_rightEncoder.reset();
}
public double getLeftDistanceInch() {
return m_leftEncoder.getDistance();
}
public double getRightDistanceInch() {
return m_rightEncoder.getDistance();
}
}

View File

@@ -14,7 +14,8 @@
"name": "Timed Skeleton (Advanced)",
"description": "Skeleton (stub) code for TimedRobot",
"tags": [
"Timed", "Skeleton"
"Timed",
"Skeleton"
],
"foldername": "timedskeleton",
"gradlebase": "java",
@@ -25,7 +26,8 @@
"name": "RobotBase Skeleton (Advanced)",
"description": "Skeleton (stub) code for RobotBase",
"tags": [
"RobotBase", "Skeleton"
"RobotBase",
"Skeleton"
],
"foldername": "robotbaseskeleton",
"gradlebase": "java",
@@ -47,7 +49,8 @@
"name": "Romi - Timed Robot",
"description": "Romi - Timed style",
"tags": [
"Timed", "Romi"
"Timed",
"Romi"
],
"foldername": "romitimed",
"gradlebase": "javaromi",
@@ -58,7 +61,8 @@
"name": "Romi - Command Robot",
"description": "Romi - Command style",
"tags": [
"Command", "Romi"
"Command",
"Romi"
],
"foldername": "romicommandbased",
"gradlebase": "javaromi",
@@ -75,5 +79,17 @@
"gradlebase": "java",
"mainclass": "Main",
"commandversion": 2
},
{
"name": "Romi - Educational Robot",
"description": "Romi - Educational Robot",
"tags": [
"Educational",
"Romi"
],
"foldername": "romieducational",
"gradlebase": "javaromi",
"mainclass": "Main",
"commandversion": 2
}
]