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