WPILibC++ 2024.3.2
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 /**
97 * Called to stop the motor when the timeout expires.
98 */
99 virtual void StopMotor() = 0;
100
101 /**
102 * Returns a description to print when an error occurs.
103 *
104 * @return Description to print when an error occurs.
105 */
106 virtual std::string GetDescription() const = 0;
107
108 private:
109 static constexpr auto kDefaultSafetyExpiration = 100_ms;
110
111 // The expiration time for this object
112 units::second_t m_expiration = kDefaultSafetyExpiration;
113
114 // True if motor safety is enabled for this motor
115 bool m_enabled = false;
116
117 // The FPGA clock value when the motor has expired
118 units::second_t m_stopTime = Timer::GetFPGATimestamp();
119
120 mutable wpi::mutex m_thisMutex;
121};
122
123} // namespace frc
The Motor Safety feature acts as a watchdog timer for an individual motor.
Definition: MotorSafety.h:25
virtual void StopMotor()=0
Called to stop the motor when the timeout expires.
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
Returns a description to print 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