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.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Servo.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
constexpr double Servo::kMaxServoAngle;
|
|
|
|
|
constexpr double Servo::kMinServoAngle;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
constexpr double Servo::kDefaultMaxServoPWM;
|
|
|
|
|
constexpr double Servo::kDefaultMinServoPWM;
|
2014-05-30 14:19:57 -04:00
|
|
|
|
2018-11-22 21:15:26 -08:00
|
|
|
Servo::Servo(int channel) : PWM(channel) {
|
2015-06-29 02:43:44 -07:00
|
|
|
// Set minimum and maximum PWM values supported by the servo
|
|
|
|
|
SetBounds(kDefaultMaxServoPWM, 0.0, 0.0, 0.0, kDefaultMinServoPWM);
|
|
|
|
|
|
|
|
|
|
// Assign defaults for period multiplier for the servo PWM control signal
|
|
|
|
|
SetPeriodMultiplier(kPeriodMultiplier_4X);
|
2017-09-19 21:17:27 -07:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Servo, channel + 1);
|
2021-06-13 16:38:05 -07:00
|
|
|
wpi::SendableRegistry::GetInstance().SetName(this, "Servo", channel);
|
2015-08-13 23:17:19 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Servo::Set(double value) {
|
|
|
|
|
SetPosition(value);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Servo::SetOffline() {
|
|
|
|
|
SetRaw(0);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double Servo::Get() const {
|
|
|
|
|
return GetPosition();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void Servo::SetAngle(double degrees) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (degrees < kMinServoAngle) {
|
|
|
|
|
degrees = kMinServoAngle;
|
|
|
|
|
} else if (degrees > kMaxServoAngle) {
|
|
|
|
|
degrees = kMaxServoAngle;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
SetPosition((degrees - kMinServoAngle) / GetServoAngleRange());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double Servo::GetAngle() const {
|
|
|
|
|
return GetPosition() * GetServoAngleRange() + kMinServoAngle;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double Servo::GetMaxAngle() const {
|
|
|
|
|
return kMaxServoAngle;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
double Servo::GetMinAngle() const {
|
|
|
|
|
return kMinServoAngle;
|
|
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void Servo::InitSendable(wpi::SendableBuilder& builder) {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("Servo");
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2021-06-12 08:06:45 -07:00
|
|
|
"Value", [=] { return Get(); }, [=](double value) { Set(value); });
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
double Servo::GetServoAngleRange() const {
|
|
|
|
|
return kMaxServoAngle - kMinServoAngle;
|
|
|
|
|
}
|