Skip to content

Commit

Permalink
Change usage of robotInit to Robot Constructor (#2841)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sciencewhiz authored Nov 11, 2024
1 parent 8154ae7 commit 18f593d
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 67 deletions.
10 changes: 4 additions & 6 deletions source/docs/networking/networking-utilities/portforwarding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ Often teams may wish to connect directly to the roboRIO for controlling their ro
.. tab-set-code::

```java
@Override
public void robotInit() {
public Robot() {
PortForwarder.add(8888, "wpilibpi.local", 80);
}
```

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

```java
@Override
public void robotInit() {
public Robot() {
PortForwarder.remove(8888);
}
```

```c++
void Robot::RobotInit {
Robot::Robot() {
wpi::PortForwarder::GetInstance().Remove(8888);
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Here is an example of generating a trajectory using clamped cubic splines for th

.. 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.

.. 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``).
.. 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.

## Concatenating Trajectories

Expand Down
2 changes: 1 addition & 1 deletion source/docs/software/basic-programming/java-gc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Sometimes the garbage collector won't run frequently enough to keep up with the

```java
Timer m_gcTimer = new Timer();
public void robotInit() {
public Robot() {
m_gcTimer.start();
}
public void periodic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This example shows how to utilize Preferences to change the setpoint of a PID co

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.

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.
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.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ In the following examples, you'll see what the screen would look like when there
SmartDashboard.putData(CommandScheduler.getInstance())
```

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.
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.

.. image:: images/displaying-status-of-commands-and-subsystems/commands-running.png
:alt: The schedulers is showing which commands are being run.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ To run LiveWindow in Test Mode, the following code is needed in the ``Robot`` cl
.. tab-set-code::

```java
@Override
public void robotInit() {
public Robot() {
enableLiveWindowInTest(true);
}
```

```c++
void Robot::RobotInit() {
Robot::Robot() {
EnableLiveWindowInTest(true);
}
```
Expand All @@ -38,8 +37,8 @@ To run LiveWindow in Test Mode, the following code is needed in the ``Robot`` cl
PWMSparkMax rightDrive;
PWMVictorSPX arm;
BuiltInAccelerometer accel;
@Override
public void robotInit() {
public Robot() {
leftDrive = new PWMSparkMax(0);
rightDrive = new PWMSparkMax(1);
arm = new PWMVictorSPX(2);
Expand All @@ -54,7 +53,7 @@ To run LiveWindow in Test Mode, the following code is needed in the ``Robot`` cl
frc::PWMSparkMax rigthDrive{1};
frc::BuiltInAccelerometer accel{};
frc::PWMVictorSPX arm{3};
void Robot::RobotInit() {
Robot::Robot() {
wpi::SendableRegistry::SetName(&arm, "SomeSubsystem", "Arm");
wpi::SendableRegistry::SetName(&accel, "SomeSubsystem", "Accelerometer");
}
Expand Down
10 changes: 5 additions & 5 deletions source/docs/software/hardware-apis/motors/wpi-drive-classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ It is the responsibility of the user to manage proper inversions for their drive

```java
PWMSparkMax m_motorRight = new PWMSparkMax(0);
@Override
public void robotInit() {
public Robot() {
m_motorRight.setInverted(true);
}
```

```c++
frc::PWMSparkMax m_motorLeft{0};
public:
void RobotInit() override {
Robot::Robot() {
m_motorRight.SetInverted(true);
}
```
Expand Down Expand Up @@ -159,7 +159,7 @@ Many FRC\ |reg| drivetrains have more than 1 motor on each side. Classes derived
:language: java
:lines: 19-25

In robotInit or Subsystem constructor:
In Robot or Subsystem constructor:

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

In robotInit or Subsystem constructor:
In Robot or Subsystem constructor:

.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2024.3.2/wpilibcExamples/src/main/cpp/examples/RamseteCommand/cpp/subsystems/DriveSubsystem.cpp
:language: c++
Expand Down
20 changes: 10 additions & 10 deletions source/docs/software/hardware-apis/sensors/counters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ In two-pulse mode, the :code:`Counter` will count up for every edge/pulse on the
```java
// Create a new Counter object in two-pulse mode
Counter counter = new Counter(Counter.Mode.k2Pulse);
@Override
public void robotInit() {
public Robot() {
// Set up the input channels for the counter
counter.setUpSource(1);
counter.setDownSource(2);
Expand All @@ -45,7 +45,7 @@ In two-pulse mode, the :code:`Counter` will count up for every edge/pulse on the
```c++
// Create a new Counter object in two-pulse mode
frc::Counter counter{frc::Counter::Mode::k2Pulse};
void Robot::RobotInit() {
Robot::Robot() {
// Set up the input channels for the counter
counter.SetUpSource(1);
counter.SetDownSource(2);
Expand All @@ -63,8 +63,8 @@ In semi-period mode, the :code:`Counter` will count the duration of the pulses o
```java
// Create a new Counter object in two-pulse mode
Counter counter = new Counter(Counter.Mode.kSemiperiod);
@Override
public void robotInit() {
public Robot() {
// Set up the input channel for the counter
counter.setUpSource(1);
// Set the encoder to count pulse duration from rising edge to falling edge
Expand Down Expand Up @@ -105,8 +105,8 @@ In pulse-length mode, the counter will count either up or down depending on the
```java
// Create a new Counter object in two-pulse mode
Counter counter = new Counter(Counter.Mode.kPulseLength);
@Override
public void robotInit() {
public Robot() {
// Set up the input channel for the counter
counter.setUpSource(1);
// Set the decoding type to 2X
Expand All @@ -119,7 +119,7 @@ In pulse-length mode, the counter will count either up or down depending on the
```c++
// Create a new Counter object in two-pulse mode
frc::Counter counter{frc::Counter::Mode::kPulseLength};
void Robot::RobotInit() {
Robot::Robot() {
// Set up the input channel for the counter
counter.SetUpSource(1);
// Set the decoding type to 2X
Expand All @@ -137,8 +137,8 @@ In external direction mode, the counter counts either up or down depending on th
```java
// Create a new Counter object in two-pulse mode
Counter counter = new Counter(Counter.Mode.kExternalDirection);
@Override
public void robotInit() {
public Robot() {
// Set up the input channels for the counter
counter.setUpSource(1);
counter.setDownSource(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ Encoders can be used on a robot drive to create a simple "drive to distance" rou
Spark rightLeader = new Spark(2);
Spark rightFollower = new Spark(3);
DifferentialDrive drive = new DifferentialDrive(leftLeader::set, rightLeader::set);
@Override
public void robotInit() {
public Robot() {
// Configures the encoder's distance-per-pulse
// The robot moves forward 1 foot per encoder rotation
// There are 256 pulses per encoder rotation
Expand All @@ -442,6 +442,7 @@ Encoders can be used on a robot drive to create a simple "drive to distance" rou
leftLeader.addFollower(leftFollower);
rightLeader.addFollower(rightFollower);
}
@Override
public void autonomousPeriodic() {
// Drives forward at half speed until the robot has moved 5 feet, then stops:
Expand All @@ -463,7 +464,7 @@ Encoders can be used on a robot drive to create a simple "drive to distance" rou
frc::Spark rightFollower{3};
frc::DifferentialDrive drive{[&](double output) { leftLeader.Set(output); },
[&](double output) { rightLeader.Set(output); }};
void Robot::RobotInit() {
Robot::Robot() {
// Configures the encoder's distance-per-pulse
// The robot moves forward 1 foot per encoder rotation
// There are 256 pulses per encoder rotation
Expand Down
26 changes: 15 additions & 11 deletions source/docs/software/hardware-apis/sensors/gyros-software.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ Gyros are extremely useful in FRC for both measuring and controlling robot headi

```java
// Use gyro declaration from above here
public void robotInit() {
public Robot() {
// Places a compass indicator for the gyro heading on the dashboard
Shuffleboard.getTab("Example tab").add(gyro);
}
```

```c++
// Use gyro declaration from above here
void Robot::RobotInit() {
Robot::Robot() {
// Places a compass indicator for the gyro heading on the dashboard
frc::Shuffleboard.GetTab("Example tab").Add(gyro);
}
Expand Down Expand Up @@ -208,8 +208,8 @@ The following example shows how to stabilize heading using a simple P loop close
Spark rightLeader = new Spark(2);
Spark rightFollower = new Spark(3);
DifferentialDrive drive = new DifferentialDrive(leftLeader::set, rightLeader::set);
@Override
public void robotInit() {
public Robot() {
// Configures the encoder's distance-per-pulse
// The robot moves forward 1 foot per encoder rotation
// There are 256 pulses per encoder rotation
Expand All @@ -220,6 +220,7 @@ The following example shows how to stabilize heading using a simple P loop close
leftLeader.addFollower(leftFollower);
rightLeader.addFollower(rightFollower);
}
@Override
public void autonomousPeriodic() {
// Setpoint is implicitly 0, since we don't want the heading to change
Expand All @@ -240,7 +241,7 @@ The following example shows how to stabilize heading using a simple P loop close
frc::Spark rightFollower{3};
frc::DifferentialDrive drive{[&](double output) { leftLeader.Set(output); },
[&](double output) { rightLeader.Set(output); }};
void Robot::RobotInit() {
Robot::Robot() {
// Invert the right side of the drivetrain. You might have to invert the other side
rightLeader.SetInverted(true);
// Configure the followers to follow the leaders
Expand Down Expand Up @@ -303,15 +304,17 @@ The following example shows how to stabilize heading using a simple P loop close
MotorControllerGroup leftMotors = new MotorControllerGroup(left1, left2);
MotorControllerGroup rightMotors = new MotorControllerGroup(right1, right2);
DifferentialDrive drive = new DifferentialDrive(leftMotors, rightMotors);
@Override
public void robotInit() {
public Robot() {
rightMotors.setInverted(true);
}
@Override
public void autonomousInit() {
// Set setpoint to current heading at start of auto
heading = gyro.getAngle();
}
@Override
public void autonomousPeriodic() {
double error = heading - gyro.getAngle();
Expand All @@ -334,7 +337,7 @@ The following example shows how to stabilize heading using a simple P loop close
frc::MotorControllerGroup leftMotors{left1, left2};
frc::MotorControllerGroup rightMotors{right1, right2};
frc::DifferentialDrive drive{leftMotors, rightMotors};
void Robot::RobotInit() {
Robot::Robot() {
rightMotors.SetInverted(true);
}
void Robot::AutonomousInit() {
Expand Down Expand Up @@ -396,10 +399,11 @@ Much like with heading stabilization, this is often accomplished with a PID loop
MotorControllerGroup leftMotors = new MotorControllerGroup(left1, left2);
MotorControllerGroup rightMotors = new MotorControllerGroup(right1, right2);
DifferentialDrive drive = new DifferentialDrive(leftMotors, rightMotors);
@Override
public void robotInit() {
public Robot() {
rightMotors.setInverted(true);
}
@Override
public void autonomousPeriodic() {
// Find the heading error; setpoint is 90
Expand All @@ -421,7 +425,7 @@ Much like with heading stabilization, this is often accomplished with a PID loop
frc::MotorControllerGroup leftMotors{left1, left2};
frc::MotorControllerGroup rightMotors{right1, right2};
frc::DifferentialDrive drive{leftMotors, rightMotors};
void Robot::RobotInit() {
Robot::Robot() {
rightMotors.SetInverted(true);
}
void Robot::AutonomousPeriodic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ Both of the following examples are extremely simplified programs that just illus

```java
DoubleArraySubscriber areasSub;
@Override
public void robotInit() {
public Robot() {
NetworkTable table = NetworkTableInstance.getDefault().getTable("GRIP/mycontoursReport");
areasSub = table.getDoubleArrayTopic("area").subscribe(new double[] {});
}
@Override
public void teleopPeriodic() {
double[] areas = areasSub.get();
Expand All @@ -38,7 +39,7 @@ Both of the following examples are extremely simplified programs that just illus

```c++
nt::DoubleArraySubscriber areasSub;
void Robot::RobotInit() override {
Robot::Robot() {
auto table = nt::NetworkTableInstance::GetDefault().GetTable("GRIP/myContoursReport");
areasSub = table->GetDoubleArrayTopic("area").Subscribe({});
}
Expand Down
6 changes: 4 additions & 2 deletions source/docs/software/networktables/robot-program.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ The example robot program below publishes incrementing X and Y values to a table
public class EasyNetworkTableExample extends TimedRobot {
DoublePublisher xPub;
DoublePublisher yPub;
public void robotInit() {
public Robot() {
// Get the default instance of NetworkTables that was created automatically
// when the robot program starts
NetworkTableInstance inst = NetworkTableInstance.getDefault();
Expand All @@ -29,6 +30,7 @@ The example robot program below publishes incrementing X and Y values to a table
xPub = table.getDoubleTopic("x").publish();
yPub = table.getDoubleTopic("y").publish();
}
double x = 0;
double y = 0;
public void teleopPeriodic() {
Expand All @@ -50,7 +52,7 @@ The example robot program below publishes incrementing X and Y values to a table
public:
nt::DoublePublisher xPub;
nt::DoublePublisher yPub;
void RobotInit() {
Robot() {
// Get the default instance of NetworkTables that was created automatically
// when the robot program starts
auto inst = nt::NetworkTableInstance::GetDefault();
Expand Down
Loading

0 comments on commit 18f593d

Please sign in to comment.