WPILibC++ 2025.2.1
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 <units/voltage.h>
16#include <wpi/deprecated.h>
19
20#include "frc/MotorSafety.h"
21#include "frc/PWM.h"
23
24namespace frc {
25class DMA;
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:
37 friend class DMA;
38
41
42 /**
43 * Set the PWM value.
44 *
45 * The PWM value is set using a range of -1.0 to 1.0, appropriately scaling
46 * the value for the FPGA.
47 *
48 * @param value The speed value between -1.0 and 1.0 to set.
49 */
50 void Set(double value) override;
51
52 /**
53 * Sets the voltage output of the PWMMotorController. Compensates for
54 * the current bus voltage to ensure that the desired voltage is output even
55 * if the battery voltage is below 12V - highly useful when the voltage
56 * outputs are "meaningful" (e.g. they come from a feedforward calculation).
57 *
58 * <p>NOTE: This function *must* be called regularly in order for voltage
59 * compensation to work properly - unlike the ordinary set function, it is not
60 * "set it and forget it."
61 *
62 * @param output The voltage to output.
63 */
64 void SetVoltage(units::volt_t output) override;
65
66 /**
67 * Get the recently set value of the PWM. This value is affected by the
68 * inversion property. If you want the value that is sent directly to the
69 * MotorController, use PWM::GetSpeed() instead.
70 *
71 * @return The most recently set value for the PWM between -1.0 and 1.0.
72 */
73 double Get() const override;
74
75 /**
76 * Gets the voltage output of the motor controller, nominally between -12 V
77 * and 12 V.
78 *
79 * @return The voltage of the motor controller, nominally between -12 V and 12
80 * V.
81 */
82 virtual units::volt_t GetVoltage() const;
83
84 void SetInverted(bool isInverted) override;
85
86 bool GetInverted() const override;
87
88 void Disable() override;
89
90 // MotorSafety interface
91 void StopMotor() override;
92 std::string GetDescription() const override;
93
94 int GetChannel() const;
95
96 /**
97 * Optionally eliminate the deadband from a motor controller.
98 *
99 * @param eliminateDeadband If true, set the motor curve on the motor
100 * controller to eliminate the deadband in the middle
101 * of the range. Otherwise, keep the full range
102 * without modifying any values.
103 */
104 void EnableDeadbandElimination(bool eliminateDeadband);
105
106 /**
107 * Make the given PWM motor controller follow the output of this one.
108 *
109 * @param follower The motor controller follower.
110 */
112
113 /**
114 * Make the given PWM motor controller follow the output of this one.
115 *
116 * @param follower The motor controller follower.
117 */
118 template <std::derived_from<PWMMotorController> T>
119 void AddFollower(T&& follower) {
120 m_owningFollowers.emplace_back(
121 std::make_unique<std::decay_t<T>>(std::forward<T>(follower)));
122 }
123
124 protected:
125 /**
126 * Constructor for a PWM Motor %Controller connected via PWM.
127 *
128 * @param name Name to use for SendableRegistry
129 * @param channel The PWM channel that the controller is attached to. 0-9 are
130 * on-board, 10-19 are on the MXP port
131 */
132 PWMMotorController(std::string_view name, int channel);
133
134 void InitSendable(wpi::SendableBuilder& builder) override;
135
136 /// PWM instances for motor controller.
138
139 private:
140 bool m_isInverted = false;
141 std::vector<PWMMotorController*> m_nonowningFollowers;
142 std::vector<std::unique_ptr<PWMMotorController>> m_owningFollowers;
143
144 PWM* GetPwm() { return &m_pwm; }
145};
146
148
149} // namespace frc
Class for configuring Direct Memory Access (DMA) of FPGA inputs.
Definition DMA.h:23
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:27
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:119
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.
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.
PWM m_pwm
PWM instances for motor controller.
Definition PWMMotorController.h:137
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.
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 CAN.h:11