blob: 0844abc8807dea3e9695adf8b4aa9f36b1afdc1b [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
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
9#include "MotorSafetyHelper.h"
10
11/**
12 * Initialize a SafePWM object by setting defaults
13 */
14void SafePWM::InitSafePWM()
15{
16 m_safetyHelper = new MotorSafetyHelper(this);
17 m_safetyHelper->SetSafetyEnabled(false);
18}
19
20/**
21 * Constructor for a SafePWM object taking a channel number
22 * @param channel The channel number to be used for the underlying PWM object
23 */
24SafePWM::SafePWM(UINT32 channel): PWM(channel)
25{
26 InitSafePWM();
27}
28
29/**
30 * Constructor for a SafePWM object taking channel and slot numbers.
31 * @param moduleNumber The digital module (1 or 2).
32 * @param channel The PWM channel number on the module (1..10).
33 */
34SafePWM::SafePWM(UINT8 moduleNumber, UINT32 channel): PWM(moduleNumber, channel)
35{
36 InitSafePWM();
37}
38
39SafePWM::~SafePWM()
40{
41 delete m_safetyHelper;
42}
43
44/*
45 * Set the expiration time for the PWM object
46 * @param timeout The timeout (in seconds) for this motor object
47 */
48void SafePWM::SetExpiration(float timeout)
49{
50 m_safetyHelper->SetExpiration(timeout);
51}
52
53/**
54 * Return the expiration time for the PWM object.
55 * @returns The expiration time value.
56 */
57float SafePWM::GetExpiration()
58{
59 return m_safetyHelper->GetExpiration();
60}
61
62/**
63 * Check if the PWM object is currently alive or stopped due to a timeout.
64 * @returns a bool value that is true if the motor has NOT timed out and should still
65 * be running.
66 */
67bool SafePWM::IsAlive()
68{
69 return m_safetyHelper->IsAlive();
70}
71
72/**
73 * Stop the motor associated with this PWM object.
74 * This is called by the MotorSafetyHelper object when it has a timeout for this PWM and needs to
75 * stop it from running.
76 */
77void SafePWM::StopMotor()
78{
79 SetRaw(kPwmDisabled);
80}
81
82/**
83 * Enable/disable motor safety for this device
84 * Turn on and off the motor safety option for this PWM object.
85 * @param enabled True if motor safety is enforced for this object
86 */
87void SafePWM::SetSafetyEnabled(bool enabled)
88{
89 m_safetyHelper->SetSafetyEnabled(enabled);
90}
91
92/**
93 * Check if motor safety is enabled for this object
94 * @returns True if motor safety is enforced for this object
95 */
96bool SafePWM::IsSafetyEnabled()
97{
98 return m_safetyHelper->IsSafetyEnabled();
99}
100
101void SafePWM::GetDescription(char *desc)
102{
103 sprintf(desc, "PWM %d on module %d", GetChannel(), GetModuleNumber());
104}
105
106/**
107 * Feed the MotorSafety timer when setting the speed.
108 * This method is called by the subclass motor whenever it updates its speed, thereby reseting
109 * the timeout value.
110 * @param speed Value to pass to the PWM class
111 */
112void SafePWM::SetSpeed(float speed)
113{
114 PWM::SetSpeed(speed);
115 m_safetyHelper->Feed();
116}
117