WPILibC++ 2024.3.2
AnalogAccelerometer.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <memory>
8
11
12#include "frc/AnalogInput.h"
13
14namespace frc {
15
16/**
17 * Handle operation of an analog accelerometer.
18 *
19 * The accelerometer reads acceleration directly through the sensor. Many
20 * sensors have multiple axis and can be treated as multiple devices. Each is
21 * calibrated by finding the center value over a period of time.
22 */
24 public wpi::SendableHelper<AnalogAccelerometer> {
25 public:
26 /**
27 * Create a new instance of an accelerometer.
28 *
29 * The constructor allocates desired analog input.
30 *
31 * @param channel The channel number for the analog input the accelerometer is
32 * connected to
33 */
34 explicit AnalogAccelerometer(int channel);
35
36 /**
37 * Create a new instance of Accelerometer from an existing AnalogInput.
38 *
39 * Make a new instance of accelerometer given an AnalogInput. This is
40 * particularly useful if the port is going to be read as an analog channel as
41 * well as through the Accelerometer class.
42 *
43 * @param channel The existing AnalogInput object for the analog input the
44 * accelerometer is connected to
45 */
46 explicit AnalogAccelerometer(AnalogInput* channel);
47
48 /**
49 * Create a new instance of Accelerometer from an existing AnalogInput.
50 *
51 * Make a new instance of accelerometer given an AnalogInput. This is
52 * particularly useful if the port is going to be read as an analog channel as
53 * well as through the Accelerometer class.
54 *
55 * @param channel The existing AnalogInput object for the analog input the
56 * accelerometer is connected to
57 */
58 explicit AnalogAccelerometer(std::shared_ptr<AnalogInput> channel);
59
60 ~AnalogAccelerometer() override = default;
61
64
65 /**
66 * Return the acceleration in Gs.
67 *
68 * The acceleration is returned units of Gs.
69 *
70 * @return The current acceleration of the sensor in Gs.
71 */
72 double GetAcceleration() const;
73
74 /**
75 * Set the accelerometer sensitivity.
76 *
77 * This sets the sensitivity of the accelerometer used for calculating the
78 * acceleration. The sensitivity varies by accelerometer model. There are
79 * constants defined for various models.
80 *
81 * @param sensitivity The sensitivity of accelerometer in Volts per G.
82 */
83 void SetSensitivity(double sensitivity);
84
85 /**
86 * Set the voltage that corresponds to 0 G.
87 *
88 * The zero G voltage varies by accelerometer model. There are constants
89 * defined for various models.
90 *
91 * @param zero The zero G voltage.
92 */
93 void SetZero(double zero);
94
95 void InitSendable(wpi::SendableBuilder& builder) override;
96
97 private:
98 /**
99 * Common function for initializing the accelerometer.
100 */
101 void InitAccelerometer();
102
103 std::shared_ptr<AnalogInput> m_analogInput;
104 double m_voltsPerG = 1.0;
105 double m_zeroGVoltage = 2.5;
106};
107
108} // namespace frc
Handle operation of an analog accelerometer.
Definition: AnalogAccelerometer.h:24
void SetZero(double zero)
Set the voltage that corresponds to 0 G.
double GetAcceleration() const
Return the acceleration in Gs.
void SetSensitivity(double sensitivity)
Set the accelerometer sensitivity.
~AnalogAccelerometer() override=default
AnalogAccelerometer(AnalogInput *channel)
Create a new instance of Accelerometer from an existing AnalogInput.
AnalogAccelerometer(AnalogAccelerometer &&)=default
AnalogAccelerometer(int channel)
Create a new instance of an accelerometer.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
AnalogAccelerometer & operator=(AnalogAccelerometer &&)=default
AnalogAccelerometer(std::shared_ptr< AnalogInput > channel)
Create a new instance of Accelerometer from an existing AnalogInput.
Analog input class.
Definition: AnalogInput.h:31
Helper class for building Sendable dashboard representations.
Definition: SendableBuilder.h:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
Interface for Sendable objects.
Definition: Sendable.h:16
Definition: AprilTagPoseEstimator.h:15