WPILibC++ 2027.0.0-alpha-5
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"
22
23namespace wpi {
24
25/**
26 * Common base class for all PWM Motor Controllers.
27 */
29 : public MotorController,
30 public MotorSafety,
32 public wpi::util::SendableHelper<PWMMotorController> {
33 public:
36
37 void SetThrottle(double throttle) override;
38
39 double GetThrottle() const override;
40
41 /**
42 * Gets the voltage output of the motor controller, nominally between -12 V
43 * and 12 V.
44 *
45 * @return The voltage of the motor controller, nominally between -12 V and 12
46 * V.
47 */
48 virtual wpi::units::volt_t GetVoltage() const;
49
50 void SetInverted(bool isInverted) override;
51
52 bool GetInverted() const override;
53
54 void Disable() override;
55
56 // MotorSafety interface
57 void StopMotor() override;
58 std::string GetDescription() const override;
59
60 int GetChannel() const;
61
62 /**
63 * Optionally eliminate the deadband from a motor controller.
64 *
65 * @param eliminateDeadband If true, set the motor curve on the motor
66 * controller to eliminate the deadband in the middle
67 * of the range. Otherwise, keep the full range
68 * without modifying any values.
69 */
70 void EnableDeadbandElimination(bool eliminateDeadband);
71
72 /**
73 * Make the given PWM motor controller follow the output of this one.
74 *
75 * @param follower The motor controller follower.
76 */
78
79 /**
80 * Make the given PWM motor controller follow the output of this one.
81 *
82 * @param follower The motor controller follower.
83 */
84 template <std::derived_from<PWMMotorController> T>
85 void AddFollower(T&& follower) {
86 m_owningFollowers.emplace_back(
87 std::make_unique<std::decay_t<T>>(std::forward<T>(follower)));
88 }
89
90 protected:
91 /**
92 * Constructor for a PWM Motor Controller connected via PWM.
93 *
94 * @param name Name to use for SendableRegistry
95 * @param channel The SmartIO channel that the controller is attached to.
96 */
97 PWMMotorController(std::string_view name, int channel);
98
100
101 /// PWM instances for motor controller.
103
104 void SetDutyCycleInternal(double dutyCycle);
105 double GetDutyCycleInternal() const;
106
107 void SetBounds(wpi::units::microsecond_t maxPwm,
108 wpi::units::microsecond_t deadbandMaxPwm,
109 wpi::units::microsecond_t centerPwm,
110 wpi::units::microsecond_t deadbandMinPwm,
111 wpi::units::microsecond_t minPwm);
112
113 private:
114 bool m_isInverted = false;
115 std::vector<PWMMotorController*> m_nonowningFollowers;
116 std::vector<std::unique_ptr<PWMMotorController>> m_owningFollowers;
117
118 wpi::hal::SimDevice m_simDevice;
119 wpi::hal::SimDouble m_simThrottle;
120
121 bool m_eliminateDeadband{0};
122 wpi::units::microsecond_t m_minPwm{0};
123 wpi::units::microsecond_t m_deadbandMinPwm{0};
124 wpi::units::microsecond_t m_centerPwm{0};
125 wpi::units::microsecond_t m_deadbandMaxPwm{0};
126 wpi::units::microsecond_t m_maxPwm{0};
127
128 wpi::units::microsecond_t GetMinPositivePwm() const;
129 wpi::units::microsecond_t GetMaxNegativePwm() const;
130 wpi::units::microsecond_t GetPositiveScaleFactor() const;
131 wpi::units::microsecond_t GetNegativeScaleFactor() const;
132
133 PWM* GetPwm() { return &m_pwm; }
134};
135
136} // namespace wpi
@ name
Definition base.h:690
Interface for motor controlling devices.
Definition MotorController.hpp:14
Class for sending pulse-width modulation (PWM) signals.
Definition PWM.hpp:21
void SetThrottle(double throttle) override
Sets the throttle of the motor controller.
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
double GetDutyCycleInternal() const
double GetThrottle() const override
Gets the throttle of the motor controller.
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
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:85
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:102
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
@ PWM
Definition HandlesInternal.hpp:61
Definition CvSource.hpp:15