WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
PWM.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2016. 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 #pragma once
9 
10 #include "SensorBase.h"
11 #include "LiveWindow/LiveWindowSendable.h"
12 #include "tables/ITableListener.h"
13 
14 #include <memory>
15 
34 class PWM : public SensorBase,
35  public ITableListener,
36  public LiveWindowSendable {
37  public:
38  enum PeriodMultiplier {
39  kPeriodMultiplier_1X = 1,
40  kPeriodMultiplier_2X = 2,
41  kPeriodMultiplier_4X = 4
42  };
43 
44  explicit PWM(uint32_t channel);
45  virtual ~PWM();
46  virtual void SetRaw(unsigned short value);
47  virtual unsigned short GetRaw() const;
48  void SetPeriodMultiplier(PeriodMultiplier mult);
49  void SetZeroLatch();
50  void EnableDeadbandElimination(bool eliminateDeadband);
51  void SetBounds(int32_t max, int32_t deadbandMax, int32_t center,
52  int32_t deadbandMin, int32_t min);
53  void SetBounds(double max, double deadbandMax, double center,
54  double deadbandMin, double min);
55  uint32_t GetChannel() const { return m_channel; }
56 
57  protected:
80  static constexpr float kDefaultPwmPeriod = 5.05;
84  static constexpr float kDefaultPwmCenter = 1.5;
88  static const int32_t kDefaultPwmStepsDown = 1000;
89  static const int32_t kPwmDisabled = 0;
90 
91  virtual void SetPosition(float pos);
92  virtual float GetPosition() const;
93  virtual void SetSpeed(float speed);
94  virtual float GetSpeed() const;
95 
96  bool m_eliminateDeadband;
97  int32_t m_maxPwm;
98  int32_t m_deadbandMaxPwm;
99  int32_t m_centerPwm;
100  int32_t m_deadbandMinPwm;
101  int32_t m_minPwm;
102 
103  void ValueChanged(ITable* source, llvm::StringRef key,
104  std::shared_ptr<nt::Value> value, bool isNew) override;
105  void UpdateTable() override;
106  void StartLiveWindowMode() override;
107  void StopLiveWindowMode() override;
108  std::string GetSmartDashboardType() const override;
109  void InitTable(std::shared_ptr<ITable> subTable) override;
110  std::shared_ptr<ITable> GetTable() const override;
111 
112  std::shared_ptr<ITable> m_table;
113 
114  private:
115  uint32_t m_channel;
116  int32_t GetMaxPositivePwm() const { return m_maxPwm; }
117  int32_t GetMinPositivePwm() const {
118  return m_eliminateDeadband ? m_deadbandMaxPwm : m_centerPwm + 1;
119  }
120  int32_t GetCenterPwm() const { return m_centerPwm; }
121  int32_t GetMaxNegativePwm() const {
122  return m_eliminateDeadband ? m_deadbandMinPwm : m_centerPwm - 1;
123  }
124  int32_t GetMinNegativePwm() const { return m_minPwm; }
125  int32_t GetPositiveScaleFactor() const {
126  return GetMaxPositivePwm() - GetMinPositivePwm();
127  }
128  int32_t GetNegativeScaleFactor() const {
129  return GetMaxNegativePwm() - GetMinNegativePwm();
130  }
131  int32_t GetFullRangeScaleFactor() const {
132  return GetMaxPositivePwm() - GetMinNegativePwm();
133  }
134 };
A table whose values can be read and written to.
Definition: ITable.h:43
static const int32_t kDefaultPwmStepsDown
kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
Definition: PWM.h:88
Live Window Sendable is a special type of object sendable to the live window.
Definition: LiveWindowSendable.h:18
Class implements the PWM generation in the FPGA.
Definition: PWM.h:34
virtual float GetPosition() const
Get the PWM value in terms of a position.
Definition: PWM.cpp:182
PWM(uint32_t channel)
Allocate a PWM given a channel number.
Definition: PWM.cpp:31
Base class for all sensors.
Definition: SensorBase.h:20
void InitTable(std::shared_ptr< ITable > subTable) override
Initializes a table for this sendable object.
Definition: PWM.cpp:369
static constexpr float kDefaultPwmCenter
kDefaultPwmCenter is the PWM range center in ms
Definition: PWM.h:84
virtual ~PWM()
Free the PWM channel.
Definition: PWM.cpp:59
void UpdateTable() override
Update the table for this sendable object with the latest values.
Definition: PWM.cpp:347
virtual void SetPosition(float pos)
Set the PWM value based on a position.
Definition: PWM.cpp:148
virtual float GetSpeed() const
Get the PWM value in terms of speed.
Definition: PWM.cpp:250
std::string GetSmartDashboardType() const override
Definition: PWM.cpp:367
void StopLiveWindowMode() override
Stop having this sendable object automatically respond to value changes.
Definition: PWM.cpp:360
virtual unsigned short GetRaw() const
Get the PWM value directly from the hardware.
Definition: PWM.cpp:292
void StartLiveWindowMode() override
Start having this sendable object automatically respond to value changes reflect the value on the tab...
Definition: PWM.cpp:353
A listener that listens to changes in values in a ITable.
Definition: ITableListener.h:18
virtual void SetRaw(unsigned short value)
Set the PWM value directly to the hardware.
Definition: PWM.cpp:277
void SetBounds(int32_t max, int32_t deadbandMax, int32_t center, int32_t deadbandMin, int32_t min)
Set the bounds on the PWM values.
Definition: PWM.cpp:95
static constexpr float kDefaultPwmPeriod
kDefaultPwmPeriod is in ms
Definition: PWM.h:80
void ValueChanged(ITable *source, llvm::StringRef key, std::shared_ptr< nt::Value > value, bool isNew) override
Called when a key-value pair is changed in a ITable.
Definition: PWM.cpp:341
void EnableDeadbandElimination(bool eliminateDeadband)
Optionally eliminate the deadband from a speed controller.
Definition: PWM.cpp:79
virtual void SetSpeed(float speed)
Set the PWM value based on a speed.
Definition: PWM.cpp:208
void SetPeriodMultiplier(PeriodMultiplier mult)
Slow down the PWM signal for old devices.
Definition: PWM.cpp:307
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39
std::shared_ptr< ITable > GetTable() const override
Definition: PWM.cpp:374