mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Add format script which invokes clang-format on the C++ source code (#41)
On Windows machines, clang-format.exe must be in the PATH environment variable.
This commit is contained in:
committed by
Peter Johnson
parent
68690643d2
commit
e14e45da76
@@ -9,118 +9,108 @@
|
||||
|
||||
#include "MotorSafetyHelper.h"
|
||||
//#include "NetworkCommunication/UsageReporting.h"
|
||||
#include "WPIErrors.h"
|
||||
#include "LiveWindow/LiveWindow.h"
|
||||
#include "WPIErrors.h"
|
||||
|
||||
/**
|
||||
* Relay constructor given a channel.
|
||||
*
|
||||
* This code initializes the relay and reserves all resources that need to be
|
||||
* locked. Initially the relay is set to both lines at 0v.
|
||||
* @param channel The channel number (0-3).
|
||||
*
|
||||
* @param channel The channel number (0-3).
|
||||
* @param direction The direction that the Relay object will control.
|
||||
*/
|
||||
Relay::Relay(uint32_t channel, Relay::Direction direction)
|
||||
: m_channel (channel)
|
||||
, m_direction (direction)
|
||||
{
|
||||
char buf[64];
|
||||
if (!SensorBase::CheckRelayChannel(m_channel))
|
||||
{
|
||||
snprintf(buf, 64, "Relay Channel %d", m_channel);
|
||||
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
|
||||
return;
|
||||
}
|
||||
: m_channel(channel), m_direction(direction) {
|
||||
char buf[64];
|
||||
if (!SensorBase::CheckRelayChannel(m_channel)) {
|
||||
snprintf(buf, 64, "Relay Channel %d", m_channel);
|
||||
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
|
||||
return;
|
||||
}
|
||||
|
||||
m_safetyHelper = std::make_unique<MotorSafetyHelper>(this);
|
||||
m_safetyHelper->SetSafetyEnabled(false);
|
||||
m_safetyHelper = std::make_unique<MotorSafetyHelper>(this);
|
||||
m_safetyHelper->SetSafetyEnabled(false);
|
||||
|
||||
sprintf(buf, "relay/%d", m_channel);
|
||||
impl = new SimContinuousOutput(buf); // TODO: Allow two different relays (targetting the different halves of a relay) to be combined to control one motor.
|
||||
LiveWindow::GetInstance()->AddActuator("Relay", 1, m_channel, this);
|
||||
go_pos = go_neg = false;
|
||||
sprintf(buf, "relay/%d", m_channel);
|
||||
impl = new SimContinuousOutput(buf); // TODO: Allow two different relays
|
||||
// (targetting the different halves of a
|
||||
// relay) to be combined to control one
|
||||
// motor.
|
||||
LiveWindow::GetInstance()->AddActuator("Relay", 1, m_channel, this);
|
||||
go_pos = go_neg = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the resource associated with a relay.
|
||||
*
|
||||
* The relay channels are set to free and the relay output is turned off.
|
||||
*/
|
||||
Relay::~Relay()
|
||||
{
|
||||
impl->Set(0);
|
||||
if (m_table != nullptr) m_table->RemoveTableListener(this);
|
||||
Relay::~Relay() {
|
||||
impl->Set(0);
|
||||
if (m_table != nullptr) m_table->RemoveTableListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the relay state.
|
||||
*
|
||||
* Valid values depend on which directions of the relay are controlled by the object.
|
||||
* Valid values depend on which directions of the relay are controlled by the
|
||||
* object.
|
||||
*
|
||||
* When set to kBothDirections, the relay can be any of the four states:
|
||||
* 0v-0v, 0v-12v, 12v-0v, 12v-12v
|
||||
*
|
||||
* When set to kForwardOnly or kReverseOnly, you can specify the constant for the
|
||||
* direction or you can simply specify kOff and kOn. Using only kOff and kOn is
|
||||
* recommended.
|
||||
* When set to kForwardOnly or kReverseOnly, you can specify the constant for
|
||||
* the direction or you can simply specify kOff and kOn. Using only kOff and
|
||||
* kOn is recommended.
|
||||
*
|
||||
* @param value The state to set the relay.
|
||||
*/
|
||||
void Relay::Set(Relay::Value value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case kOff:
|
||||
if (m_direction == kBothDirections || m_direction == kForwardOnly)
|
||||
{
|
||||
go_pos = false;
|
||||
}
|
||||
if (m_direction == kBothDirections || m_direction == kReverseOnly)
|
||||
{
|
||||
go_neg = false;
|
||||
}
|
||||
break;
|
||||
case kOn:
|
||||
if (m_direction == kBothDirections || m_direction == kForwardOnly)
|
||||
{
|
||||
go_pos = true;
|
||||
}
|
||||
if (m_direction == kBothDirections || m_direction == kReverseOnly)
|
||||
{
|
||||
go_neg = true;
|
||||
}
|
||||
break;
|
||||
case kForward:
|
||||
if (m_direction == kReverseOnly)
|
||||
{
|
||||
wpi_setWPIError(IncompatibleMode);
|
||||
break;
|
||||
}
|
||||
if (m_direction == kBothDirections || m_direction == kForwardOnly)
|
||||
{
|
||||
go_pos = true;
|
||||
}
|
||||
if (m_direction == kBothDirections)
|
||||
{
|
||||
go_neg = false;
|
||||
}
|
||||
break;
|
||||
case kReverse:
|
||||
if (m_direction == kForwardOnly)
|
||||
{
|
||||
wpi_setWPIError(IncompatibleMode);
|
||||
break;
|
||||
}
|
||||
if (m_direction == kBothDirections)
|
||||
{
|
||||
go_pos = false;
|
||||
}
|
||||
if (m_direction == kBothDirections || m_direction == kReverseOnly)
|
||||
{
|
||||
go_neg = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
impl->Set((go_pos ? 1 : 0) + (go_neg ? -1 : 0));
|
||||
void Relay::Set(Relay::Value value) {
|
||||
switch (value) {
|
||||
case kOff:
|
||||
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
||||
go_pos = false;
|
||||
}
|
||||
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
||||
go_neg = false;
|
||||
}
|
||||
break;
|
||||
case kOn:
|
||||
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
||||
go_pos = true;
|
||||
}
|
||||
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
||||
go_neg = true;
|
||||
}
|
||||
break;
|
||||
case kForward:
|
||||
if (m_direction == kReverseOnly) {
|
||||
wpi_setWPIError(IncompatibleMode);
|
||||
break;
|
||||
}
|
||||
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
||||
go_pos = true;
|
||||
}
|
||||
if (m_direction == kBothDirections) {
|
||||
go_neg = false;
|
||||
}
|
||||
break;
|
||||
case kReverse:
|
||||
if (m_direction == kForwardOnly) {
|
||||
wpi_setWPIError(IncompatibleMode);
|
||||
break;
|
||||
}
|
||||
if (m_direction == kBothDirections) {
|
||||
go_pos = false;
|
||||
}
|
||||
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
||||
go_neg = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
impl->Set((go_pos ? 1 : 0) + (go_neg ? -1 : 0));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,29 +119,29 @@ void Relay::Set(Relay::Value value)
|
||||
* Gets the current state of the relay.
|
||||
*
|
||||
* When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
|
||||
* kForward/kReverse (per the recommendation in Set)
|
||||
* kForward/kReverse (per the recommendation in Set).
|
||||
*
|
||||
* @return The current state of the relay as a Relay::Value
|
||||
*/
|
||||
Relay::Value Relay::Get() const {
|
||||
// TODO: Don't assume that the go_pos and go_neg fields are correct?
|
||||
if ((go_pos || m_direction == kReverseOnly) && (go_neg || m_direction == kForwardOnly)) {
|
||||
return kOn;
|
||||
} else if (go_pos) {
|
||||
return kForward;
|
||||
} else if (go_neg) {
|
||||
return kReverse;
|
||||
} else {
|
||||
return kOff;
|
||||
}
|
||||
// TODO: Don't assume that the go_pos and go_neg fields are correct?
|
||||
if ((go_pos || m_direction == kReverseOnly) &&
|
||||
(go_neg || m_direction == kForwardOnly)) {
|
||||
return kOn;
|
||||
} else if (go_pos) {
|
||||
return kForward;
|
||||
} else if (go_neg) {
|
||||
return kReverse;
|
||||
} else {
|
||||
return kOff;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t Relay::GetChannel() const {
|
||||
return m_channel;
|
||||
}
|
||||
uint32_t Relay::GetChannel() const { return m_channel; }
|
||||
|
||||
/**
|
||||
* Set the expiration time for the Relay object
|
||||
* Set the expiration time for the Relay object.
|
||||
*
|
||||
* @param timeout The timeout (in seconds) for this relay object
|
||||
*/
|
||||
void Relay::SetExpiration(float timeout) {
|
||||
@@ -160,19 +150,22 @@ void Relay::SetExpiration(float timeout) {
|
||||
|
||||
/**
|
||||
* Return the expiration time for the relay object.
|
||||
*
|
||||
* @return The expiration time value.
|
||||
*/
|
||||
float Relay::GetExpiration() const { return m_safetyHelper->GetExpiration(); }
|
||||
|
||||
/**
|
||||
* Check if the relay object is currently alive or stopped due to a timeout.
|
||||
* @returns a bool value that is true if the motor has NOT timed out and should
|
||||
* still be running.
|
||||
*
|
||||
* @return a bool value that is true if the motor has NOT timed out and should
|
||||
* still be running.
|
||||
*/
|
||||
bool Relay::IsAlive() const { return m_safetyHelper->IsAlive(); }
|
||||
|
||||
/**
|
||||
* Stop the motor associated with this PWM object.
|
||||
*
|
||||
* This is called by the MotorSafetyHelper object when it has a timeout for this
|
||||
* relay and needs to stop it from running.
|
||||
*/
|
||||
@@ -180,7 +173,9 @@ void Relay::StopMotor() { Set(kOff); }
|
||||
|
||||
/**
|
||||
* Enable/disable motor safety for this device
|
||||
*
|
||||
* Turn on and off the motor safety option for this relay object.
|
||||
*
|
||||
* @param enabled True if motor safety is enforced for this object
|
||||
*/
|
||||
void Relay::SetSafetyEnabled(bool enabled) {
|
||||
@@ -188,8 +183,9 @@ void Relay::SetSafetyEnabled(bool enabled) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if motor safety is enabled for this object
|
||||
* @returns True if motor safety is enforced for this object
|
||||
* Check if motor safety is enabled for this object.
|
||||
*
|
||||
* @return True if motor safety is enforced for this object
|
||||
*/
|
||||
bool Relay::IsSafetyEnabled() const {
|
||||
return m_safetyHelper->IsSafetyEnabled();
|
||||
@@ -202,49 +198,45 @@ void Relay::GetDescription(std::ostringstream& desc) const {
|
||||
void Relay::ValueChanged(ITable* source, llvm::StringRef key,
|
||||
std::shared_ptr<nt::Value> value, bool isNew) {
|
||||
if (!value->IsString()) return;
|
||||
if (value->GetString() == "Off") Set(kOff);
|
||||
else if (value->GetString() == "Forward") Set(kForward);
|
||||
else if (value->GetString() == "Reverse") Set(kReverse);
|
||||
if (value->GetString() == "Off")
|
||||
Set(kOff);
|
||||
else if (value->GetString() == "Forward")
|
||||
Set(kForward);
|
||||
else if (value->GetString() == "Reverse")
|
||||
Set(kReverse);
|
||||
}
|
||||
|
||||
void Relay::UpdateTable() {
|
||||
if(m_table != nullptr){
|
||||
if (Get() == kOn) {
|
||||
m_table->PutString("Value", "On");
|
||||
}
|
||||
else if (Get() == kForward) {
|
||||
m_table->PutString("Value", "Forward");
|
||||
}
|
||||
else if (Get() == kReverse) {
|
||||
m_table->PutString("Value", "Reverse");
|
||||
}
|
||||
else {
|
||||
m_table->PutString("Value", "Off");
|
||||
}
|
||||
}
|
||||
if (m_table != nullptr) {
|
||||
if (Get() == kOn) {
|
||||
m_table->PutString("Value", "On");
|
||||
} else if (Get() == kForward) {
|
||||
m_table->PutString("Value", "Forward");
|
||||
} else if (Get() == kReverse) {
|
||||
m_table->PutString("Value", "Reverse");
|
||||
} else {
|
||||
m_table->PutString("Value", "Off");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Relay::StartLiveWindowMode() {
|
||||
if(m_table != nullptr){
|
||||
m_table->AddTableListener("Value", this, true);
|
||||
}
|
||||
if (m_table != nullptr) {
|
||||
m_table->AddTableListener("Value", this, true);
|
||||
}
|
||||
}
|
||||
|
||||
void Relay::StopLiveWindowMode() {
|
||||
if(m_table != nullptr){
|
||||
m_table->RemoveTableListener(this);
|
||||
}
|
||||
if (m_table != nullptr) {
|
||||
m_table->RemoveTableListener(this);
|
||||
}
|
||||
}
|
||||
|
||||
std::string Relay::GetSmartDashboardType() const {
|
||||
return "Relay";
|
||||
}
|
||||
std::string Relay::GetSmartDashboardType() const { return "Relay"; }
|
||||
|
||||
void Relay::InitTable(std::shared_ptr<ITable> subTable) {
|
||||
m_table = subTable;
|
||||
UpdateTable();
|
||||
m_table = subTable;
|
||||
UpdateTable();
|
||||
}
|
||||
|
||||
std::shared_ptr<ITable> Relay::GetTable() const {
|
||||
return m_table;
|
||||
}
|
||||
std::shared_ptr<ITable> Relay::GetTable() const { return m_table; }
|
||||
|
||||
Reference in New Issue
Block a user