WPILibC++  2020.3.2-60-g3011ebe
MotorSafety.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <wpi/mutex.h>
11 #include <wpi/raw_ostream.h>
12 
13 #include "frc/ErrorBase.h"
14 #include "frc/Timer.h"
15 
16 namespace frc {
17 
24 class MotorSafety : public ErrorBase {
25  public:
26  MotorSafety();
27  virtual ~MotorSafety();
28 
29  MotorSafety(MotorSafety&& rhs);
30  MotorSafety& operator=(MotorSafety&& rhs);
31 
37  void Feed();
38 
44  void SetExpiration(double expirationTime);
45 
51  double GetExpiration() const;
52 
58  bool IsAlive() const;
59 
67  void SetSafetyEnabled(bool enabled);
68 
76  bool IsSafetyEnabled() const;
77 
85  void Check();
86 
93  static void CheckMotors();
94 
95  virtual void StopMotor() = 0;
96  virtual void GetDescription(wpi::raw_ostream& desc) const = 0;
97 
98  private:
99  static constexpr double kDefaultSafetyExpiration = 0.1;
100 
101  // The expiration time for this object
102  double m_expiration = kDefaultSafetyExpiration;
103 
104  // True if motor safety is enabled for this motor
105  bool m_enabled = false;
106 
107  // The FPGA clock value when the motor has expired
108  double m_stopTime = Timer::GetFPGATimestamp();
109 
110  mutable wpi::mutex m_thisMutex;
111 };
112 
113 } // namespace frc
frc::MotorSafety::GetExpiration
double GetExpiration() const
Retrieve the timeout value for the corresponding motor safety object.
frc::MotorSafety::CheckMotors
static void CheckMotors()
Check the motors to see if any have timed out.
frc::MotorSafety::IsAlive
bool IsAlive() const
Determine if the motor is still operating or has timed out.
frc::MotorSafety::Feed
void Feed()
Feed the motor safety object.
frc::MotorSafety
This base class runs a watchdog timer and calls the subclass's StopMotor() function if the timeout ex...
Definition: MotorSafety.h:24
frc::MotorSafety::SetSafetyEnabled
void SetSafetyEnabled(bool enabled)
Enable/disable motor safety for this device.
frc::MotorSafety::SetExpiration
void SetExpiration(double expirationTime)
Set the expiration time for the corresponding motor safety object.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc
A class that enforces constraints on the differential drive kinematics.
Definition: PDPSim.h:16
wpi::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:47
frc::Timer::GetFPGATimestamp
static double GetFPGATimestamp()
Return the FPGA system clock time in seconds.
frc::MotorSafety::Check
void Check()
Check if this motor has exceeded its timeout.
frc::MotorSafety::IsSafetyEnabled
bool IsSafetyEnabled() const
Return the state of the motor safety enabled flag.