From 936d3b9f838dfbe0db5332e5bd2038eeac2dbe0b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 24 Apr 2021 20:22:39 -0700 Subject: [PATCH] [templates] Add Java template for educational robot (#3309) Educational robot is a very minimal template designed for educational use (rather than competition). --- .../educational/EducationalRobot.java | 77 +++++++++++++++++++ .../wpilibj/templates/educational/Main.java | 25 ++++++ .../wpilibj/templates/educational/Robot.java | 23 ++++++ .../first/wpilibj/templates/templates.json | 11 +++ 4 files changed, 136 insertions(+) create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/EducationalRobot.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/Main.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/Robot.java diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/EducationalRobot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/EducationalRobot.java new file mode 100644 index 0000000000..931a394b39 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/EducationalRobot.java @@ -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.educational; + +import edu.wpi.first.hal.HAL; +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; + + @SuppressWarnings("PMD.CyclomaticComplexity") + @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()) { + m_ds.InDisabled(true); + disabled(); + m_ds.InDisabled(false); + while (isDisabled()) { + m_ds.waitForData(); + } + } else if (isAutonomous()) { + m_ds.InAutonomous(true); + autonomous(); + m_ds.InAutonomous(false); + while (isAutonomousEnabled()) { + m_ds.waitForData(); + } + } else if (isTest()) { + m_ds.InTest(true); + test(); + m_ds.InTest(false); + while (isTest() && isEnabled()) { + m_ds.waitForData(); + } + } else { + m_ds.InOperatorControl(true); + teleop(); + m_ds.InOperatorControl(false); + while (isOperatorControlEnabled()) { + m_ds.waitForData(); + } + } + } + } + + @Override + public void endCompetition() { + m_exit = true; + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/Main.java new file mode 100644 index 0000000000..80b9bc9d02 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/Main.java @@ -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.educational; + +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. + * + *

If you change your main robot class, change the parameter type. + */ + public static void main(String... args) { + RobotBase.startRobot(Robot::new); + } +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/Robot.java new file mode 100644 index 0000000000..0643977fed --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/educational/Robot.java @@ -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.educational; + +/** + * 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() {} +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/templates.json b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/templates.json index 2e0689940f..a1a555106c 100644 --- a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/templates.json +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/templates.json @@ -75,5 +75,16 @@ "gradlebase": "javaromi", "mainclass": "Main", "commandversion": 2 + }, + { + "name": "Educational Robot", + "description": "Educational Robot - Not for competition use", + "tags": [ + "Educational" + ], + "foldername": "educational", + "gradlebase": "java", + "mainclass": "Main", + "commandversion": 2 } ]