blob: 205c6a1c82ab46da8e692b6ee44a90a48b678c6b [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#ifndef PWM_H_
8#define PWM_H_
9
10#include "SensorBase.h"
11#include "LiveWindow/LiveWindowSendable.h"
12#include "tables/ITableListener.h"
13
14class DigitalModule;
15
16/**
17 * Class implements the PWM generation in the FPGA.
18 *
19 * The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They are mapped
20 * to the hardware dependent values, in this case 0-255 for the FPGA.
21 * Changes are immediately sent to the FPGA, and the update occurs at the next
22 * FPGA cycle. There is no delay.
23 *
24 * As of revision 0.1.10 of the FPGA, the FPGA interprets the 0-255 values as follows:
25 * - 255 = full "forward"
26 * - 254 to 129 = linear scaling from "full forward" to "center"
27 * - 128 = center value
28 * - 127 to 2 = linear scaling from "center" to "full reverse"
29 * - 1 = full "reverse"
30 * - 0 = disabled (i.e. PWM output is held low)
31 */
32class PWM : public SensorBase, public ITableListener, public LiveWindowSendable
33{
34 friend class DigitalModule;
35public:
36 typedef enum {kPeriodMultiplier_1X = 1, kPeriodMultiplier_2X = 2, kPeriodMultiplier_4X = 4} PeriodMultiplier;
37
38 explicit PWM(UINT32 channel);
39 PWM(UINT8 moduleNumber, UINT32 channel);
40 virtual ~PWM();
41 virtual void SetRaw(UINT8 value);
42 virtual UINT8 GetRaw();
43 void SetPeriodMultiplier(PeriodMultiplier mult);
44 void EnableDeadbandElimination(bool eliminateDeadband);
45 void SetBounds(INT32 max, INT32 deadbandMax, INT32 center, INT32 deadbandMin, INT32 min);
46 UINT32 GetChannel() {return m_channel;}
47 UINT32 GetModuleNumber();
48
49protected:
50 /**
51 * kDefaultPwmPeriod is "ticks" where each tick is 6.525us
52 *
53 * - 20ms periods (50 Hz) are the "safest" setting in that this works for all devices
54 * - 20ms periods seem to be desirable for Vex Motors
55 * - 20ms periods are the specified period for HS-322HD servos, but work reliably down
56 * to 10.0 ms; starting at about 8.5ms, the servo sometimes hums and get hot;
57 * by 5.0ms the hum is nearly continuous
58 * - 10ms periods work well for Victor 884
59 * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed controllers.
60 * Due to the shipping firmware on the Jaguar, we can't run the update period less
61 * than 5.05 ms.
62 *
63 * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period scaling is implemented as an
64 * output squelch to get longer periods for old devices.
65 *
66 * Set to 5.05 ms period / 6.525us clock = 774
67 */
68 static const UINT32 kDefaultPwmPeriod = 774;
69
70 /**
71 * kDefaultMinPwmHigh is "ticks" where each tick is 6.525us
72 *
73 * - There are 128 pwm values less than the center, so...
74 * - The minimum output pulse length is 1.5ms - 128 * 6.525us = 0.665ms
75 * - 0.665ms / 6.525us per tick = 102
76 */
77 static const UINT32 kDefaultMinPwmHigh = 102;
78
79 static const INT32 kPwmDisabled = 0;
80
81 virtual void SetPosition(float pos);
82 virtual float GetPosition();
83 virtual void SetSpeed(float speed);
84 virtual float GetSpeed();
85
86 bool m_eliminateDeadband;
87 INT32 m_maxPwm;
88 INT32 m_deadbandMaxPwm;
89 INT32 m_centerPwm;
90 INT32 m_deadbandMinPwm;
91 INT32 m_minPwm;
92
93 void ValueChanged(ITable* source, const std::string& key, EntryValue value, bool isNew);
94 void UpdateTable();
95 void StartLiveWindowMode();
96 void StopLiveWindowMode();
97 std::string GetSmartDashboardType();
98 void InitTable(ITable *subTable);
99 ITable * GetTable();
100
101 ITable *m_table;
102
103private:
104 void InitPWM(UINT8 moduleNumber, UINT32 channel);
105 UINT32 m_channel;
106 DigitalModule *m_module;
107 INT32 GetMaxPositivePwm() { return m_maxPwm; };
108 INT32 GetMinPositivePwm() { return m_eliminateDeadband ? m_deadbandMaxPwm : m_centerPwm + 1; };
109 INT32 GetCenterPwm() { return m_centerPwm; };
110 INT32 GetMaxNegativePwm() { return m_eliminateDeadband ? m_deadbandMinPwm : m_centerPwm - 1; };
111 INT32 GetMinNegativePwm() { return m_minPwm; };
112 INT32 GetPositiveScaleFactor() {return GetMaxPositivePwm() - GetMinPositivePwm();} ///< The scale for positive speeds.
113 INT32 GetNegativeScaleFactor() {return GetMaxNegativePwm() - GetMinNegativePwm();} ///< The scale for negative speeds.
114 INT32 GetFullRangeScaleFactor() {return GetMaxPositivePwm() - GetMinNegativePwm();} ///< The scale for positions.
115};
116
117#endif