WPILibC++ 2024.3.1
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 void SetInverted(bool isInverted) override;
76
77 bool GetInverted() const override;
78
79 void Disable() override;
80
81 // MotorSafety interface
82 void StopMotor() override;
83 std::string GetDescription() const override;
84
85 int GetChannel() const;
86
87 /**
88 * Optionally eliminate the deadband from a motor controller.
89 *
90 * @param eliminateDeadband If true, set the motor curve on the motor
91 * controller to eliminate the deadband in the middle
92 * of the range. Otherwise, keep the full range
93 * without modifying any values.
94 */
95 void EnableDeadbandElimination(bool eliminateDeadband);
96
97 /**
98 * Make the given PWM motor controller follow the output of this one.
99 *
100 * @param follower The motor controller follower.
101 */
103
104 /**
105 * Make the given PWM motor controller follow the output of this one.
106 *
107 * @param follower The motor controller follower.
108 */
109 template <std::derived_from<PWMMotorController> T>
110 void AddFollower(T&& follower) {
111 m_owningFollowers.emplace_back(
112 std::make_unique<std::decay_t<T>>(std::forward<T>(follower)));
113 }
114
115 protected:
116 /**
117 * Constructor for a PWM Motor %Controller connected via PWM.
118 *
119 * @param name Name to use for SendableRegistry
120 * @param channel The PWM channel that the controller is attached to. 0-9 are
121 * on-board, 10-19 are on the MXP port
122 */
124
125 void InitSendable(wpi::SendableBuilder& builder) override;
126
127 /// PWM instances for motor controller.
129
130 private:
131 bool m_isInverted = false;
132 std::vector<PWMMotorController*> m_nonowningFollowers;
133 std::vector<std::unique_ptr<PWMMotorController>> m_owningFollowers;
134
135 PWM* GetPwm() { return &m_pwm; }
136};
137
139
140} // 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: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:110
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
Called to stop the motor when the timeout expires.
PWM m_pwm
PWM instances for motor controller.
Definition: PWMMotorController.h:128
void SetVoltage(units::volt_t output) override
Sets the voltage output of the PWMMotorController.
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:19
Interface for Sendable objects.
Definition: Sendable.h:16
basic_string_view< char > string_view
Definition: core.h:501
#define WPI_IGNORE_DEPRECATED
Definition: deprecated.h:20
#define WPI_UNIGNORE_DEPRECATED
Definition: deprecated.h:31
Definition: AprilTagDetector_cv.h:11
constexpr const char * name(const T &)