WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
ExpansionHubServo.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
12#include "wpi/units/angle.hpp"
13#include "wpi/units/time.hpp"
14
15namespace wpi {
16
17/** This class controls a specific servo in positional/servo mode hooked up to
18 * an ExpansionHub. */
20 public:
21 /**
22 * Constructs a servo at the requested channel on a specific USB port.
23 *
24 * @sa ExpansionHubCRServo if the servo is in continuous rotation mode
25 * @param usbId The USB port ID the hub is connected to
26 * @param channel The servo channel
27 */
28 ExpansionHubServo(int usbId, int channel);
30
31 /**
32 * Set the servo position.
33 *
34 * Servo values range from 0.0 to 1.0 corresponding to the range of full left
35 * to full right.
36 *
37 * @param value Position from 0.0 to 1.0.
38 */
39 void SetPosition(double value);
40
41 /**
42 * Sets the servo angle.
43 *
44 * Servo angles range defaults to 0 to 180 degrees, but can be changed with
45 * setAngleRange().
46 *
47 * @param angle Position in angle units. Will be clamped to be within the
48 * current angle range.
49 */
50 void SetAngle(wpi::units::degree_t angle);
51
52 /**
53 * Sets the raw pulse width output on the servo.
54 *
55 * @param pulseWidth Pulse width
56 */
57 void SetPulseWidth(wpi::units::microsecond_t pulseWidth);
58
59 /**
60 * Sets if the servo output is enabled or not. Defaults to false.
61 *
62 * @param enabled True to enable, false to disable
63 */
64 void SetEnabled(bool enabled);
65
66 /**
67 * Sets the frame period for the servo. Defaults to 20ms.
68 *
69 * @param framePeriod The frame period
70 */
71 void SetFramePeriod(wpi::units::microsecond_t framePeriod);
72
73 /**
74 * Gets if the underlying ExpansionHub is connected.
75 *
76 * @return True if hub is connected, otherwise false
77 */
78 bool IsHubConnected() const { return m_hub.IsHubConnected(); }
79
80 /**
81 * Sets the angle range for the SetAngle call.
82 * By default, this is 0 to 180 degrees.
83 *
84 * Maximum angle must be greater than minimum angle.
85 *
86 * @param minAngle Minimum angle
87 * @param maxAngle Maximum angle
88 */
89 void SetAngleRange(wpi::units::degree_t minAngle,
90 wpi::units::degree_t maxAngle);
91
92 /**
93 * Sets the PWM range for the servo.
94 * By default, this is 600 to 2400 microseconds.
95 *
96 * Maximum must be greater than minimum.
97 *
98 * @param minPwm Minimum PWM
99 * @param maxPwm Maximum PWM
100 */
101 void SetPWMRange(wpi::units::microsecond_t minPwm,
102 wpi::units::microsecond_t maxPwm);
103
104 /**
105 * Sets whether the servo is reversed.
106 *
107 * This will reverse both SetPosition() and SetAngle().
108 *
109 * @param reversed True to reverse, false for normal
110 */
111 void SetReversed(bool reversed);
112
113 private:
114 wpi::units::microsecond_t GetFullRangeScaleFactor();
115 wpi::units::degree_t GetServoAngleRange();
116
117 ExpansionHub m_hub;
118 int m_channel;
119
120 wpi::units::degree_t m_maxServoAngle = 180.0_deg;
121 wpi::units::degree_t m_minServoAngle = 0.0_deg;
122
123 wpi::units::microsecond_t m_minPwm = 600_us;
124 wpi::units::microsecond_t m_maxPwm = 2400_us;
125
126 bool m_reversed = false;
127
128 wpi::nt::IntegerPublisher m_pulseWidthPublisher;
129 wpi::nt::IntegerPublisher m_framePeriodPublisher;
130 wpi::nt::BooleanPublisher m_enabledPublisher;
131};
132} // namespace wpi
This class controls a REV ExpansionHub plugged in over USB to Systemcore.
Definition ExpansionHub.hpp:18
void SetPWMRange(wpi::units::microsecond_t minPwm, wpi::units::microsecond_t maxPwm)
Sets the PWM range for the servo.
ExpansionHubServo(int usbId, int channel)
Constructs a servo at the requested channel on a specific USB port.
void SetPosition(double value)
Set the servo position.
void SetAngleRange(wpi::units::degree_t minAngle, wpi::units::degree_t maxAngle)
Sets the angle range for the SetAngle call.
void SetReversed(bool reversed)
Sets whether the servo is reversed.
void SetAngle(wpi::units::degree_t angle)
Sets the servo angle.
void SetPulseWidth(wpi::units::microsecond_t pulseWidth)
Sets the raw pulse width output on the servo.
void SetFramePeriod(wpi::units::microsecond_t framePeriod)
Sets the frame period for the servo.
~ExpansionHubServo() noexcept
void SetEnabled(bool enabled)
Sets if the servo output is enabled or not.
bool IsHubConnected() const
Gets if the underlying ExpansionHub is connected.
Definition ExpansionHubServo.hpp:78
NetworkTables Boolean publisher.
Definition BooleanTopic.hpp:126
NetworkTables Integer publisher.
Definition IntegerTopic.hpp:126
Definition CvSource.hpp:15