mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
[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:
@@ -13,8 +13,8 @@
|
||||
|
||||
using namespace frc;
|
||||
|
||||
static constexpr auto kSamplePeriod = 0.0005_s;
|
||||
static constexpr double kCalibrationSampleTime = 5.0;
|
||||
static constexpr auto kSamplePeriod = 0.5_ms;
|
||||
static constexpr auto kCalibrationSampleTime = 5_s;
|
||||
static constexpr double kDegreePerSecondPerLSB = 0.0125;
|
||||
|
||||
// static constexpr int kRateRegister = 0x00;
|
||||
@@ -121,7 +121,7 @@ void ADXRS450_Gyro::Reset() {
|
||||
}
|
||||
|
||||
void ADXRS450_Gyro::Calibrate() {
|
||||
Wait(0.1);
|
||||
Wait(100_ms);
|
||||
|
||||
m_spi.SetAccumulatorIntegratedCenter(0);
|
||||
m_spi.ResetAccumulator();
|
||||
|
||||
@@ -141,7 +141,7 @@ void AnalogInput::ResetAccumulator() {
|
||||
const double sampleTime = 1.0 / GetSampleRate();
|
||||
const double overSamples = 1 << GetOversampleBits();
|
||||
const double averageSamples = 1 << GetAverageBits();
|
||||
Wait(sampleTime * overSamples * averageSamples);
|
||||
Wait(units::second_t{sampleTime * overSamples * averageSamples});
|
||||
}
|
||||
|
||||
void AnalogInput::SetAccumulatorCenter(int center) {
|
||||
|
||||
@@ -24,7 +24,7 @@ Counter::Counter(Mode mode) {
|
||||
&m_index, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "InitializeCounter");
|
||||
|
||||
SetMaxPeriod(0.5);
|
||||
SetMaxPeriod(0.5_s);
|
||||
|
||||
HAL_Report(HALUsageReporting::kResourceType_Counter, m_index + 1, mode + 1);
|
||||
SendableRegistry::GetInstance().AddLW(this, "Counter", m_index);
|
||||
@@ -273,16 +273,16 @@ void Counter::Reset() {
|
||||
FRC_CheckErrorStatus(status, "{}", "Reset");
|
||||
}
|
||||
|
||||
double Counter::GetPeriod() const {
|
||||
units::second_t Counter::GetPeriod() const {
|
||||
int32_t status = 0;
|
||||
double value = HAL_GetCounterPeriod(m_counter, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "GetPeriod");
|
||||
return value;
|
||||
return units::second_t{value};
|
||||
}
|
||||
|
||||
void Counter::SetMaxPeriod(double maxPeriod) {
|
||||
void Counter::SetMaxPeriod(units::second_t maxPeriod) {
|
||||
int32_t status = 0;
|
||||
HAL_SetCounterMaxPeriod(m_counter, maxPeriod, &status);
|
||||
HAL_SetCounterMaxPeriod(m_counter, maxPeriod.to<double>(), &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetMaxPeriod");
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ class MatchDataSender {
|
||||
|
||||
using namespace frc;
|
||||
|
||||
static constexpr double kJoystickUnpluggedMessageInterval = 1.0;
|
||||
static constexpr auto kJoystickUnpluggedMessageInterval = 1_s;
|
||||
|
||||
static int& GetDSLastCount() {
|
||||
// There is a rollover error condition here. At Packet# = n * (uintmax), this
|
||||
@@ -485,12 +485,12 @@ int DriverStation::GetLocation() const {
|
||||
}
|
||||
|
||||
void DriverStation::WaitForData() {
|
||||
WaitForData(0);
|
||||
WaitForData(0_s);
|
||||
}
|
||||
|
||||
bool DriverStation::WaitForData(double timeout) {
|
||||
auto timeoutTime =
|
||||
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
|
||||
bool DriverStation::WaitForData(units::second_t timeout) {
|
||||
auto timeoutTime = std::chrono::steady_clock::now() +
|
||||
std::chrono::steady_clock::duration{timeout};
|
||||
|
||||
std::unique_lock lock(m_waitForDataMutex);
|
||||
int& lastCount = GetDSLastCount();
|
||||
@@ -500,7 +500,7 @@ bool DriverStation::WaitForData(double timeout) {
|
||||
return true;
|
||||
}
|
||||
while (m_waitForDataCounter == currentCount) {
|
||||
if (timeout > 0) {
|
||||
if (timeout > 0_s) {
|
||||
auto timedOut = m_waitForDataCond.wait_until(lock, timeoutTime);
|
||||
if (timedOut == std::cv_status::timeout) {
|
||||
return false;
|
||||
@@ -586,7 +586,7 @@ DriverStation::DriverStation() {
|
||||
|
||||
void DriverStation::ReportJoystickUnpluggedErrorV(fmt::string_view format,
|
||||
fmt::format_args args) {
|
||||
double currentTime = Timer::GetFPGATimestamp();
|
||||
auto currentTime = Timer::GetFPGATimestamp();
|
||||
if (currentTime > m_nextMessageTime) {
|
||||
ReportErrorV(err::Error, "", 0, "", format, args);
|
||||
m_nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval;
|
||||
@@ -596,7 +596,7 @@ void DriverStation::ReportJoystickUnpluggedErrorV(fmt::string_view format,
|
||||
void DriverStation::ReportJoystickUnpluggedWarningV(fmt::string_view format,
|
||||
fmt::format_args args) {
|
||||
if (IsFMSAttached() || !m_silenceJoystickWarning) {
|
||||
double currentTime = Timer::GetFPGATimestamp();
|
||||
auto currentTime = Timer::GetFPGATimestamp();
|
||||
if (currentTime > m_nextMessageTime) {
|
||||
ReportErrorV(warn::Warning, "", 0, "", format, args);
|
||||
m_nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval;
|
||||
|
||||
@@ -79,16 +79,16 @@ void Encoder::Reset() {
|
||||
FRC_CheckErrorStatus(status, "{}", "Reset");
|
||||
}
|
||||
|
||||
double Encoder::GetPeriod() const {
|
||||
units::second_t Encoder::GetPeriod() const {
|
||||
int32_t status = 0;
|
||||
double value = HAL_GetEncoderPeriod(m_encoder, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "GetPeriod");
|
||||
return value;
|
||||
return units::second_t{value};
|
||||
}
|
||||
|
||||
void Encoder::SetMaxPeriod(double maxPeriod) {
|
||||
void Encoder::SetMaxPeriod(units::second_t maxPeriod) {
|
||||
int32_t status = 0;
|
||||
HAL_SetEncoderMaxPeriod(m_encoder, maxPeriod, &status);
|
||||
HAL_SetEncoderMaxPeriod(m_encoder, maxPeriod.to<double>(), &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetMaxPeriod");
|
||||
}
|
||||
|
||||
|
||||
@@ -85,12 +85,13 @@ void InterruptableSensorBase::CancelInterrupts() {
|
||||
}
|
||||
|
||||
InterruptableSensorBase::WaitResult InterruptableSensorBase::WaitForInterrupt(
|
||||
double timeout, bool ignorePrevious) {
|
||||
units::second_t timeout, bool ignorePrevious) {
|
||||
FRC_Assert(m_interrupt != HAL_kInvalidHandle);
|
||||
int32_t status = 0;
|
||||
int result;
|
||||
|
||||
result = HAL_WaitForInterrupt(m_interrupt, timeout, ignorePrevious, &status);
|
||||
result = HAL_WaitForInterrupt(m_interrupt, timeout.to<double>(),
|
||||
ignorePrevious, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "WaitForInterrupt");
|
||||
|
||||
// Rising edge result is the interrupt bit set in the byte 0xFF
|
||||
@@ -116,20 +117,20 @@ void InterruptableSensorBase::DisableInterrupts() {
|
||||
FRC_CheckErrorStatus(status, "{}", "DisableInterrupts");
|
||||
}
|
||||
|
||||
double InterruptableSensorBase::ReadRisingTimestamp() {
|
||||
units::second_t InterruptableSensorBase::ReadRisingTimestamp() {
|
||||
FRC_Assert(m_interrupt != HAL_kInvalidHandle);
|
||||
int32_t status = 0;
|
||||
int64_t timestamp = HAL_ReadInterruptRisingTimestamp(m_interrupt, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "ReadRisingTimestamp");
|
||||
return timestamp * 1e-6;
|
||||
return units::microsecond_t(timestamp);
|
||||
}
|
||||
|
||||
double InterruptableSensorBase::ReadFallingTimestamp() {
|
||||
units::second_t InterruptableSensorBase::ReadFallingTimestamp() {
|
||||
FRC_Assert(m_interrupt != HAL_kInvalidHandle);
|
||||
int32_t status = 0;
|
||||
int64_t timestamp = HAL_ReadInterruptFallingTimestamp(m_interrupt, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "ReadFallingTimestamp");
|
||||
return timestamp * 1e-6;
|
||||
return units::microsecond_t(timestamp);
|
||||
}
|
||||
|
||||
void InterruptableSensorBase::SetUpSourceEdge(bool risingEdge,
|
||||
|
||||
@@ -47,12 +47,12 @@ void MotorSafety::Feed() {
|
||||
m_stopTime = Timer::GetFPGATimestamp() + m_expiration;
|
||||
}
|
||||
|
||||
void MotorSafety::SetExpiration(double expirationTime) {
|
||||
void MotorSafety::SetExpiration(units::second_t expirationTime) {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
m_expiration = expirationTime;
|
||||
}
|
||||
|
||||
double MotorSafety::GetExpiration() const {
|
||||
units::second_t MotorSafety::GetExpiration() const {
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
return m_expiration;
|
||||
}
|
||||
@@ -74,7 +74,7 @@ bool MotorSafety::IsSafetyEnabled() const {
|
||||
|
||||
void MotorSafety::Check() {
|
||||
bool enabled;
|
||||
double stopTime;
|
||||
units::second_t stopTime;
|
||||
|
||||
{
|
||||
std::scoped_lock lock(m_thisMutex);
|
||||
|
||||
@@ -151,26 +151,18 @@ void Notifier::SetHandler(std::function<void()> handler) {
|
||||
m_handler = handler;
|
||||
}
|
||||
|
||||
void Notifier::StartSingle(double delay) {
|
||||
StartSingle(units::second_t(delay));
|
||||
}
|
||||
|
||||
void Notifier::StartSingle(units::second_t delay) {
|
||||
std::scoped_lock lock(m_processMutex);
|
||||
m_periodic = false;
|
||||
m_period = delay.to<double>();
|
||||
m_period = delay;
|
||||
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
|
||||
UpdateAlarm();
|
||||
}
|
||||
|
||||
void Notifier::StartPeriodic(double period) {
|
||||
StartPeriodic(units::second_t(period));
|
||||
}
|
||||
|
||||
void Notifier::StartPeriodic(units::second_t period) {
|
||||
std::scoped_lock lock(m_processMutex);
|
||||
m_periodic = true;
|
||||
m_period = period.to<double>();
|
||||
m_period = period;
|
||||
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
|
||||
UpdateAlarm();
|
||||
}
|
||||
|
||||
@@ -307,10 +307,6 @@ int SPI::ReadAutoReceivedData(uint32_t* buffer, int numToRead,
|
||||
return val;
|
||||
}
|
||||
|
||||
int SPI::ReadAutoReceivedData(uint32_t* buffer, int numToRead, double timeout) {
|
||||
return ReadAutoReceivedData(buffer, numToRead, units::second_t(timeout));
|
||||
}
|
||||
|
||||
int SPI::GetAutoDroppedCount() {
|
||||
int32_t status = 0;
|
||||
int32_t val = HAL_GetSPIAutoDroppedCount(m_port, &status);
|
||||
|
||||
@@ -31,7 +31,7 @@ SerialPort::SerialPort(int baudRate, Port port, int dataBits,
|
||||
FRC_CheckErrorStatus(status, "SetSerialStopBits {}", stopBits);
|
||||
|
||||
// Set the default timeout to 5 seconds.
|
||||
SetTimeout(5.0);
|
||||
SetTimeout(5_s);
|
||||
|
||||
// Don't wait until the buffer is full to transmit.
|
||||
SetWriteBufferMode(kFlushOnAccess);
|
||||
@@ -61,7 +61,7 @@ SerialPort::SerialPort(int baudRate, std::string_view portName, Port port,
|
||||
FRC_CheckErrorStatus(status, "SetSerialStopBits {}", stopBits);
|
||||
|
||||
// Set the default timeout to 5 seconds.
|
||||
SetTimeout(5.0);
|
||||
SetTimeout(5_s);
|
||||
|
||||
// Don't wait until the buffer is full to transmit.
|
||||
SetWriteBufferMode(kFlushOnAccess);
|
||||
@@ -122,9 +122,9 @@ int SerialPort::Write(std::string_view buffer) {
|
||||
return retVal;
|
||||
}
|
||||
|
||||
void SerialPort::SetTimeout(double timeout) {
|
||||
void SerialPort::SetTimeout(units::second_t timeout) {
|
||||
int32_t status = 0;
|
||||
HAL_SetSerialTimeout(m_portHandle, timeout, &status);
|
||||
HAL_SetSerialTimeout(m_portHandle, timeout.to<double>(), &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetTimeout");
|
||||
}
|
||||
|
||||
|
||||
@@ -76,10 +76,10 @@ bool Solenoid::IsBlackListed() const {
|
||||
return (value != 0);
|
||||
}
|
||||
|
||||
void Solenoid::SetPulseDuration(double durationSeconds) {
|
||||
int32_t durationMS = durationSeconds * 1000;
|
||||
void Solenoid::SetPulseDuration(units::second_t duration) {
|
||||
int32_t status = 0;
|
||||
HAL_SetOneShotDuration(m_solenoidHandle, durationMS, &status);
|
||||
HAL_SetOneShotDuration(m_solenoidHandle,
|
||||
units::millisecond_t{duration}.to<int32_t>(), &status);
|
||||
FRC_CheckErrorStatus(status, "Module {} Channel {}", m_moduleNumber,
|
||||
m_channel);
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ void TimedRobot::EndCompetition() {
|
||||
TimedRobot::TimedRobot(double period) : TimedRobot(units::second_t(period)) {}
|
||||
|
||||
TimedRobot::TimedRobot(units::second_t period) : IterativeRobotBase(period) {
|
||||
m_startTime = frc2::Timer::GetFPGATimestamp();
|
||||
m_startTime = Timer::GetFPGATimestamp();
|
||||
AddPeriodic([=] { LoopFunc(); }, period);
|
||||
|
||||
int32_t status = 0;
|
||||
|
||||
@@ -4,16 +4,27 @@
|
||||
|
||||
#include "frc/Timer.h"
|
||||
|
||||
#include <units/time.h>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "frc/DriverStation.h"
|
||||
#include "frc/RobotController.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
void Wait(double seconds) {
|
||||
frc2::Wait(units::second_t(seconds));
|
||||
void Wait(units::second_t seconds) {
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::duration<double>(seconds.to<double>()));
|
||||
}
|
||||
|
||||
double GetTime() {
|
||||
return frc2::GetTime().to<double>();
|
||||
units::second_t GetTime() {
|
||||
using std::chrono::duration;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::system_clock;
|
||||
|
||||
return units::second_t(
|
||||
duration_cast<duration<double>>(system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
}
|
||||
|
||||
} // namespace frc
|
||||
@@ -24,30 +35,57 @@ Timer::Timer() {
|
||||
Reset();
|
||||
}
|
||||
|
||||
double Timer::Get() const {
|
||||
return m_timer.Get().to<double>();
|
||||
units::second_t Timer::Get() const {
|
||||
if (m_running) {
|
||||
return (GetFPGATimestamp() - m_startTime) + m_accumulatedTime;
|
||||
} else {
|
||||
return m_accumulatedTime;
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::Reset() {
|
||||
m_timer.Reset();
|
||||
m_accumulatedTime = 0_s;
|
||||
m_startTime = GetFPGATimestamp();
|
||||
}
|
||||
|
||||
void Timer::Start() {
|
||||
m_timer.Start();
|
||||
if (!m_running) {
|
||||
m_startTime = GetFPGATimestamp();
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::Stop() {
|
||||
m_timer.Stop();
|
||||
if (m_running) {
|
||||
m_accumulatedTime = Get();
|
||||
m_running = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Timer::HasPeriodPassed(double period) {
|
||||
return m_timer.HasPeriodPassed(units::second_t(period));
|
||||
bool Timer::HasElapsed(units::second_t period) const {
|
||||
return Get() > period;
|
||||
}
|
||||
|
||||
double Timer::GetFPGATimestamp() {
|
||||
return frc2::Timer::GetFPGATimestamp().to<double>();
|
||||
bool Timer::HasPeriodPassed(units::second_t period) {
|
||||
return AdvanceIfElapsed(period);
|
||||
}
|
||||
|
||||
double Timer::GetMatchTime() {
|
||||
return frc2::Timer::GetMatchTime().to<double>();
|
||||
bool Timer::AdvanceIfElapsed(units::second_t period) {
|
||||
if (Get() > period) {
|
||||
// Advance the start time by the period.
|
||||
m_startTime += period;
|
||||
// Don't set it to the current time... we want to avoid drift.
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
units::second_t Timer::GetFPGATimestamp() {
|
||||
// FPGA returns the timestamp in microseconds
|
||||
return units::second_t(frc::RobotController::GetFPGATime() * 1.0e-6);
|
||||
}
|
||||
|
||||
units::second_t Timer::GetMatchTime() {
|
||||
return units::second_t(frc::DriverStation::GetInstance().GetMatchTime());
|
||||
}
|
||||
|
||||
@@ -142,14 +142,16 @@ double Ultrasonic::GetRangeInches() const {
|
||||
if (m_simRange) {
|
||||
return m_simRange.Get();
|
||||
}
|
||||
return m_counter.GetPeriod() * kSpeedOfSoundInchesPerSec / 2.0;
|
||||
return units::inch_t{m_counter.GetPeriod() * kSpeedOfSound / 2.0}
|
||||
.to<double>();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
double Ultrasonic::GetRangeMM() const {
|
||||
return GetRangeInches() * 25.4;
|
||||
return units::millimeter_t{m_counter.GetPeriod() * kSpeedOfSound / 2.0}
|
||||
.to<double>();
|
||||
}
|
||||
|
||||
bool Ultrasonic::IsEnabled() const {
|
||||
@@ -180,7 +182,7 @@ void Ultrasonic::Initialize() {
|
||||
// Link this instance on the list
|
||||
m_sensors.emplace_back(this);
|
||||
|
||||
m_counter.SetMaxPeriod(1.0);
|
||||
m_counter.SetMaxPeriod(1_s);
|
||||
m_counter.SetSemiPeriodMode(true);
|
||||
m_counter.Reset();
|
||||
m_enabled = true; // Make it available for round robin scheduling
|
||||
@@ -204,7 +206,7 @@ void Ultrasonic::UltrasonicChecker() {
|
||||
sensor->m_pingChannel->Pulse(kPingTime); // do the ping
|
||||
}
|
||||
|
||||
Wait(0.1); // wait for ping to return
|
||||
Wait(100_ms); // wait for ping to return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
#include <wpi/priority_queue.h>
|
||||
|
||||
#include "frc/Errors.h"
|
||||
#include "frc2/Timer.h"
|
||||
#include "frc/Timer.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
@@ -131,9 +131,6 @@ void Watchdog::Impl::Main() {
|
||||
}
|
||||
}
|
||||
|
||||
Watchdog::Watchdog(double timeout, std::function<void()> callback)
|
||||
: Watchdog(units::second_t{timeout}, callback) {}
|
||||
|
||||
Watchdog::Watchdog(units::second_t timeout, std::function<void()> callback)
|
||||
: m_timeout(timeout), m_callback(std::move(callback)), m_impl(GetImpl()) {}
|
||||
|
||||
@@ -167,16 +164,12 @@ Watchdog& Watchdog::operator=(Watchdog&& rhs) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
double Watchdog::GetTime() const {
|
||||
return (frc2::Timer::GetFPGATimestamp() - m_startTime).to<double>();
|
||||
}
|
||||
|
||||
void Watchdog::SetTimeout(double timeout) {
|
||||
SetTimeout(units::second_t{timeout});
|
||||
units::second_t Watchdog::GetTime() const {
|
||||
return Timer::GetFPGATimestamp() - m_startTime;
|
||||
}
|
||||
|
||||
void Watchdog::SetTimeout(units::second_t timeout) {
|
||||
m_startTime = frc2::Timer::GetFPGATimestamp();
|
||||
m_startTime = Timer::GetFPGATimestamp();
|
||||
m_tracer.ClearEpochs();
|
||||
|
||||
std::scoped_lock lock(m_impl->m_mutex);
|
||||
@@ -189,9 +182,9 @@ void Watchdog::SetTimeout(units::second_t timeout) {
|
||||
m_impl->UpdateAlarm();
|
||||
}
|
||||
|
||||
double Watchdog::GetTimeout() const {
|
||||
units::second_t Watchdog::GetTimeout() const {
|
||||
std::scoped_lock lock(m_impl->m_mutex);
|
||||
return m_timeout.to<double>();
|
||||
return m_timeout;
|
||||
}
|
||||
|
||||
bool Watchdog::IsExpired() const {
|
||||
@@ -212,7 +205,7 @@ void Watchdog::Reset() {
|
||||
}
|
||||
|
||||
void Watchdog::Enable() {
|
||||
m_startTime = frc2::Timer::GetFPGATimestamp();
|
||||
m_startTime = Timer::GetFPGATimestamp();
|
||||
m_tracer.ClearEpochs();
|
||||
|
||||
std::scoped_lock lock(m_impl->m_mutex);
|
||||
|
||||
@@ -1,133 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#include "frc2/Timer.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "frc/DriverStation.h"
|
||||
#include "frc/RobotController.h"
|
||||
|
||||
namespace frc2 {
|
||||
|
||||
void Wait(units::second_t seconds) {
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::duration<double>(seconds.to<double>()));
|
||||
}
|
||||
|
||||
units::second_t GetTime() {
|
||||
using std::chrono::duration;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::system_clock;
|
||||
|
||||
return units::second_t(
|
||||
duration_cast<duration<double>>(system_clock::now().time_since_epoch())
|
||||
.count());
|
||||
}
|
||||
|
||||
} // namespace frc2
|
||||
|
||||
using namespace frc2;
|
||||
|
||||
Timer::Timer() {
|
||||
Reset();
|
||||
}
|
||||
|
||||
Timer::Timer(const Timer& rhs)
|
||||
: m_startTime(rhs.m_startTime),
|
||||
m_accumulatedTime(rhs.m_accumulatedTime),
|
||||
m_running(rhs.m_running) {}
|
||||
|
||||
Timer& Timer::operator=(const Timer& rhs) {
|
||||
std::scoped_lock lock(m_mutex, rhs.m_mutex);
|
||||
|
||||
m_startTime = rhs.m_startTime;
|
||||
m_accumulatedTime = rhs.m_accumulatedTime;
|
||||
m_running = rhs.m_running;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Timer::Timer(Timer&& rhs)
|
||||
: m_startTime(std::move(rhs.m_startTime)),
|
||||
m_accumulatedTime(std::move(rhs.m_accumulatedTime)),
|
||||
m_running(std::move(rhs.m_running)) {}
|
||||
|
||||
Timer& Timer::operator=(Timer&& rhs) {
|
||||
std::scoped_lock lock(m_mutex, rhs.m_mutex);
|
||||
|
||||
m_startTime = std::move(rhs.m_startTime);
|
||||
m_accumulatedTime = std::move(rhs.m_accumulatedTime);
|
||||
m_running = std::move(rhs.m_running);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
units::second_t Timer::Get() const {
|
||||
units::second_t result;
|
||||
units::second_t currentTime = GetFPGATimestamp();
|
||||
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (m_running) {
|
||||
result = (currentTime - m_startTime) + m_accumulatedTime;
|
||||
} else {
|
||||
result = m_accumulatedTime;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Timer::Reset() {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
m_accumulatedTime = 0_s;
|
||||
m_startTime = GetFPGATimestamp();
|
||||
}
|
||||
|
||||
void Timer::Start() {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (!m_running) {
|
||||
m_startTime = GetFPGATimestamp();
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Timer::Stop() {
|
||||
units::second_t temp = Get();
|
||||
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (m_running) {
|
||||
m_accumulatedTime = temp;
|
||||
m_running = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Timer::HasElapsed(units::second_t period) const {
|
||||
return Get() > period;
|
||||
}
|
||||
|
||||
bool Timer::HasPeriodPassed(units::second_t period) {
|
||||
return AdvanceIfElapsed(period);
|
||||
}
|
||||
|
||||
bool Timer::AdvanceIfElapsed(units::second_t period) {
|
||||
if (Get() > period) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
// Advance the start time by the period.
|
||||
m_startTime += period;
|
||||
// Don't set it to the current time... we want to avoid drift.
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
units::second_t Timer::GetFPGATimestamp() {
|
||||
// FPGA returns the timestamp in microseconds
|
||||
return units::second_t(frc::RobotController::GetFPGATime() * 1.0e-6);
|
||||
}
|
||||
|
||||
units::second_t Timer::GetMatchTime() {
|
||||
return units::second_t(frc::DriverStation::GetInstance().GetMatchTime());
|
||||
}
|
||||
@@ -17,7 +17,7 @@ NidecBrushless::NidecBrushless(int pwmChannel, int dioChannel)
|
||||
auto& registry = SendableRegistry::GetInstance();
|
||||
registry.AddChild(this, &m_dio);
|
||||
registry.AddChild(this, &m_pwm);
|
||||
SetExpiration(0.0);
|
||||
SetExpiration(0_s);
|
||||
SetSafetyEnabled(false);
|
||||
|
||||
// the dio controls the output (in PWM mode)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <units/time.h>
|
||||
|
||||
#include "frc/AnalogTrigger.h"
|
||||
#include "frc/CounterBase.h"
|
||||
@@ -368,7 +369,7 @@ class Counter : public CounterBase,
|
||||
*
|
||||
* @returns The period between the last two pulses in units of seconds.
|
||||
*/
|
||||
double GetPeriod() const override;
|
||||
units::second_t GetPeriod() const override;
|
||||
|
||||
/**
|
||||
* Set the maximum period where the device is still considered "moving".
|
||||
@@ -380,7 +381,7 @@ class Counter : public CounterBase,
|
||||
* @param maxPeriod The maximum period where the counted device is considered
|
||||
* moving in seconds.
|
||||
*/
|
||||
void SetMaxPeriod(double maxPeriod) final;
|
||||
void SetMaxPeriod(units::second_t maxPeriod) final;
|
||||
|
||||
/**
|
||||
* Select whether you want to continue updating the event timer output when
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/time.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
/**
|
||||
@@ -27,8 +29,8 @@ class CounterBase {
|
||||
|
||||
virtual int Get() const = 0;
|
||||
virtual void Reset() = 0;
|
||||
virtual double GetPeriod() const = 0;
|
||||
virtual void SetMaxPeriod(double maxPeriod) = 0;
|
||||
virtual units::second_t GetPeriod() const = 0;
|
||||
virtual void SetMaxPeriod(units::second_t maxPeriod) = 0;
|
||||
virtual bool GetStopped() const = 0;
|
||||
virtual bool GetDirection() const = 0;
|
||||
};
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <hal/DriverStationTypes.h>
|
||||
#include <units/time.h>
|
||||
#include <wpi/condition_variable.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
@@ -334,11 +335,11 @@ class DriverStation {
|
||||
* This is a good way to delay processing until there is new driver station
|
||||
* data to act on.
|
||||
*
|
||||
* @param timeout Timeout time in seconds
|
||||
* @param timeout Timeout
|
||||
*
|
||||
* @return true if new data, otherwise false
|
||||
*/
|
||||
bool WaitForData(double timeout);
|
||||
bool WaitForData(units::second_t timeout);
|
||||
|
||||
/**
|
||||
* Return the approximate match time.
|
||||
@@ -494,7 +495,7 @@ class DriverStation {
|
||||
bool m_userInTeleop = false;
|
||||
bool m_userInTest = false;
|
||||
|
||||
double m_nextMessageTime = 0;
|
||||
units::second_t m_nextMessageTime = 0_s;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -167,7 +167,7 @@ class Encoder : public CounterBase,
|
||||
*
|
||||
* @return Period in seconds of the most recent pulse.
|
||||
*/
|
||||
double GetPeriod() const override;
|
||||
units::second_t GetPeriod() const override;
|
||||
|
||||
/**
|
||||
* Sets the maximum period for stopped detection.
|
||||
@@ -188,7 +188,7 @@ class Encoder : public CounterBase,
|
||||
WPI_DEPRECATED(
|
||||
"Use SetMinRate() in favor of this method. This takes unscaled periods "
|
||||
"and SetMinRate() scales using value from SetDistancePerPulse().")
|
||||
void SetMaxPeriod(double maxPeriod) override;
|
||||
void SetMaxPeriod(units::second_t maxPeriod) override;
|
||||
|
||||
/**
|
||||
* Determine if the encoder is stopped.
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include <hal/Interrupts.h>
|
||||
#include <units/time.h>
|
||||
|
||||
#include "frc/AnalogTriggerType.h"
|
||||
|
||||
@@ -86,12 +87,12 @@ class InterruptableSensorBase {
|
||||
* waiting for an interrupt. This is not threadsafe, and can cause memory
|
||||
* corruption
|
||||
*
|
||||
* @param timeout Timeout in seconds
|
||||
* @param timeout Timeout
|
||||
* @param ignorePrevious If true, ignore interrupts that happened before
|
||||
* WaitForInterrupt was called.
|
||||
* @return What interrupts fired
|
||||
*/
|
||||
virtual WaitResult WaitForInterrupt(double timeout,
|
||||
virtual WaitResult WaitForInterrupt(units::second_t timeout,
|
||||
bool ignorePrevious = true);
|
||||
|
||||
/**
|
||||
@@ -116,7 +117,7 @@ class InterruptableSensorBase {
|
||||
*
|
||||
* @return Timestamp in seconds since boot.
|
||||
*/
|
||||
virtual double ReadRisingTimestamp();
|
||||
virtual units::second_t ReadRisingTimestamp();
|
||||
|
||||
/**
|
||||
* Return the timestamp for the falling interrupt that occurred most recently.
|
||||
@@ -127,7 +128,7 @@ class InterruptableSensorBase {
|
||||
*
|
||||
* @return Timestamp in seconds since boot.
|
||||
*/
|
||||
virtual double ReadFallingTimestamp();
|
||||
virtual units::second_t ReadFallingTimestamp();
|
||||
|
||||
/**
|
||||
* Set which edge to trigger interrupts on
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <units/time.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "frc/Timer.h"
|
||||
@@ -36,16 +37,16 @@ class MotorSafety {
|
||||
/**
|
||||
* Set the expiration time for the corresponding motor safety object.
|
||||
*
|
||||
* @param expirationTime The timeout value in seconds.
|
||||
* @param expirationTime The timeout value.
|
||||
*/
|
||||
void SetExpiration(double expirationTime);
|
||||
void SetExpiration(units::second_t expirationTime);
|
||||
|
||||
/**
|
||||
* Retrieve the timeout value for the corresponding motor safety object.
|
||||
*
|
||||
* @return the timeout value in seconds.
|
||||
* @return the timeout value.
|
||||
*/
|
||||
double GetExpiration() const;
|
||||
units::second_t GetExpiration() const;
|
||||
|
||||
/**
|
||||
* Determine if the motor is still operating or has timed out.
|
||||
@@ -93,16 +94,16 @@ class MotorSafety {
|
||||
virtual std::string GetDescription() const = 0;
|
||||
|
||||
private:
|
||||
static constexpr double kDefaultSafetyExpiration = 0.1;
|
||||
static constexpr auto kDefaultSafetyExpiration = 100_ms;
|
||||
|
||||
// The expiration time for this object
|
||||
double m_expiration = kDefaultSafetyExpiration;
|
||||
units::second_t m_expiration = kDefaultSafetyExpiration;
|
||||
|
||||
// True if motor safety is enabled for this motor
|
||||
bool m_enabled = false;
|
||||
|
||||
// The FPGA clock value when the motor has expired
|
||||
double m_stopTime = Timer::GetFPGATimestamp();
|
||||
units::second_t m_stopTime = Timer::GetFPGATimestamp();
|
||||
|
||||
mutable wpi::mutex m_thisMutex;
|
||||
};
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <units/time.h>
|
||||
#include <wpi/deprecated.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
namespace frc {
|
||||
@@ -80,19 +79,6 @@ class Notifier {
|
||||
*/
|
||||
void SetHandler(std::function<void()> handler);
|
||||
|
||||
/**
|
||||
* Register for single event notification.
|
||||
*
|
||||
* A timer event is queued for a single event after the specified delay.
|
||||
*
|
||||
* @deprecated Use unit-safe StartSingle(units::second_t delay) method
|
||||
* instead.
|
||||
*
|
||||
* @param delay Seconds to wait before the handler is called.
|
||||
*/
|
||||
WPI_DEPRECATED("Use unit-safe StartSingle method instead.")
|
||||
void StartSingle(double delay);
|
||||
|
||||
/**
|
||||
* Register for single event notification.
|
||||
*
|
||||
@@ -102,22 +88,6 @@ class Notifier {
|
||||
*/
|
||||
void StartSingle(units::second_t delay);
|
||||
|
||||
/**
|
||||
* Register for periodic event notification.
|
||||
*
|
||||
* A timer event is queued for periodic event notification. Each time the
|
||||
* interrupt occurs, the event will be immediately requeued for the same time
|
||||
* interval.
|
||||
*
|
||||
* @deprecated Use unit-safe StartPeriodic(units::second_t period) method
|
||||
* instead
|
||||
*
|
||||
* @param period Period in seconds to call the handler starting one period
|
||||
* after the call to this method.
|
||||
*/
|
||||
WPI_DEPRECATED("Use unit-safe StartPeriodic method instead.")
|
||||
void StartPeriodic(double period);
|
||||
|
||||
/**
|
||||
* Register for periodic event notification.
|
||||
*
|
||||
@@ -184,10 +154,10 @@ class Notifier {
|
||||
std::function<void()> m_handler;
|
||||
|
||||
// The absolute expiration time
|
||||
double m_expirationTime = 0;
|
||||
units::second_t m_expirationTime = 0_s;
|
||||
|
||||
// The relative time (either periodic or single)
|
||||
double m_period = 0;
|
||||
units::second_t m_period = 0_s;
|
||||
|
||||
// True if this is a periodic event
|
||||
bool m_periodic = false;
|
||||
|
||||
@@ -242,32 +242,6 @@ class SPI {
|
||||
int ReadAutoReceivedData(uint32_t* buffer, int numToRead,
|
||||
units::second_t timeout);
|
||||
|
||||
/**
|
||||
* Read data that has been transferred by the automatic SPI transfer engine.
|
||||
*
|
||||
* Transfers may be made a byte at a time, so it's necessary for the caller
|
||||
* to handle cases where an entire transfer has not been completed.
|
||||
*
|
||||
* Each received data sequence consists of a timestamp followed by the
|
||||
* received data bytes, one byte per word (in the least significant byte).
|
||||
* The length of each received data sequence is the same as the combined
|
||||
* size of the data and zeroSize set in SetAutoTransmitData().
|
||||
*
|
||||
* Blocks until numToRead words have been read or timeout expires.
|
||||
* May be called with numToRead=0 to retrieve how many words are available.
|
||||
*
|
||||
* @deprecated Use unit safe version instead.
|
||||
* ReadAutoReceivedData(uint32_t* buffer, int numToRead, <!--
|
||||
* --> units::second_t timeout)
|
||||
*
|
||||
* @param buffer buffer where read words are stored
|
||||
* @param numToRead number of words to read
|
||||
* @param timeout timeout in seconds (ms resolution)
|
||||
* @return Number of words remaining to be read
|
||||
*/
|
||||
WPI_DEPRECATED("Use ReadAutoReceivedData with unit-safety instead")
|
||||
int ReadAutoReceivedData(uint32_t* buffer, int numToRead, double timeout);
|
||||
|
||||
/**
|
||||
* Get the number of bytes dropped by the automatic SPI transfer engine due
|
||||
* to the receive buffer being full.
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <string_view>
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <wpi/deprecated.h>
|
||||
#include <units/time.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -155,9 +155,9 @@ class SerialPort {
|
||||
* This defines the timeout for transactions with the hardware.
|
||||
* It will affect reads and very large writes.
|
||||
*
|
||||
* @param timeout The number of seconds to to wait for I/O.
|
||||
* @param timeout The time to wait for I/O.
|
||||
*/
|
||||
void SetTimeout(double timeout);
|
||||
void SetTimeout(units::second_t timeout);
|
||||
|
||||
/**
|
||||
* Specify the size of the input buffer.
|
||||
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <frc2/Timer.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <units/time.h>
|
||||
|
||||
#include "frc/Timer.h"
|
||||
|
||||
namespace frc {
|
||||
/**
|
||||
* A class that limits the rate of change of an input value. Useful for
|
||||
@@ -36,7 +36,7 @@ class SlewRateLimiter {
|
||||
explicit SlewRateLimiter(Rate_t rateLimit, Unit_t initialValue = Unit_t{0})
|
||||
: m_rateLimit{rateLimit},
|
||||
m_prevVal{initialValue},
|
||||
m_prevTime{frc2::Timer::GetFPGATimestamp()} {}
|
||||
m_prevTime{Timer::GetFPGATimestamp()} {}
|
||||
|
||||
/**
|
||||
* Filters the input to limit its slew rate.
|
||||
@@ -46,7 +46,7 @@ class SlewRateLimiter {
|
||||
* rate.
|
||||
*/
|
||||
Unit_t Calculate(Unit_t input) {
|
||||
units::second_t currentTime = frc2::Timer::GetFPGATimestamp();
|
||||
units::second_t currentTime = Timer::GetFPGATimestamp();
|
||||
units::second_t elapsedTime = currentTime - m_prevTime;
|
||||
m_prevVal += std::clamp(input - m_prevVal, -m_rateLimit * elapsedTime,
|
||||
m_rateLimit * elapsedTime);
|
||||
@@ -62,7 +62,7 @@ class SlewRateLimiter {
|
||||
*/
|
||||
void Reset(Unit_t value) {
|
||||
m_prevVal = value;
|
||||
m_prevTime = frc2::Timer::GetFPGATimestamp();
|
||||
m_prevTime = Timer::GetFPGATimestamp();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <units/time.h>
|
||||
|
||||
#include "frc/SolenoidBase.h"
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
@@ -93,7 +94,7 @@ class Solenoid : public SolenoidBase,
|
||||
*
|
||||
* @see startPulse()
|
||||
*/
|
||||
void SetPulseDuration(double durationSeconds);
|
||||
void SetPulseDuration(units::second_t duration);
|
||||
|
||||
/**
|
||||
* Trigger the PCM to generate a pulse of the duration set in
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#include <wpi/priority_queue.h>
|
||||
|
||||
#include "frc/IterativeRobotBase.h"
|
||||
#include "frc2/Timer.h"
|
||||
#include "frc/Timer.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace frc {
|
||||
*/
|
||||
class TimedRobot : public IterativeRobotBase {
|
||||
public:
|
||||
static constexpr units::second_t kDefaultPeriod = 20_ms;
|
||||
static constexpr auto kDefaultPeriod = 20_ms;
|
||||
|
||||
/**
|
||||
* Provide an alternate "main loop" via StartCompetition().
|
||||
@@ -99,12 +99,11 @@ class TimedRobot : public IterativeRobotBase {
|
||||
units::second_t period, units::second_t offset)
|
||||
: func{std::move(func)},
|
||||
period{period},
|
||||
expirationTime{
|
||||
startTime + offset +
|
||||
units::math::floor((frc2::Timer::GetFPGATimestamp() - startTime) /
|
||||
period) *
|
||||
period +
|
||||
period} {}
|
||||
expirationTime{startTime + offset +
|
||||
units::math::floor(
|
||||
(Timer::GetFPGATimestamp() - startTime) / period) *
|
||||
period +
|
||||
period} {}
|
||||
|
||||
bool operator>(const Callback& rhs) const {
|
||||
return expirationTime > rhs.expirationTime;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc2/Timer.h"
|
||||
#include <units/time.h>
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -18,23 +18,17 @@ namespace frc {
|
||||
*
|
||||
* @param seconds Length of time to pause, in seconds.
|
||||
*/
|
||||
void Wait(double seconds);
|
||||
void Wait(units::second_t seconds);
|
||||
|
||||
/**
|
||||
* @brief Gives real-time clock system time with nanosecond resolution
|
||||
* @return The time, just in case you want the robot to start autonomous at 8pm
|
||||
* on Saturday.
|
||||
*/
|
||||
double GetTime();
|
||||
units::second_t GetTime();
|
||||
|
||||
/**
|
||||
* Timer objects measure accumulated time in seconds.
|
||||
*
|
||||
* The timer object functions like a stopwatch. It can be started, stopped, and
|
||||
* cleared. When the timer is running its value counts up in seconds. When
|
||||
* stopped, the timer holds the current value. The implementation simply records
|
||||
* the time when started and subtracts the current time whenever the value is
|
||||
* requested.
|
||||
* A wrapper for the frc::Timer class that returns unit-typed values.
|
||||
*/
|
||||
class Timer {
|
||||
public:
|
||||
@@ -48,10 +42,10 @@ class Timer {
|
||||
|
||||
virtual ~Timer() = default;
|
||||
|
||||
Timer(const Timer& rhs) = default;
|
||||
Timer& operator=(const Timer& rhs) = default;
|
||||
Timer(Timer&& rhs) = default;
|
||||
Timer& operator=(Timer&& rhs) = default;
|
||||
Timer(const Timer&) = default;
|
||||
Timer& operator=(const Timer&) = default;
|
||||
Timer(Timer&&) = default;
|
||||
Timer& operator=(Timer&&) = default;
|
||||
|
||||
/**
|
||||
* Get the current time from the timer. If the clock is running it is derived
|
||||
@@ -60,7 +54,7 @@ class Timer {
|
||||
*
|
||||
* @return Current time value for this timer in seconds
|
||||
*/
|
||||
double Get() const;
|
||||
units::second_t Get() const;
|
||||
|
||||
/**
|
||||
* Reset the timer by setting the time to 0.
|
||||
@@ -74,7 +68,8 @@ class Timer {
|
||||
* Start the timer running.
|
||||
*
|
||||
* Just set the running flag to true indicating that all time requests should
|
||||
* be relative to the system clock.
|
||||
* be relative to the system clock. Note that this method is a no-op if the
|
||||
* timer is already running.
|
||||
*/
|
||||
void Start();
|
||||
|
||||
@@ -87,15 +82,33 @@ class Timer {
|
||||
*/
|
||||
void Stop();
|
||||
|
||||
/**
|
||||
* Check if the period specified has passed.
|
||||
*
|
||||
* @param seconds The period to check.
|
||||
* @return True if the period has passed.
|
||||
*/
|
||||
bool HasElapsed(units::second_t period) const;
|
||||
|
||||
/**
|
||||
* Check if the period specified has passed and if it has, advance the start
|
||||
* time by that period. This is useful to decide if it's time to do periodic
|
||||
* work without drifting later by the time it took to get around to checking.
|
||||
*
|
||||
* @param period The period to check for (in seconds).
|
||||
* @param period The period to check for.
|
||||
* @return True if the period has passed.
|
||||
*/
|
||||
bool HasPeriodPassed(double period);
|
||||
bool HasPeriodPassed(units::second_t period);
|
||||
|
||||
/**
|
||||
* Check if the period specified has passed and if it has, advance the start
|
||||
* time by that period. This is useful to decide if it's time to do periodic
|
||||
* work without drifting later by the time it took to get around to checking.
|
||||
*
|
||||
* @param period The period to check for.
|
||||
* @return True if the period has passed.
|
||||
*/
|
||||
bool AdvanceIfElapsed(units::second_t period);
|
||||
|
||||
/**
|
||||
* Return the FPGA system clock time in seconds.
|
||||
@@ -105,7 +118,7 @@ class Timer {
|
||||
*
|
||||
* @returns Robot running time in seconds.
|
||||
*/
|
||||
static double GetFPGATimestamp();
|
||||
static units::second_t GetFPGATimestamp();
|
||||
|
||||
/**
|
||||
* Return the approximate match time.
|
||||
@@ -122,10 +135,12 @@ class Timer {
|
||||
*
|
||||
* @return Time remaining in current match period (auto or teleop)
|
||||
*/
|
||||
static double GetMatchTime();
|
||||
static units::second_t GetMatchTime();
|
||||
|
||||
private:
|
||||
frc2::Timer m_timer;
|
||||
units::second_t m_startTime = 0_s;
|
||||
units::second_t m_accumulatedTime = 0_s;
|
||||
bool m_running = false;
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <vector>
|
||||
|
||||
#include <hal/SimDevice.h>
|
||||
#include <units/time.h>
|
||||
#include <units/velocity.h>
|
||||
|
||||
#include "frc/Counter.h"
|
||||
#include "frc/smartdashboard/Sendable.h"
|
||||
@@ -174,8 +176,8 @@ class Ultrasonic : public Sendable, public SendableHelper<Ultrasonic> {
|
||||
static constexpr int kPriority = 64;
|
||||
|
||||
// Max time (ms) between readings.
|
||||
static constexpr double kMaxUltrasonicTime = 0.1;
|
||||
static constexpr double kSpeedOfSoundInchesPerSec = 1130.0 * 12.0;
|
||||
static constexpr auto kMaxUltrasonicTime = 0.1_s;
|
||||
static constexpr auto kSpeedOfSound = 1130_fps;
|
||||
|
||||
// Thread doing the round-robin automatic sensing
|
||||
static std::thread m_thread;
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
#include <units/time.h>
|
||||
#include <wpi/StringRef.h>
|
||||
#include <wpi/deprecated.h>
|
||||
|
||||
#include "frc/Tracer.h"
|
||||
|
||||
@@ -26,19 +25,6 @@ namespace frc {
|
||||
*/
|
||||
class Watchdog {
|
||||
public:
|
||||
/**
|
||||
* Watchdog constructor.
|
||||
*
|
||||
* @deprecated use unit-safe version instead.
|
||||
* Watchdog(units::second_t timeout, std::function<void()> callback)
|
||||
*
|
||||
* @param timeout The watchdog's timeout in seconds with microsecond
|
||||
* resolution.
|
||||
* @param callback This function is called when the timeout expires.
|
||||
*/
|
||||
WPI_DEPRECATED("Use unit-safe version instead")
|
||||
Watchdog(double timeout, std::function<void()> callback);
|
||||
|
||||
/**
|
||||
* Watchdog constructor.
|
||||
*
|
||||
@@ -48,11 +34,6 @@ class Watchdog {
|
||||
*/
|
||||
Watchdog(units::second_t timeout, std::function<void()> callback);
|
||||
|
||||
template <typename Callable, typename Arg, typename... Args>
|
||||
WPI_DEPRECATED("Use unit-safe version instead")
|
||||
Watchdog(double timeout, Callable&& f, Arg&& arg, Args&&... args)
|
||||
: Watchdog(units::second_t{timeout}, arg, args...) {}
|
||||
|
||||
template <typename Callable, typename Arg, typename... Args>
|
||||
Watchdog(units::second_t timeout, Callable&& f, Arg&& arg, Args&&... args)
|
||||
: Watchdog(timeout,
|
||||
@@ -65,21 +46,9 @@ class Watchdog {
|
||||
Watchdog& operator=(Watchdog&& rhs);
|
||||
|
||||
/**
|
||||
* Returns the time in seconds since the watchdog was last fed.
|
||||
* Returns the time since the watchdog was last fed.
|
||||
*/
|
||||
double GetTime() const;
|
||||
|
||||
/**
|
||||
* Sets the watchdog's timeout.
|
||||
*
|
||||
* @deprecated use the unit safe version instead.
|
||||
* SetTimeout(units::second_t timeout)
|
||||
*
|
||||
* @param timeout The watchdog's timeout in seconds with microsecond
|
||||
* resolution.
|
||||
*/
|
||||
WPI_DEPRECATED("Use unit-safe version instead")
|
||||
void SetTimeout(double timeout);
|
||||
units::second_t GetTime() const;
|
||||
|
||||
/**
|
||||
* Sets the watchdog's timeout.
|
||||
@@ -90,9 +59,9 @@ class Watchdog {
|
||||
void SetTimeout(units::second_t timeout);
|
||||
|
||||
/**
|
||||
* Returns the watchdog's timeout in seconds.
|
||||
* Returns the watchdog's timeout.
|
||||
*/
|
||||
double GetTimeout() const;
|
||||
units::second_t GetTimeout() const;
|
||||
|
||||
/**
|
||||
* Returns true if the watchdog timer has expired.
|
||||
@@ -143,7 +112,7 @@ class Watchdog {
|
||||
|
||||
private:
|
||||
// Used for timeout print rate-limiting
|
||||
static constexpr units::second_t kMinPrintPeriod = 1_s;
|
||||
static constexpr auto kMinPrintPeriod = 1_s;
|
||||
|
||||
units::second_t m_startTime = 0_s;
|
||||
units::second_t m_timeout;
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/time.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
namespace frc2 {
|
||||
|
||||
/**
|
||||
* Pause the task for a specified time.
|
||||
*
|
||||
* Pause the execution of the program for a specified period of time given in
|
||||
* seconds. Motors will continue to run at their last assigned values, and
|
||||
* sensors will continue to update. Only the task containing the wait will pause
|
||||
* until the wait time is expired.
|
||||
*
|
||||
* @param seconds Length of time to pause, in seconds.
|
||||
*/
|
||||
void Wait(units::second_t seconds);
|
||||
|
||||
/**
|
||||
* @brief Gives real-time clock system time with nanosecond resolution
|
||||
* @return The time, just in case you want the robot to start autonomous at 8pm
|
||||
* on Saturday.
|
||||
*/
|
||||
units::second_t GetTime();
|
||||
|
||||
/**
|
||||
* A wrapper for the frc::Timer class that returns unit-typed values.
|
||||
*/
|
||||
class Timer {
|
||||
public:
|
||||
/**
|
||||
* Create a new timer object.
|
||||
*
|
||||
* Create a new timer object and reset the time to zero. The timer is
|
||||
* initially not running and must be started.
|
||||
*/
|
||||
Timer();
|
||||
|
||||
virtual ~Timer() = default;
|
||||
|
||||
Timer(const Timer& rhs);
|
||||
Timer& operator=(const Timer& rhs);
|
||||
Timer(Timer&& rhs);
|
||||
Timer& operator=(Timer&& rhs);
|
||||
|
||||
/**
|
||||
* Get the current time from the timer. If the clock is running it is derived
|
||||
* from the current system clock the start time stored in the timer class. If
|
||||
* the clock is not running, then return the time when it was last stopped.
|
||||
*
|
||||
* @return Current time value for this timer in seconds
|
||||
*/
|
||||
units::second_t Get() const;
|
||||
|
||||
/**
|
||||
* Reset the timer by setting the time to 0.
|
||||
*
|
||||
* Make the timer startTime the current time so new requests will be relative
|
||||
* to now.
|
||||
*/
|
||||
void Reset();
|
||||
|
||||
/**
|
||||
* Start the timer running.
|
||||
*
|
||||
* Just set the running flag to true indicating that all time requests should
|
||||
* be relative to the system clock. Note that this method is a no-op if the
|
||||
* timer is already running.
|
||||
*/
|
||||
void Start();
|
||||
|
||||
/**
|
||||
* Stop the timer.
|
||||
*
|
||||
* This computes the time as of now and clears the running flag, causing all
|
||||
* subsequent time requests to be read from the accumulated time rather than
|
||||
* looking at the system clock.
|
||||
*/
|
||||
void Stop();
|
||||
|
||||
/**
|
||||
* Check if the period specified has passed.
|
||||
*
|
||||
* @param seconds The period to check.
|
||||
* @return True if the period has passed.
|
||||
*/
|
||||
bool HasElapsed(units::second_t period) const;
|
||||
|
||||
/**
|
||||
* Check if the period specified has passed and if it has, advance the start
|
||||
* time by that period. This is useful to decide if it's time to do periodic
|
||||
* work without drifting later by the time it took to get around to checking.
|
||||
*
|
||||
* @param period The period to check for.
|
||||
* @return True if the period has passed.
|
||||
*/
|
||||
bool HasPeriodPassed(units::second_t period);
|
||||
|
||||
/**
|
||||
* Check if the period specified has passed and if it has, advance the start
|
||||
* time by that period. This is useful to decide if it's time to do periodic
|
||||
* work without drifting later by the time it took to get around to checking.
|
||||
*
|
||||
* @param period The period to check for.
|
||||
* @return True if the period has passed.
|
||||
*/
|
||||
bool AdvanceIfElapsed(units::second_t period);
|
||||
|
||||
/**
|
||||
* Return the FPGA system clock time in seconds.
|
||||
*
|
||||
* Return the time from the FPGA hardware clock in seconds since the FPGA
|
||||
* started. Rolls over after 71 minutes.
|
||||
*
|
||||
* @returns Robot running time in seconds.
|
||||
*/
|
||||
static units::second_t GetFPGATimestamp();
|
||||
|
||||
/**
|
||||
* Return the approximate match time.
|
||||
*
|
||||
* The FMS does not send an official match time to the robots, but does send
|
||||
* an approximate match time. The value will count down the time remaining in
|
||||
* the current period (auto or teleop).
|
||||
*
|
||||
* Warning: This is not an official time (so it cannot be used to dispute ref
|
||||
* calls or guarantee that a function will trigger before the match ends).
|
||||
*
|
||||
* The Practice Match function of the DS approximates the behavior seen on the
|
||||
* field.
|
||||
*
|
||||
* @return Time remaining in current match period (auto or teleop)
|
||||
*/
|
||||
static units::second_t GetMatchTime();
|
||||
|
||||
private:
|
||||
units::second_t m_startTime = 0_s;
|
||||
units::second_t m_accumulatedTime = 0_s;
|
||||
bool m_running = false;
|
||||
mutable wpi::mutex m_mutex;
|
||||
};
|
||||
|
||||
} // namespace frc2
|
||||
Reference in New Issue
Block a user