Skip to content

Commit 18f593d

Browse files
authored
Change usage of robotInit to Robot Constructor (#2841)
* Change usage of robotInit to Robot Constructor Fixes #2679 Supersedes #2689 Does not touch RobotBuilder docs, as those are generated and need to be updated by regenerating the code Does not touch stacktraces article, as the code and stacktraces need to be updated together Does not touch Python. robotpy/examples#120 * Remove bad Override * Remove additional reference
1 parent 8154ae7 commit 18f593d

File tree

16 files changed

+73
-67
lines changed

16 files changed

+73
-67
lines changed

source/docs/networking/networking-utilities/portforwarding.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ Often teams may wish to connect directly to the roboRIO for controlling their ro
99
.. tab-set-code::
1010

1111
```java
12-
@Override
13-
public void robotInit() {
12+
public Robot() {
1413
PortForwarder.add(8888, "wpilibpi.local", 80);
1514
}
1615
```
1716

1817
```c++
19-
void Robot::RobotInit {
18+
Robot::Robot() {
2019
wpi::PortForwarder::GetInstance().Add(8888, "wpilibpi.local", 80);
2120
}
2221
```
@@ -34,14 +33,13 @@ To stop forwarding on a specified port, simply call ``remove(int port)`` with po
3433
.. tab-set-code::
3534

3635
```java
37-
@Override
38-
public void robotInit() {
36+
public Robot() {
3937
PortForwarder.remove(8888);
4038
}
4139
```
4240

4341
```c++
44-
void Robot::RobotInit {
42+
Robot::Robot() {
4543
wpi::PortForwarder::GetInstance().Remove(8888);
4644
}
4745
```

source/docs/software/advanced-controls/trajectories/trajectory-generation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Here is an example of generating a trajectory using clamped cubic splines for th
6363

6464
.. note:: The Java code utilizes the [Units](https://github.wpilib.org/allwpilib/docs/development/java/edu/wpi/first/math/util/Units.html) utility, for easy unit conversions.
6565

66-
.. note:: Generating a typical trajectory takes about 10 ms to 25 ms. This isn't long, but it's still highly recommended to generate all trajectories on startup (``robotInit``).
66+
.. note:: Generating a typical trajectory takes about 10 ms to 25 ms. This isn't long, but it's still highly recommended to generate all trajectories on startup in the ``Robot`` constructor.
6767

6868
## Concatenating Trajectories
6969

source/docs/software/basic-programming/java-gc.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Sometimes the garbage collector won't run frequently enough to keep up with the
130130

131131
```java
132132
Timer m_gcTimer = new Timer();
133-
public void robotInit() {
133+
public Robot() {
134134
m_gcTimer.start();
135135
}
136136
public void periodic() {

source/docs/software/basic-programming/robot-preferences.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ This example shows how to utilize Preferences to change the setpoint of a PID co
4444

4545
Preferences are stored using a name, the key. It's helpful to store the key in a constant, like ``kArmPositionKey`` and ``kArmPKey`` in the code above to avoid typing it multiple times and avoid typos. We also declare variables, ``kArmKp`` and ``armPositionDeg`` to hold the data retrieved from preferences.
4646

47-
In ``robotInit``, each key is checked to see if it already exists in the Preferences database. The ``containsKey`` method takes one parameter, the key to check if data for that key already exists in the preferences database. If it doesn't exist, a default value is written. The ``setDouble`` method takes two parameters, the key to write and the data to write. There are similar methods for other data types like booleans, ints, and strings.
47+
In ``Arm`` constructor, each key is checked to see if it already exists in the Preferences database. The ``containsKey`` method takes one parameter, the key to check if data for that key already exists in the preferences database. If it doesn't exist, a default value is written. The ``setDouble`` method takes two parameters, the key to write and the data to write. There are similar methods for other data types like booleans, ints, and strings.
4848

4949
If using the Command Framework, this type of code could be placed in the constructor of a Subsystem or Command.
5050

source/docs/software/dashboards/smartdashboard/displaying-status-of-commands-and-subsystems.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ In the following examples, you'll see what the screen would look like when there
3434
SmartDashboard.putData(CommandScheduler.getInstance())
3535
```
3636

37-
You can display the status of the Scheduler (the code that schedules your commands to run). This is easily done by adding a single line to the ``RobotInit`` method in your RobotProgram as shown here. In this example the Scheduler instance is written using the ``putData`` method to SmartDashboard. This line of code produces the display in the previous image.
37+
You can display the status of the Scheduler (the code that schedules your commands to run). This is easily done by adding a single line to the ``Robot`` constructor in your Robot program as shown here. In this example the Scheduler instance is written using the ``putData`` method to SmartDashboard. This line of code produces the display in the previous image.
3838

3939
.. image:: images/displaying-status-of-commands-and-subsystems/commands-running.png
4040
:alt: The schedulers is showing which commands are being run.

source/docs/software/dashboards/smartdashboard/test-mode-and-live-window/enabling-test-mode.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ To run LiveWindow in Test Mode, the following code is needed in the ``Robot`` cl
1212
.. tab-set-code::
1313

1414
```java
15-
@Override
16-
public void robotInit() {
15+
public Robot() {
1716
enableLiveWindowInTest(true);
1817
}
1918
```
2019

2120
```c++
22-
void Robot::RobotInit() {
21+
Robot::Robot() {
2322
EnableLiveWindowInTest(true);
2423
}
2524
```
@@ -38,8 +37,8 @@ To run LiveWindow in Test Mode, the following code is needed in the ``Robot`` cl
3837
PWMSparkMax rightDrive;
3938
PWMVictorSPX arm;
4039
BuiltInAccelerometer accel;
41-
@Override
42-
public void robotInit() {
40+
41+
public Robot() {
4342
leftDrive = new PWMSparkMax(0);
4443
rightDrive = new PWMSparkMax(1);
4544
arm = new PWMVictorSPX(2);
@@ -54,7 +53,7 @@ To run LiveWindow in Test Mode, the following code is needed in the ``Robot`` cl
5453
frc::PWMSparkMax rigthDrive{1};
5554
frc::BuiltInAccelerometer accel{};
5655
frc::PWMVictorSPX arm{3};
57-
void Robot::RobotInit() {
56+
Robot::Robot() {
5857
wpi::SendableRegistry::SetName(&arm, "SomeSubsystem", "Arm");
5958
wpi::SendableRegistry::SetName(&accel, "SomeSubsystem", "Accelerometer");
6059
}

source/docs/software/hardware-apis/motors/wpi-drive-classes.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ It is the responsibility of the user to manage proper inversions for their drive
3232

3333
```java
3434
PWMSparkMax m_motorRight = new PWMSparkMax(0);
35-
@Override
36-
public void robotInit() {
35+
36+
public Robot() {
3737
m_motorRight.setInverted(true);
3838
}
3939
```
4040

4141
```c++
4242
frc::PWMSparkMax m_motorLeft{0};
4343
public:
44-
void RobotInit() override {
44+
Robot::Robot() {
4545
m_motorRight.SetInverted(true);
4646
}
4747
```
@@ -159,7 +159,7 @@ Many FRC\ |reg| drivetrains have more than 1 motor on each side. Classes derived
159159
:language: java
160160
:lines: 19-25
161161

162-
In robotInit or Subsystem constructor:
162+
In Robot or Subsystem constructor:
163163

164164
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.3.2/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/ramsetecommand/subsystems/DriveSubsystem.java
165165
:language: java
@@ -175,7 +175,7 @@ Many FRC\ |reg| drivetrains have more than 1 motor on each side. Classes derived
175175
.. tab-item:: C++ (Source)
176176
:sync: C++ (Source)
177177

178-
In robotInit or Subsystem constructor:
178+
In Robot or Subsystem constructor:
179179

180180
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.3.2/wpilibcExamples/src/main/cpp/examples/RamseteCommand/cpp/subsystems/DriveSubsystem.cpp
181181
:language: c++

source/docs/software/hardware-apis/sensors/counters.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ In two-pulse mode, the :code:`Counter` will count up for every edge/pulse on the
3131
```java
3232
// Create a new Counter object in two-pulse mode
3333
Counter counter = new Counter(Counter.Mode.k2Pulse);
34-
@Override
35-
public void robotInit() {
34+
35+
public Robot() {
3636
// Set up the input channels for the counter
3737
counter.setUpSource(1);
3838
counter.setDownSource(2);
@@ -45,7 +45,7 @@ In two-pulse mode, the :code:`Counter` will count up for every edge/pulse on the
4545
```c++
4646
// Create a new Counter object in two-pulse mode
4747
frc::Counter counter{frc::Counter::Mode::k2Pulse};
48-
void Robot::RobotInit() {
48+
Robot::Robot() {
4949
// Set up the input channels for the counter
5050
counter.SetUpSource(1);
5151
counter.SetDownSource(2);
@@ -63,8 +63,8 @@ In semi-period mode, the :code:`Counter` will count the duration of the pulses o
6363
```java
6464
// Create a new Counter object in two-pulse mode
6565
Counter counter = new Counter(Counter.Mode.kSemiperiod);
66-
@Override
67-
public void robotInit() {
66+
67+
public Robot() {
6868
// Set up the input channel for the counter
6969
counter.setUpSource(1);
7070
// Set the encoder to count pulse duration from rising edge to falling edge
@@ -105,8 +105,8 @@ In pulse-length mode, the counter will count either up or down depending on the
105105
```java
106106
// Create a new Counter object in two-pulse mode
107107
Counter counter = new Counter(Counter.Mode.kPulseLength);
108-
@Override
109-
public void robotInit() {
108+
109+
public Robot() {
110110
// Set up the input channel for the counter
111111
counter.setUpSource(1);
112112
// Set the decoding type to 2X
@@ -119,7 +119,7 @@ In pulse-length mode, the counter will count either up or down depending on the
119119
```c++
120120
// Create a new Counter object in two-pulse mode
121121
frc::Counter counter{frc::Counter::Mode::kPulseLength};
122-
void Robot::RobotInit() {
122+
Robot::Robot() {
123123
// Set up the input channel for the counter
124124
counter.SetUpSource(1);
125125
// Set the decoding type to 2X
@@ -137,8 +137,8 @@ In external direction mode, the counter counts either up or down depending on th
137137
```java
138138
// Create a new Counter object in two-pulse mode
139139
Counter counter = new Counter(Counter.Mode.kExternalDirection);
140-
@Override
141-
public void robotInit() {
140+
141+
public Robot() {
142142
// Set up the input channels for the counter
143143
counter.setUpSource(1);
144144
counter.setDownSource(2);

source/docs/software/hardware-apis/sensors/encoders-software.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ Encoders can be used on a robot drive to create a simple "drive to distance" rou
430430
Spark rightLeader = new Spark(2);
431431
Spark rightFollower = new Spark(3);
432432
DifferentialDrive drive = new DifferentialDrive(leftLeader::set, rightLeader::set);
433-
@Override
434-
public void robotInit() {
433+
434+
public Robot() {
435435
// Configures the encoder's distance-per-pulse
436436
// The robot moves forward 1 foot per encoder rotation
437437
// There are 256 pulses per encoder rotation
@@ -442,6 +442,7 @@ Encoders can be used on a robot drive to create a simple "drive to distance" rou
442442
leftLeader.addFollower(leftFollower);
443443
rightLeader.addFollower(rightFollower);
444444
}
445+
445446
@Override
446447
public void autonomousPeriodic() {
447448
// Drives forward at half speed until the robot has moved 5 feet, then stops:
@@ -463,7 +464,7 @@ Encoders can be used on a robot drive to create a simple "drive to distance" rou
463464
frc::Spark rightFollower{3};
464465
frc::DifferentialDrive drive{[&](double output) { leftLeader.Set(output); },
465466
[&](double output) { rightLeader.Set(output); }};
466-
void Robot::RobotInit() {
467+
Robot::Robot() {
467468
// Configures the encoder's distance-per-pulse
468469
// The robot moves forward 1 foot per encoder rotation
469470
// There are 256 pulses per encoder rotation

source/docs/software/hardware-apis/sensors/gyros-software.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,15 @@ Gyros are extremely useful in FRC for both measuring and controlling robot headi
162162

163163
```java
164164
// Use gyro declaration from above here
165-
public void robotInit() {
165+
public Robot() {
166166
// Places a compass indicator for the gyro heading on the dashboard
167167
Shuffleboard.getTab("Example tab").add(gyro);
168168
}
169169
```
170170

171171
```c++
172172
// Use gyro declaration from above here
173-
void Robot::RobotInit() {
173+
Robot::Robot() {
174174
// Places a compass indicator for the gyro heading on the dashboard
175175
frc::Shuffleboard.GetTab("Example tab").Add(gyro);
176176
}
@@ -208,8 +208,8 @@ The following example shows how to stabilize heading using a simple P loop close
208208
Spark rightLeader = new Spark(2);
209209
Spark rightFollower = new Spark(3);
210210
DifferentialDrive drive = new DifferentialDrive(leftLeader::set, rightLeader::set);
211-
@Override
212-
public void robotInit() {
211+
212+
public Robot() {
213213
// Configures the encoder's distance-per-pulse
214214
// The robot moves forward 1 foot per encoder rotation
215215
// There are 256 pulses per encoder rotation
@@ -220,6 +220,7 @@ The following example shows how to stabilize heading using a simple P loop close
220220
leftLeader.addFollower(leftFollower);
221221
rightLeader.addFollower(rightFollower);
222222
}
223+
223224
@Override
224225
public void autonomousPeriodic() {
225226
// Setpoint is implicitly 0, since we don't want the heading to change
@@ -240,7 +241,7 @@ The following example shows how to stabilize heading using a simple P loop close
240241
frc::Spark rightFollower{3};
241242
frc::DifferentialDrive drive{[&](double output) { leftLeader.Set(output); },
242243
[&](double output) { rightLeader.Set(output); }};
243-
void Robot::RobotInit() {
244+
Robot::Robot() {
244245
// Invert the right side of the drivetrain. You might have to invert the other side
245246
rightLeader.SetInverted(true);
246247
// Configure the followers to follow the leaders
@@ -303,15 +304,17 @@ The following example shows how to stabilize heading using a simple P loop close
303304
MotorControllerGroup leftMotors = new MotorControllerGroup(left1, left2);
304305
MotorControllerGroup rightMotors = new MotorControllerGroup(right1, right2);
305306
DifferentialDrive drive = new DifferentialDrive(leftMotors, rightMotors);
306-
@Override
307-
public void robotInit() {
307+
308+
public Robot() {
308309
rightMotors.setInverted(true);
309310
}
311+
310312
@Override
311313
public void autonomousInit() {
312314
// Set setpoint to current heading at start of auto
313315
heading = gyro.getAngle();
314316
}
317+
315318
@Override
316319
public void autonomousPeriodic() {
317320
double error = heading - gyro.getAngle();
@@ -334,7 +337,7 @@ The following example shows how to stabilize heading using a simple P loop close
334337
frc::MotorControllerGroup leftMotors{left1, left2};
335338
frc::MotorControllerGroup rightMotors{right1, right2};
336339
frc::DifferentialDrive drive{leftMotors, rightMotors};
337-
void Robot::RobotInit() {
340+
Robot::Robot() {
338341
rightMotors.SetInverted(true);
339342
}
340343
void Robot::AutonomousInit() {
@@ -396,10 +399,11 @@ Much like with heading stabilization, this is often accomplished with a PID loop
396399
MotorControllerGroup leftMotors = new MotorControllerGroup(left1, left2);
397400
MotorControllerGroup rightMotors = new MotorControllerGroup(right1, right2);
398401
DifferentialDrive drive = new DifferentialDrive(leftMotors, rightMotors);
399-
@Override
400-
public void robotInit() {
402+
403+
public Robot() {
401404
rightMotors.setInverted(true);
402405
}
406+
403407
@Override
404408
public void autonomousPeriodic() {
405409
// Find the heading error; setpoint is 90
@@ -421,7 +425,7 @@ Much like with heading stabilization, this is often accomplished with a PID loop
421425
frc::MotorControllerGroup leftMotors{left1, left2};
422426
frc::MotorControllerGroup rightMotors{right1, right2};
423427
frc::DifferentialDrive drive{leftMotors, rightMotors};
424-
void Robot::RobotInit() {
428+
Robot::Robot() {
425429
rightMotors.SetInverted(true);
426430
}
427431
void Robot::AutonomousPeriodic() {

source/docs/software/networktables/reading-array-values-published-by-networktables.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ Both of the following examples are extremely simplified programs that just illus
2020

2121
```java
2222
DoubleArraySubscriber areasSub;
23-
@Override
24-
public void robotInit() {
23+
24+
public Robot() {
2525
NetworkTable table = NetworkTableInstance.getDefault().getTable("GRIP/mycontoursReport");
2626
areasSub = table.getDoubleArrayTopic("area").subscribe(new double[] {});
2727
}
28+
2829
@Override
2930
public void teleopPeriodic() {
3031
double[] areas = areasSub.get();
@@ -38,7 +39,7 @@ Both of the following examples are extremely simplified programs that just illus
3839

3940
```c++
4041
nt::DoubleArraySubscriber areasSub;
41-
void Robot::RobotInit() override {
42+
Robot::Robot() {
4243
auto table = nt::NetworkTableInstance::GetDefault().GetTable("GRIP/myContoursReport");
4344
areasSub = table->GetDoubleArrayTopic("area").Subscribe({});
4445
}

source/docs/software/networktables/robot-program.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ The example robot program below publishes incrementing X and Y values to a table
1515
public class EasyNetworkTableExample extends TimedRobot {
1616
DoublePublisher xPub;
1717
DoublePublisher yPub;
18-
public void robotInit() {
18+
19+
public Robot() {
1920
// Get the default instance of NetworkTables that was created automatically
2021
// when the robot program starts
2122
NetworkTableInstance inst = NetworkTableInstance.getDefault();
@@ -29,6 +30,7 @@ The example robot program below publishes incrementing X and Y values to a table
2930
xPub = table.getDoubleTopic("x").publish();
3031
yPub = table.getDoubleTopic("y").publish();
3132
}
33+
3234
double x = 0;
3335
double y = 0;
3436
public void teleopPeriodic() {
@@ -50,7 +52,7 @@ The example robot program below publishes incrementing X and Y values to a table
5052
public:
5153
nt::DoublePublisher xPub;
5254
nt::DoublePublisher yPub;
53-
void RobotInit() {
55+
Robot() {
5456
// Get the default instance of NetworkTables that was created automatically
5557
// when the robot program starts
5658
auto inst = nt::NetworkTableInstance::GetDefault();

0 commit comments

Comments
 (0)