[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;
}