blob: 52007fb66168c3970aa4c1e0f4e276b5bb280680 [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#pragma once
7
8#include "SensorBase.h"
9#include "simulation/SimContinuousOutput.h"
10#include "LiveWindow/LiveWindowSendable.h"
11#include "tables/ITableListener.h"
12
13#include <memory>
14
15/**
16 * Class implements the PWM generation in the FPGA.
17 *
18 * The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They are mapped
19 * to the hardware dependent values, in this case 0-255 for the FPGA.
20 * Changes are immediately sent to the FPGA, and the update occurs at the next
21 * FPGA cycle. There is no delay.
22 *
23 * As of revision 0.1.10 of the FPGA, the FPGA interprets the 0-255 values as follows:
24 * - 255 = full "forward"
25 * - 254 to 129 = linear scaling from "full forward" to "center"
26 * - 128 = center value
27 * - 127 to 2 = linear scaling from "center" to "full reverse"
28 * - 1 = full "reverse"
29 * - 0 = disabled (i.e. PWM output is held low)
30 */
31class PWM : public SensorBase, public ITableListener, public LiveWindowSendable
32{
33public:
34 enum PeriodMultiplier
35 {
36 kPeriodMultiplier_1X = 1,
37 kPeriodMultiplier_2X = 2,
38 kPeriodMultiplier_4X = 4
39 };
40
41 explicit PWM(uint32_t channel);
42 virtual ~PWM();
43 virtual void SetRaw(unsigned short value);
44 void SetPeriodMultiplier(PeriodMultiplier mult);
45 void EnableDeadbandElimination(bool eliminateDeadband);
46 void SetBounds(int32_t max, int32_t deadbandMax, int32_t center, int32_t deadbandMin,
47 int32_t min);
48 void SetBounds(double max, double deadbandMax, double center, double deadbandMin, double min);
49 uint32_t GetChannel() const
50 {
51 return m_channel;
52 }
53
54protected:
55 /**
56 * kDefaultPwmPeriod is in ms
57 *
58 * - 20ms periods (50 Hz) are the "safest" setting in that this works for all devices
59 * - 20ms periods seem to be desirable for Vex Motors
60 * - 20ms periods are the specified period for HS-322HD servos, but work reliably down
61 * to 10.0 ms; starting at about 8.5ms, the servo sometimes hums and get hot;
62 * by 5.0ms the hum is nearly continuous
63 * - 10ms periods work well for Victor 884
64 * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed controllers.
65 * Due to the shipping firmware on the Jaguar, we can't run the update period less
66 * than 5.05 ms.
67 *
68 * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period scaling is implemented as an
69 * output squelch to get longer periods for old devices.
70 */
71 static const float kDefaultPwmPeriod;
72 /**
73 * kDefaultPwmCenter is the PWM range center in ms
74 */
75 static const float kDefaultPwmCenter;
76 /**
77 * kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
78 */
79 static const int32_t kDefaultPwmStepsDown;
80 static const int32_t kPwmDisabled;
81
82 virtual void SetPosition(float pos);
83 virtual float GetPosition() const;
84 virtual void SetSpeed(float speed);
85 virtual float GetSpeed() const;
86
87 bool m_eliminateDeadband;
88 int32_t m_centerPwm;
89
90 void ValueChanged(ITable* source, llvm::StringRef key,
91 std::shared_ptr<nt::Value> value, bool isNew) override;
92 void UpdateTable() override;
93 void StartLiveWindowMode() override;
94 void StopLiveWindowMode() override;
95 std::string GetSmartDashboardType() const override;
96 void InitTable(std::shared_ptr<ITable> subTable) override;
97 std::shared_ptr<ITable> GetTable() const override;
98
99 std::shared_ptr<ITable> m_table;
100
101private:
102 uint32_t m_channel;
103 SimContinuousOutput* impl;
104};