blob: 3905ae12aa412bdd792e669178c7dc2cfdb554da [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2014-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "AnalogOutput.h"
9
10#include <limits>
11#include <sstream>
12
13#include "HAL/HAL.h"
14#include "HAL/Ports.h"
15#include "LiveWindow/LiveWindow.h"
16#include "WPIErrors.h"
17
18using namespace frc;
19
20/**
21 * Construct an analog output on the given channel.
22 *
23 * All analog outputs are located on the MXP port.
24 *
25 * @param channel The channel number on the roboRIO to represent.
26 */
27AnalogOutput::AnalogOutput(int channel) {
28 std::stringstream buf;
29 buf << "analog input " << channel;
30
31 if (!SensorBase::CheckAnalogOutputChannel(channel)) {
32 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
33 m_channel = std::numeric_limits<int>::max();
34 m_port = HAL_kInvalidHandle;
35 return;
36 }
37
38 m_channel = channel;
39
40 HAL_PortHandle port = HAL_GetPort(m_channel);
41 int32_t status = 0;
42 m_port = HAL_InitializeAnalogOutputPort(port, &status);
43 if (status != 0) {
44 wpi_setErrorWithContextRange(status, 0, HAL_GetNumAnalogOutputs(), channel,
45 HAL_GetErrorMessage(status));
46 m_channel = std::numeric_limits<int>::max();
47 m_port = HAL_kInvalidHandle;
48 return;
49 }
50
51 LiveWindow::GetInstance()->AddActuator("AnalogOutput", m_channel, this);
52 HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel);
53}
54
55/**
56 * Destructor.
57 *
58 * Frees analog output resource.
59 */
60AnalogOutput::~AnalogOutput() { HAL_FreeAnalogOutputPort(m_port); }
61
62/**
63 * Get the channel of this AnalogOutput.
64 */
65int AnalogOutput::GetChannel() { return m_channel; }
66
67/**
68 * Set the value of the analog output.
69 *
70 * @param voltage The output value in Volts, from 0.0 to +5.0
71 */
72void AnalogOutput::SetVoltage(double voltage) {
73 int32_t status = 0;
74 HAL_SetAnalogOutput(m_port, voltage, &status);
75
76 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
77}
78
79/**
80 * Get the voltage of the analog output
81 *
82 * @return The value in Volts, from 0.0 to +5.0
83 */
84double AnalogOutput::GetVoltage() const {
85 int32_t status = 0;
86 double voltage = HAL_GetAnalogOutput(m_port, &status);
87
88 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
89
90 return voltage;
91}
92
93void AnalogOutput::UpdateTable() {
94 if (m_table != nullptr) {
95 m_table->PutNumber("Value", GetVoltage());
96 }
97}
98
99void AnalogOutput::StartLiveWindowMode() {}
100
101void AnalogOutput::StopLiveWindowMode() {}
102
103std::string AnalogOutput::GetSmartDashboardType() const {
104 return "Analog Output";
105}
106
107void AnalogOutput::InitTable(std::shared_ptr<ITable> subTable) {
108 m_table = subTable;
109 UpdateTable();
110}
111
112std::shared_ptr<ITable> AnalogOutput::GetTable() const { return m_table; }