blob: e66a81ec4c13bec64d68d5a1785aa41b602a0e78 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. 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 <memory>
11#include <string>
12
13#include "HAL/Types.h"
14#include "LiveWindow/LiveWindowSendable.h"
15#include "SensorBase.h"
16#include "tables/ITableListener.h"
17
18namespace frc {
19
20/**
21 * Class implements the PWM generation in the FPGA.
22 *
23 * The values supplied as arguments for PWM outputs range from -1.0 to 1.0. They
24 * are mapped to the hardware dependent values, in this case 0-2000 for the
25 * FPGA. Changes are immediately sent to the FPGA, and the update occurs at the
26 * next FPGA cycle. There is no delay.
27 *
28 * As of revision 0.1.10 of the FPGA, the FPGA interprets the 0-2000 values as
29 * follows:
30 * - 2000 = maximum pulse width
31 * - 1999 to 1001 = linear scaling from "full forward" to "center"
32 * - 1000 = center value
33 * - 999 to 2 = linear scaling from "center" to "full reverse"
34 * - 1 = minimum pulse width (currently .5ms)
35 * - 0 = disabled (i.e. PWM output is held low)
36 */
37class PWM : public SensorBase,
38 public ITableListener,
39 public LiveWindowSendable {
40 public:
41 enum PeriodMultiplier {
42 kPeriodMultiplier_1X = 1,
43 kPeriodMultiplier_2X = 2,
44 kPeriodMultiplier_4X = 4
45 };
46
47 explicit PWM(int channel);
48 virtual ~PWM();
49 virtual void SetRaw(uint16_t value);
50 virtual uint16_t GetRaw() const;
51 virtual void SetPosition(double pos);
52 virtual double GetPosition() const;
53 virtual void SetSpeed(double speed);
54 virtual double GetSpeed() const;
55 virtual void SetDisabled();
56 void SetPeriodMultiplier(PeriodMultiplier mult);
57 void SetZeroLatch();
58 void EnableDeadbandElimination(bool eliminateDeadband);
59 void SetBounds(double max, double deadbandMax, double center,
60 double deadbandMin, double min);
61 void SetRawBounds(int max, int deadbandMax, int center, int deadbandMin,
62 int min);
63 void GetRawBounds(int32_t* max, int32_t* deadbandMax, int32_t* center,
64 int32_t* deadbandMin, int32_t* min);
65 int GetChannel() const { return m_channel; }
66
67 protected:
68 void ValueChanged(ITable* source, llvm::StringRef key,
69 std::shared_ptr<nt::Value> value, bool isNew) override;
70 void UpdateTable() override;
71 void StartLiveWindowMode() override;
72 void StopLiveWindowMode() override;
73 std::string GetSmartDashboardType() const override;
74 void InitTable(std::shared_ptr<ITable> subTable) override;
75 std::shared_ptr<ITable> GetTable() const override;
76
77 std::shared_ptr<ITable> m_table;
78
79 private:
80 int m_channel;
81 HAL_DigitalHandle m_handle;
82};
83
84} // namespace frc