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