blob: 801e3eef6f1e09f1e90fd32a214d1fbcf048535a [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7
8#include "SafePWM.h"
9
10/**
11 * Constructor for a SafePWM object taking a channel number.
12 * @param channel The PWM channel number 0-9 are on-board, 10-19 are on the MXP
13 * port
14 */
15SafePWM::SafePWM(uint32_t channel) : PWM(channel) {
16 m_safetyHelper = std::make_unique<MotorSafetyHelper>(this);
17 m_safetyHelper->SetSafetyEnabled(false);
18}
19
20/**
21 * Set the expiration time for the PWM object
22 * @param timeout The timeout (in seconds) for this motor object
23 */
24void SafePWM::SetExpiration(float timeout) {
25 m_safetyHelper->SetExpiration(timeout);
26}
27
28/**
29 * Return the expiration time for the PWM object.
30 * @returns The expiration time value.
31 */
32float SafePWM::GetExpiration() const { return m_safetyHelper->GetExpiration(); }
33
34/**
35 * Check if the PWM object is currently alive or stopped due to a timeout.
36 * @returns a bool value that is true if the motor has NOT timed out and should
37 * still
38 * be running.
39 */
40bool SafePWM::IsAlive() const { return m_safetyHelper->IsAlive(); }
41
42/**
43 * Stop the motor associated with this PWM object.
44 * This is called by the MotorSafetyHelper object when it has a timeout for this
45 * PWM and needs to
46 * stop it from running.
47 */
48void SafePWM::StopMotor() { SetRaw(kPwmDisabled); }
49
50/**
51 * Enable/disable motor safety for this device
52 * Turn on and off the motor safety option for this PWM object.
53 * @param enabled True if motor safety is enforced for this object
54 */
55void SafePWM::SetSafetyEnabled(bool enabled) {
56 m_safetyHelper->SetSafetyEnabled(enabled);
57}
58
59/**
60 * Check if motor safety is enabled for this object
61 * @returns True if motor safety is enforced for this object
62 */
63bool SafePWM::IsSafetyEnabled() const {
64 return m_safetyHelper->IsSafetyEnabled();
65}
66
67void SafePWM::GetDescription(std::ostringstream& desc) const {
68 desc << "PWM " << GetChannel();
69}
70
71/**
72 * Feed the MotorSafety timer when setting the speed.
73 * This method is called by the subclass motor whenever it updates its speed,
74 * thereby reseting
75 * the timeout value.
76 * @param speed Value to pass to the PWM class
77 */
78void SafePWM::SetSpeed(float speed) {
79 PWM::SetSpeed(speed);
80 m_safetyHelper->Feed();
81}