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
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <memory>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include "frc/AnalogInput.h"
namespace frc {
/**
* Handle operation of an analog accelerometer.
*
* The accelerometer reads acceleration directly through the sensor. Many
* sensors have multiple axis and can be treated as multiple devices. Each is
* calibrated by finding the center value over a period of time.
*/
class AnalogAccelerometer : public wpi::Sendable,
public wpi::SendableHelper<AnalogAccelerometer> {
public:
/**
* Create a new instance of an accelerometer.
*
* The constructor allocates desired analog input.
*
* @param channel The channel number for the analog input the accelerometer is
* connected to
*/
explicit AnalogAccelerometer(int channel);
/**
* Create a new instance of Accelerometer from an existing AnalogInput.
*
* Make a new instance of accelerometer given an AnalogInput. This is
* particularly useful if the port is going to be read as an analog channel as
* well as through the Accelerometer class.
*
* @param channel The existing AnalogInput object for the analog input the
* accelerometer is connected to
*/
explicit AnalogAccelerometer(AnalogInput* channel);
/**
* Create a new instance of Accelerometer from an existing AnalogInput.
*
* Make a new instance of accelerometer given an AnalogInput. This is
* particularly useful if the port is going to be read as an analog channel as
* well as through the Accelerometer class.
*
* @param channel The existing AnalogInput object for the analog input the
* accelerometer is connected to
*/
explicit AnalogAccelerometer(std::shared_ptr<AnalogInput> channel);
~AnalogAccelerometer() override = default;
AnalogAccelerometer(AnalogAccelerometer&&) = default;
AnalogAccelerometer& operator=(AnalogAccelerometer&&) = default;
/**
* Return the acceleration in Gs.
*
* The acceleration is returned units of Gs.
*
* @return The current acceleration of the sensor in Gs.
*/
double GetAcceleration() const;
/**
* Set the accelerometer sensitivity.
*
* This sets the sensitivity of the accelerometer used for calculating the
* acceleration. The sensitivity varies by accelerometer model.
*
* @param sensitivity The sensitivity of accelerometer in Volts per G.
*/
void SetSensitivity(double sensitivity);
/**
* Set the voltage that corresponds to 0 G.
*
* The zero G voltage varies by accelerometer model.
*
* @param zero The zero G voltage.
*/
void SetZero(double zero);
void InitSendable(wpi::SendableBuilder& builder) override;
private:
/**
* Common function for initializing the accelerometer.
*/
void InitAccelerometer();
std::shared_ptr<AnalogInput> m_analogInput;
double m_voltsPerG = 1.0;
double m_zeroGVoltage = 2.5;
};
} // namespace frc