2014-06-12 18:07:45 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2014-2017 FIRST. All Rights Reserved. */
|
2014-06-12 18:07:45 -04:00
|
|
|
/* 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 "AnalogOutput.h"
|
|
|
|
|
|
2015-11-15 14:49:50 -08:00
|
|
|
#include <limits>
|
2015-06-30 15:01:20 -04:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
#include <HAL/Ports.h>
|
|
|
|
|
#include <llvm/SmallString.h>
|
|
|
|
|
#include <llvm/raw_ostream.h>
|
|
|
|
|
|
2016-09-05 13:55:31 -07:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2015-06-29 02:43:44 -07:00
|
|
|
/**
|
|
|
|
|
* Construct an analog output on the given channel.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-06-29 02:43:44 -07:00
|
|
|
* All analog outputs are located on the MXP port.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param channel The channel number on the roboRIO to represent.
|
2015-06-29 02:43:44 -07:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
AnalogOutput::AnalogOutput(int channel) {
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<32> str;
|
|
|
|
|
llvm::raw_svector_ostream buf(str);
|
|
|
|
|
buf << "analog output " << channel;
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2016-07-21 23:24:06 -07:00
|
|
|
if (!SensorBase::CheckAnalogOutputChannel(channel)) {
|
2015-06-30 15:01:20 -04:00
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
|
2016-09-06 00:01:45 -07:00
|
|
|
m_channel = std::numeric_limits<int>::max();
|
2016-07-09 00:24:26 -07:00
|
|
|
m_port = HAL_kInvalidHandle;
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
m_channel = channel;
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_PortHandle port = HAL_GetPort(m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
m_port = HAL_InitializeAnalogOutputPort(port, &status);
|
2016-06-27 11:32:40 -07:00
|
|
|
if (status != 0) {
|
2016-07-13 20:29:28 -07:00
|
|
|
wpi_setErrorWithContextRange(status, 0, HAL_GetNumAnalogOutputs(), channel,
|
|
|
|
|
HAL_GetErrorMessage(status));
|
2016-09-06 00:01:45 -07:00
|
|
|
m_channel = std::numeric_limits<int>::max();
|
2016-07-09 00:24:26 -07:00
|
|
|
m_port = HAL_kInvalidHandle;
|
2016-06-27 11:32:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
Revert changes preventing old user code from compiling.
I'm not 100% sure whether we want these, but they are a quick
find and replace to do.
Basically, there are two primary things that we have done
this summer that break existing user code:
-Changing GetInstance() calls to return references instead
of pointers. This forces users to change from doing something
like LiveWindow::GetInstance()->AddSensor() to LiveWindow::GetInstance().AddSensor().
-Making PIDGet() and related calls const, forcing users to change
the function signatures wherever they override them.
The GetInstance() calls don't really matter to me either way,
especially since there are no real ownership issues going on there,
unlike the rest of the smart pointer-related changes.
For the const stuff, it is certainly more correct to mandate that
user PIDGet() functions be const and the such, but at the same time,
I'm not sure that there is any strong need for it, and the errors
generated are not the most helpful. While this wouldn't necessarily
be an issue for more experienced teams or completely new teams (who
don't have any old code to be reusing), it may cause issues for more
average teams who aren't familiar with the intricacies of C++ anything.
Change-Id: I6e7007982069292ea70e6d0fc8ca40203340df1b
2015-07-24 19:19:40 -04:00
|
|
|
LiveWindow::GetInstance()->AddActuator("AnalogOutput", m_channel, this);
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel);
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Destructor.
|
|
|
|
|
*
|
|
|
|
|
* Frees analog output resource.
|
2014-12-29 14:09:37 -05:00
|
|
|
*/
|
2016-07-09 00:24:26 -07:00
|
|
|
AnalogOutput::~AnalogOutput() { HAL_FreeAnalogOutputPort(m_port); }
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2016-12-23 11:20:13 -08:00
|
|
|
/**
|
|
|
|
|
* Get the channel of this AnalogOutput.
|
|
|
|
|
*/
|
|
|
|
|
int AnalogOutput::GetChannel() { return m_channel; }
|
|
|
|
|
|
2014-06-12 18:07:45 -04:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Set the value of the analog output.
|
2014-06-12 18:07:45 -04:00
|
|
|
*
|
|
|
|
|
* @param voltage The output value in Volts, from 0.0 to +5.0
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void AnalogOutput::SetVoltage(double voltage) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetAnalogOutput(m_port, voltage, &status);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the voltage of the analog output
|
|
|
|
|
*
|
|
|
|
|
* @return The value in Volts, from 0.0 to +5.0
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double AnalogOutput::GetVoltage() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double voltage = HAL_GetAnalogOutput(m_port, &status);
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
return voltage;
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AnalogOutput::UpdateTable() {
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueEntry) m_valueEntry.SetDouble(GetVoltage());
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogOutput::StartLiveWindowMode() {}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogOutput::StopLiveWindowMode() {}
|
2014-06-12 18:07:45 -04:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string AnalogOutput::GetSmartDashboardType() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return "Analog Output";
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
void AnalogOutput::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();
|
|
|
|
|
}
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|