WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
PWMMotorController.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <concepts>
8#include <memory>
9#include <string>
10#include <string_view>
11#include <type_traits>
12#include <utility>
13#include <vector>
14
15#include <hal/SimDevice.h>
16#include <units/voltage.h>
17#include <wpi/deprecated.h>
20
21#include "frc/MotorSafety.h"
22#include "frc/PWM.h"
24
25namespace frc {
26
28
29/**
30 * Common base class for all PWM Motor Controllers.
31 */
33 public MotorSafety,
34 public wpi::Sendable,
35 public wpi::SendableHelper<PWMMotorController> {
36 public:
39
40 /**
41 * Set the PWM value.
42 *
43 * The PWM value is set using a range of -1.0 to 1.0, appropriately scaling
44 * the value for the FPGA.
45 *
46 * @param value The speed value between -1.0 and 1.0 to set.
47 */
48 void Set(double value) override;
49
50 /**
51 * Sets the voltage output of the PWMMotorController. Compensates for
52 * the current bus voltage to ensure that the desired voltage is output even
53 * if the battery voltage is below 12V - highly useful when the voltage
54 * outputs are "meaningful" (e.g. they come from a feedforward calculation).
55 *
56 * <p>NOTE: This function *must* be called regularly in order for voltage
57 * compensation to work properly - unlike the ordinary set function, it is not
58 * "set it and forget it."
59 *
60 * @param output The voltage to output.
61 */
62 void SetVoltage(units::volt_t output) override;
63
64 /**
65 * Get the recently set value of the PWM. This value is affected by the
66 * inversion property. If you want the value that is sent directly to the
67 * MotorController, use PWM::GetSpeed() instead.
68 *
69 * @return The most recently set value for the PWM between -1.0 and 1.0.
70 */
71 double Get() const override;
72
73 /**
74 * Gets the voltage output of the motor controller, nominally between -12 V
75 * and 12 V.
76 *
77 * @return The voltage of the motor controller, nominally between -12 V and 12
78 * V.
79 */
80 virtual units::volt_t GetVoltage() const;
81
82 void SetInverted(bool isInverted) override;
83
84 bool GetInverted() const override;
85
86 void Disable() override;
87
88 // MotorSafety interface
89 void StopMotor() override;
90 std::string GetDescription() const override;
91
92 int GetChannel() const;
93
94 /**
95 * Optionally eliminate the deadband from a motor controller.
96 *
97 * @param eliminateDeadband If true, set the motor curve on the motor
98 * controller to eliminate the deadband in the middle
99 * of the range. Otherwise, keep the full range
100 * without modifying any values.
101 */
102 void EnableDeadbandElimination(bool eliminateDeadband);
103
104 /**
105 * Make the given PWM motor controller follow the output of this one.
106 *
107 * @param follower The motor controller follower.
108 */
110
111 /**
112 * Make the given PWM motor controller follow the output of this one.
113 *
114 * @param follower The motor controller follower.
115 */
116 template <std::derived_from<PWMMotorController> T>
117 void AddFollower(T&& follower) {
118 m_owningFollowers.emplace_back(
119 std::make_unique<std::decay_t<T>>(std::forward<T>(follower)));
120 }
121
122 protected:
123 /**
124 * Constructor for a PWM Motor %Controller connected via PWM.
125 *
126 * @param name Name to use for SendableRegistry
127 * @param channel The PWM channel that the controller is attached to. 0-9 are
128 * on-board, 10-19 are on the MXP port
129 */
130 PWMMotorController(std::string_view name, int channel);
131
132 void InitSendable(wpi::SendableBuilder& builder) override;
133
134 /// PWM instances for motor controller.
136
137 void SetSpeed(double speed);
138 double GetSpeed() const;
139
140 void SetBounds(units::microsecond_t maxPwm,
141 units::microsecond_t deadbandMaxPwm,
142 units::microsecond_t centerPwm,
143 units::microsecond_t deadbandMinPwm,
144 units::microsecond_t minPwm);
145
146 private:
147 bool m_isInverted = false;
148 std::vector<PWMMotorController*> m_nonowningFollowers;
149 std::vector<std::unique_ptr<PWMMotorController>> m_owningFollowers;
150
151 hal::SimDevice m_simDevice;
152 hal::SimDouble m_simSpeed;
153
154 bool m_eliminateDeadband{0};
155 units::microsecond_t m_minPwm{0};
156 units::microsecond_t m_deadbandMinPwm{0};
157 units::microsecond_t m_centerPwm{0};
158 units::microsecond_t m_deadbandMaxPwm{0};
159 units::microsecond_t m_maxPwm{0};
160
161 units::microsecond_t GetMinPositivePwm() const;
162 units::microsecond_t GetMaxNegativePwm() const;
163 units::microsecond_t GetPositiveScaleFactor() const;
164 units::microsecond_t GetNegativeScaleFactor() const;
165
166 PWM* GetPwm() { return &m_pwm; }
167};
168
170
171} // namespace frc
Interface for motor controlling devices.
Definition MotorController.h:14
The Motor Safety feature acts as a watchdog timer for an individual motor.
Definition MotorSafety.h:25
Class implements the PWM generation in the FPGA.
Definition PWM.h:26
Common base class for all PWM Motor Controllers.
Definition PWMMotorController.h:35
void Disable() override
Common interface for disabling a motor.
void AddFollower(T &&follower)
Make the given PWM motor controller follow the output of this one.
Definition PWMMotorController.h:117
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
PWMMotorController(std::string_view name, int channel)
Constructor for a PWM Motor Controller connected via PWM.
void SetSpeed(double speed)
bool GetInverted() const override
Common interface for returning the inversion state of a motor controller.
PWMMotorController & operator=(PWMMotorController &&)=default
void StopMotor() override
Common interface to stop the motor until Set is called again.
double GetSpeed() const
PWM m_pwm
PWM instances for motor controller.
Definition PWMMotorController.h:135
void SetBounds(units::microsecond_t maxPwm, units::microsecond_t deadbandMaxPwm, units::microsecond_t centerPwm, units::microsecond_t deadbandMinPwm, units::microsecond_t minPwm)
void SetVoltage(units::volt_t output) override
Sets the voltage output of the PWMMotorController.
virtual units::volt_t GetVoltage() const
Gets the voltage output of the motor controller, nominally between -12 V and 12 V.
void Set(double value) override
Set the PWM value.
double Get() const override
Get the recently set value of the PWM.
PWMMotorController(PWMMotorController &&)=default
std::string GetDescription() const override
Returns a description to print when an error occurs.
void AddFollower(PWMMotorController &follower)
Make the given PWM motor controller follow the output of this one.
void EnableDeadbandElimination(bool eliminateDeadband)
Optionally eliminate the deadband from a motor controller.
void SetInverted(bool isInverted) override
Common interface for inverting direction of a motor controller.
A move-only C++ wrapper around a HAL simulator device handle.
Definition SimDevice.h:645
C++ wrapper around a HAL simulator double value handle.
Definition SimDevice.h:536
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.h:21
Interface for Sendable objects.
Definition Sendable.h:16
#define WPI_IGNORE_DEPRECATED
Definition deprecated.h:16
#define WPI_UNIGNORE_DEPRECATED
Definition deprecated.h:27
Definition SystemServer.h:9