2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2008-2017. 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"
|
|
|
|
|
|
|
|
|
|
#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);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
Servo::~Servo() {
|
|
|
|
|
if (m_table != nullptr) {
|
|
|
|
|
m_table->RemoveTableListener(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
void Servo::ValueChanged(ITable* source, llvm::StringRef key,
|
|
|
|
|
std::shared_ptr<nt::Value> value, bool isNew) {
|
|
|
|
|
if (!value->IsDouble()) return;
|
|
|
|
|
Set(value->GetDouble());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Servo::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutNumber("Value", Get());
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Servo::StartLiveWindowMode() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->AddTableListener("Value", this, true);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Servo::StopLiveWindowMode() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->RemoveTableListener(this);
|
|
|
|
|
}
|
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
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Servo::InitTable(std::shared_ptr<ITable> subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
std::shared_ptr<ITable> Servo::GetTable() const { return m_table; }
|