mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
Change C++ WaitCommand to use units (#1865)
Also remove unique_ptr usage.
This commit is contained in:
@@ -28,9 +28,9 @@ void Command::Initialize() {}
|
||||
void Command::Execute() {}
|
||||
void Command::End(bool interrupted) {}
|
||||
|
||||
ParallelRaceGroup Command::WithTimeout(double seconds) && {
|
||||
ParallelRaceGroup Command::WithTimeout(units::second_t duration) && {
|
||||
std::vector<std::unique_ptr<Command>> temp;
|
||||
temp.emplace_back(std::make_unique<WaitCommand>(seconds));
|
||||
temp.emplace_back(std::make_unique<WaitCommand>(duration));
|
||||
temp.emplace_back(std::move(*this).TransferOwnership());
|
||||
return ParallelRaceGroup(std::move(temp));
|
||||
}
|
||||
|
||||
@@ -9,19 +9,18 @@
|
||||
|
||||
using namespace frc2;
|
||||
|
||||
WaitCommand::WaitCommand(double seconds) : m_duration{seconds} {
|
||||
auto secondsStr = std::to_string(seconds);
|
||||
SetName(wpi::Twine(m_name) + ": " + wpi::Twine(secondsStr) + " seconds");
|
||||
m_timer = std::make_unique<frc::Timer>();
|
||||
WaitCommand::WaitCommand(units::second_t duration) : m_duration{duration} {
|
||||
auto durationStr = std::to_string(duration.to<double>());
|
||||
SetName(wpi::Twine(m_name) + ": " + wpi::Twine(durationStr) + " seconds");
|
||||
}
|
||||
|
||||
void WaitCommand::Initialize() {
|
||||
m_timer->Reset();
|
||||
m_timer->Start();
|
||||
m_timer.Reset();
|
||||
m_timer.Start();
|
||||
}
|
||||
|
||||
void WaitCommand::End(bool interrupted) { m_timer->Stop(); }
|
||||
void WaitCommand::End(bool interrupted) { m_timer.Stop(); }
|
||||
|
||||
bool WaitCommand::IsFinished() { return m_timer->HasPeriodPassed(m_duration); }
|
||||
bool WaitCommand::IsFinished() { return m_timer.HasPeriodPassed(m_duration); }
|
||||
|
||||
bool WaitCommand::RunsWhenDisabled() const { return true; }
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <units/units.h>
|
||||
#include <wpi/ArrayRef.h>
|
||||
#include <wpi/Demangle.h>
|
||||
#include <wpi/SmallSet.h>
|
||||
@@ -103,10 +104,10 @@ class Command : public frc::ErrorBase {
|
||||
* interrupted and un-scheduled. Note that the timeout only applies to the
|
||||
* command returned by this method; the calling command is not itself changed.
|
||||
*
|
||||
* @param seconds the timeout duration
|
||||
* @param duration the timeout duration
|
||||
* @return the command with the timeout added
|
||||
*/
|
||||
ParallelRaceGroup WithTimeout(double seconds) &&;
|
||||
ParallelRaceGroup WithTimeout(units::second_t duration) &&;
|
||||
|
||||
/**
|
||||
* Decorates this command with an interrupt condition. If the specified
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <units/units.h>
|
||||
#include <wpi/Twine.h>
|
||||
|
||||
#include "CommandBase.h"
|
||||
@@ -27,9 +26,9 @@ class WaitCommand : public CommandHelper<CommandBase, WaitCommand> {
|
||||
* Creates a new WaitCommand. This command will do nothing, and end after the
|
||||
* specified duration.
|
||||
*
|
||||
* @param seconds the time to wait, in seconds
|
||||
* @param duration the time to wait
|
||||
*/
|
||||
explicit WaitCommand(double seconds);
|
||||
explicit WaitCommand(units::second_t duration);
|
||||
|
||||
WaitCommand(WaitCommand&& other) = default;
|
||||
|
||||
@@ -44,9 +43,9 @@ class WaitCommand : public CommandHelper<CommandBase, WaitCommand> {
|
||||
bool RunsWhenDisabled() const override;
|
||||
|
||||
protected:
|
||||
std::unique_ptr<frc::Timer> m_timer;
|
||||
frc::Timer m_timer;
|
||||
|
||||
private:
|
||||
double m_duration;
|
||||
units::second_t m_duration;
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
Reference in New Issue
Block a user