blob: 5179c1cc70096d628f039e054157478d8c93d279 [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. 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 the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "ADXL345_I2C.h"
9
10#include "HAL/HAL.h"
11#include "I2C.h"
12#include "LiveWindow/LiveWindow.h"
13
14using namespace frc;
15
16const int ADXL345_I2C::kAddress;
17const int ADXL345_I2C::kPowerCtlRegister;
18const int ADXL345_I2C::kDataFormatRegister;
19const int ADXL345_I2C::kDataRegister;
20constexpr double ADXL345_I2C::kGsPerLSB;
21
22/**
23 * Constructs the ADXL345 Accelerometer over I2C.
24 *
25 * @param port The I2C port the accelerometer is attached to
26 * @param range The range (+ or -) that the accelerometer will measure
27 * @param deviceAddress The I2C address of the accelerometer (0x1D or 0x53)
28 */
29ADXL345_I2C::ADXL345_I2C(I2C::Port port, Range range, int deviceAddress)
30 : m_i2c(port, deviceAddress) {
31 // Turn on the measurements
32 m_i2c.Write(kPowerCtlRegister, kPowerCtl_Measure);
33 // Specify the data format to read
34 SetRange(range);
35
36 HAL_Report(HALUsageReporting::kResourceType_ADXL345,
37 HALUsageReporting::kADXL345_I2C, 0);
38 LiveWindow::GetInstance()->AddSensor("ADXL345_I2C", port, this);
39}
40
41void ADXL345_I2C::SetRange(Range range) {
42 m_i2c.Write(kDataFormatRegister,
43 kDataFormat_FullRes | static_cast<uint8_t>(range));
44}
45
46double ADXL345_I2C::GetX() { return GetAcceleration(kAxis_X); }
47
48double ADXL345_I2C::GetY() { return GetAcceleration(kAxis_Y); }
49
50double ADXL345_I2C::GetZ() { return GetAcceleration(kAxis_Z); }
51
52/**
53 * Get the acceleration of one axis in Gs.
54 *
55 * @param axis The axis to read from.
56 * @return Acceleration of the ADXL345 in Gs.
57 */
58double ADXL345_I2C::GetAcceleration(ADXL345_I2C::Axes axis) {
59 int16_t rawAccel = 0;
60 m_i2c.Read(kDataRegister + static_cast<int>(axis), sizeof(rawAccel),
61 reinterpret_cast<uint8_t*>(&rawAccel));
62 return rawAccel * kGsPerLSB;
63}
64
65/**
66 * Get the acceleration of all axes in Gs.
67 *
68 * @return An object containing the acceleration measured on each axis of the
69 * ADXL345 in Gs.
70 */
71ADXL345_I2C::AllAxes ADXL345_I2C::GetAccelerations() {
72 AllAxes data = AllAxes();
73 int16_t rawData[3];
74 m_i2c.Read(kDataRegister, sizeof(rawData),
75 reinterpret_cast<uint8_t*>(rawData));
76
77 data.XAxis = rawData[0] * kGsPerLSB;
78 data.YAxis = rawData[1] * kGsPerLSB;
79 data.ZAxis = rawData[2] * kGsPerLSB;
80 return data;
81}
82
83std::string ADXL345_I2C::GetSmartDashboardType() const {
84 return "3AxisAccelerometer";
85}
86
87void ADXL345_I2C::InitTable(std::shared_ptr<ITable> subtable) {
88 m_table = subtable;
89 UpdateTable();
90}
91
92void ADXL345_I2C::UpdateTable() {
93 m_table->PutNumber("X", GetX());
94 m_table->PutNumber("Y", GetY());
95 m_table->PutNumber("Z", GetZ());
96}
97
98std::shared_ptr<ITable> ADXL345_I2C::GetTable() const { return m_table; }