blob: 78df478f3a3af8c25d24efec825b0294b81e73fc [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6
7#include "Accelerometer.h"
8#include "AnalogModule.h"
9#include "NetworkCommunication/UsageReporting.h"
10#include "WPIErrors.h"
11#include "LiveWindow/LiveWindow.h"
12
13/**
14 * Common function for initializing the accelerometer.
15 */
16void Accelerometer::InitAccelerometer()
17{
jerrym37afdca2013-03-03 01:17:57 +000018 m_table = NULL;
jerrymf1579332013-02-07 01:56:28 +000019 m_voltsPerG = 1.0;
20 m_zeroGVoltage = 2.5;
21 nUsageReporting::report(nUsageReporting::kResourceType_Accelerometer, m_analogChannel->GetChannel(), m_analogChannel->GetModuleNumber() - 1);
22 LiveWindow::GetInstance()->AddSensor("Accelerometer", m_analogChannel->GetModuleNumber(), m_analogChannel->GetChannel(), this);
23}
24
25/**
26 * Create a new instance of an accelerometer.
27 *
28 * The accelerometer is assumed to be in the first analog module in the given analog channel. The
29 * constructor allocates desired analog channel.
30 */
31Accelerometer::Accelerometer(UINT32 channel)
32{
33 m_analogChannel = new AnalogChannel(channel);
34 m_allocatedChannel = true;
35 InitAccelerometer();
36}
37
38/**
39 * Create new instance of accelerometer.
40 *
41 * Make a new instance of the accelerometer given a module and channel. The constructor allocates
42 * the desired analog channel from the specified module
43 *
44 * @param moduleNumber The analog module (1 or 2).
45 * @param channel The analog channel (1..8)
46 */
47Accelerometer::Accelerometer(UINT8 moduleNumber, UINT32 channel)
48{
49 m_analogChannel = new AnalogChannel(moduleNumber, channel);
50 m_allocatedChannel = true;
51 InitAccelerometer();
52}
53
54/**
55 * Create a new instance of Accelerometer from an existing AnalogChannel.
56 * Make a new instance of accelerometer given an AnalogChannel. This is particularly
57 * useful if the port is going to be read as an analog channel as well as through
58 * the Accelerometer class.
59 */
60Accelerometer::Accelerometer(AnalogChannel *channel)
61{
62 if (channel == NULL)
63 {
64 wpi_setWPIError(NullParameter);
65 }
66 else
67 {
68 m_analogChannel = channel;
69 InitAccelerometer();
70 }
71 m_allocatedChannel = false;
72}
73
74/**
75 * Delete the analog components used for the accelerometer.
76 */
77Accelerometer::~Accelerometer()
78{
79 if (m_allocatedChannel)
80 {
81 delete m_analogChannel;
82 }
83}
84
85/**
86 * Return the acceleration in Gs.
87 *
88 * The acceleration is returned units of Gs.
89 *
90 * @return The current acceleration of the sensor in Gs.
91 */
92float Accelerometer::GetAcceleration()
93{
94 return (m_analogChannel->GetAverageVoltage() - m_zeroGVoltage) / m_voltsPerG;
95}
96
97/**
98 * Set the accelerometer sensitivity.
99 *
100 * This sets the sensitivity of the accelerometer used for calculating the acceleration.
101 * The sensitivity varys by accelerometer model. There are constants defined for various models.
102 *
103 * @param sensitivity The sensitivity of accelerometer in Volts per G.
104 */
105void Accelerometer::SetSensitivity(float sensitivity)
106{
107 m_voltsPerG = sensitivity;
108}
109
110/**
111 * Set the voltage that corresponds to 0 G.
112 *
113 * The zero G voltage varys by accelerometer model. There are constants defined for various models.
114 *
115 * @param zero The zero G voltage.
116 */
117void Accelerometer::SetZero(float zero)
118{
119 m_zeroGVoltage = zero;
120}
121
122/**
123 * Get the Acceleration for the PID Source parent.
124 *
125 * @return The current acceleration in Gs.
126 */
127double Accelerometer::PIDGet()
128{
129 return GetAcceleration();
130}
131
132void Accelerometer::UpdateTable() {
133 if (m_table != NULL) {
134 m_table->PutNumber("Value", GetAcceleration());
135 }
136}
137
138void Accelerometer::StartLiveWindowMode() {
139}
140
141void Accelerometer::StopLiveWindowMode() {
142}
143
144std::string Accelerometer::GetSmartDashboardType() {
145 return "Accelerometer";
146}
147
148void Accelerometer::InitTable(ITable *subTable) {
149 m_table = subTable;
150 UpdateTable();
151}
152
153ITable * Accelerometer::GetTable() {
154 return m_table;
155}
156