Skip to content

Commit f760939

Browse files
committed
Fixup MPU calibration and Z angle calculation
1 parent 9347507 commit f760939

4 files changed

Lines changed: 59 additions & 47 deletions

File tree

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"version": "https://github.com/RoboticsBrno/Esp32-lx16a/archive/refs/tags/v1.2.1.zip"
3636
}
3737
],
38-
"version": "1.4.1",
38+
"version": "1.5.0",
3939
"frameworks": ["espidf", "arduino"],
4040
"platforms": "espressif32",
4141
"build": {

src/RBCXMpu.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ static constexpr int CALIB_OFFSET_NB_MES = 1;
2525

2626
namespace rb {
2727

28-
Mpu::Mpu() { m_preInterval = xTaskGetTickCount() * portTICK_RATE_MS; }
28+
Mpu::Mpu() {
29+
m_lastTicks = xTaskGetTickCount();
30+
}
2931

3032
Mpu::~Mpu() {}
3133

@@ -57,15 +59,14 @@ void Mpu::setState(const CoprocStat_MpuStat& msg) {
5759
m_compressCoef = msg.compressCoef;
5860
calculateAcc(msg.accel);
5961
calculateGyro(msg.gyro);
60-
61-
if (m_firstRead) {
62-
m_firstRead = false;
63-
calcOffsets();
64-
}
6562
calculateAngle();
6663
}
6764

68-
void Mpu::calcOffsets() {
65+
void Mpu::clearCalibrationData() {
66+
memset(&m_mpuMotionOffset, 0, sizeof(m_mpuMotionOffset));
67+
}
68+
69+
void Mpu::setCalibrationData() {
6970
m_mpuMotionOffset.accel.x = m_mpuMotion.accel.x / CALIB_OFFSET_NB_MES;
7071
m_mpuMotionOffset.accel.y = m_mpuMotion.accel.y / CALIB_OFFSET_NB_MES;
7172
m_mpuMotionOffset.accel.z
@@ -101,13 +102,10 @@ void Mpu::calculateGyro(const CoprocStat_MpuVector& gyro) {
101102
}
102103

103104
void Mpu::calculateAngle() {
104-
m_mpuMotion.angle.x = 0;
105-
m_mpuMotion.angle.y = 0;
106-
m_mpuMotion.angle.z = 0;
107-
108-
float sgZ = (m_mpuMotion.accel.z >= 0)
105+
const float sgZ = (m_mpuMotion.accel.z >= 0)
109106
- (m_mpuMotion.accel.z
110107
< 0); // allow one angle to go from -180° to +180°
108+
111109
m_mpuMotion.angleAcc.x
112110
= atan2(m_mpuMotion.accel.y,
113111
sgZ
@@ -120,31 +118,34 @@ void Mpu::calculateAngle() {
120118
sqrt(m_mpuMotion.accel.z * m_mpuMotion.accel.z
121119
+ m_mpuMotion.accel.y * m_mpuMotion.accel.y))
122120
* RAD_2_DEG; // [- 90°,+ 90°]
123-
124-
unsigned long Tnew = xTaskGetTickCount() * portTICK_RATE_MS;
125-
m_interval = (Tnew - m_preInterval) * 1e-3;
126-
m_preInterval = Tnew;
121+
122+
const TickType_t curTicks = xTaskGetTickCount();
123+
const float elapsedSeconds = float((curTicks - m_lastTicks) * portTICK_RATE_MS) / 1000.f;
124+
m_lastTicks = curTicks;
127125

128126
// Correctly wrap X and Y angles (special thanks to Edgar Bonet!)
129127
// https://github.com/gabriel-milan/TinyMPU6050/issues/6
130128
m_mpuMotion.angle.x
131129
= wrap((m_mpuMotion.angleAcc.x
132-
+ wrap(m_mpuMotion.angle.x + m_mpuMotion.gyro.x * m_interval
130+
+ wrap( m_mpuMotion.gyro.x * elapsedSeconds
133131
- m_mpuMotion.angleAcc.x,
134132
180))
135133
+ m_mpuMotion.angleAcc.x,
136134
180);
137135
m_mpuMotion.angle.y = wrap(
138136
(m_mpuMotion.angleAcc.y
139-
+ wrap(m_mpuMotion.angle.y + sgZ * m_mpuMotion.gyro.y * m_interval
137+
+ wrap(sgZ * m_mpuMotion.gyro.y * elapsedSeconds
140138
- m_mpuMotion.angleAcc.y,
141139
90))
142140
+ m_mpuMotion.angleAcc.y,
143141
90);
144142

145-
// angleZ += gyroZ*m_interval; // not wrapped (to do???)
146143
m_mpuMotion.angle.z
147-
+= m_mpuMotion.gyro.z * m_interval; // not wrapped (to do???)
144+
+= m_mpuMotion.gyro.z * elapsedSeconds; // not wrapped (to do???)
145+
}
146+
147+
void Mpu::resetAngleZ() {
148+
m_mpuMotion.angle.z = 0;
148149
}
149150

150151
/* Wrap an angle in the range [-limit,+limit] (special thanks to Edgar Bonet!) */
@@ -171,8 +172,6 @@ float Mpu::getAngleX() { return m_mpuMotion.angle.x; }
171172
float Mpu::getAngleY() { return m_mpuMotion.angle.y; }
172173
float Mpu::getAngleZ() { return m_mpuMotion.angle.z; }
173174

174-
long Mpu::getInterval() { return m_interval; }
175-
176175
void Mpu::setCompressCoef(uint8_t coef) {
177176
sendMpuReq(
178177
CoprocReq_MpuReq {

src/RBCXMpu.h

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#pragma once
2+
3+
#include <freertos/FreeRTOS.h>
4+
25
#include "rbcx.pb.h"
36
namespace rb {
47

@@ -57,83 +60,95 @@ class Mpu {
5760
*/
5861
void sendStop();
5962

63+
/**
64+
* @brief Records current Gyro and Accelerometer data as "start" position
65+
* and subtracts it from future readings
66+
*/
67+
void setCalibrationData();
68+
69+
/**
70+
* @brief Clears the data recorded by setCalibrationData
71+
*/
72+
void clearCalibrationData();
73+
6074
/**
6175
* @brief Get the Mpu acceleration data.
62-
* @return The Mpu acceleration data.
76+
* @return The Mpu acceleration data in Gs.
6377
*/
6478
MpuVector getAcc();
6579

6680
/**
6781
* @brief Get the Mpu acceleration data on the X axis.
68-
* @return The Mpu acceleration data on the X axis.
82+
* @return The Mpu acceleration data on the X axis in Gs.
6983
*/
7084
float getAccX();
7185

7286
/**
7387
* @brief Get the Mpu acceleration data on the Y axis.
74-
* @return The Mpu acceleration data on the Y axis.
88+
* @return The Mpu acceleration data on the Y axis in Gs.
7589
*/
7690
float getAccY();
7791

7892
/**
7993
* @brief Get the Mpu acceleration data on the Z axis.
80-
* @return The Mpu acceleration data on the Z axis.
94+
* @return The Mpu acceleration data on the Z axis in Gs.
8195
*/
8296
float getAccZ();
8397

8498
/**
8599
* @brief Get the Mpu gyroscope data.
86-
* @return The Mpu gyroscope data.
100+
* @return The Mpu gyroscope data in degrees per second.
87101
*/
88102
MpuVector getGyro();
89103

90104
/**
91105
* @brief Get the Mpu gyroscope data on the X axis.
92-
* @return The Mpu gyroscope data on the X axis.
106+
* @return The Mpu gyroscope data on the X axis in degrees per second.
93107
*/
94108
float getGyroX();
95109

96110
/**
97111
* @brief Get the Mpu gyroscope data on the Y axis.
98-
* @return The Mpu gyroscope data on the Y axis.
112+
* @return The Mpu gyroscope data on the Y axis in degrees per second.
99113
*/
100114
float getGyroY();
101115

102116
/**
103117
* @brief Get the Mpu gyroscope data on the Z axis.
104-
* @return The Mpu gyroscope data on the Z axis.
118+
* @return The Mpu gyroscope data on the Z axis in degrees per second.
105119
*/
106120
float getGyroZ();
107121

108122
/**
109123
* @brief Get the Mpu angle data.
110-
* @return The Mpu angle data.
124+
* @return The Mpu angle data in degrees.
111125
*/
112126
MpuVector getAngle();
113127

114128
/**
115129
* @brief Get the Mpu angle data on the X axis.
116-
* @return The Mpu angle data on the X axis.
130+
* @return The Mpu angle data on the X axis in degrees.
117131
*/
118132
float getAngleX();
119133

120134
/**
121135
* @brief Get the Mpu angle data on the Y axis.
122-
* @return The Mpu angle data on the Y axis.
136+
* @return The Mpu angle data on the Y axis in degrees.
123137
*/
124138
float getAngleY();
125139

126140
/**
127141
* @brief Get the Mpu angle data on the Z axis.
128-
* @return The Mpu angle data on the Z axis.
142+
*
143+
* The Z angle is relative to the original heading of the robot at the start of the program,
144+
* or after latest resetAngleZ() call, NOT to geographic north.
145+
*
146+
* @return The Mpu angle data on the Z axis in degrees.
129147
*/
130148
float getAngleZ();
131149

132-
/**
133-
* @brief Get the Mpu sending interval.
134-
* @return The Mpu sending interval
135-
*/
136-
long getInterval();
150+
// Reset heading back to 0
151+
void resetAngleZ();
137152

138153
/**
139154
* @brief Set the Mpu compression coefficient (10 - 20).
@@ -152,10 +167,9 @@ class Mpu {
152167
MpuMotion9 m_mpuMotion;
153168
MpuMotion6 m_mpuMotionOffset;
154169

155-
long m_preInterval;
156-
int32_t m_interval;
157-
bool m_firstRead = true;
158-
uint8_t m_compressCoef = 4;
170+
TickType_t m_lastTicks;
171+
172+
uint8_t m_compressCoef;
159173

160174
Mpu();
161175
Mpu(const Mpu&) = delete;
@@ -166,7 +180,6 @@ class Mpu {
166180
void calculateGyro(const CoprocStat_MpuVector& gyro);
167181
void calculateAngle();
168182

169-
void calcOffsets();
170183
float wrap(float angle, float limit);
171184

172185
void sendMpuReq(CoprocReq_MpuReq mpuReq);

src/RBCXVersion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
#define RBCX
66

77
#define RB3204_MAJOR 1
8-
#define RB3204_MINOR 4
9-
#define RB3204_PATCH 1
8+
#define RB3204_MINOR 5
9+
#define RB3204_PATCH 0

0 commit comments

Comments
 (0)