2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
#include <hal/SimDevice.h>
|
2023-06-22 19:43:16 -07:00
|
|
|
#include <units/angle.h>
|
|
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
#include "frc/PWM.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
namespace sim {
|
|
|
|
|
class ServoSim;
|
|
|
|
|
} // namespace sim
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Standard hobby style servo.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* The range parameters default to the appropriate values for the Hitec HS-322HD
|
2017-11-16 00:33:51 -08:00
|
|
|
* servo provided in the FIRST Kit of Parts in 2008.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2025-03-20 19:23:22 -07:00
|
|
|
class Servo : public wpi::Sendable, public wpi::SendableHelper<Servo> {
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
2025-03-20 19:23:22 -07:00
|
|
|
friend class frc::sim::ServoSim;
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
2024-01-05 07:35:59 -08:00
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* By default, 2.4 ms is used as the max PWM value and 0.6 ms is used as the
|
|
|
|
|
* min PWM value.
|
|
|
|
|
*
|
2018-05-31 20:47:15 -07:00
|
|
|
* @param channel The PWM channel to which the servo is attached. 0-9 are
|
|
|
|
|
* on-board, 10-19 are on the MXP port
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
explicit Servo(int channel);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
Servo(Servo&&) = default;
|
|
|
|
|
Servo& operator=(Servo&&) = default;
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Set the servo position.
|
|
|
|
|
*
|
|
|
|
|
* Servo values range from 0.0 to 1.0 corresponding to the range of full left
|
|
|
|
|
* to full right.
|
|
|
|
|
*
|
|
|
|
|
* @param value Position from 0.0 to 1.0.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void Set(double value);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the servo position.
|
|
|
|
|
*
|
|
|
|
|
* Servo values range from 0.0 to 1.0 corresponding to the range of full left
|
2021-12-23 22:22:18 -08:00
|
|
|
* to full right. This returns the commanded position, not the position that
|
|
|
|
|
* the servo is actually at, as the servo does not report its own position.
|
2018-05-31 20:47:15 -07:00
|
|
|
*
|
|
|
|
|
* @return Position from 0.0 to 1.0.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Get() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the servo angle.
|
|
|
|
|
*
|
2021-12-19 13:53:31 -08:00
|
|
|
* The angles are based on the HS-322HD Servo, and have a range of 0 to 180
|
|
|
|
|
* degrees.
|
2018-05-31 20:47:15 -07:00
|
|
|
*
|
|
|
|
|
* Servo angles that are out of the supported range of the servo simply
|
|
|
|
|
* "saturate" in that direction. In other words, if the servo has a range of
|
|
|
|
|
* (X degrees to Y degrees) than angles of less than X result in an angle of
|
|
|
|
|
* X being set and angles of more than Y degrees result in an angle of Y being
|
|
|
|
|
* set.
|
|
|
|
|
*
|
2021-10-14 18:09:38 -07:00
|
|
|
* @param angle The angle in degrees to set the servo.
|
2018-05-31 20:47:15 -07:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void SetAngle(double angle);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the servo angle.
|
|
|
|
|
*
|
2021-12-23 22:22:18 -08:00
|
|
|
* This returns the commanded angle, not the angle that the servo is actually
|
|
|
|
|
* at, as the servo does not report its own angle.
|
2018-05-31 20:47:15 -07:00
|
|
|
*
|
|
|
|
|
* @return The angle in degrees to which the servo is set.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetAngle() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
int GetChannel() const;
|
2014-05-02 17:54:01 -04:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void InitSendable(wpi::SendableBuilder& builder) override;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2025-03-20 19:23:22 -07:00
|
|
|
static double GetServoAngleRange();
|
|
|
|
|
units::microsecond_t GetFullRangeScaleFactor() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
static constexpr double kMaxServoAngle = 180.0;
|
2016-11-20 07:25:03 -08:00
|
|
|
static constexpr double kMinServoAngle = 0.0;
|
2014-05-30 14:19:57 -04:00
|
|
|
|
2023-06-22 19:43:16 -07:00
|
|
|
static constexpr units::millisecond_t kDefaultMaxServoPWM = 2.4_ms;
|
|
|
|
|
static constexpr units::millisecond_t kDefaultMinServoPWM = 0.6_ms;
|
2025-03-20 19:23:22 -07:00
|
|
|
|
|
|
|
|
units::millisecond_t m_maxPwm = kDefaultMaxServoPWM;
|
|
|
|
|
units::millisecond_t m_minPwm = kDefaultMinServoPWM;
|
|
|
|
|
|
|
|
|
|
hal::SimDevice m_simDevice;
|
|
|
|
|
hal::SimDouble m_simPosition;
|
|
|
|
|
|
|
|
|
|
PWM m_pwm;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|