2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
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-12-29 16:19:23 -08:00
|
|
|
#include <wpi/SmallPtrSet.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/SmallString.h>
|
2018-12-29 16:19:23 -08:00
|
|
|
#include <wpi/raw_ostream.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-12-29 16:19:23 -08:00
|
|
|
#include "frc/DriverStation.h"
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2018-12-29 16:19:23 -08:00
|
|
|
static wpi::SmallPtrSet<MotorSafety*, 32> instanceList;
|
|
|
|
|
static wpi::mutex listMutex;
|
|
|
|
|
|
|
|
|
|
MotorSafety::MotorSafety() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(listMutex);
|
2018-12-29 16:19:23 -08:00
|
|
|
instanceList.insert(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MotorSafety::~MotorSafety() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(listMutex);
|
2018-12-29 16:19:23 -08:00
|
|
|
instanceList.erase(this);
|
2018-12-01 01:34:52 -08:00
|
|
|
}
|
2018-11-22 21:15:26 -08:00
|
|
|
|
2018-12-29 16:19:23 -08:00
|
|
|
MotorSafety::MotorSafety(MotorSafety&& rhs)
|
2021-04-18 20:35:29 -07:00
|
|
|
: m_expiration(std::move(rhs.m_expiration)),
|
2018-12-29 16:19:23 -08:00
|
|
|
m_enabled(std::move(rhs.m_enabled)),
|
|
|
|
|
m_stopTime(std::move(rhs.m_stopTime)) {}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
MotorSafety& MotorSafety::operator=(MotorSafety&& rhs) {
|
2019-07-07 19:15:59 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex, rhs.m_thisMutex);
|
|
|
|
|
|
2018-12-29 16:19:23 -08:00
|
|
|
m_expiration = std::move(rhs.m_expiration);
|
2018-11-22 21:15:26 -08:00
|
|
|
m_enabled = std::move(rhs.m_enabled);
|
2018-12-29 16:19:23 -08:00
|
|
|
m_stopTime = std::move(rhs.m_stopTime);
|
2018-11-22 21:15:26 -08:00
|
|
|
|
|
|
|
|
return *this;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
void MotorSafety::Feed() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-12-29 16:19:23 -08:00
|
|
|
m_stopTime = Timer::GetFPGATimestamp() + m_expiration;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
void MotorSafety::SetExpiration(double expirationTime) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-12-29 16:19:23 -08:00
|
|
|
m_expiration = expirationTime;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
double MotorSafety::GetExpiration() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-12-29 16:19:23 -08:00
|
|
|
return m_expiration;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
bool MotorSafety::IsAlive() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-12-29 16:19:23 -08:00
|
|
|
return !m_enabled || m_stopTime > Timer::GetFPGATimestamp();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
void MotorSafety::SetSafetyEnabled(bool enabled) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-11-22 21:15:26 -08:00
|
|
|
m_enabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MotorSafety::IsSafetyEnabled() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-11-22 21:15:26 -08:00
|
|
|
return m_enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-29 16:19:23 -08:00
|
|
|
void MotorSafety::Check() {
|
|
|
|
|
bool enabled;
|
|
|
|
|
double stopTime;
|
|
|
|
|
|
|
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_thisMutex);
|
2018-12-29 16:19:23 -08:00
|
|
|
enabled = m_enabled;
|
|
|
|
|
stopTime = m_stopTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DriverStation& ds = DriverStation::GetInstance();
|
|
|
|
|
if (!enabled || ds.IsDisabled() || ds.IsTest()) {
|
2018-11-22 21:15:26 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2018-12-29 16:19:23 -08:00
|
|
|
if (stopTime < Timer::GetFPGATimestamp()) {
|
|
|
|
|
wpi::SmallString<128> buf;
|
|
|
|
|
wpi::raw_svector_ostream desc(buf);
|
|
|
|
|
GetDescription(desc);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_ReportError(err::Timeout, "{}... Output not updated often enough",
|
|
|
|
|
desc.str());
|
2018-12-29 16:19:23 -08:00
|
|
|
StopMotor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MotorSafety::CheckMotors() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(listMutex);
|
2018-12-29 16:19:23 -08:00
|
|
|
for (auto elem : instanceList) {
|
|
|
|
|
elem->Check();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|