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