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