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