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