[wpilibc] Transition C++ classes to units::second_t (#3396)

A lot of these are breaking changes. frc::Timer was replaced with the
contents of frc2::Timer. The others were in-place argument changes or
removing deprecated non-unit overloads.
This commit is contained in:
Tyler Veness
2021-05-28 22:06:59 -07:00
committed by GitHub
parent 827b17a52b
commit e09293a15e
99 changed files with 503 additions and 790 deletions

View File

@@ -153,7 +153,7 @@ double PIDBase::GetSetpoint() const {
double PIDBase::GetDeltaSetpoint() const {
std::scoped_lock lock(m_thisMutex);
return (m_setpoint - m_prevSetpoint) / m_setpointTimer.Get();
return (m_setpoint - m_prevSetpoint) / m_setpointTimer.Get().to<double>();
}
double PIDBase::GetError() const {

View File

@@ -19,20 +19,21 @@ using namespace frc;
int Command::m_commandCounter = 0;
Command::Command() : Command("", -1.0) {}
Command::Command() : Command("", -1_s) {}
Command::Command(std::string_view name) : Command(name, -1.0) {}
Command::Command(std::string_view name) : Command(name, -1_s) {}
Command::Command(double timeout) : Command("", timeout) {}
Command::Command(units::second_t timeout) : Command("", timeout) {}
Command::Command(Subsystem& subsystem) : Command("", -1.0) {
Command::Command(Subsystem& subsystem) : Command("", -1_s) {
Requires(&subsystem);
}
Command::Command(std::string_view name, double timeout) {
Command::Command(std::string_view name, units::second_t timeout) {
// We use -1.0 to indicate no timeout.
if (timeout < 0.0 && timeout != -1.0) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0.0", timeout);
if (timeout < 0_s && timeout != -1_s) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0 s",
timeout.to<double>());
}
m_timeout = timeout;
@@ -47,22 +48,24 @@ Command::Command(std::string_view name, double timeout) {
}
Command::Command(std::string_view name, Subsystem& subsystem)
: Command(name, -1.0) {
: Command(name, -1_s) {
Requires(&subsystem);
}
Command::Command(double timeout, Subsystem& subsystem) : Command("", timeout) {
Command::Command(units::second_t timeout, Subsystem& subsystem)
: Command("", timeout) {
Requires(&subsystem);
}
Command::Command(std::string_view name, double timeout, Subsystem& subsystem)
Command::Command(std::string_view name, units::second_t timeout,
Subsystem& subsystem)
: Command(name, timeout) {
Requires(&subsystem);
}
double Command::TimeSinceInitialized() const {
if (m_startTime < 0.0) {
return 0.0;
units::second_t Command::TimeSinceInitialized() const {
if (m_startTime < 0_s) {
return 0_s;
} else {
return Timer::GetFPGATimestamp() - m_startTime;
}
@@ -170,16 +173,17 @@ int Command::GetID() const {
return m_commandID;
}
void Command::SetTimeout(double timeout) {
if (timeout < 0.0) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0.0", timeout);
void Command::SetTimeout(units::second_t timeout) {
if (timeout < 0_s) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0 s",
timeout.to<double>());
} else {
m_timeout = timeout;
}
}
bool Command::IsTimedOut() const {
return m_timeout != -1 && TimeSinceInitialized() >= m_timeout;
return m_timeout != -1_s && TimeSinceInitialized() >= m_timeout;
}
bool Command::AssertUnlocked(std::string_view message) {
@@ -264,7 +268,7 @@ void Command::Removed() {
void Command::StartRunning() {
m_running = true;
m_startTime = -1;
m_startTime = -1_s;
m_completed = false;
}

View File

@@ -29,15 +29,16 @@ void CommandGroup::AddSequential(Command* command) {
}
}
void CommandGroup::AddSequential(Command* command, double timeout) {
void CommandGroup::AddSequential(Command* command, units::second_t timeout) {
if (!command) {
throw FRC_MakeError(err::NullParameter, "{}", "command");
}
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
}
if (timeout < 0.0) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0.0", timeout);
if (timeout < 0_s) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0 s",
timeout.to<double>());
}
m_commands.emplace_back(command, CommandGroupEntry::kSequence_InSequence,
@@ -72,15 +73,16 @@ void CommandGroup::AddParallel(Command* command) {
}
}
void CommandGroup::AddParallel(Command* command, double timeout) {
void CommandGroup::AddParallel(Command* command, units::second_t timeout) {
if (!command) {
throw FRC_MakeError(err::NullParameter, "{}", "command");
}
if (!AssertUnlocked("Cannot add new command to command group")) {
return;
}
if (timeout < 0.0) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0.0", timeout);
if (timeout < 0_s) {
throw FRC_MakeError(err::ParameterOutOfRange, "timeout {} < 0 s",
timeout.to<double>());
}
m_commands.emplace_back(command, CommandGroupEntry::kSequence_BranchChild,

View File

@@ -9,15 +9,15 @@
using namespace frc;
CommandGroupEntry::CommandGroupEntry(Command* command, Sequence state,
double timeout)
units::second_t timeout)
: m_timeout(timeout), m_command(command), m_state(state) {}
bool CommandGroupEntry::IsTimedOut() const {
if (m_timeout < 0.0) {
if (m_timeout < 0_s) {
return false;
}
double time = m_command->TimeSinceInitialized();
if (time == 0.0) {
auto time = m_command->TimeSinceInitialized();
if (time == 0_s) {
return false;
}
return time >= m_timeout;

View File

@@ -6,16 +6,16 @@
using namespace frc;
TimedCommand::TimedCommand(std::string_view name, double timeout)
TimedCommand::TimedCommand(std::string_view name, units::second_t timeout)
: Command(name, timeout) {}
TimedCommand::TimedCommand(double timeout) : Command(timeout) {}
TimedCommand::TimedCommand(units::second_t timeout) : Command(timeout) {}
TimedCommand::TimedCommand(std::string_view name, double timeout,
TimedCommand::TimedCommand(std::string_view name, units::second_t timeout,
Subsystem& subsystem)
: Command(name, timeout, subsystem) {}
TimedCommand::TimedCommand(double timeout, Subsystem& subsystem)
TimedCommand::TimedCommand(units::second_t timeout, Subsystem& subsystem)
: Command(timeout, subsystem) {}
bool TimedCommand::IsFinished() {

View File

@@ -8,8 +8,8 @@
using namespace frc;
WaitCommand::WaitCommand(double timeout)
: TimedCommand(fmt::format("Wait({})", timeout), timeout) {}
WaitCommand::WaitCommand(units::second_t timeout)
: TimedCommand(fmt::format("Wait({})", timeout.to<double>()), timeout) {}
WaitCommand::WaitCommand(std::string_view name, double timeout)
WaitCommand::WaitCommand(std::string_view name, units::second_t timeout)
: TimedCommand(name, timeout) {}

View File

@@ -8,10 +8,10 @@
using namespace frc;
WaitForChildren::WaitForChildren(double timeout)
WaitForChildren::WaitForChildren(units::second_t timeout)
: Command("WaitForChildren", timeout) {}
WaitForChildren::WaitForChildren(std::string_view name, double timeout)
WaitForChildren::WaitForChildren(std::string_view name, units::second_t timeout)
: Command(name, timeout) {}
bool WaitForChildren::IsFinished() {

View File

@@ -8,12 +8,12 @@
using namespace frc;
WaitUntilCommand::WaitUntilCommand(double time)
WaitUntilCommand::WaitUntilCommand(units::second_t time)
: Command("WaitUntilCommand", time) {
m_time = time;
}
WaitUntilCommand::WaitUntilCommand(std::string_view name, double time)
WaitUntilCommand::WaitUntilCommand(std::string_view name, units::second_t time)
: Command(name, time) {
m_time = time;
}

View File

@@ -8,6 +8,7 @@
#include <string>
#include <string_view>
#include <units/time.h>
#include <wpi/SmallPtrSet.h>
#include "frc/commands/Subsystem.h"
@@ -64,10 +65,10 @@ class Command : public Sendable, public SendableHelper<Command> {
/**
* Creates a new command with the given timeout and a default name.
*
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
* @see IsTimedOut()
*/
explicit Command(double timeout);
explicit Command(units::second_t timeout);
/**
* Creates a new command with the given timeout and a default name.
@@ -80,10 +81,10 @@ class Command : public Sendable, public SendableHelper<Command> {
* Creates a new command with the given name and timeout.
*
* @param name the name of the command
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
* @see IsTimedOut()
*/
Command(std::string_view name, double timeout);
Command(std::string_view name, units::second_t timeout);
/**
* Creates a new command with the given name and timeout.
@@ -96,19 +97,19 @@ class Command : public Sendable, public SendableHelper<Command> {
/**
* Creates a new command with the given name and timeout.
*
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
* @param subsystem the subsystem that the command requires @see IsTimedOut()
*/
Command(double timeout, Subsystem& subsystem);
Command(units::second_t timeout, Subsystem& subsystem);
/**
* Creates a new command with the given name and timeout.
*
* @param name the name of the command
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
* @param subsystem the subsystem that the command requires @see IsTimedOut()
*/
Command(std::string_view name, double timeout, Subsystem& subsystem);
Command(std::string_view name, units::second_t timeout, Subsystem& subsystem);
~Command() override = default;
@@ -116,13 +117,13 @@ class Command : public Sendable, public SendableHelper<Command> {
Command& operator=(Command&&) = default;
/**
* Returns the time since this command was initialized (in seconds).
* Returns the time since this command was initialized.
*
* This function will work even if there is no specified timeout.
*
* @return the time since this command was initialized (in seconds).
* @return the time since this command was initialized.
*/
double TimeSinceInitialized() const;
units::second_t TimeSinceInitialized() const;
/**
* This method specifies that the given Subsystem is used by this command.
@@ -271,10 +272,10 @@ class Command : public Sendable, public SendableHelper<Command> {
/**
* Sets the timeout of this command.
*
* @param timeout the timeout (in seconds)
* @param timeout the timeout
* @see IsTimedOut()
*/
void SetTimeout(double timeout);
void SetTimeout(units::second_t timeout);
/**
* Returns whether or not the TimeSinceInitialized() method returns a number
@@ -445,10 +446,10 @@ class Command : public Sendable, public SendableHelper<Command> {
void StartTiming();
// The time since this command was initialized
double m_startTime = -1;
units::second_t m_startTime = -1_s;
// The time (in seconds) before this command "times out" (-1 if no timeout)
double m_timeout;
units::second_t m_timeout;
// Whether or not this command has been initialized
bool m_initialized = false;

View File

@@ -7,6 +7,8 @@
#include <string_view>
#include <vector>
#include <units/time.h>
#include "frc/commands/Command.h"
#include "frc/commands/CommandGroupEntry.h"
@@ -74,9 +76,9 @@ class CommandGroup : public Command {
* It is recommended that this method be called in the constructor.
*
* @param command The Command to be added
* @param timeout The timeout (in seconds)
* @param timeout The timeout
*/
void AddSequential(Command* command, double timeout);
void AddSequential(Command* command, units::second_t timeout);
/**
* Adds a new child Command to the group. The Command will be started after
@@ -121,9 +123,9 @@ class CommandGroup : public Command {
* It is recommended that this method be called in the constructor.
*
* @param command The command to be added
* @param timeout The timeout (in seconds)
* @param timeout The timeout
*/
void AddParallel(Command* command, double timeout);
void AddParallel(Command* command, units::second_t timeout);
bool IsInterruptible() const;

View File

@@ -4,6 +4,8 @@
#pragma once
#include <units/time.h>
namespace frc {
class Command;
@@ -17,14 +19,15 @@ class CommandGroupEntry {
};
CommandGroupEntry() = default;
CommandGroupEntry(Command* command, Sequence state, double timeout = -1.0);
CommandGroupEntry(Command* command, Sequence state,
units::second_t timeout = -1_s);
CommandGroupEntry(CommandGroupEntry&&) = default;
CommandGroupEntry& operator=(CommandGroupEntry&&) = default;
bool IsTimedOut() const;
double m_timeout = -1.0;
units::second_t m_timeout = -1_s;
Command* m_command = nullptr;
Sequence m_state = kSequence_InSequence;
};

View File

@@ -6,6 +6,8 @@
#include <string_view>
#include <units/time.h>
#include "frc/commands/Command.h"
namespace frc {
@@ -21,33 +23,34 @@ class TimedCommand : public Command {
* Creates a new TimedCommand with the given name and timeout.
*
* @param name the name of the command
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
*/
TimedCommand(std::string_view name, double timeout);
TimedCommand(std::string_view name, units::second_t timeout);
/**
* Creates a new WaitCommand with the given timeout.
*
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
*/
explicit TimedCommand(double timeout);
explicit TimedCommand(units::second_t timeout);
/**
* Creates a new TimedCommand with the given name and timeout.
*
* @param name the name of the command
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
* @param subsystem the subsystem that the command requires
*/
TimedCommand(std::string_view name, double timeout, Subsystem& subsystem);
TimedCommand(std::string_view name, units::second_t timeout,
Subsystem& subsystem);
/**
* Creates a new WaitCommand with the given timeout.
*
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
* @param subsystem the subsystem that the command requires
*/
TimedCommand(double timeout, Subsystem& subsystem);
TimedCommand(units::second_t timeout, Subsystem& subsystem);
~TimedCommand() override = default;

View File

@@ -6,6 +6,8 @@
#include <string_view>
#include <units/time.h>
#include "frc/commands/TimedCommand.h"
namespace frc {
@@ -16,16 +18,16 @@ class WaitCommand : public TimedCommand {
* Creates a new WaitCommand with the given name and timeout.
*
* @param name the name of the command
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
*/
explicit WaitCommand(double timeout);
explicit WaitCommand(units::second_t timeout);
/**
* Creates a new WaitCommand with the given timeout.
*
* @param timeout the time (in seconds) before this command "times out"
* @param timeout the time before this command "times out"
*/
WaitCommand(std::string_view name, double timeout);
WaitCommand(std::string_view name, units::second_t timeout);
~WaitCommand() override = default;

View File

@@ -6,14 +6,16 @@
#include <string_view>
#include <units/time.h>
#include "frc/commands/Command.h"
namespace frc {
class WaitForChildren : public Command {
public:
explicit WaitForChildren(double timeout);
WaitForChildren(std::string_view name, double timeout);
explicit WaitForChildren(units::second_t timeout);
WaitForChildren(std::string_view name, units::second_t timeout);
~WaitForChildren() override = default;
WaitForChildren(WaitForChildren&&) = default;

View File

@@ -6,6 +6,8 @@
#include <string_view>
#include <units/time.h>
#include "frc/commands/Command.h"
namespace frc {
@@ -20,9 +22,9 @@ class WaitUntilCommand : public Command {
*
* @see CommandGroup
*/
explicit WaitUntilCommand(double time);
explicit WaitUntilCommand(units::second_t time);
WaitUntilCommand(std::string_view name, double time);
WaitUntilCommand(std::string_view name, units::second_t time);
~WaitUntilCommand() override = default;
@@ -36,7 +38,7 @@ class WaitUntilCommand : public Command {
bool IsFinished() override;
private:
double m_time;
units::second_t m_time;
};
} // namespace frc