-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMpu6050.cpp
More file actions
246 lines (215 loc) · 7.35 KB
/
Mpu6050.cpp
File metadata and controls
246 lines (215 loc) · 7.35 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (c) 2025 P.Cook (alias 'plainFlight')
*
* This file is part of the PlainFlightController distribution (https://github.com/plainFlight/plainFlightController).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file Mpu6050.hpp
* @brief This class contains methods that handle communications with the MPU6050.
*/
#include "Mpu6050.hpp"
/**
* @brief Constructor that sets the desired gyro rate.
*/
Mpu6050::Mpu6050()
{
if constexpr(Config::USE_250_DEGS_SECOND)
{
m_scaleFactor = GYRO_SCALE_FACTOR_250;
}
if constexpr(Config::USE_500_DEGS_SECOND)
{
m_scaleFactor = GYRO_SCALE_FACTOR_500;
}
}
/**
* @brief Initialises the MPU6050.
*/
void
Mpu6050::initialise()
{
begin();
reset();
delay(50);
setConfig(CONFIG);
if constexpr(Config::USE_250_DEGS_SECOND)
{
setGyroConfig(GYRO_CONFIG_250);
}
if constexpr(Config::USE_500_DEGS_SECOND)
{
setGyroConfig(GYRO_CONFIG_500);
}
setAccelerometerConfig(ACCEL_CONFIG);
}
/**
* @brief Sets up and start the SoftWire I2C transfer.
*/
void
Mpu6050::begin()
{
i2c.begin(BoardConfig::I2C_SDA,BoardConfig::I2C_SCL,I2C_CLK_1MHZ);
i2c.begin();
}
/**
* @brief Resets the MPU6050.
* @note Reset initialises all registers to zero.
*/
void
Mpu6050::reset()
{
i2c.beginTransmission(MPU6050_ADD);
i2c.write(0x6B); //Register
i2c.write(0x00); //Data
i2c.endTransmission(true);
}
/**
* @brief Sets the mpu configuration.
* @param Data representing the desired configuration register value.
*/
void
Mpu6050::setConfig(const uint8_t config)
{
i2c.beginTransmission(MPU6050_ADD);
i2c.write(0x1A); //Register
i2c.write(config); //Data
i2c.endTransmission(true);
}
/**
* @brief Sets the gyro scale to operate at.
* @param Data representing the desired configuration register value.
*/
void
Mpu6050::setGyroConfig(const uint8_t gyroScale)
{
i2c.beginTransmission(MPU6050_ADD);
i2c.write(0x1B); //Register
i2c.write(gyroScale); //Data
i2c.endTransmission(true);
}
/**
* @brief Sets the accelerometer scale to operate at.
* @param Data representing the desired configuration register value.
*/
void
Mpu6050::setAccelerometerConfig(const uint8_t accelScale)
{
i2c.beginTransmission(MPU6050_ADD);
i2c.write(0x1C); //Register
i2c.write(accelScale); //Data
i2c.endTransmission(true);
}
/**
* @brief Reads the gyro, temperature and accelerometer data form the mpu6050.
* @param Pointer to data structure where mpu data is stored.
* @return true when data successfully read.
*/
bool
Mpu6050::readData(MpuData* const data)
{
i2c.beginTransmission(MPU6050_ADD);
i2c.write(0x3B); //Register
i2c.endTransmission(false);
const uint8_t bytesReceived = i2c.requestFrom(MPU6050_ADD, 14, true); //Get gyro, temp and accelerometer data
if (14U == bytesReceived)
{
if constexpr(Config::IMU_ROLLED_RIGHT_90)
{
data->rawAccel_X = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawAccel_Z = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawAccel_Y = -(static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->temperature = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_X = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_Z = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_Y = -(static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
}
else if constexpr(Config::IMU_ROLLED_180)
{
data->rawAccel_X = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawAccel_Y = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawAccel_Z = -(static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->temperature = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_X = -(static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_Y = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_Z = -(static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
}
else
{
data->rawAccel_X = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawAccel_Y = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawAccel_Z = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->temperature = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_X = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_Y = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
data->rawGyro_Z = (static_cast<int16_t>(i2c.read()) << 8) | static_cast<int16_t>(i2c.read());
}
data->gyro_X = static_cast<float>(data->rawGyro_X - data->gyroOffset_X) / m_scaleFactor;
data->accel_X = static_cast<float>(data->rawAccel_X) / ACCEL_SCALE_FACTOR_16G;
data->gyro_Y = static_cast<float>(data->rawGyro_Y - data->gyroOffset_Y) / m_scaleFactor;
data->accel_Y = static_cast<float>(data->rawAccel_Y) / ACCEL_SCALE_FACTOR_16G;
data->gyro_Z = static_cast<float>(data->rawGyro_Z - data->gyroOffset_Z) / m_scaleFactor;
data->accel_Z = static_cast<float>(data->rawAccel_Z) / ACCEL_SCALE_FACTOR_16G;
if constexpr(Config::DEBUG_MPU6050)
{
Serial.print("\t gx:");
Serial.print(data->gyro_X);
Serial.print("\t gy:");
Serial.print(data->gyro_Y);
Serial.print("\t gz:");
Serial.print(data->gyro_Z);
Serial.print("\t ax:");
Serial.print(data->accel_X);
Serial.print("\t ay:");
Serial.print(data->accel_Y);
Serial.print("\t az:");
Serial.println(data->accel_Z);
}
return true;
}
else
{
if constexpr(Config::DEBUG_MPU6050)
{
Serial.println("MPU6050 read error !");
}
return false;
}
}
/**
* @brief Reads a register data value.
* @param Data representing the desired register address to read.
*/
uint8_t
Mpu6050::readRegister(const uint8_t theRegister)
{
i2c.beginTransmission(MPU6050_ADD);
i2c.write(theRegister); //Register
i2c.endTransmission(false);
i2c.requestFrom(MPU6050_ADD, 1, true); //Get gyro, temp and accelerometer data
return i2c.read();
}
/**
* @brief Gets the mpu device ID.
* @return The device ID.
*/
uint8_t
Mpu6050::whoAmI()
{
i2c.beginTransmission(MPU6050_ADD);
i2c.write(0x75); //Register
i2c.endTransmission(false);
i2c.requestFrom(MPU6050_ADD, 1, true); //Get who am I data
return i2c.read();
}