2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
#include "frc/MotorSafety.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <utility>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/SmallString.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/WPIErrors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
MotorSafety::MotorSafety(MotorSafety&& rhs)
|
2018-12-01 01:34:52 -08:00
|
|
|
: ErrorBase(std::move(rhs)), m_enabled(std::move(rhs.m_enabled)) {
|
|
|
|
|
m_watchdog = Watchdog(rhs.m_watchdog.GetTimeout(), [this] { TimeoutFunc(); });
|
|
|
|
|
}
|
2018-11-22 21:15:26 -08:00
|
|
|
|
|
|
|
|
MotorSafety& MotorSafety::operator=(MotorSafety&& rhs) {
|
|
|
|
|
ErrorBase::operator=(std::move(rhs));
|
|
|
|
|
|
2018-12-01 01:34:52 -08:00
|
|
|
m_watchdog = Watchdog(rhs.m_watchdog.GetTimeout(), [this] { TimeoutFunc(); });
|
2018-11-22 21:15:26 -08:00
|
|
|
m_enabled = std::move(rhs.m_enabled);
|
|
|
|
|
|
|
|
|
|
return *this;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
void MotorSafety::Feed() {
|
2017-11-22 17:10:21 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2018-12-01 01:34:52 -08:00
|
|
|
m_watchdog.Reset();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
void MotorSafety::SetExpiration(double expirationTime) {
|
2017-11-22 17:10:21 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2018-12-01 01:34:52 -08:00
|
|
|
m_watchdog.SetTimeout(expirationTime);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
double MotorSafety::GetExpiration() const {
|
2017-11-22 17:10:21 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2018-12-01 01:34:52 -08:00
|
|
|
return m_watchdog.GetTimeout();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
bool MotorSafety::IsAlive() const {
|
2017-11-22 17:10:21 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2018-12-01 01:34:52 -08:00
|
|
|
return !m_enabled || !m_watchdog.IsExpired();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
void MotorSafety::SetSafetyEnabled(bool enabled) {
|
|
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
2018-12-01 01:34:52 -08:00
|
|
|
if (enabled) {
|
|
|
|
|
m_watchdog.Enable();
|
|
|
|
|
} else {
|
|
|
|
|
m_watchdog.Disable();
|
|
|
|
|
}
|
2018-11-22 21:15:26 -08:00
|
|
|
m_enabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MotorSafety::IsSafetyEnabled() const {
|
|
|
|
|
std::lock_guard<wpi::mutex> lock(m_thisMutex);
|
|
|
|
|
return m_enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 01:34:52 -08:00
|
|
|
void MotorSafety::TimeoutFunc() {
|
|
|
|
|
auto& ds = DriverStation::GetInstance();
|
|
|
|
|
if (ds.IsDisabled() || ds.IsTest()) {
|
2018-11-22 21:15:26 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2018-12-01 01:34:52 -08:00
|
|
|
wpi::SmallString<128> buf;
|
|
|
|
|
wpi::raw_svector_ostream desc(buf);
|
|
|
|
|
GetDescription(desc);
|
|
|
|
|
desc << "... Output not updated often enough.";
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, desc.str());
|
|
|
|
|
StopMotor();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|