Skip to content

Commit 19a8850

Browse files
authored
[examples] Add TimesliceRobot templates (wpilibsuite#3683)
1 parent 9047682 commit 19a8850

11 files changed

Lines changed: 486 additions & 0 deletions

File tree

‎styleguide/spotbugs-exclude.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@
116116
<Bug pattern="UC_USELESS_VOID_METHOD" />
117117
<Class name="edu.wpi.first.wpilibj.templates.timed.Robot" />
118118
</Match>
119+
<Match>
120+
<Bug pattern="UC_USELESS_VOID_METHOD" />
121+
<Class name="edu.wpi.first.wpilibj.templates.timeslice.Robot" />
122+
</Match>
123+
<Match>
124+
<Bug pattern="UC_USELESS_VOID_METHOD" />
125+
<Class name="edu.wpi.first.wpilibj.templates.timesliceskeleton.Robot" />
126+
</Match>
119127
<Match>
120128
<Bug pattern="URF_UNREAD_FIELD" />
121129
<Class name="edu.wpi.first.wpilibj.ADIS16448_IMU" />

‎wpilibcExamples/src/main/cpp/templates/templates.json‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@
4141
"gradlebase": "cpp",
4242
"commandversion": 2
4343
},
44+
{
45+
"name": "Timeslice Robot",
46+
"description": "Timeslice style",
47+
"tags": [
48+
"Timeslice"
49+
],
50+
"foldername": "timeslice",
51+
"gradlebase": "cpp",
52+
"commandversion": 2
53+
},
54+
{
55+
"name": "Timeslice Skeleton (Advanced)",
56+
"description": "Skeleton (stub) code for TimesliceRobot",
57+
"tags": [
58+
"Timeslice",
59+
"Skeleton"
60+
],
61+
"foldername": "timesliceskeleton",
62+
"gradlebase": "cpp",
63+
"commandversion": 2
64+
},
4465
{
4566
"name": "RobotBase Skeleton (Advanced)",
4667
"description": "Skeleton (stub) code for RobotBase - Not recommended for competition use",
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#include "Robot.h"
6+
7+
#include <fmt/core.h>
8+
#include <frc/livewindow/LiveWindow.h>
9+
#include <frc/smartdashboard/SmartDashboard.h>
10+
11+
// Run robot periodic() functions for 5 ms, and run controllers every 10 ms
12+
Robot::Robot() : frc::TimesliceRobot{5_ms, 10_ms} {
13+
// LiveWindow causes drastic overruns in robot periodic functions that will
14+
// interfere with controllers
15+
frc::LiveWindow::DisableAllTelemetry();
16+
17+
// Runs for 2 ms after robot periodic functions
18+
Schedule([=] {}, 2_ms);
19+
20+
// Runs for 2 ms after first controller function
21+
Schedule([=] {}, 2_ms);
22+
23+
// Total usage:
24+
// 5 ms (robot) + 2 ms (controller 1) + 2 ms (controller 2) = 9 ms
25+
// 9 ms / 10 ms -> 90% allocated
26+
}
27+
28+
void Robot::RobotInit() {
29+
m_chooser.SetDefaultOption(kAutoNameDefault, kAutoNameDefault);
30+
m_chooser.AddOption(kAutoNameCustom, kAutoNameCustom);
31+
frc::SmartDashboard::PutData("Auto Modes", &m_chooser);
32+
}
33+
34+
/**
35+
* This function is called every robot packet, no matter the mode. Use
36+
* this for items like diagnostics that you want ran during disabled,
37+
* autonomous, teleoperated and test.
38+
*
39+
* <p> This runs after the mode specific periodic functions, but before
40+
* LiveWindow and SmartDashboard integrated updating.
41+
*/
42+
void Robot::RobotPeriodic() {}
43+
44+
/**
45+
* This autonomous (along with the chooser code above) shows how to select
46+
* between different autonomous modes using the dashboard. The sendable chooser
47+
* code works with the Java SmartDashboard. If you prefer the LabVIEW Dashboard,
48+
* remove all of the chooser code and uncomment the GetString line to get the
49+
* auto name from the text box below the Gyro.
50+
*
51+
* You can add additional auto modes by adding additional comparisons to the
52+
* if-else structure below with additional strings. If using the SendableChooser
53+
* make sure to add them to the chooser code above as well.
54+
*/
55+
void Robot::AutonomousInit() {
56+
m_autoSelected = m_chooser.GetSelected();
57+
// m_autoSelected = SmartDashboard::GetString("Auto Selector",
58+
// kAutoNameDefault);
59+
fmt::print("Auto selected: {}\n", m_autoSelected);
60+
61+
if (m_autoSelected == kAutoNameCustom) {
62+
// Custom Auto goes here
63+
} else {
64+
// Default Auto goes here
65+
}
66+
}
67+
68+
void Robot::AutonomousPeriodic() {
69+
if (m_autoSelected == kAutoNameCustom) {
70+
// Custom Auto goes here
71+
} else {
72+
// Default Auto goes here
73+
}
74+
}
75+
76+
void Robot::TeleopInit() {}
77+
78+
void Robot::TeleopPeriodic() {}
79+
80+
void Robot::DisabledInit() {}
81+
82+
void Robot::DisabledPeriodic() {}
83+
84+
void Robot::TestInit() {}
85+
86+
void Robot::TestPeriodic() {}
87+
88+
#ifndef RUNNING_FRC_TESTS
89+
int main() {
90+
return frc::StartRobot<Robot>();
91+
}
92+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#pragma once
6+
7+
#include <string>
8+
9+
#include <frc/TimesliceRobot.h>
10+
#include <frc/smartdashboard/SendableChooser.h>
11+
12+
class Robot : public frc::TimesliceRobot {
13+
public:
14+
Robot();
15+
16+
void RobotInit() override;
17+
void RobotPeriodic() override;
18+
void AutonomousInit() override;
19+
void AutonomousPeriodic() override;
20+
void TeleopInit() override;
21+
void TeleopPeriodic() override;
22+
void DisabledInit() override;
23+
void DisabledPeriodic() override;
24+
void TestInit() override;
25+
void TestPeriodic() override;
26+
27+
private:
28+
frc::SendableChooser<std::string> m_chooser;
29+
const std::string kAutoNameDefault = "Default";
30+
const std::string kAutoNameCustom = "My Auto";
31+
std::string m_autoSelected;
32+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#include "Robot.h"
6+
7+
#include <frc/livewindow/LiveWindow.h>
8+
9+
// Run robot periodic() functions for 5 ms, and run controllers every 10 ms
10+
Robot::Robot() : frc::TimesliceRobot{5_ms, 10_ms} {
11+
// LiveWindow causes drastic overruns in robot periodic functions that will
12+
// interfere with controllers
13+
frc::LiveWindow::DisableAllTelemetry();
14+
15+
// Runs for 2 ms after robot periodic functions
16+
Schedule([=] {}, 2_ms);
17+
18+
// Runs for 2 ms after first controller function
19+
Schedule([=] {}, 2_ms);
20+
21+
// Total usage:
22+
// 5 ms (robot) + 2 ms (controller 1) + 2 ms (controller 2) = 9 ms
23+
// 9 ms / 10 ms -> 90% allocated
24+
}
25+
26+
void Robot::RobotInit() {}
27+
void Robot::RobotPeriodic() {}
28+
29+
void Robot::AutonomousInit() {}
30+
void Robot::AutonomousPeriodic() {}
31+
32+
void Robot::TeleopInit() {}
33+
void Robot::TeleopPeriodic() {}
34+
35+
void Robot::DisabledInit() {}
36+
void Robot::DisabledPeriodic() {}
37+
38+
void Robot::TestInit() {}
39+
void Robot::TestPeriodic() {}
40+
41+
#ifndef RUNNING_FRC_TESTS
42+
int main() {
43+
return frc::StartRobot<Robot>();
44+
}
45+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
#pragma once
6+
7+
#include <frc/TimesliceRobot.h>
8+
9+
class Robot : public frc::TimesliceRobot {
10+
public:
11+
Robot();
12+
13+
void RobotInit() override;
14+
void RobotPeriodic() override;
15+
16+
void AutonomousInit() override;
17+
void AutonomousPeriodic() override;
18+
19+
void TeleopInit() override;
20+
void TeleopPeriodic() override;
21+
22+
void DisabledInit() override;
23+
void DisabledPeriodic() override;
24+
25+
void TestInit() override;
26+
void TestPeriodic() override;
27+
};

‎wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/templates/templates.json‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@
4545
"mainclass": "Main",
4646
"commandversion": 2
4747
},
48+
{
49+
"name": "Timesice Robot",
50+
"description": "Timeslice style",
51+
"tags": [
52+
"Timeslice"
53+
],
54+
"foldername": "timeslice",
55+
"gradlebase": "java",
56+
"mainclass": "Main",
57+
"commandversion": 2
58+
},
59+
{
60+
"name": "Timeslice Skeleton (Advanced)",
61+
"description": "Skeleton (stub) code for TimesliceRobot",
62+
"tags": [
63+
"Timeslice",
64+
"Skeleton"
65+
],
66+
"foldername": "timesliceskeleton",
67+
"gradlebase": "java",
68+
"mainclass": "Main",
69+
"commandversion": 2
70+
},
4871
{
4972
"name": "RobotBase Skeleton (Advanced)",
5073
"description": "Skeleton (stub) code for RobotBase - Not recommended for competition use",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) FIRST and other WPILib contributors.
2+
// Open Source Software; you can modify and/or share it under the terms of
3+
// the WPILib BSD license file in the root directory of this project.
4+
5+
package edu.wpi.first.wpilibj.templates.timeslice;
6+
7+
import edu.wpi.first.wpilibj.RobotBase;
8+
9+
/**
10+
* Do NOT add any static variables to this class, or any initialization at all. Unless you know what
11+
* you are doing, do not modify this file except to change the parameter class to the startRobot
12+
* call.
13+
*/
14+
public final class Main {
15+
private Main() {}
16+
17+
/**
18+
* Main initialization function. Do not perform any initialization here.
19+
*
20+
* <p>If you change your main robot class, change the parameter type.
21+
*/
22+
public static void main(String... args) {
23+
RobotBase.startRobot(Robot::new);
24+
}
25+
}

0 commit comments

Comments
 (0)