mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[wpilib] Separate ExpansionHubServo into separate Servo and CRServo classes (#8770)
This removes the confusion of the `ExpansionHubServo` class serving both purposes, and thus having a `set` method that functions as `setPosition` when in servo mode and `setThrottle` when not in continuous mode. It also removes the `setContinuousRotationMethod` which could be confused for a method that switches the actual servo firmware itself from servo to continuous mode, which is not a thing that is physically possible I think. --------- Signed-off-by: Zach Harel <zach@zharel.me>
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
namespace wpi {
|
||||
class ExpansionHubServo;
|
||||
class ExpansionHubMotor;
|
||||
class ExpansionHubCRServo;
|
||||
|
||||
/** This class controls a REV ExpansionHub plugged in over USB to Systemcore. */
|
||||
class ExpansionHub {
|
||||
@@ -29,6 +30,7 @@ class ExpansionHub {
|
||||
|
||||
friend class ExpansionHubServo;
|
||||
friend class ExpansionHubMotor;
|
||||
friend class ExpansionHubCRServo;
|
||||
|
||||
/**
|
||||
* Constructs a servo at the requested channel on this hub.
|
||||
@@ -40,6 +42,8 @@ class ExpansionHub {
|
||||
*/
|
||||
ExpansionHubServo MakeServo(int channel);
|
||||
|
||||
ExpansionHubCRServo MakeCRServo(int channel);
|
||||
|
||||
/**
|
||||
* Constructs a motor at the requested channel on this hub.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
// 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 {
|
||||
|
||||
/** This class controls a specific servo in continuous rotation mode hooked up
|
||||
* to an ExpansionHub. */
|
||||
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
|
||||
@@ -14,12 +14,14 @@
|
||||
|
||||
namespace wpi {
|
||||
|
||||
/** This class controls a specific servo hooked up to an ExpansionHub. */
|
||||
/** This class controls a specific servo in positional/servo mode hooked up to
|
||||
* an ExpansionHub. */
|
||||
class ExpansionHubServo {
|
||||
public:
|
||||
/**
|
||||
* Constructs a servo at the requested channel on a specific USB port.
|
||||
*
|
||||
* @sa ExpansionHubCRServo if the servo is in continuous rotation mode
|
||||
* @param usbId The USB port ID the hub is connected to
|
||||
* @param channel The servo channel
|
||||
*/
|
||||
@@ -34,16 +36,16 @@ class ExpansionHubServo {
|
||||
*
|
||||
* @param value Position from 0.0 to 1.0.
|
||||
*/
|
||||
void Set(double value);
|
||||
void SetPosition(double value);
|
||||
|
||||
/**
|
||||
* Sets the servo angle
|
||||
* Sets the servo angle.
|
||||
*
|
||||
* Servo angles range from 0 to 180 degrees. Use Set() with your own scaler
|
||||
* for other angle ranges.
|
||||
* Servo angles range defaults to 0 to 180 degrees, but can be changed with
|
||||
* setAngleRange().
|
||||
*
|
||||
* @param angle Position in angle units. Will be scaled between 0 and 180
|
||||
* degrees
|
||||
* @param angle Position in angle units. Will be clamped to be within the
|
||||
* current angle range.
|
||||
*/
|
||||
void SetAngle(wpi::units::degree_t angle);
|
||||
|
||||
@@ -76,7 +78,7 @@ class ExpansionHubServo {
|
||||
bool IsHubConnected() const { return m_hub.IsHubConnected(); }
|
||||
|
||||
/**
|
||||
* Sets the angle range for the setAngle call.
|
||||
* Sets the angle range for the SetAngle call.
|
||||
* By default, this is 0 to 180 degrees.
|
||||
*
|
||||
* Maximum angle must be greater than minimum angle.
|
||||
@@ -102,22 +104,12 @@ class ExpansionHubServo {
|
||||
/**
|
||||
* Sets whether the servo is reversed.
|
||||
*
|
||||
* This will reverse both Set() and SetAngle().
|
||||
* This will reverse both SetPosition() and SetAngle().
|
||||
*
|
||||
* @param reversed True to reverse, false for normal
|
||||
*/
|
||||
void SetReversed(bool reversed);
|
||||
|
||||
/**
|
||||
* Enables or disables continuous rotation mode.
|
||||
*
|
||||
* In continuous rotation mode, the servo will interpret
|
||||
* Set() commands to between -1.0 and 1.0, instead of 0.0 to 1.0.
|
||||
*
|
||||
* @param enable True to enable continuous rotation mode, false to disable
|
||||
*/
|
||||
void SetContinuousRotationMode(bool enable);
|
||||
|
||||
private:
|
||||
wpi::units::microsecond_t GetFullRangeScaleFactor();
|
||||
wpi::units::degree_t GetServoAngleRange();
|
||||
@@ -132,7 +124,6 @@ class ExpansionHubServo {
|
||||
wpi::units::microsecond_t m_maxPwm = 2400_us;
|
||||
|
||||
bool m_reversed = false;
|
||||
bool m_continuousMode = false;
|
||||
|
||||
wpi::nt::IntegerPublisher m_pulseWidthPublisher;
|
||||
wpi::nt::IntegerPublisher m_framePeriodPublisher;
|
||||
|
||||
Reference in New Issue
Block a user