2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Servo.h"
|
|
|
|
|
|
2014-01-06 10:12:21 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
|
|
|
|
|
constexpr float Servo::kMaxServoAngle;
|
|
|
|
|
constexpr float Servo::kMinServoAngle;
|
|
|
|
|
|
2014-05-30 14:19:57 -04:00
|
|
|
constexpr float Servo::kDefaultMaxServoPWM;
|
|
|
|
|
constexpr float Servo::kDefaultMinServoPWM;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Common initialization code called by all constructors.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* InitServo() assigns defaults for the period multiplier for the servo PWM
|
|
|
|
|
* control signal, as
|
2013-12-15 18:30:16 -05:00
|
|
|
* well as the minimum and maximum PWM values supported by the servo.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Servo::InitServo() {
|
|
|
|
|
SetBounds(kDefaultMaxServoPWM, 0.0, 0.0, 0.0, kDefaultMinServoPWM);
|
|
|
|
|
SetPeriodMultiplier(kPeriodMultiplier_4X);
|
|
|
|
|
|
|
|
|
|
LiveWindow::GetInstance()->AddActuator("Servo", GetChannel(), this);
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Servo, GetChannel());
|
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
|
|
|
|
|
* on-board, 10-19 are on the MXP port
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Servo::Servo(uint32_t channel) : SafePWM(channel) {
|
|
|
|
|
InitServo();
|
|
|
|
|
// printf("Done initializing servo %d\n", channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Servo::~Servo() {}
|
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.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Servo::Set(float 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.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
float 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
|
|
|
|
|
* "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.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Servo::SetAngle(float degrees) {
|
|
|
|
|
if (degrees < kMinServoAngle) {
|
|
|
|
|
degrees = kMinServoAngle;
|
|
|
|
|
} else if (degrees > kMaxServoAngle) {
|
|
|
|
|
degrees = kMaxServoAngle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetPosition(((float)(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).
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The angle in degrees to which the servo is set.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
float Servo::GetAngle() const {
|
|
|
|
|
return (float)GetPosition() * GetServoAngleRange() + kMinServoAngle;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Servo::ValueChanged(ITable* source, const std::string& key,
|
|
|
|
|
EntryValue value, bool isNew) {
|
|
|
|
|
Set(value.f);
|
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-06-25 15:07:55 -04:00
|
|
|
void Servo::InitTable(ITable* subTable) {
|
|
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable* Servo::GetTable() const { return m_table; }
|