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>
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
#include "SensorBase.h"
|
|
|
|
|
#include "SmartDashboard/SendableBuilder.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
#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
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel);
|
2017-12-04 23:28:33 -08:00
|
|
|
SetName("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
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void AnalogOutput::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Analog Output");
|
|
|
|
|
builder.AddDoubleProperty("Value", [=]() { return GetVoltage(); },
|
|
|
|
|
[=](double value) { SetVoltage(value); });
|
2014-06-12 18:07:45 -04:00
|
|
|
}
|