WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
ExpansionHubMotor.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
13#include "wpi/units/current.hpp"
14#include "wpi/units/voltage.hpp"
15
16namespace wpi {
17
18/** This class controls a specific motor and encoder hooked up to an
19 * ExpansionHub. */
21 public:
22 /**
23 * Constructs a servo at the requested channel on a specific USB port.
24 *
25 * @param usbId The USB port ID the hub is connected to
26 * @param channel The motor channel
27 */
28 ExpansionHubMotor(int usbId, int channel);
29
31
32 /**
33 * Sets the throttle.
34 *
35 * @param throttle The throttle where -1 indicates full reverse and 1
36 * indicates full forward.
37 */
38 void SetThrottle(double throttle);
39
40 /**
41 * Sets the voltage to run the motor at. This value will be continuously
42 * scaled to match the input voltage.
43 *
44 * @param voltage The voltage to drive the motor at
45 */
46 void SetVoltage(wpi::units::volt_t voltage);
47
48 /**
49 * Command the motor to drive to a specific position setpoint. This value will
50 * be scaled by SetDistancePerCount and influenced by the PID constants.
51 *
52 * @param setpoint The position setpoint to drive the motor to
53 */
54 void SetPositionSetpoint(double setpoint);
55
56 /**
57 * Command the motor to drive to a specific velocity setpoint. This value will
58 * be scaled by SetDistancePerCount and influenced by the PID constants.
59 *
60 * @param setpoint The velocity setpoint to drive the motor to
61 */
62 void SetVelocitySetpoint(double setpoint);
63
64 /**
65 * Sets if the motor output is enabled or not. Defaults to false.
66 *
67 * @param enabled True to enable, false to disable
68 */
69 void SetEnabled(bool enabled);
70
71 /**
72 * Sets if the motor should float or brake when 0 is commanded. Defaults to
73 * false.
74 *
75 * @param floatOn0 True to float when commanded 0, false to brake
76 */
77 void SetFloatOn0(bool floatOn0);
78
79 /**
80 * Gets the current being pulled by the motor.
81 *
82 * @return Motor current
83 */
84 wpi::units::ampere_t GetCurrent() const;
85
86 /**
87 * Sets the distance per count of the encoder. Used to scale encoder readings.
88 *
89 * @param perCount The distance moved per count of the encoder
90 */
91 void SetDistancePerCount(double perCount);
92
93 /**
94 * Gets the current velocity of the motor encoder. Scaled into
95 * distancePerCount units.
96 *
97 * @return Encoder velocity
98 */
99 double GetEncoderVelocity() const;
100
101 /**
102 * Gets the current position of the motor encoder. Scaled into
103 * distancePerCount units.
104 *
105 * @return Encoder position
106 */
107 double GetEncoderPosition() const;
108
109 /**
110 * Sets if the motor and encoder should be reversed.
111 *
112 * @param reversed True to reverse encoder, false otherwise
113 */
114 void SetReversed(bool reversed);
115
116 /** Reset the encoder count to 0. */
118
119 /**
120 * Gets the PID constants object for velocity PID.
121 *
122 * @return Velocity PID constants object
123 */
125
126 /**
127 * Gets the PID constants object for position PID.
128 *
129 * @return Position PID constants object
130 */
132
133 /**
134 * Gets if the underlying ExpansionHub is connected.
135 *
136 * @return True if hub is connected, otherwise false
137 */
138 bool IsHubConnected() { return m_hub.IsHubConnected(); }
139
140 /**
141 * Sets this motor to follow another motor on the same hub.
142 *
143 * This does not support following motors that are also followers.
144 * Additionally, the direction of both motors will be the same.
145 *
146 * @param leader The motor to follow
147 */
148 void Follow(const ExpansionHubMotor& leader);
149
150 private:
151 ExpansionHub m_hub;
152 int m_channel;
153
154 wpi::nt::DoubleSubscriber m_encoderSubscriber;
155 wpi::nt::DoubleSubscriber m_encoderVelocitySubscriber;
156 wpi::nt::DoubleSubscriber m_currentSubscriber;
157
158 wpi::nt::DoublePublisher m_setpointPublisher;
159 wpi::nt::BooleanPublisher m_floatOn0Publisher;
160 wpi::nt::BooleanPublisher m_enabledPublisher;
161
162 wpi::nt::IntegerPublisher m_modePublisher;
163
164 wpi::nt::BooleanPublisher m_reversedPublisher;
165 wpi::nt::BooleanPublisher m_resetEncoderPublisher;
166
167 wpi::nt::DoublePublisher m_distancePerCountPublisher;
168
169 ExpansionHubVelocityConstants m_velocityConstants;
170 ExpansionHubPositionConstants m_positionConstants;
171};
172} // namespace wpi
This class controls a REV ExpansionHub plugged in over USB to Systemcore.
Definition ExpansionHub.hpp:18
void SetFloatOn0(bool floatOn0)
Sets if the motor should float or brake when 0 is commanded.
void Follow(const ExpansionHubMotor &leader)
Sets this motor to follow another motor on the same hub.
ExpansionHubMotor(int usbId, int channel)
Constructs a servo at the requested channel on a specific USB port.
void SetThrottle(double throttle)
Sets the throttle.
void SetPositionSetpoint(double setpoint)
Command the motor to drive to a specific position setpoint.
~ExpansionHubMotor() noexcept
ExpansionHubPositionConstants & GetPositionConstants()
Gets the PID constants object for position PID.
void SetVoltage(wpi::units::volt_t voltage)
Sets the voltage to run the motor at.
void SetEnabled(bool enabled)
Sets if the motor output is enabled or not.
void SetVelocitySetpoint(double setpoint)
Command the motor to drive to a specific velocity setpoint.
void SetDistancePerCount(double perCount)
Sets the distance per count of the encoder.
void SetReversed(bool reversed)
Sets if the motor and encoder should be reversed.
wpi::units::ampere_t GetCurrent() const
Gets the current being pulled by the motor.
double GetEncoderPosition() const
Gets the current position of the motor encoder.
double GetEncoderVelocity() const
Gets the current velocity of the motor encoder.
void ResetEncoder()
Reset the encoder count to 0.
ExpansionHubVelocityConstants & GetVelocityConstants()
Gets the PID constants object for velocity PID.
bool IsHubConnected()
Gets if the underlying ExpansionHub is connected.
Definition ExpansionHubMotor.hpp:138
This class contains feedback and feedforward constants for an ExpansionHub motor.
Definition ExpansionHubPositionConstants.hpp:15
This class contains feedback and feedforward constants for an ExpansionHub motor.
Definition ExpansionHubVelocityConstants.hpp:14
NetworkTables Boolean publisher.
Definition BooleanTopic.hpp:126
NetworkTables Double publisher.
Definition DoubleTopic.hpp:126
NetworkTables Double subscriber.
Definition DoubleTopic.hpp:33
NetworkTables Integer publisher.
Definition IntegerTopic.hpp:126
Definition CvSource.hpp:15