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