2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2025-11-08 15:08:38 -08:00
|
|
|
#include "wpi/framework/TimedRobot.hpp"
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
#include <stdint.h>
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2021-08-05 23:54:50 -07:00
|
|
|
#include <cstdio>
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
[cmd3] Enforce command lifetimes across all opmode and command scopes (#8705)
Commands are no longer able to outlive their schedule-site's scope,
regardless of how they were scheduled (set as a default command, bound
to a trigger, or manually scheduled)
As a consequence, default commands need better tracking so the default
command setting can be released when their scope exits and the next-most
appropriate default command can be rescheduled (eg, an opmode sets a
default command, then the globally-scoped default is restored when the
opmode exits). Some complexity is required here to make it work well for
edge cases.
Like `schedule()`, `setDefaultCommand()` will immediately start the new
default command if called inside of another command to avoid 1-loop
delays. However, this does not apply when called by the _current_
default command, as it would result in attempting to cancel the default
command while it's mounted (which is impossible and would throw an
exception)
```java
class Robot extends OpModeRobot {
final Drive drive = new Drive();
final CommandXboxController controller = new CommandXboxController(1);
public Robot() {
// global default command, active unless overridden in an opmode or command
drive.setDefaultCommand(drive.stop());
// global trigger binding, always active
controller.rightBumper().onTrue(drive.setX());
}
}
@Teleop
class ExampleOpMode extends PeriodicOpMode {
public ExampleOpMode(Robot robot) {
// opmode-specific default command
robot.drive.setDefaultCommand(robot.drive.operatorControl(robot.controller));
// opmode-specific binding
robot.controller.leftBumper().whileTrue(robot.drive.stop());
// opmode-specific binding that takes precedence over the global binding
// because it happens last; it "wins out" over the `setX()` binding
robot.controller.rightBumper().onTrue(robot.drive.selfTest());
}
@Override
public void periodic() {
Scheduler.getDefault().run();
}
}
```
2026-04-09 20:05:42 -04:00
|
|
|
#include "wpi/driverstation/DriverStation.hpp"
|
2026-04-10 16:40:17 -04:00
|
|
|
#include "wpi/hal/DriverStation.hpp"
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "wpi/hal/UsageReporting.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/system/Errors.hpp"
|
2026-04-10 16:40:17 -04:00
|
|
|
#include "wpi/system/RobotController.hpp"
|
2018-04-30 00:00:09 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi;
|
2017-07-08 10:50:56 -04:00
|
|
|
|
|
|
|
|
void TimedRobot::StartCompetition() {
|
2020-10-03 22:26:19 -07:00
|
|
|
if constexpr (IsSimulation()) {
|
2020-02-19 02:05:16 -05:00
|
|
|
SimulationInit();
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 23:57:26 -07:00
|
|
|
// Tell the DS that the robot is ready to be enabled
|
2021-08-05 23:54:50 -07:00
|
|
|
std::puts("\n********** Robot program startup complete **********");
|
[cmd3] Enforce command lifetimes across all opmode and command scopes (#8705)
Commands are no longer able to outlive their schedule-site's scope,
regardless of how they were scheduled (set as a default command, bound
to a trigger, or manually scheduled)
As a consequence, default commands need better tracking so the default
command setting can be released when their scope exits and the next-most
appropriate default command can be rescheduled (eg, an opmode sets a
default command, then the globally-scoped default is restored when the
opmode exits). Some complexity is required here to make it work well for
edge cases.
Like `schedule()`, `setDefaultCommand()` will immediately start the new
default command if called inside of another command to avoid 1-loop
delays. However, this does not apply when called by the _current_
default command, as it would result in attempting to cancel the default
command while it's mounted (which is impossible and would throw an
exception)
```java
class Robot extends OpModeRobot {
final Drive drive = new Drive();
final CommandXboxController controller = new CommandXboxController(1);
public Robot() {
// global default command, active unless overridden in an opmode or command
drive.setDefaultCommand(drive.stop());
// global trigger binding, always active
controller.rightBumper().onTrue(drive.setX());
}
}
@Teleop
class ExampleOpMode extends PeriodicOpMode {
public ExampleOpMode(Robot robot) {
// opmode-specific default command
robot.drive.setDefaultCommand(robot.drive.operatorControl(robot.controller));
// opmode-specific binding
robot.controller.leftBumper().whileTrue(robot.drive.stop());
// opmode-specific binding that takes precedence over the global binding
// because it happens last; it "wins out" over the `setX()` binding
robot.controller.rightBumper().onTrue(robot.drive.selfTest());
}
@Override
public void periodic() {
Scheduler.getDefault().run();
}
}
```
2026-04-09 20:05:42 -04:00
|
|
|
DriverStation::ObserveUserProgramStarting();
|
2017-09-05 23:57:26 -07:00
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
// Loop forever, calling the appropriate mode-dependent function
|
2017-07-08 10:50:56 -04:00
|
|
|
while (true) {
|
2026-04-10 16:40:17 -04:00
|
|
|
if (!m_callbacks.RunCallbacks(m_notifier)) {
|
2020-12-28 12:58:06 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 21:33:09 -08:00
|
|
|
void TimedRobot::EndCompetition() {
|
2025-11-29 11:00:18 -08:00
|
|
|
HAL_DestroyNotifier(m_notifier);
|
2026-03-21 00:34:46 -07:00
|
|
|
m_notifier = HAL_INVALID_HANDLE;
|
2019-11-05 21:33:09 -08:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
TimedRobot::TimedRobot(wpi::units::second_t period)
|
|
|
|
|
: IterativeRobotBase(period) {
|
2026-03-15 15:08:41 -07:00
|
|
|
m_startTime = std::chrono::microseconds{RobotController::GetMonotonicTime()};
|
2022-10-15 16:33:14 -07:00
|
|
|
AddPeriodic([=, this] { LoopFunc(); }, period);
|
2020-10-16 17:56:37 -07:00
|
|
|
|
2019-08-17 00:56:48 -04:00
|
|
|
int32_t status = 0;
|
2025-11-29 11:00:18 -08:00
|
|
|
m_notifier = HAL_CreateNotifier(&status);
|
2025-11-07 20:00:43 -05:00
|
|
|
WPILIB_CheckErrorStatus(status, "InitializeNotifier");
|
2019-11-09 11:41:58 -08:00
|
|
|
HAL_SetNotifierName(m_notifier, "TimedRobot", &status);
|
2017-07-08 10:50:56 -04:00
|
|
|
|
2025-02-07 12:37:23 -08:00
|
|
|
HAL_ReportUsage("Framework", "TimedRobot");
|
2017-07-08 10:50:56 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
TimedRobot::TimedRobot(wpi::units::hertz_t frequency)
|
|
|
|
|
: TimedRobot{1 / frequency} {}
|
2025-10-29 03:18:55 +00:00
|
|
|
|
2018-04-30 00:00:09 -07:00
|
|
|
TimedRobot::~TimedRobot() {
|
2026-03-21 00:34:46 -07:00
|
|
|
if (m_notifier != HAL_INVALID_HANDLE) {
|
2025-11-29 11:00:18 -08:00
|
|
|
HAL_DestroyNotifier(m_notifier);
|
2024-10-15 22:56:13 -04:00
|
|
|
}
|
2018-04-30 00:00:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-10-16 17:56:37 -07:00
|
|
|
void TimedRobot::AddPeriodic(std::function<void()> callback,
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::units::second_t period,
|
|
|
|
|
wpi::units::second_t offset) {
|
2026-04-10 16:40:17 -04:00
|
|
|
m_callbacks.Add(std::move(callback), m_startTime, period, offset);
|
2018-04-30 00:00:09 -07:00
|
|
|
}
|