WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
PWMMotorController.hpp
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 "wpi/hal/SimDevice.hpp"
19#include "wpi/units/voltage.hpp"
23
24namespace wpi {
25
27
28/**
29 * Common base class for all PWM Motor Controllers.
30 */
32 : public MotorController,
33 public MotorSafety,
35 public wpi::util::SendableHelper<PWMMotorController> {
36 public:
39
40 void SetDutyCycle(double dutyCycle) override;
41
42 double GetDutyCycle() const override;
43
44 /**
45 * Gets the voltage output of the motor controller, nominally between -12 V
46 * and 12 V.
47 *
48 * @return The voltage of the motor controller, nominally between -12 V and 12
49 * V.
50 */
51 virtual wpi::units::volt_t GetVoltage() const;
52
53 void SetInverted(bool isInverted) override;
54
55 bool GetInverted() const override;
56
57 void Disable() override;
58
59 // MotorSafety interface
60 void StopMotor() override;
61 std::string GetDescription() const override;
62
63 int GetChannel() const;
64
65 /**
66 * Optionally eliminate the deadband from a motor controller.
67 *
68 * @param eliminateDeadband If true, set the motor curve on the motor
69 * controller to eliminate the deadband in the middle
70 * of the range. Otherwise, keep the full range
71 * without modifying any values.
72 */
73 void EnableDeadbandElimination(bool eliminateDeadband);
74
75 /**
76 * Make the given PWM motor controller follow the output of this one.
77 *
78 * @param follower The motor controller follower.
79 */
81
82 /**
83 * Make the given PWM motor controller follow the output of this one.
84 *
85 * @param follower The motor controller follower.
86 */
87 template <std::derived_from<PWMMotorController> T>
88 void AddFollower(T&& follower) {
89 m_owningFollowers.emplace_back(
90 std::make_unique<std::decay_t<T>>(std::forward<T>(follower)));
91 }
92
93 protected:
94 /**
95 * Constructor for a PWM Motor %Controller connected via PWM.
96 *
97 * @param name Name to use for SendableRegistry
98 * @param channel The PWM channel that the controller is attached to. 0-9 are
99 * on-board, 10-19 are on the MXP port
100 */
101 PWMMotorController(std::string_view name, int channel);
102
104
105 /// PWM instances for motor controller.
107
108 void SetDutyCycleInternal(double dutyCycle);
109 double GetDutyCycleInternal() const;
110
111 void SetBounds(wpi::units::microsecond_t maxPwm,
112 wpi::units::microsecond_t deadbandMaxPwm,
113 wpi::units::microsecond_t centerPwm,
114 wpi::units::microsecond_t deadbandMinPwm,
115 wpi::units::microsecond_t minPwm);
116
117 private:
118 bool m_isInverted = false;
119 std::vector<PWMMotorController*> m_nonowningFollowers;
120 std::vector<std::unique_ptr<PWMMotorController>> m_owningFollowers;
121
122 wpi::hal::SimDevice m_simDevice;
123 wpi::hal::SimDouble m_simDutyCycle;
124
125 bool m_eliminateDeadband{0};
126 wpi::units::microsecond_t m_minPwm{0};
127 wpi::units::microsecond_t m_deadbandMinPwm{0};
128 wpi::units::microsecond_t m_centerPwm{0};
129 wpi::units::microsecond_t m_deadbandMaxPwm{0};
130 wpi::units::microsecond_t m_maxPwm{0};
131
132 wpi::units::microsecond_t GetMinPositivePwm() const;
133 wpi::units::microsecond_t GetMaxNegativePwm() const;
134 wpi::units::microsecond_t GetPositiveScaleFactor() const;
135 wpi::units::microsecond_t GetNegativeScaleFactor() const;
136
137 PWM* GetPwm() { return &m_pwm; }
138};
139
141
142} // namespace wpi
@ name
Definition base.h:690
Interface for motor controlling devices.
Definition MotorController.hpp:14
Class implements the PWM generation in the FPGA.
Definition PWM.hpp:26
virtual wpi::units::volt_t GetVoltage() const
Gets the voltage output of the motor controller, nominally between -12 V and 12 V.
void Disable() override
Disables the motor controller.
PWMMotorController & operator=(PWMMotorController &&)=default
void SetDutyCycle(double dutyCycle) override
Sets the duty cycle of the motor controller.
double GetDutyCycleInternal() const
void AddFollower(PWMMotorController &follower)
Make the given PWM motor controller follow the output of this one.
void SetInverted(bool isInverted) override
Sets the inversion state of the motor controller.
void SetDutyCycleInternal(double dutyCycle)
PWMMotorController(std::string_view name, int channel)
Constructor for a PWM Motor Controller connected via PWM.
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
bool GetInverted() const override
Gets the inversion state of the motor controller.
std::string GetDescription() const override
Returns a description to print when an error occurs.
PWMMotorController(PWMMotorController &&)=default
double GetDutyCycle() const override
Gets the duty cycle of the motor controller.
void EnableDeadbandElimination(bool eliminateDeadband)
Optionally eliminate the deadband from a motor controller.
void AddFollower(T &&follower)
Make the given PWM motor controller follow the output of this one.
Definition PWMMotorController.hpp:88
void SetBounds(wpi::units::microsecond_t maxPwm, wpi::units::microsecond_t deadbandMaxPwm, wpi::units::microsecond_t centerPwm, wpi::units::microsecond_t deadbandMinPwm, wpi::units::microsecond_t minPwm)
void StopMotor() override
Called to stop the motor when the timeout expires.
PWM m_pwm
PWM instances for motor controller.
Definition PWMMotorController.hpp:106
A move-only C++ wrapper around a HAL simulator device handle.
Definition SimDevice.hpp:278
C++ wrapper around a HAL simulator double value handle.
Definition SimDevice.hpp:169
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.hpp:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.hpp:21
Interface for Sendable objects.
Definition Sendable.hpp:16
#define WPI_IGNORE_DEPRECATED
Definition deprecated.hpp:15
#define WPI_UNIGNORE_DEPRECATED
Definition deprecated.hpp:26
@ PWM
Definition HandlesInternal.hpp:55
Definition CvSource.hpp:15