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