WPILibC++ 2024.1.1-beta-4
MotorSafety.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <string>
8
9#include <units/time.h>
10#include <wpi/mutex.h>
11
12#include "frc/Timer.h"
13
14namespace frc {
15
16/**
17 * The Motor Safety feature acts as a watchdog timer for an individual motor. It
18 * operates by maintaining a timer that tracks how long it has been since the
19 * feed() method has been called for that actuator. Code in the Driver Station
20 * class initiates a comparison of these timers to the timeout values for any
21 * actuator with safety enabled every 5 received packets (100ms nominal).
22 *
23 * The subclass should call Feed() whenever the motor value is updated.
24 */
26 public:
28 virtual ~MotorSafety();
29
32
33 /**
34 * Feed the motor safety object.
35 *
36 * Resets the timer on this object that is used to do the timeouts.
37 */
38 void Feed();
39
40 /**
41 * Set the expiration time for the corresponding motor safety object.
42 *
43 * @param expirationTime The timeout value.
44 */
45 void SetExpiration(units::second_t expirationTime);
46
47 /**
48 * Retrieve the timeout value for the corresponding motor safety object.
49 *
50 * @return the timeout value.
51 */
52 units::second_t GetExpiration() const;
53
54 /**
55 * Determine if the motor is still operating or has timed out.
56 *
57 * @return true if the motor is still operating normally and hasn't timed out.
58 */
59 bool IsAlive() const;
60
61 /**
62 * Enable/disable motor safety for this device.
63 *
64 * Turn on and off the motor safety option for this PWM object.
65 *
66 * @param enabled True if motor safety is enforced for this object.
67 */
68 void SetSafetyEnabled(bool enabled);
69
70 /**
71 * Return the state of the motor safety enabled flag.
72 *
73 * Return if the motor safety is currently enabled for this device.
74 *
75 * @return True if motor safety is enforced for this device.
76 */
77 bool IsSafetyEnabled() const;
78
79 /**
80 * Check if this motor has exceeded its timeout.
81 *
82 * This method is called periodically to determine if this motor has exceeded
83 * its timeout value. If it has, the stop method is called, and the motor is
84 * shut down until its value is updated again.
85 */
86 void Check();
87
88 /**
89 * Check the motors to see if any have timed out.
90 *
91 * This static method is called periodically to poll all the motors and stop
92 * any that have timed out.
93 */
94 static void CheckMotors();
95
96 virtual void StopMotor() = 0;
97
98 /**
99 * The return value from this method is printed out when an error occurs
100 *
101 * This method must not throw!
102 */
103 virtual std::string GetDescription() const = 0;
104
105 private:
106 static constexpr auto kDefaultSafetyExpiration = 100_ms;
107
108 // The expiration time for this object
109 units::second_t m_expiration = kDefaultSafetyExpiration;
110
111 // True if motor safety is enabled for this motor
112 bool m_enabled = false;
113
114 // The FPGA clock value when the motor has expired
115 units::second_t m_stopTime = Timer::GetFPGATimestamp();
116
117 mutable wpi::mutex m_thisMutex;
118};
119
120} // namespace frc
The Motor Safety feature acts as a watchdog timer for an individual motor.
Definition: MotorSafety.h:25
virtual void StopMotor()=0
void Check()
Check if this motor has exceeded its timeout.
units::second_t GetExpiration() const
Retrieve the timeout value for the corresponding motor safety object.
static void CheckMotors()
Check the motors to see if any have timed out.
void SetExpiration(units::second_t expirationTime)
Set the expiration time for the corresponding motor safety object.
void SetSafetyEnabled(bool enabled)
Enable/disable motor safety for this device.
virtual std::string GetDescription() const =0
The return value from this method is printed out when an error occurs.
void Feed()
Feed the motor safety object.
bool IsSafetyEnabled() const
Return the state of the motor safety enabled flag.
MotorSafety & operator=(MotorSafety &&rhs)
MotorSafety(MotorSafety &&rhs)
bool IsAlive() const
Determine if the motor is still operating or has timed out.
virtual ~MotorSafety()
static units::second_t GetFPGATimestamp()
Return the FPGA system clock time in seconds.
Definition: AprilTagPoseEstimator.h:15
::std::mutex mutex
Definition: mutex.h:17