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