Remove MotorSafetyHelper, create MotorSafety base class instead (#562)

Most of the MotorSafety implementation was moved into the MotorSafety base
class. SafePWM's inheritance of MotorSafety was moved into PWM to
eliminate Java needing a helper class.

In Java, a helper class for Sendable (SendableImpl) was added due to
lack of multiple inheritance.
This commit is contained in:
Tyler Veness
2018-11-22 21:15:26 -08:00
committed by Peter Johnson
parent df347e3d80
commit acb786a791
39 changed files with 710 additions and 1023 deletions

View File

@@ -1,19 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
// clang-format off
#ifdef _MSC_VER
#pragma message "warning: MotorSafetyHelper.h is deprecated; include frc/MotorSafetyHelper.h instead"
#else
#warning "MotorSafetyHelper.h is deprecated; include frc/MotorSafetyHelper.h instead"
#endif
// clang-format on
#include "frc/MotorSafetyHelper.h"

View File

@@ -1,19 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
// clang-format off
#ifdef _MSC_VER
#pragma message "warning: SafePWM.h is deprecated; include frc/SafePWM.h instead"
#else
#warning "SafePWM.h is deprecated; include frc/SafePWM.h instead"
#endif
// clang-format on
#include "frc/SafePWM.h"

View File

@@ -7,25 +7,107 @@
#pragma once
#define DEFAULT_SAFETY_EXPIRATION 0.1
#include <wpi/mutex.h>
#include <wpi/raw_ostream.h>
#include "frc/ErrorBase.h"
#include "frc/Timer.h"
namespace frc {
class MotorSafety {
/**
* This base class runs a watchdog timer and calls the subclass's StopMotor()
* function if the timeout expires.
*
* The subclass should call Feed() whenever the motor value is updated.
*/
class MotorSafety : public ErrorBase {
public:
MotorSafety() = default;
MotorSafety(MotorSafety&&) = default;
MotorSafety& operator=(MotorSafety&&) = default;
MotorSafety();
virtual ~MotorSafety();
MotorSafety(MotorSafety&& rhs);
MotorSafety& operator=(MotorSafety&& rhs);
/**
* Feed the motor safety object.
*
* Resets the timer on this object that is used to do the timeouts.
*/
void Feed();
/**
* Set the expiration time for the corresponding motor safety object.
*
* @param expirationTime The timeout value in seconds.
*/
void SetExpiration(double expirationTime);
/**
* Retrieve the timeout value for the corresponding motor safety object.
*
* @return the timeout value in seconds.
*/
double GetExpiration() const;
/**
* Determine if the motor is still operating or has timed out.
*
* @return true if the motor is still operating normally and hasn't timed out.
*/
bool IsAlive() const;
/**
* Enable/disable motor safety for this device.
*
* Turn on and off the motor safety option for this PWM object.
*
* @param enabled True if motor safety is enforced for this object.
*/
void SetSafetyEnabled(bool enabled);
/**
* Return the state of the motor safety enabled flag.
*
* Return if the motor safety is currently enabled for this device.
*
* @return True if motor safety is enforced for this device.
*/
bool IsSafetyEnabled() const;
/**
* Check if this motor has exceeded its timeout.
*
* This method is called periodically to determine if this motor has exceeded
* its timeout value. If it has, the stop method is called, and the motor is
* shut down until its value is updated again.
*/
void Check();
/**
* Check the motors to see if any have timed out.
*
* This static method is called periodically to poll all the motors and stop
* any that have timed out.
*/
static void CheckMotors();
virtual void SetExpiration(double timeout) = 0;
virtual double GetExpiration() const = 0;
virtual bool IsAlive() const = 0;
virtual void StopMotor() = 0;
virtual void SetSafetyEnabled(bool enabled) = 0;
virtual bool IsSafetyEnabled() const = 0;
virtual void GetDescription(wpi::raw_ostream& desc) const = 0;
private:
static constexpr double kDefaultSafetyExpiration = 0.1;
// The expiration time for this object
double m_expiration = kDefaultSafetyExpiration;
// True if motor safety is enabled for this motor
bool m_enabled = false;
// The FPGA clock value when the motor has expired
double m_stopTime = Timer::GetFPGATimestamp();
mutable wpi::mutex m_thisMutex;
};
} // namespace frc

View File

@@ -1,120 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <wpi/mutex.h>
#include "frc/ErrorBase.h"
namespace frc {
class MotorSafety;
class MotorSafetyHelper : public ErrorBase {
public:
/**
* The constructor for a MotorSafetyHelper object.
*
* The helper object is constructed for every object that wants to implement
* the Motor Safety protocol. The helper object has the code to actually do
* the timing and call the motors Stop() method when the timeout expires. The
* motor object is expected to call the Feed() method whenever the motors
* value is updated.
*
* @param safeObject a pointer to the motor object implementing MotorSafety.
* This is used to call the Stop() method on the motor.
*/
explicit MotorSafetyHelper(MotorSafety* safeObject);
~MotorSafetyHelper();
MotorSafetyHelper(MotorSafetyHelper&&) = default;
MotorSafetyHelper& operator=(MotorSafetyHelper&&) = default;
/**
* Feed the motor safety object.
*
* Resets the timer on this object that is used to do the timeouts.
*/
void Feed();
/**
* Set the expiration time for the corresponding motor safety object.
*
* @param expirationTime The timeout value in seconds.
*/
void SetExpiration(double expirationTime);
/**
* Retrieve the timeout value for the corresponding motor safety object.
*
* @return the timeout value in seconds.
*/
double GetExpiration() const;
/**
* Determine if the motor is still operating or has timed out.
*
* @return a true value if the motor is still operating normally and hasn't
* timed out.
*/
bool IsAlive() const;
/**
* Check if this motor has exceeded its timeout.
*
* This method is called periodically to determine if this motor has exceeded
* its timeout value. If it has, the stop method is called, and the motor is
* shut down until its value is updated again.
*/
void Check();
/**
* Enable/disable motor safety for this device
*
* Turn on and off the motor safety option for this PWM object.
*
* @param enabled True if motor safety is enforced for this object
*/
void SetSafetyEnabled(bool enabled);
/**
* Return the state of the motor safety enabled flag
*
* Return if the motor safety is currently enabled for this devicce.
*
* @return True if motor safety is enforced for this device
*/
bool IsSafetyEnabled() const;
/**
* Check the motors to see if any have timed out.
*
* This static method is called periodically to poll all the motors and stop
* any that have timed out.
*/
static void CheckMotors();
private:
// The expiration time for this object
double m_expiration;
// True if motor safety is enabled for this motor
bool m_enabled;
// The FPGA clock value when this motor has expired
double m_stopTime;
// Protect accesses to the state for this object
mutable wpi::mutex m_thisMutex;
// The object that is using the helper
MotorSafety* m_safeObject;
};
} // namespace frc

View File

@@ -12,7 +12,6 @@
#include "frc/DigitalOutput.h"
#include "frc/ErrorBase.h"
#include "frc/MotorSafety.h"
#include "frc/MotorSafetyHelper.h"
#include "frc/PWM.h"
#include "frc/SpeedController.h"
#include "frc/smartdashboard/SendableBase.h"
@@ -22,8 +21,7 @@ namespace frc {
/**
* Nidec Brushless Motor.
*/
class NidecBrushless : public ErrorBase,
public SendableBase,
class NidecBrushless : public SendableBase,
public SpeedController,
public MotorSafety {
public:
@@ -70,15 +68,6 @@ class NidecBrushless : public ErrorBase,
*/
void Disable() override;
/**
* Stop the motor.
*
* This is called by the MotorSafetyHelper object when it has a timeout for
* this PWM and needs to stop it from running. Calling Set() will re-enable
* the motor.
*/
void StopMotor() override;
/**
* Re-enable the motor after Disable() has been called. The Set() function
* must be called to set a new motor speed.
@@ -94,32 +83,7 @@ class NidecBrushless : public ErrorBase,
void PIDWrite(double output) override;
// MotorSafety interface
/**
* Set the safety expiration time.
*
* @param timeout The timeout (in seconds) for this motor object
*/
void SetExpiration(double timeout) override;
/**
* Return the safety expiration time.
*
* @return The expiration time value.
*/
double GetExpiration() const override;
/**
* Check if the motor is currently alive or stopped due to a timeout.
*
* @return a bool value that is true if the motor has NOT timed out and should
* still be running.
*/
bool IsAlive() const override;
void SetSafetyEnabled(bool enabled) override;
bool IsSafetyEnabled() const override;
void StopMotor() override;
void GetDescription(wpi::raw_ostream& desc) const override;
/**
@@ -133,7 +97,6 @@ class NidecBrushless : public ErrorBase,
void InitSendable(SendableBuilder& builder) override;
private:
MotorSafetyHelper m_safetyHelper;
bool m_isInverted = false;
std::atomic_bool m_disabled{false};
DigitalOutput m_dio;

View File

@@ -10,8 +10,9 @@
#include <stdint.h>
#include <hal/Types.h>
#include <wpi/raw_ostream.h>
#include "frc/ErrorBase.h"
#include "frc/MotorSafety.h"
#include "frc/smartdashboard/SendableBase.h"
namespace frc {
@@ -33,7 +34,7 @@ namespace frc {
* - 1 = minimum pulse width (currently .5ms)
* - 0 = disabled (i.e. PWM output is held low)
*/
class PWM : public ErrorBase, public SendableBase {
class PWM : public MotorSafety, public SendableBase {
public:
/**
* Represents the amount to multiply the minimum servo-pulse pwm period by.
@@ -75,6 +76,10 @@ class PWM : public ErrorBase, public SendableBase {
PWM(PWM&& rhs);
PWM& operator=(PWM&& rhs);
// MotorSafety interface
void StopMotor() override;
void GetDescription(wpi::raw_ostream& desc) const override;
/**
* Set the PWM value directly to the hardware.
*

View File

@@ -7,7 +7,7 @@
#pragma once
#include "frc/SafePWM.h"
#include "frc/PWM.h"
#include "frc/SpeedController.h"
namespace frc {
@@ -15,7 +15,7 @@ namespace frc {
/**
* Common base class for all PWM Speed Controllers.
*/
class PWMSpeedController : public SafePWM, public SpeedController {
class PWMSpeedController : public PWM, public SpeedController {
public:
PWMSpeedController(PWMSpeedController&&) = default;
PWMSpeedController& operator=(PWMSpeedController&&) = default;

View File

@@ -18,8 +18,6 @@
namespace frc {
class MotorSafetyHelper;
/**
* Class for Spike style relay outputs.
*
@@ -32,7 +30,7 @@ class MotorSafetyHelper;
* independently for something that does not care about voltage polarity (like
* a solenoid).
*/
class Relay : public MotorSafety, public ErrorBase, public SendableBase {
class Relay : public MotorSafety, public SendableBase {
public:
enum Value { kOff, kOn, kForward, kReverse };
enum Direction { kBothDirections, kForwardOnly, kReverseOnly };
@@ -89,52 +87,9 @@ class Relay : public MotorSafety, public ErrorBase, public SendableBase {
int GetChannel() const;
/**
* Set the expiration time for the Relay object.
*
* @param timeout The timeout (in seconds) for this relay object
*/
void SetExpiration(double timeout) override;
/**
* Return the expiration time for the relay object.
*
* @return The expiration time value.
*/
double GetExpiration() const override;
/**
* Check if the relay object is currently alive or stopped due to a timeout.
*
* @return a bool value that is true if the motor has NOT timed out and should
* still be running.
*/
bool IsAlive() const override;
/**
* Stop the motor associated with this PWM object.
*
* This is called by the MotorSafetyHelper object when it has a timeout for
* this relay and needs to stop it from running.
*/
// MotorSafety interface
void StopMotor() override;
/**
* Enable/disable motor safety for this device.
*
* Turn on and off the motor safety option for this relay object.
*
* @param enabled True if motor safety is enforced for this object
*/
void SetSafetyEnabled(bool enabled) override;
/**
* Check if motor safety is enabled for this object.
*
* @returns True if motor safety is enforced for this object
*/
bool IsSafetyEnabled() const override;
void GetDescription(wpi::raw_ostream& desc) const override;
void InitSendable(SendableBuilder& builder) override;
@@ -145,8 +100,6 @@ class Relay : public MotorSafety, public ErrorBase, public SendableBase {
HAL_RelayHandle m_forwardHandle = HAL_kInvalidHandle;
HAL_RelayHandle m_reverseHandle = HAL_kInvalidHandle;
std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
};
} // namespace frc

View File

@@ -14,7 +14,6 @@
#include "frc/ErrorBase.h"
#include "frc/MotorSafety.h"
#include "frc/MotorSafetyHelper.h"
namespace frc {
@@ -32,7 +31,7 @@ class GenericHID;
* function (intended for hand created drive code, such as autonomous) or with
* the Tank/Arcade functions intended to be used for Operator Control driving.
*/
class RobotDrive : public MotorSafety, public ErrorBase {
class RobotDrive : public MotorSafety {
public:
enum MotorType {
kFrontLeftMotor = 0,
@@ -401,12 +400,7 @@ class RobotDrive : public MotorSafety, public ErrorBase {
*/
void SetMaxOutput(double maxOutput);
void SetExpiration(double timeout) override;
double GetExpiration() const override;
bool IsAlive() const override;
void StopMotor() override;
bool IsSafetyEnabled() const override;
void SetSafetyEnabled(bool enabled) override;
void GetDescription(wpi::raw_ostream& desc) const override;
protected:
@@ -444,7 +438,6 @@ class RobotDrive : public MotorSafety, public ErrorBase {
std::shared_ptr<SpeedController> m_frontRightMotor;
std::shared_ptr<SpeedController> m_rearLeftMotor;
std::shared_ptr<SpeedController> m_rearRightMotor;
std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
private:
int GetNumMotors() {

View File

@@ -1,105 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <memory>
#include <wpi/raw_ostream.h>
#include "frc/MotorSafety.h"
#include "frc/MotorSafetyHelper.h"
#include "frc/PWM.h"
namespace frc {
/**
* A safe version of the PWM class.
*
* It is safe because it implements the MotorSafety interface that provides
* timeouts in the event that the motor value is not updated before the
* expiration time. This delegates the actual work to a MotorSafetyHelper
* object that is used for all objects that implement MotorSafety.
*/
class SafePWM : public PWM, public MotorSafety {
public:
/**
* Constructor for a SafePWM object taking a channel number.
*
* @param channel The PWM channel number 0-9 are on-board, 10-19 are on the
* MXP port
*/
explicit SafePWM(int channel);
virtual ~SafePWM() = default;
SafePWM(SafePWM&&) = default;
SafePWM& operator=(SafePWM&&) = default;
/**
* Set the expiration time for the PWM object.
*
* @param timeout The timeout (in seconds) for this motor object
*/
void SetExpiration(double timeout);
/**
* Return the expiration time for the PWM object.
*
* @returns The expiration time value.
*/
double GetExpiration() const;
/**
* Check if the PWM object is currently alive or stopped due to a timeout.
*
* @return a bool value that is true if the motor has NOT timed out and should
* still be running.
*/
bool IsAlive() const;
/**
* Stop the motor associated with this PWM object.
*
* This is called by the MotorSafetyHelper object when it has a timeout for
* this PWM and needs to stop it from running.
*/
void StopMotor();
/**
* Enable/disable motor safety for this device.
*
* Turn on and off the motor safety option for this PWM object.
*
* @param enabled True if motor safety is enforced for this object
*/
void SetSafetyEnabled(bool enabled);
/**
* Check if motor safety is enabled for this object.
*
* @returns True if motor safety is enforced for this object
*/
bool IsSafetyEnabled() const;
void GetDescription(wpi::raw_ostream& desc) const;
/**
* Feed the MotorSafety timer when setting the speed.
*
* This method is called by the subclass motor whenever it updates its speed,
* thereby reseting the timeout value.
*
* @param speed Value to pass to the PWM class
*/
virtual void SetSpeed(double speed);
private:
std::unique_ptr<MotorSafetyHelper> m_safetyHelper;
};
} // namespace frc

View File

@@ -7,7 +7,7 @@
#pragma once
#include "frc/SafePWM.h"
#include "frc/PWM.h"
#include "frc/SpeedController.h"
namespace frc {
@@ -18,7 +18,7 @@ namespace frc {
* The range parameters default to the appropriate values for the Hitec HS-322HD
* servo provided in the FIRST Kit of Parts in 2008.
*/
class Servo : public SafePWM {
class Servo : public PWM {
public:
/**
* @param channel The PWM channel to which the servo is attached. 0-9 are

View File

@@ -13,7 +13,6 @@
#include <wpi/raw_ostream.h>
#include "frc/MotorSafety.h"
#include "frc/MotorSafetyHelper.h"
#include "frc/smartdashboard/SendableBase.h"
namespace frc {
@@ -72,12 +71,7 @@ class RobotDriveBase : public MotorSafety, public SendableBase {
*/
void FeedWatchdog();
void SetExpiration(double timeout) override;
double GetExpiration() const override;
bool IsAlive() const override;
void StopMotor() override = 0;
bool IsSafetyEnabled() const override;
void SetSafetyEnabled(bool enabled) override;
void GetDescription(wpi::raw_ostream& desc) const override = 0;
protected:
@@ -103,7 +97,6 @@ class RobotDriveBase : public MotorSafety, public SendableBase {
double m_deadband = 0.02;
double m_maxOutput = 1.0;
MotorSafetyHelper m_safetyHelper{this};
};
} // namespace frc