blob: 000d4b85707372ca7404125198970ec53f2b1e44 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7
8#include "ADXL345_I2C.h"
9#include "I2C.h"
10#include "HAL/HAL.hpp"
11#include "LiveWindow/LiveWindow.h"
12
13const uint8_t ADXL345_I2C::kAddress;
14const uint8_t ADXL345_I2C::kPowerCtlRegister;
15const uint8_t ADXL345_I2C::kDataFormatRegister;
16const uint8_t ADXL345_I2C::kDataRegister;
17constexpr double ADXL345_I2C::kGsPerLSB;
18
19/**
20 * Constructor.
21 *
22 * @param port The I2C port the accelerometer is attached to
23 * @param range The range (+ or -) that the accelerometer will measure.
24 */
25ADXL345_I2C::ADXL345_I2C(Port port, Range range) : I2C(port, kAddress) {
26 // Turn on the measurements
27 Write(kPowerCtlRegister, kPowerCtl_Measure);
28 // Specify the data format to read
29 SetRange(range);
30
31 HALReport(HALUsageReporting::kResourceType_ADXL345,
32 HALUsageReporting::kADXL345_I2C, 0);
33 LiveWindow::GetInstance()->AddSensor("ADXL345_I2C", port, this);
34}
35
36/** {@inheritdoc} */
37void ADXL345_I2C::SetRange(Range range) {
38 Write(kDataFormatRegister, kDataFormat_FullRes | (uint8_t)range);
39}
40
41/** {@inheritdoc} */
42double ADXL345_I2C::GetX() { return GetAcceleration(kAxis_X); }
43
44/** {@inheritdoc} */
45double ADXL345_I2C::GetY() { return GetAcceleration(kAxis_Y); }
46
47/** {@inheritdoc} */
48double ADXL345_I2C::GetZ() { return GetAcceleration(kAxis_Z); }
49
50/**
51 * Get the acceleration of one axis in Gs.
52 *
53 * @param axis The axis to read from.
54 * @return Acceleration of the ADXL345 in Gs.
55 */
56double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axes axis) {
57 int16_t rawAccel = 0;
58 Read(kDataRegister + (uint8_t)axis, sizeof(rawAccel), (uint8_t *)&rawAccel);
59 return rawAccel * kGsPerLSB;
60}
61
62/**
63 * Get the acceleration of all axes in Gs.
64 *
65 * @return An object containing the acceleration measured on each axis of the
66 * ADXL345 in Gs.
67 */
68ADXL345_I2C::AllAxes ADXL345_I2C::GetAccelerations() {
69 AllAxes data = AllAxes();
70 int16_t rawData[3];
71 Read(kDataRegister, sizeof(rawData), (uint8_t *)rawData);
72
73 data.XAxis = rawData[0] * kGsPerLSB;
74 data.YAxis = rawData[1] * kGsPerLSB;
75 data.ZAxis = rawData[2] * kGsPerLSB;
76 return data;
77}
78
79std::string ADXL345_I2C::GetSmartDashboardType() const {
80 return "3AxisAccelerometer";
81}
82
83void ADXL345_I2C::InitTable(std::shared_ptr<ITable> subtable) {
84 m_table = subtable;
85 UpdateTable();
86}
87
88void ADXL345_I2C::UpdateTable() {
89 m_table->PutNumber("X", GetX());
90 m_table->PutNumber("Y", GetY());
91 m_table->PutNumber("Z", GetZ());
92}
93
94std::shared_ptr<ITable> ADXL345_I2C::GetTable() const { return m_table; }