-
Notifications
You must be signed in to change notification settings - Fork 701
[wpilib] Separate ExpansionHubServo into separate Servo and CRServo classes #8770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zachwaffle4
wants to merge
2
commits into
wpilibsuite:2027
Choose a base branch
from
zachwaffle4:exhub-servo
base: 2027
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
wpilibc/src/main/native/cpp/hardware/expansionhub/ExpansionHubCRServo.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright (c) FIRST and other WPILib contributors. | ||
| // Open Source Software; you can modify and/or share it under the terms of | ||
| // the WPILib BSD license file in the root directory of this project. | ||
|
|
||
| #include "wpi/hardware/expansionhub/ExpansionHubCRServo.hpp" | ||
|
|
||
| #include "wpi/system/Errors.hpp" | ||
| #include "wpi/system/SystemServer.hpp" | ||
|
|
||
| using namespace wpi; | ||
|
|
||
| ExpansionHubCRServo::ExpansionHubCRServo(int usbId, int channel) | ||
| : m_hub{usbId}, m_channel{channel} { | ||
| WPILIB_AssertMessage(channel >= 0 && channel < ExpansionHub::NumServoPorts, | ||
| "ExHub Servo Channel {} out of range", channel); | ||
|
|
||
| if (!m_hub.CheckAndReserveServo(channel)) { | ||
| throw WPILIB_MakeError(err::ResourceAlreadyAllocated, "Channel {}", | ||
| channel); | ||
| } | ||
|
|
||
| m_hub.ReportUsage(fmt::format("ExHubCRServo[{}]", channel), "ExHubCRServo"); | ||
|
|
||
| auto systemServer = SystemServer::GetSystemServer(); | ||
|
|
||
| wpi::nt::PubSubOptions options; | ||
| options.sendAll = true; | ||
| options.keepDuplicates = true; | ||
| options.periodic = 0.005; | ||
|
|
||
| m_pulseWidthPublisher = | ||
| systemServer | ||
| .GetIntegerTopic( | ||
| fmt::format("/rhsp/{}/servo{}/pulseWidth", usbId, channel)) | ||
| .Publish(options); | ||
|
|
||
| m_pulseWidthPublisher.Set(1500); | ||
|
|
||
| m_framePeriodPublisher = | ||
| systemServer | ||
| .GetIntegerTopic( | ||
| fmt::format("/rhsp/{}/servo{}/framePeriod", usbId, channel)) | ||
| .Publish(options); | ||
|
|
||
| m_framePeriodPublisher.Set(20000); | ||
|
|
||
| m_enabledPublisher = systemServer | ||
| .GetBooleanTopic(fmt::format( | ||
| "/rhsp/{}/servo{}/enabled", usbId, channel)) | ||
| .Publish(options); | ||
| } | ||
|
|
||
| ExpansionHubCRServo::~ExpansionHubCRServo() noexcept { | ||
| m_hub.UnreserveServo(m_channel); | ||
| } | ||
|
|
||
| void ExpansionHubCRServo::SetThrottle(double value) { | ||
| value = std::clamp(value, -1.0, 1.0); | ||
| value = (value + 1.0) / 2.0; | ||
| if (m_reversed) { | ||
| value = 1.0 - value; | ||
| } | ||
| auto rawValue = ((value * GetFullRangeScaleFactor()) + m_minPwm); | ||
| SetPulseWidth(rawValue); | ||
| } | ||
|
|
||
| void ExpansionHubCRServo::SetPulseWidth(wpi::units::microsecond_t pulseWidth) { | ||
| SetEnabled(true); | ||
| m_pulseWidthPublisher.Set(pulseWidth.value()); | ||
| } | ||
|
|
||
| void ExpansionHubCRServo::SetEnabled(bool enabled) { | ||
| m_enabledPublisher.Set(enabled); | ||
| } | ||
|
|
||
| void ExpansionHubCRServo::SetFramePeriod( | ||
| wpi::units::microsecond_t framePeriod) { | ||
| m_framePeriodPublisher.Set(framePeriod.value()); | ||
| } | ||
|
|
||
| wpi::units::microsecond_t ExpansionHubCRServo::GetFullRangeScaleFactor() const { | ||
| return m_maxPwm - m_minPwm; | ||
| } | ||
|
|
||
| void ExpansionHubCRServo::SetPWMRange(wpi::units::microsecond_t minPwm, | ||
| wpi::units::microsecond_t maxPwm) { | ||
| if (maxPwm <= minPwm) { | ||
| throw WPILIB_MakeError(err::ParameterOutOfRange, | ||
| "Max PWM must be greater than Min PWM"); | ||
| } | ||
| m_minPwm = minPwm; | ||
| m_maxPwm = maxPwm; | ||
| } | ||
|
|
||
| void ExpansionHubCRServo::SetReversed(bool reversed) { | ||
| m_reversed = reversed; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
wpilibc/src/main/native/include/wpi/hardware/expansionhub/ExpansionHubCRServo.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) FIRST and other WPILib contributors. | ||
| // Open Source Software; you can modify and/or share it under the terms of | ||
| // the WPILib BSD license file in the root directory of this project. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "wpi/hardware/expansionhub/ExpansionHub.hpp" | ||
| #include "wpi/nt/BooleanTopic.hpp" | ||
| #include "wpi/nt/IntegerTopic.hpp" | ||
| #include "wpi/units/time.hpp" | ||
|
|
||
| namespace wpi { | ||
| class ExpansionHubCRServo { | ||
| public: | ||
| /** | ||
| * Constructs a continuous rotation servo at the requested channel on a | ||
| * specific USB port. | ||
| * | ||
| * @sa ExpansionHubServo for a servo mode, or non-continuous rotation servo | ||
| * @param usbId The USB port ID the hub is connected to | ||
| * @param channel The servo channel | ||
| */ | ||
| ExpansionHubCRServo(int usbId, int channel); | ||
| ~ExpansionHubCRServo() noexcept; | ||
|
|
||
| /** | ||
| * Set the servo throttle. | ||
| * | ||
| * Throttle values range from -1.0 to 1.0 corresponding to full reverse to | ||
| * full forward. | ||
| * | ||
| * @param value Throttle from -1.0 to 1.0. | ||
| */ | ||
| void SetThrottle(double value); | ||
|
|
||
| /** | ||
| * Sets the raw pulse width output on the servo. | ||
| * | ||
| * @param pulseWidth Pulse width | ||
| */ | ||
| void SetPulseWidth(wpi::units::microsecond_t pulseWidth); | ||
|
|
||
| /** | ||
| * Sets if the servo output is enabled or not. Defaults to false. | ||
| * | ||
| * @param enabled True to enable, false to disable | ||
| */ | ||
| void SetEnabled(bool enabled); | ||
|
|
||
| /** | ||
| * Sets the frame period for the servo. Defaults to 20ms. | ||
| * | ||
| * @param framePeriod The frame period | ||
| */ | ||
| void SetFramePeriod(wpi::units::microsecond_t framePeriod); | ||
|
|
||
| /** | ||
| * Gets if the underlying ExpansionHub is connected. | ||
| * | ||
| * @return True if hub is connected, otherwise false | ||
| */ | ||
| bool IsHubConnected() const { return m_hub.IsHubConnected(); } | ||
|
|
||
| /** | ||
| * Sets the PWM range for the servo. | ||
| * By default, this is 600 to 2400 microseconds. | ||
| * | ||
| * Maximum must be greater than minimum. | ||
| * | ||
| * @param minPwm Minimum PWM | ||
| * @param maxPwm Maximum PWM | ||
| */ | ||
| void SetPWMRange(wpi::units::microsecond_t minPwm, | ||
| wpi::units::microsecond_t maxPwm); | ||
|
|
||
| /** | ||
| * Sets whether the servo is reversed. | ||
| * | ||
| * This will reverse SetThrottle. | ||
| * | ||
| * @param reversed True to reverse, false for normal | ||
| */ | ||
| void SetReversed(bool reversed); | ||
|
|
||
| private: | ||
| wpi::units::microsecond_t GetFullRangeScaleFactor() const; | ||
|
|
||
| ExpansionHub m_hub; | ||
| int m_channel; | ||
|
|
||
| wpi::units::microsecond_t m_minPwm = 600_us; | ||
| wpi::units::microsecond_t m_maxPwm = 2400_us; | ||
|
|
||
| bool m_reversed = false; | ||
|
|
||
| wpi::nt::IntegerPublisher m_pulseWidthPublisher; | ||
| wpi::nt::IntegerPublisher m_framePeriodPublisher; | ||
| wpi::nt::BooleanPublisher m_enabledPublisher; | ||
| }; | ||
| } // namespace wpi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This new class deserves a doc comment too.