From 19a8850fb17dd563f2986a6bb7c8421999502ed4 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Fri, 15 Sep 2023 20:42:21 -0700 Subject: [PATCH] [examples] Add TimesliceRobot templates (#3683) --- styleguide/spotbugs-exclude.xml | 8 ++ .../src/main/cpp/templates/templates.json | 21 ++++ .../cpp/templates/timeslice/cpp/Robot.cpp | 92 ++++++++++++++ .../cpp/templates/timeslice/include/Robot.h | 32 +++++ .../templates/timesliceskeleton/cpp/Robot.cpp | 45 +++++++ .../timesliceskeleton/include/Robot.h | 27 ++++ .../first/wpilibj/templates/templates.json | 23 ++++ .../wpilibj/templates/timeslice/Main.java | 25 ++++ .../wpilibj/templates/timeslice/Robot.java | 118 ++++++++++++++++++ .../templates/timesliceskeleton/Main.java | 25 ++++ .../templates/timesliceskeleton/Robot.java | 70 +++++++++++ 11 files changed, 486 insertions(+) create mode 100644 wpilibcExamples/src/main/cpp/templates/timeslice/cpp/Robot.cpp create mode 100644 wpilibcExamples/src/main/cpp/templates/timeslice/include/Robot.h create mode 100644 wpilibcExamples/src/main/cpp/templates/timesliceskeleton/cpp/Robot.cpp create mode 100644 wpilibcExamples/src/main/cpp/templates/timesliceskeleton/include/Robot.h create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timeslice/Main.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timeslice/Robot.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timesliceskeleton/Main.java create mode 100644 wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timesliceskeleton/Robot.java diff --git a/styleguide/spotbugs-exclude.xml b/styleguide/spotbugs-exclude.xml index 0e8e41ffc69..f224099af60 100644 --- a/styleguide/spotbugs-exclude.xml +++ b/styleguide/spotbugs-exclude.xml @@ -116,6 +116,14 @@ + + + + + + + + diff --git a/wpilibcExamples/src/main/cpp/templates/templates.json b/wpilibcExamples/src/main/cpp/templates/templates.json index a90275b4e45..13e197b2718 100644 --- a/wpilibcExamples/src/main/cpp/templates/templates.json +++ b/wpilibcExamples/src/main/cpp/templates/templates.json @@ -41,6 +41,27 @@ "gradlebase": "cpp", "commandversion": 2 }, + { + "name": "Timeslice Robot", + "description": "Timeslice style", + "tags": [ + "Timeslice" + ], + "foldername": "timeslice", + "gradlebase": "cpp", + "commandversion": 2 + }, + { + "name": "Timeslice Skeleton (Advanced)", + "description": "Skeleton (stub) code for TimesliceRobot", + "tags": [ + "Timeslice", + "Skeleton" + ], + "foldername": "timesliceskeleton", + "gradlebase": "cpp", + "commandversion": 2 + }, { "name": "RobotBase Skeleton (Advanced)", "description": "Skeleton (stub) code for RobotBase - Not recommended for competition use", diff --git a/wpilibcExamples/src/main/cpp/templates/timeslice/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/templates/timeslice/cpp/Robot.cpp new file mode 100644 index 00000000000..548457b5428 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/templates/timeslice/cpp/Robot.cpp @@ -0,0 +1,92 @@ +// 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" + +#include +#include +#include + +// Run robot periodic() functions for 5 ms, and run controllers every 10 ms +Robot::Robot() : frc::TimesliceRobot{5_ms, 10_ms} { + // LiveWindow causes drastic overruns in robot periodic functions that will + // interfere with controllers + frc::LiveWindow::DisableAllTelemetry(); + + // Runs for 2 ms after robot periodic functions + Schedule([=] {}, 2_ms); + + // Runs for 2 ms after first controller function + Schedule([=] {}, 2_ms); + + // Total usage: + // 5 ms (robot) + 2 ms (controller 1) + 2 ms (controller 2) = 9 ms + // 9 ms / 10 ms -> 90% allocated +} + +void Robot::RobotInit() { + m_chooser.SetDefaultOption(kAutoNameDefault, kAutoNameDefault); + m_chooser.AddOption(kAutoNameCustom, kAutoNameCustom); + frc::SmartDashboard::PutData("Auto Modes", &m_chooser); +} + +/** + * This function is called every robot packet, no matter the mode. Use + * this for items like diagnostics that you want ran during disabled, + * autonomous, teleoperated and test. + * + *

This runs after the mode specific periodic functions, but before + * LiveWindow and SmartDashboard integrated updating. + */ +void Robot::RobotPeriodic() {} + +/** + * This autonomous (along with the chooser code above) shows how to select + * between different autonomous modes using the dashboard. The sendable chooser + * code works with the Java SmartDashboard. If you prefer the LabVIEW Dashboard, + * remove all of the chooser code and uncomment the GetString line to get the + * auto name from the text box below the Gyro. + * + * You can add additional auto modes by adding additional comparisons to the + * if-else structure below with additional strings. If using the SendableChooser + * make sure to add them to the chooser code above as well. + */ +void Robot::AutonomousInit() { + m_autoSelected = m_chooser.GetSelected(); + // m_autoSelected = SmartDashboard::GetString("Auto Selector", + // kAutoNameDefault); + fmt::print("Auto selected: {}\n", m_autoSelected); + + if (m_autoSelected == kAutoNameCustom) { + // Custom Auto goes here + } else { + // Default Auto goes here + } +} + +void Robot::AutonomousPeriodic() { + if (m_autoSelected == kAutoNameCustom) { + // Custom Auto goes here + } else { + // Default Auto goes here + } +} + +void Robot::TeleopInit() {} + +void Robot::TeleopPeriodic() {} + +void Robot::DisabledInit() {} + +void Robot::DisabledPeriodic() {} + +void Robot::TestInit() {} + +void Robot::TestPeriodic() {} + +#ifndef RUNNING_FRC_TESTS +int main() { + return frc::StartRobot(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/templates/timeslice/include/Robot.h b/wpilibcExamples/src/main/cpp/templates/timeslice/include/Robot.h new file mode 100644 index 00000000000..aaafdf7df19 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/templates/timeslice/include/Robot.h @@ -0,0 +1,32 @@ +// 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 + +#include +#include + +class Robot : public frc::TimesliceRobot { + public: + Robot(); + + void RobotInit() override; + void RobotPeriodic() override; + void AutonomousInit() override; + void AutonomousPeriodic() override; + void TeleopInit() override; + void TeleopPeriodic() override; + void DisabledInit() override; + void DisabledPeriodic() override; + void TestInit() override; + void TestPeriodic() override; + + private: + frc::SendableChooser m_chooser; + const std::string kAutoNameDefault = "Default"; + const std::string kAutoNameCustom = "My Auto"; + std::string m_autoSelected; +}; diff --git a/wpilibcExamples/src/main/cpp/templates/timesliceskeleton/cpp/Robot.cpp b/wpilibcExamples/src/main/cpp/templates/timesliceskeleton/cpp/Robot.cpp new file mode 100644 index 00000000000..10f98dd5656 --- /dev/null +++ b/wpilibcExamples/src/main/cpp/templates/timesliceskeleton/cpp/Robot.cpp @@ -0,0 +1,45 @@ +// 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" + +#include + +// Run robot periodic() functions for 5 ms, and run controllers every 10 ms +Robot::Robot() : frc::TimesliceRobot{5_ms, 10_ms} { + // LiveWindow causes drastic overruns in robot periodic functions that will + // interfere with controllers + frc::LiveWindow::DisableAllTelemetry(); + + // Runs for 2 ms after robot periodic functions + Schedule([=] {}, 2_ms); + + // Runs for 2 ms after first controller function + Schedule([=] {}, 2_ms); + + // Total usage: + // 5 ms (robot) + 2 ms (controller 1) + 2 ms (controller 2) = 9 ms + // 9 ms / 10 ms -> 90% allocated +} + +void Robot::RobotInit() {} +void Robot::RobotPeriodic() {} + +void Robot::AutonomousInit() {} +void Robot::AutonomousPeriodic() {} + +void Robot::TeleopInit() {} +void Robot::TeleopPeriodic() {} + +void Robot::DisabledInit() {} +void Robot::DisabledPeriodic() {} + +void Robot::TestInit() {} +void Robot::TestPeriodic() {} + +#ifndef RUNNING_FRC_TESTS +int main() { + return frc::StartRobot(); +} +#endif diff --git a/wpilibcExamples/src/main/cpp/templates/timesliceskeleton/include/Robot.h b/wpilibcExamples/src/main/cpp/templates/timesliceskeleton/include/Robot.h new file mode 100644 index 00000000000..6659bf99c6c --- /dev/null +++ b/wpilibcExamples/src/main/cpp/templates/timesliceskeleton/include/Robot.h @@ -0,0 +1,27 @@ +// 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 + +class Robot : public frc::TimesliceRobot { + public: + Robot(); + + void RobotInit() override; + void RobotPeriodic() override; + + void AutonomousInit() override; + void AutonomousPeriodic() override; + + void TeleopInit() override; + void TeleopPeriodic() override; + + void DisabledInit() override; + void DisabledPeriodic() override; + + void TestInit() override; + void TestPeriodic() override; +}; 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 8f797d56909..cfc985ad6e0 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 @@ -45,6 +45,29 @@ "mainclass": "Main", "commandversion": 2 }, + { + "name": "Timesice Robot", + "description": "Timeslice style", + "tags": [ + "Timeslice" + ], + "foldername": "timeslice", + "gradlebase": "java", + "mainclass": "Main", + "commandversion": 2 + }, + { + "name": "Timeslice Skeleton (Advanced)", + "description": "Skeleton (stub) code for TimesliceRobot", + "tags": [ + "Timeslice", + "Skeleton" + ], + "foldername": "timesliceskeleton", + "gradlebase": "java", + "mainclass": "Main", + "commandversion": 2 + }, { "name": "RobotBase Skeleton (Advanced)", "description": "Skeleton (stub) code for RobotBase - Not recommended for competition use", diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timeslice/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timeslice/Main.java new file mode 100644 index 00000000000..313d1793099 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timeslice/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.timeslice; + +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/timeslice/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timeslice/Robot.java new file mode 100644 index 00000000000..661a06b75fb --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timeslice/Robot.java @@ -0,0 +1,118 @@ +// 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.timeslice; + +import edu.wpi.first.wpilibj.TimesliceRobot; +import edu.wpi.first.wpilibj.livewindow.LiveWindow; +import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; + +/** + * The VM is configured to automatically run this class, and to call the functions corresponding to + * each mode, as described in the TimesliceRobot documentation. 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 TimesliceRobot { + private static final String kDefaultAuto = "Default"; + private static final String kCustomAuto = "My Auto"; + private String m_autoSelected; + private final SendableChooser m_chooser = new SendableChooser<>(); + + /** Robot constructor. */ + public Robot() { + // Run robot periodic() functions for 5 ms, and run controllers every 10 ms + super(0.005, 0.01); + + // LiveWindow causes drastic overruns in robot periodic functions that will + // interfere with controllers + LiveWindow.disableAllTelemetry(); + + // Runs for 2 ms after robot periodic functions + schedule(() -> {}, 0.002); + + // Runs for 2 ms after first controller function + schedule(() -> {}, 0.002); + + // Total usage: 5 ms (robot) + 2 ms (controller 1) + 2 ms (controller 2) + // = 9 ms -> 90% allocated + } + + /** + * This function is run when the robot is first started up and should be used for any + * initialization code. + */ + @Override + public void robotInit() { + m_chooser.setDefaultOption("Default Auto", kDefaultAuto); + m_chooser.addOption("My Auto", kCustomAuto); + SmartDashboard.putData("Auto choices", m_chooser); + } + + /** + * This function is called every robot packet, no matter the mode. Use this for items like + * diagnostics that you want ran during disabled, autonomous, teleoperated and test. + * + *

This runs after the mode specific periodic functions, but before LiveWindow and + * SmartDashboard integrated updating. + */ + @Override + public void robotPeriodic() {} + + /** + * This autonomous (along with the chooser code above) shows how to select between different + * autonomous modes using the dashboard. The sendable chooser code works with the Java + * SmartDashboard. If you prefer the LabVIEW Dashboard, remove all of the chooser code and + * uncomment the getString line to get the auto name from the text box below the Gyro + * + *

You can add additional auto modes by adding additional comparisons to the switch structure + * below with additional strings. If using the SendableChooser make sure to add them to the + * chooser code above as well. + */ + @Override + public void autonomousInit() { + m_autoSelected = m_chooser.getSelected(); + // m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto); + System.out.println("Auto selected: " + m_autoSelected); + } + + /** This function is called periodically during autonomous. */ + @Override + public void autonomousPeriodic() { + switch (m_autoSelected) { + case kCustomAuto: + // Put custom auto code here + break; + case kDefaultAuto: + default: + // Put default auto code here + break; + } + } + + /** This function is called once when teleop is enabled. */ + @Override + public void teleopInit() {} + + /** This function is called periodically during operator control. */ + @Override + public void teleopPeriodic() {} + + /** This function is called once when the robot is disabled. */ + @Override + public void disabledInit() {} + + /** This function is called periodically when disabled. */ + @Override + public void disabledPeriodic() {} + + /** This function is called once when test mode is enabled. */ + @Override + public void testInit() {} + + /** This function is called periodically during test mode. */ + @Override + public void testPeriodic() {} +} diff --git a/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timesliceskeleton/Main.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timesliceskeleton/Main.java new file mode 100644 index 00000000000..bc92fc44361 --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timesliceskeleton/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.timesliceskeleton; + +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/timesliceskeleton/Robot.java b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timesliceskeleton/Robot.java new file mode 100644 index 00000000000..37ea2b5c34b --- /dev/null +++ b/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/timesliceskeleton/Robot.java @@ -0,0 +1,70 @@ +// 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.timesliceskeleton; + +import edu.wpi.first.wpilibj.TimesliceRobot; +import edu.wpi.first.wpilibj.livewindow.LiveWindow; + +/** + * The VM is configured to automatically run this class, and to call the functions corresponding to + * each mode, as described in the TimesliceRobot documentation. 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 TimesliceRobot { + /** Robot constructor. */ + public Robot() { + // Run robot periodic() functions for 5 ms, and run controllers every 10 ms + super(0.005, 0.01); + + // LiveWindow causes drastic overruns in robot periodic functions that will + // interfere with controllers + LiveWindow.disableAllTelemetry(); + + // Runs for 2 ms after robot periodic functions + schedule(() -> {}, 0.002); + + // Runs for 2 ms after first controller function + schedule(() -> {}, 0.002); + + // Total usage: + // 5 ms (robot) + 2 ms (controller 1) + 2 ms (controller 2) = 9 ms + // 9 ms / 10 ms -> 90% allocated + } + + /** + * This function is run when the robot is first started up and should be used for any + * initialization code. + */ + @Override + public void robotInit() {} + + @Override + public void robotPeriodic() {} + + @Override + public void autonomousInit() {} + + @Override + public void autonomousPeriodic() {} + + @Override + public void teleopInit() {} + + @Override + public void teleopPeriodic() {} + + @Override + public void disabledInit() {} + + @Override + public void disabledPeriodic() {} + + @Override + public void testInit() {} + + @Override + public void testPeriodic() {} +}