blob: 6b2e1e901c9dee5495e0a058ff6dcae8d7284742 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-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 "AnalogAccelerometer.h"
9
10#include "HAL/HAL.h"
11#include "LiveWindow/LiveWindow.h"
12#include "WPIErrors.h"
13
14using namespace frc;
15
16/**
17 * Common function for initializing the accelerometer.
18 */
19void AnalogAccelerometer::InitAccelerometer() {
20 HAL_Report(HALUsageReporting::kResourceType_Accelerometer,
21 m_analogInput->GetChannel());
22 LiveWindow::GetInstance()->AddSensor("Accelerometer",
23 m_analogInput->GetChannel(), this);
24}
25
26/**
27 * Create a new instance of an accelerometer.
28 *
29 * The constructor allocates desired analog input.
30 *
31 * @param channel The channel number for the analog input the accelerometer is
32 * connected to
33 */
34AnalogAccelerometer::AnalogAccelerometer(int channel) {
35 m_analogInput = std::make_shared<AnalogInput>(channel);
36 InitAccelerometer();
37}
38
39/**
40 * Create a new instance of Accelerometer from an existing AnalogInput.
41 *
42 * Make a new instance of accelerometer given an AnalogInput. This is
43 * particularly useful if the port is going to be read as an analog channel as
44 * well as through the Accelerometer class.
45 *
46 * @param channel The existing AnalogInput object for the analog input the
47 * accelerometer is connected to
48 */
49AnalogAccelerometer::AnalogAccelerometer(AnalogInput* channel)
50 : m_analogInput(channel, NullDeleter<AnalogInput>()) {
51 if (channel == nullptr) {
52 wpi_setWPIError(NullParameter);
53 } else {
54 InitAccelerometer();
55 }
56}
57
58/**
59 * Create a new instance of Accelerometer from an existing AnalogInput.
60 *
61 * Make a new instance of accelerometer given an AnalogInput. This is
62 * particularly useful if the port is going to be read as an analog channel as
63 * well as through the Accelerometer class.
64 *
65 * @param channel The existing AnalogInput object for the analog input the
66 * accelerometer is connected to
67 */
68AnalogAccelerometer::AnalogAccelerometer(std::shared_ptr<AnalogInput> channel)
69 : m_analogInput(channel) {
70 if (channel == nullptr) {
71 wpi_setWPIError(NullParameter);
72 } else {
73 InitAccelerometer();
74 }
75}
76
77/**
78 * Return the acceleration in Gs.
79 *
80 * The acceleration is returned units of Gs.
81 *
82 * @return The current acceleration of the sensor in Gs.
83 */
84double AnalogAccelerometer::GetAcceleration() const {
85 return (m_analogInput->GetAverageVoltage() - m_zeroGVoltage) / m_voltsPerG;
86}
87
88/**
89 * Set the accelerometer sensitivity.
90 *
91 * This sets the sensitivity of the accelerometer used for calculating the
92 * acceleration. The sensitivity varies by accelerometer model. There are
93 * constants defined for various models.
94 *
95 * @param sensitivity The sensitivity of accelerometer in Volts per G.
96 */
97void AnalogAccelerometer::SetSensitivity(double sensitivity) {
98 m_voltsPerG = sensitivity;
99}
100
101/**
102 * Set the voltage that corresponds to 0 G.
103 *
104 * The zero G voltage varies by accelerometer model. There are constants defined
105 * for various models.
106 *
107 * @param zero The zero G voltage.
108 */
109void AnalogAccelerometer::SetZero(double zero) { m_zeroGVoltage = zero; }
110
111/**
112 * Get the Acceleration for the PID Source parent.
113 *
114 * @return The current acceleration in Gs.
115 */
116double AnalogAccelerometer::PIDGet() { return GetAcceleration(); }
117
118void AnalogAccelerometer::UpdateTable() {
119 if (m_table != nullptr) {
120 m_table->PutNumber("Value", GetAcceleration());
121 }
122}
123
124void AnalogAccelerometer::StartLiveWindowMode() {}
125
126void AnalogAccelerometer::StopLiveWindowMode() {}
127
128std::string AnalogAccelerometer::GetSmartDashboardType() const {
129 return "Accelerometer";
130}
131
132void AnalogAccelerometer::InitTable(std::shared_ptr<ITable> subTable) {
133 m_table = subTable;
134 UpdateTable();
135}
136
137std::shared_ptr<ITable> AnalogAccelerometer::GetTable() const {
138 return m_table;
139}