blob: deda965465f3bfcca15350217549e1507473319a [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "frc/ADXL362.h"
6
7#include <hal/FRCUsageReporting.h>
Austin Schuh812d0d12021-11-04 20:16:48 -07008#include <networktables/NTSendableBuilder.h>
9#include <wpi/sendable/SendableRegistry.h>
Brian Silverman8fce7482020-01-05 13:18:21 -080010
Austin Schuh812d0d12021-11-04 20:16:48 -070011#include "frc/Errors.h"
Brian Silverman8fce7482020-01-05 13:18:21 -080012
13using namespace frc;
14
15static constexpr int kRegWrite = 0x0A;
16static constexpr int kRegRead = 0x0B;
17
18static constexpr int kPartIdRegister = 0x02;
19static constexpr int kDataRegister = 0x0E;
20static constexpr int kFilterCtlRegister = 0x2C;
21static constexpr int kPowerCtlRegister = 0x2D;
22
23// static constexpr int kFilterCtl_Range2G = 0x00;
24// static constexpr int kFilterCtl_Range4G = 0x40;
25// static constexpr int kFilterCtl_Range8G = 0x80;
26static constexpr int kFilterCtl_ODR_100Hz = 0x03;
27
28static constexpr int kPowerCtl_UltraLowNoise = 0x20;
29// static constexpr int kPowerCtl_AutoSleep = 0x04;
30static constexpr int kPowerCtl_Measure = 0x02;
31
32ADXL362::ADXL362(Range range) : ADXL362(SPI::Port::kOnboardCS1, range) {}
33
34ADXL362::ADXL362(SPI::Port port, Range range)
Austin Schuh812d0d12021-11-04 20:16:48 -070035 : m_spi(port), m_simDevice("Accel:ADXL362", port) {
Brian Silverman8fce7482020-01-05 13:18:21 -080036 if (m_simDevice) {
Austin Schuh812d0d12021-11-04 20:16:48 -070037 m_simRange = m_simDevice.CreateEnumDouble("range", hal::SimDevice::kOutput,
38 {"2G", "4G", "8G", "16G"},
39 {2.0, 4.0, 8.0, 16.0}, 0);
40 m_simX = m_simDevice.CreateDouble("x", hal::SimDevice::kInput, 0.0);
41 m_simY = m_simDevice.CreateDouble("y", hal::SimDevice::kInput, 0.0);
42 m_simZ = m_simDevice.CreateDouble("z", hal::SimDevice::kInput, 0.0);
Brian Silverman8fce7482020-01-05 13:18:21 -080043 }
44
45 m_spi.SetClockRate(3000000);
46 m_spi.SetMSBFirst();
47 m_spi.SetSampleDataOnTrailingEdge();
48 m_spi.SetClockActiveLow();
49 m_spi.SetChipSelectActiveLow();
50
51 uint8_t commands[3];
52 if (!m_simDevice) {
53 // Validate the part ID
54 commands[0] = kRegRead;
55 commands[1] = kPartIdRegister;
56 commands[2] = 0;
57 m_spi.Transaction(commands, commands, 3);
58 if (commands[2] != 0xF2) {
Austin Schuh812d0d12021-11-04 20:16:48 -070059 FRC_ReportError(err::Error, "{}", "could not find ADXL362");
Brian Silverman8fce7482020-01-05 13:18:21 -080060 m_gsPerLSB = 0.0;
61 return;
62 }
63 }
64
65 SetRange(range);
66
67 // Turn on the measurements
68 commands[0] = kRegWrite;
69 commands[1] = kPowerCtlRegister;
70 commands[2] = kPowerCtl_Measure | kPowerCtl_UltraLowNoise;
71 m_spi.Write(commands, 3);
72
73 HAL_Report(HALUsageReporting::kResourceType_ADXL362, port + 1);
74
Austin Schuh812d0d12021-11-04 20:16:48 -070075 wpi::SendableRegistry::AddLW(this, "ADXL362", port);
76}
77
78SPI::Port ADXL362::GetSpiPort() const {
79 return m_spi.GetPort();
Brian Silverman8fce7482020-01-05 13:18:21 -080080}
81
82void ADXL362::SetRange(Range range) {
Austin Schuh812d0d12021-11-04 20:16:48 -070083 if (m_gsPerLSB == 0.0) {
84 return;
85 }
Brian Silverman8fce7482020-01-05 13:18:21 -080086
87 uint8_t commands[3];
88
89 switch (range) {
90 case kRange_2G:
91 m_gsPerLSB = 0.001;
92 break;
93 case kRange_4G:
94 m_gsPerLSB = 0.002;
95 break;
96 case kRange_8G:
97 case kRange_16G: // 16G not supported; treat as 8G
98 m_gsPerLSB = 0.004;
99 break;
100 }
101
102 // Specify the data format to read
103 commands[0] = kRegWrite;
104 commands[1] = kFilterCtlRegister;
105 commands[2] =
106 kFilterCtl_ODR_100Hz | static_cast<uint8_t>((range & 0x03) << 6);
107 m_spi.Write(commands, 3);
108
Austin Schuh812d0d12021-11-04 20:16:48 -0700109 if (m_simRange) {
110 m_simRange.Set(range);
111 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800112}
113
Austin Schuh812d0d12021-11-04 20:16:48 -0700114double ADXL362::GetX() {
115 return GetAcceleration(kAxis_X);
116}
Brian Silverman8fce7482020-01-05 13:18:21 -0800117
Austin Schuh812d0d12021-11-04 20:16:48 -0700118double ADXL362::GetY() {
119 return GetAcceleration(kAxis_Y);
120}
Brian Silverman8fce7482020-01-05 13:18:21 -0800121
Austin Schuh812d0d12021-11-04 20:16:48 -0700122double ADXL362::GetZ() {
123 return GetAcceleration(kAxis_Z);
124}
Brian Silverman8fce7482020-01-05 13:18:21 -0800125
126double ADXL362::GetAcceleration(ADXL362::Axes axis) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700127 if (m_gsPerLSB == 0.0) {
128 return 0.0;
129 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800130
Austin Schuh812d0d12021-11-04 20:16:48 -0700131 if (axis == kAxis_X && m_simX) {
132 return m_simX.Get();
133 }
134 if (axis == kAxis_Y && m_simY) {
135 return m_simY.Get();
136 }
137 if (axis == kAxis_Z && m_simZ) {
138 return m_simZ.Get();
139 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800140
141 uint8_t buffer[4];
142 uint8_t command[4] = {0, 0, 0, 0};
143 command[0] = kRegRead;
144 command[1] = kDataRegister + static_cast<uint8_t>(axis);
145 m_spi.Transaction(command, buffer, 4);
146
147 // Sensor is little endian... swap bytes
148 int16_t rawAccel = buffer[3] << 8 | buffer[2];
149 return rawAccel * m_gsPerLSB;
150}
151
152ADXL362::AllAxes ADXL362::GetAccelerations() {
153 AllAxes data;
154 if (m_gsPerLSB == 0.0) {
155 data.XAxis = data.YAxis = data.ZAxis = 0.0;
156 return data;
157 }
158 if (m_simX && m_simY && m_simZ) {
159 data.XAxis = m_simX.Get();
160 data.YAxis = m_simY.Get();
161 data.ZAxis = m_simZ.Get();
162 return data;
163 }
164
165 uint8_t dataBuffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
166 int16_t rawData[3];
167
168 // Select the data address.
169 dataBuffer[0] = kRegRead;
170 dataBuffer[1] = kDataRegister;
171 m_spi.Transaction(dataBuffer, dataBuffer, 8);
172
173 for (int i = 0; i < 3; i++) {
174 // Sensor is little endian... swap bytes
175 rawData[i] = dataBuffer[i * 2 + 3] << 8 | dataBuffer[i * 2 + 2];
176 }
177
178 data.XAxis = rawData[0] * m_gsPerLSB;
179 data.YAxis = rawData[1] * m_gsPerLSB;
180 data.ZAxis = rawData[2] * m_gsPerLSB;
181
182 return data;
183}
184
Austin Schuh812d0d12021-11-04 20:16:48 -0700185void ADXL362::InitSendable(nt::NTSendableBuilder& builder) {
Brian Silverman8fce7482020-01-05 13:18:21 -0800186 builder.SetSmartDashboardType("3AxisAccelerometer");
187 auto x = builder.GetEntry("X").GetHandle();
188 auto y = builder.GetEntry("Y").GetHandle();
189 auto z = builder.GetEntry("Z").GetHandle();
Austin Schuh812d0d12021-11-04 20:16:48 -0700190 builder.SetUpdateTable([=] {
Brian Silverman8fce7482020-01-05 13:18:21 -0800191 auto data = GetAccelerations();
192 nt::NetworkTableEntry(x).SetDouble(data.XAxis);
193 nt::NetworkTableEntry(y).SetDouble(data.YAxis);
194 nt::NetworkTableEntry(z).SetDouble(data.ZAxis);
195 });
196}