Skip to content

Commit eff4343

Browse files
committed
Added HMC5883L_CALIBRATE
1 parent 08aa5a4 commit eff4343

22 files changed

+1821
-0
lines changed

HMC5883L_CALIBRATE/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
set(EXTRA_COMPONENT_DIRS ../components/I2Cdev
6+
../components/MPU6050
7+
../components/AK8963
8+
../components/HMC5883L
9+
../components/websocket)
10+
11+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
12+
project(LSM303DLHC_CALIBRATE)

HMC5883L_CALIBRATE/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# HMC5883L_CALIBRATE
2+
HMC5883L is connected as slave device of MPU9XXX.
3+
You can use this to get the compass offset value for each axis.
4+
5+
# Software requiment
6+
ESP-IDF V4.4/V5.0.
7+
ESP-IDF V5 is required when using ESP32-C2.
8+
9+
10+
# Hardware requirements
11+
GY-86/87 9DoF MotionTracking device.
12+
13+
# Wireing
14+
|GY86/87||ESP32|ESP32-S2/S3|ESP32-C2/C3||
15+
|:-:|:-:|:-:|:-:|:-:|:-:|
16+
|VCC_IN|--|N/C|N/C|N/C||
17+
|3.3V|--|3.3V|3.3V|3.3V||
18+
|GND|--|GND|GND|GND||
19+
|SCL|--|GPIO22|GPIO12|GPIO5|(*1)|
20+
|SDA|--|GPIO21|GPIO11|GPIO4|(*1)|
21+
22+
(*1)You can change it to any pin using menuconfig.
23+
24+
# Compass calibration
25+
```
26+
git clone https://github.com/nopnop2002/esp-idf-mpu6050-dmp
27+
cd esp-idf-mpu6050-dmp/HMC5883L_CALIBRATE
28+
idf.py set-target {esp32/esp32s2/esp32s3/esp32c2/esp32c3}
29+
idf.py menuconfig
30+
idf.py flash
31+
```
32+
33+
### Configuration
34+
To find the offset value, set the compass offset to 0.
35+
![config-AK8963-1](https://user-images.githubusercontent.com/6020549/227429885-7326b087-f37e-4f42-9f7b-0928e27e1b01.jpg)
36+
![config-AK8963-2](https://user-images.githubusercontent.com/6020549/227429891-0a10160d-e845-4a79-b188-7e3ae59c279f.jpg)
37+
38+
### Execute calibration
39+
ESP32 acts as a web server.
40+
I used [this](https://github.com/Molorius/esp32-websocket) component.
41+
This component can communicate directly with the browser.
42+
It's a great job.
43+
Enter the following in the address bar of your web browser.
44+
```
45+
http:://{IP of ESP32}/
46+
or
47+
http://esp32.local/
48+
```
49+
50+
As you move the IMU it plots the X, Y and Z values.
51+
X, Y, Z offset are displayed.
52+
53+
![hmc5883l-1](https://user-images.githubusercontent.com/6020549/227674738-9d9eb071-e494-4de3-8829-d1426c398f3d.jpg)
54+
![hmc5883l-2](https://user-images.githubusercontent.com/6020549/227674741-15c93ca8-17d4-487d-bc3e-12792c536a46.jpg)
55+
56+
### Execute calibration again
57+
If you set the offset you got from the calibration and run it again, the circle position will change.
58+
59+
![hmc5883l-11](https://user-images.githubusercontent.com/6020549/227674769-63d77758-c217-4f0f-b7ac-1f310876191b.jpg)
60+
![hmc5883l-12](https://user-images.githubusercontent.com/6020549/227674771-85e8e65c-44c4-4bac-b964-07f62f8bf31b.jpg)
61+
62+

HMC5883L_CALIBRATE/html/error.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>ESP32 Error 404</title>
5+
<link type="text/css" href="test.css" rel="stylesheet"/>
6+
</head>
7+
<body>
8+
9+
10+
11+
<h1>Error 404</h1>
12+
<p>Unknown page. <a href="/">Return home.</a></p>
13+
14+
15+
</body>
16+
</html>

HMC5883L_CALIBRATE/html/favicon.ico

1.12 KB
Binary file not shown.

HMC5883L_CALIBRATE/html/main.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Your favorite style is defined here.
3+
*/
4+
5+
.float_noborder{
6+
float: left;
7+
}
8+
9+
.float_border{
10+
float: left;
11+
border: 1px solid #333;
12+
}
13+
14+
.no_float{
15+
clear: both;
16+
}

HMC5883L_CALIBRATE/html/main.js

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
//document.getElementById("datetime").innerHTML = "WebSocket is not connected";
2+
3+
var websocket = new WebSocket('ws://'+location.hostname+'/');
4+
var xmax = -9999;
5+
var xmin = 9999;
6+
var xcenter = 0;
7+
var xoffset = 0;
8+
var ymax = -9999;
9+
var ymin = 9999;
10+
var ycenter = 0;
11+
var yoffset = 0;
12+
var zmax = -9999;
13+
var zmin = 9999;
14+
var zcenter = 0;
15+
var zoffset = 0;
16+
17+
function CreateTrace() {
18+
var trace1 = {
19+
x:[], y:[],
20+
mode: 'markers',
21+
marker: {
22+
size: 4,
23+
color: 'rgb(255, 0, 0)',
24+
line: {
25+
color: 'rgb(60, 60, 60)',
26+
width: 0.5},
27+
opacity: 0.8},
28+
type: 'scatter'
29+
};
30+
31+
var trace2 = {
32+
x:[], y:[],
33+
mode: 'markers',
34+
marker: {
35+
size: 4,
36+
color: 'rgb(0, 0, 255)',
37+
line: {
38+
color: 'rgb(60, 60, 60)',
39+
width: 0.5},
40+
opacity: 0.8},
41+
type: 'scatter'
42+
};
43+
44+
var data1 = [trace1];
45+
var data2 = [trace2];
46+
var layout1 = {
47+
title:'X-Axis / Y-Axis',
48+
plot_bgcolor: 'rgb(66, 203, 245)',
49+
width: 450,
50+
};
51+
var layout2 = {
52+
title:'Y-Axis / Z-Axis',
53+
plot_bgcolor: 'rgb(252, 244, 3)',
54+
width: 450,
55+
};
56+
57+
Plotly.newPlot('myDiv1', data1, layout1)
58+
Plotly.newPlot('myDiv2', data2, layout2)
59+
}
60+
61+
62+
function ClearValuePush() {
63+
console.log('ClearChartPush');
64+
xmax = -9999;
65+
xmin = 9999;
66+
xcenter = 0;
67+
xoffset = 0;
68+
ymax = -9999;
69+
ymin = 9999;
70+
ycenter = 0;
71+
yoffset = 0;
72+
zmax = -9999;
73+
zmin = 9999;
74+
zcenter = 0;
75+
zoffset = 0;
76+
77+
Plotly.deleteTraces('myDiv1', 0);
78+
Plotly.deleteTraces('myDiv2', 0);
79+
CreateTrace();
80+
}
81+
82+
function sendText(name) {
83+
console.log('sendText');
84+
var data = {};
85+
data["id"] = name;
86+
console.log('data=', data);
87+
json_data = JSON.stringify(data);
88+
console.log('json_data=' + json_data);
89+
websocket.send(json_data);
90+
}
91+
92+
websocket.onopen = function(evt) {
93+
console.log('WebSocket connection opened');
94+
var data = {};
95+
data["id"] = "init";
96+
console.log('data=', data);
97+
json_data = JSON.stringify(data);
98+
console.log('json_data=' + json_data);
99+
websocket.send(json_data);
100+
//document.getElementById("datetime").innerHTML = "WebSocket is connected!";
101+
102+
CreateTrace();
103+
}
104+
105+
websocket.onmessage = function(evt) {
106+
var msg = evt.data;
107+
console.log("msg=" + msg);
108+
var values = msg.split('\4'); // \4 is EOT
109+
//console.log("values=" + values);
110+
switch(values[0]) {
111+
case 'HEAD':
112+
console.log("HEAD values[1]=" + values[1]);
113+
var h1 = document.getElementById( 'header' );
114+
h1.textContent = values[1];
115+
break;
116+
117+
case 'DATA':
118+
console.log("DATA values[1]=" + values[1]);
119+
console.log("DATA values[2]=" + values[2]);
120+
console.log("DATA values[3]=" + values[3]);
121+
var xval = parseInt(values[1], 10);
122+
var yval = parseInt(values[2], 10);
123+
var zval = parseInt(values[3], 10);
124+
if (xval > 1000 || xval < -1000) return;
125+
if (yval > 1000 || yval < -1000) return;
126+
if (zval > 1000 || zval < -1000) return;
127+
128+
Plotly.extendTraces('myDiv1', {
129+
x: [[xval]],
130+
y: [[yval]]
131+
}, [0])
132+
Plotly.extendTraces('myDiv2', {
133+
x: [[yval]],
134+
y: [[zval]]
135+
}, [0])
136+
137+
if (xval < xmin) xmin = xval;
138+
if (xval > xmax) xmax = xval;
139+
//console.log("xmin=" + xmin);
140+
//console.log("xmax=" + xmax);
141+
xcenter = (xmax - xmin) / 2.0;
142+
//console.log("xcenter=" + xcenter);
143+
xoffset = xcenter - xmax;
144+
//console.log("xoffset=" + xoffset);
145+
document.getElementById('xmin_val').innerText = xmin.toString();
146+
document.getElementById('xmax_val').innerText = xmax.toString();
147+
document.getElementById('xcenter_val').innerText = xcenter.toString();
148+
document.getElementById('xoffset_val').innerText = xoffset.toString();
149+
150+
if (yval < ymin) ymin = yval;
151+
if (yval > ymax) ymax = yval;
152+
//console.log("ymin=" + ymin);
153+
//console.log("ymax=" + ymax);
154+
ycenter = (ymax - ymin) / 2.0;
155+
//console.log("ycenter=" + ycenter);
156+
yoffset = ycenter - ymax;
157+
//console.log("yoffset=" + yoffset);
158+
document.getElementById('ymin_val').innerText = ymin.toString();
159+
document.getElementById('ymax_val').innerText = ymax.toString();
160+
document.getElementById('ycenter_val').innerText = ycenter.toString();
161+
document.getElementById('yoffset_val').innerText = yoffset.toString();
162+
163+
if (zval < zmin) zmin = zval;
164+
if (zval > zmax) zmax = zval;
165+
//console.log("zmin=" + zmin);
166+
//console.log("zmax=" + zmax);
167+
zcenter = (zmax - zmin) / 2.0;
168+
//console.log("zcenter=" + zcenter);
169+
zoffset = zcenter - zmax;
170+
//console.log("zoffset=" + zoffset);
171+
document.getElementById('zmin_val').innerText = zmin.toString();
172+
document.getElementById('zmax_val').innerText = zmax.toString();
173+
document.getElementById('zcenter_val').innerText = zcenter.toString();
174+
document.getElementById('zoffset_val').innerText = zoffset.toString();
175+
176+
default:
177+
break;
178+
}
179+
}
180+
181+
websocket.onclose = function(evt) {
182+
console.log('Websocket connection closed');
183+
//document.getElementById("datetime").innerHTML = "WebSocket closed";
184+
}
185+
186+
websocket.onerror = function(evt) {
187+
console.log('Websocket error: ' + evt);
188+
//document.getElementById("datetime").innerHTML = "WebSocket error????!!!1!!";
189+
}

HMC5883L_CALIBRATE/html/root.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
<link rel="stylesheet" type="text/css" href="main.css">
6+
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
7+
<title>Compass Calibrate</title>
8+
</head>
9+
10+
<body>
11+
<h1 id="header"></h1>
12+
13+
<div id="myDiv1" class="float_border"></div>
14+
<div id="myDiv2" class="float_border"></div>
15+
16+
<div id="ClearValue" class="no_float">
17+
<button onclick="ClearValuePush()">ClearValue</button>
18+
</div>
19+
20+
<div id="xaxis1" class="no_float">
21+
<span id = "label">xmin</span>
22+
<span id = "xmin_val">0000</span>
23+
<span id = "label">xmax</span>
24+
<span id = "xmax_val">9999</span>
25+
<span id = "label">center</span>
26+
<span id = "xcenter_val">0000</span>
27+
<span id = "label">offset</span>
28+
<span id = "xoffset_val">0000</span>
29+
</div>
30+
31+
<div id="yaxis" class="no_float">
32+
<span id = "label">ymin</span>
33+
<span id = "ymin_val">0000</span>
34+
<span id = "label">ymax</span>
35+
<span id = "ymax_val">9999</span>
36+
<span id = "label">center</span>
37+
<span id = "ycenter_val">0000</span>
38+
<span id = "label">offset</span>
39+
<span id = "yoffset_val">0000</span>
40+
</div>
41+
42+
<div id="zaxis" class="no_float">
43+
<span id = "label">zmin</span>
44+
<span id = "zmin_val">0000</span>
45+
<span id = "label">zmax</span>
46+
<span id = "zmax_val">9999</span>
47+
<span id = "label">center</span>
48+
<span id = "zcenter_val">0000</span>
49+
<span id = "label">offset</span>
50+
<span id = "zoffset_val">0000</span>
51+
</div>
52+
53+
<script type="text/javascript" src="main.js"></script>
54+
</body>
55+
</html>
56+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
idf_component_register(SRCS "main.cpp" "mpu6050.cpp" "wifi.c" "udp_trans.c" "web_server.c" "web_client.c"
2+
INCLUDE_DIRS "."
3+
EMBED_FILES "../html/error.html"
4+
"../html/favicon.ico"
5+
"../html/main.js"
6+
"../html/root.html"
7+
"../html/main.css")

0 commit comments

Comments
 (0)