Watchdog: use units::second_t instead of double (#1863)

Fixes #1827.
This commit is contained in:
Tyler Veness
2019-09-03 15:58:31 -07:00
committed by Peter Johnson
parent 761bc3ef85
commit 7112add67f
7 changed files with 63 additions and 25 deletions

View File

@@ -27,8 +27,8 @@ IterativeRobotBase::IterativeRobotBase(double period)
: IterativeRobotBase(units::second_t(period)) {}
IterativeRobotBase::IterativeRobotBase(units::second_t period)
: m_period(period.to<double>()),
m_watchdog(period.to<double>(), [this] { PrintLoopOverrunMessage(); }) {}
: m_period(period),
m_watchdog(period, [this] { PrintLoopOverrunMessage(); }) {}
void IterativeRobotBase::RobotInit() {
wpi::outs() << "Default " << __FUNCTION__ << "() method... Override me!\n";
@@ -174,7 +174,8 @@ void IterativeRobotBase::PrintLoopOverrunMessage() {
wpi::SmallString<128> str;
wpi::raw_svector_ostream buf(str);
buf << "Loop time of " << wpi::format("%.6f", m_period) << "s overrun\n";
buf << "Loop time of " << wpi::format("%.6f", m_period.to<double>())
<< "s overrun\n";
DriverStation::ReportWarning(str);
}

View File

@@ -25,7 +25,7 @@ void TimedRobot::StartCompetition() {
// Tell the DS that the robot is ready to be enabled
HAL_ObserveUserProgramStarting();
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
m_expirationTime = units::second_t{Timer::GetFPGATimestamp()} + m_period;
UpdateAlarm();
// Loop forever, calling the appropriate mode-dependent function

View File

@@ -54,7 +54,7 @@ void Watchdog::Thread::Main() {
if (!watchdog->m_suppressTimeoutMessage) {
wpi::outs() << "Watchdog not fed within "
<< wpi::format("%.6f",
watchdog->m_timeout.count() / 1.0e6)
watchdog->m_timeout.count() / 1.0e9)
<< "s\n";
}
}
@@ -78,9 +78,10 @@ void Watchdog::Thread::Main() {
}
Watchdog::Watchdog(double timeout, std::function<void()> callback)
: m_timeout(static_cast<int64_t>(timeout * 1.0e6)),
m_callback(callback),
m_owner(&GetThreadOwner()) {}
: Watchdog(units::second_t{timeout}, callback) {}
Watchdog::Watchdog(units::second_t timeout, std::function<void()> callback)
: m_timeout(timeout), m_callback(callback), m_owner(&GetThreadOwner()) {}
Watchdog::~Watchdog() { Disable(); }
@@ -89,6 +90,13 @@ double Watchdog::GetTime() const {
}
void Watchdog::SetTimeout(double timeout) {
SetTimeout(units::second_t{timeout});
}
void Watchdog::SetTimeout(units::second_t timeout) {
using std::chrono::duration_cast;
using std::chrono::microseconds;
m_startTime = hal::fpga_clock::now();
m_epochs.clear();
@@ -96,11 +104,11 @@ void Watchdog::SetTimeout(double timeout) {
auto thr = m_owner->GetThread();
if (!thr) return;
m_timeout = std::chrono::microseconds(static_cast<int64_t>(timeout * 1.0e6));
m_timeout = timeout;
m_isExpired = false;
thr->m_watchdogs.remove(this);
m_expirationTime = m_startTime + m_timeout;
m_expirationTime = m_startTime + duration_cast<microseconds>(m_timeout);
thr->m_watchdogs.emplace(this);
thr->m_cond.notify_all();
}
@@ -109,7 +117,7 @@ double Watchdog::GetTimeout() const {
// Locks mutex
auto thr = m_owner->GetThread();
return m_timeout.count() / 1.0e6;
return m_timeout.count() / 1.0e9;
}
bool Watchdog::IsExpired() const {
@@ -140,6 +148,9 @@ void Watchdog::PrintEpochs() {
void Watchdog::Reset() { Enable(); }
void Watchdog::Enable() {
using std::chrono::duration_cast;
using std::chrono::microseconds;
m_startTime = hal::fpga_clock::now();
m_epochs.clear();
@@ -150,7 +161,7 @@ void Watchdog::Enable() {
m_isExpired = false;
thr->m_watchdogs.remove(this);
m_expirationTime = m_startTime + m_timeout;
m_expirationTime = m_startTime + duration_cast<microseconds>(m_timeout);
thr->m_watchdogs.emplace(this);
thr->m_cond.notify_all();
}