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