Files
allwpilib/wpilibc/src/main/native/cpp/Servo.cpp
Peter Johnson 471f375a38 Simplify Sendable interface (#1864)
This removes the name and subsystem from individual objects, and instead
puts this data into a new singleton class, SendableRegistry.  Much of
LiveWindow has been refactored into SendableRegistry.

In C++, a new CRTP helper class, SendableHelper, has been added to provide
move and destruction functionality.

Shims for GetName, SetName, GetSubsystem, and SetSubsystem have been added
to Command and Subsystem (both old and new), and also to SendableHelper to
prevent code breakage.

This deprecates SendableBase in preparation for future removal.
2019-09-14 15:22:54 -05:00

67 lines
2.1 KiB
C++

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "frc/Servo.h"
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
constexpr double Servo::kMaxServoAngle;
constexpr double Servo::kMinServoAngle;
constexpr double Servo::kDefaultMaxServoPWM;
constexpr double Servo::kDefaultMinServoPWM;
Servo::Servo(int channel) : PWM(channel) {
// 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);
HAL_Report(HALUsageReporting::kResourceType_Servo, channel);
SendableRegistry::GetInstance().SetName(this, "Servo", channel);
}
void Servo::Set(double value) { SetPosition(value); }
void Servo::SetOffline() { SetRaw(0); }
double Servo::Get() const { return GetPosition(); }
void Servo::SetAngle(double degrees) {
if (degrees < kMinServoAngle) {
degrees = kMinServoAngle;
} else if (degrees > kMaxServoAngle) {
degrees = kMaxServoAngle;
}
SetPosition((degrees - kMinServoAngle) / GetServoAngleRange());
}
double Servo::GetAngle() const {
return GetPosition() * GetServoAngleRange() + kMinServoAngle;
}
double Servo::GetMaxAngle() const { return kMaxServoAngle; }
double Servo::GetMinAngle() const { return kMinServoAngle; }
void Servo::InitSendable(SendableBuilder& builder) {
builder.SetSmartDashboardType("Servo");
builder.AddDoubleProperty("Value", [=]() { return Get(); },
[=](double value) { Set(value); });
}
double Servo::GetServoAngleRange() const {
return kMaxServoAngle - kMinServoAngle;
}