2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2008-2017 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Servo.h"
|
|
|
|
|
|
2017-09-19 21:17:27 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
|
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
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param channel The PWM channel to which the servo is attached. 0-9 are
|
2016-05-20 17:30:37 -07:00
|
|
|
* on-board, 10-19 are on the MXP port
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
Servo::Servo(int channel) : SafePWM(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
|
|
|
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Servo, channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
Servo::~Servo() {
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueListener != 0) m_valueEntry.RemoveListener(m_valueListener);
|
2015-08-13 23:17:19 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the servo position.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Servo values range from 0.0 to 1.0 corresponding to the range of full left to
|
|
|
|
|
* full right.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
|
|
|
|
* @param value Position from 0.0 to 1.0.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void Servo::Set(double value) { SetPosition(value); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the servo to offline.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* Set the servo raw value to 0 (undriven)
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Servo::SetOffline() { SetRaw(0); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the servo position.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Servo values range from 0.0 to 1.0 corresponding to the range of full left to
|
|
|
|
|
* full right.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
|
|
|
|
* @return Position from 0.0 to 1.0.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Servo::Get() const { return GetPosition(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the servo angle.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Assume that the servo angle is linear with respect to the PWM value (big
|
|
|
|
|
* assumption, need to test).
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Servo angles that are out of the supported range of the servo simply
|
2016-05-20 17:30:37 -07:00
|
|
|
* "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.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
|
|
|
|
* @param degrees The angle in degrees to set the servo.
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the servo angle.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Assume that the servo angle is linear with respect to the PWM value (big
|
|
|
|
|
* assumption, need to test).
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The angle in degrees to which the servo is set.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Servo::GetAngle() const {
|
|
|
|
|
return GetPosition() * GetServoAngleRange() + kMinServoAngle;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Servo::UpdateTable() {
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueEntry) m_valueEntry.SetDouble(Get());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Servo::StartLiveWindowMode() {
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueEntry) {
|
|
|
|
|
m_valueListener = m_valueEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsDouble()) return;
|
|
|
|
|
Set(event.value->GetDouble());
|
|
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Servo::StopLiveWindowMode() {
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueListener != 0) {
|
|
|
|
|
m_valueEntry.RemoveListener(m_valueListener);
|
|
|
|
|
m_valueListener = 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Servo::GetSmartDashboardType() const { return "Servo"; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
void Servo::InitTable(std::shared_ptr<nt::NetworkTable> subTable) {
|
2017-09-02 00:35:30 -07:00
|
|
|
if (subTable) {
|
|
|
|
|
m_valueEntry = subTable->GetEntry("Value");
|
2017-09-02 00:17:43 -07:00
|
|
|
UpdateTable();
|
|
|
|
|
} else {
|
|
|
|
|
m_valueEntry = nt::NetworkTableEntry();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|