Skip to content

Commit 192c8af

Browse files
committed
1.0.1
1 parent f0fdf9d commit 192c8af

File tree

6 files changed

+79
-81
lines changed

6 files changed

+79
-81
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# SensorFusion
22
Using IMUs is one of the most struggling part of every Arduino lovers, here there is a simple solution.
33

4-
![heading](https://image.ibb.co/cNL6n9/2.png)
4+
![Alt text](/extras/axis.png?raw=true "axis")
55

66
This library will work with every IMU, it just need the raw data of gyroscope and accelerometer (the magnetometer isn't mandatory), it is based on these two libraries:
77
- https://github.com/PaulStoffregen/MahonyAHRS
88
- https://github.com/PaulStoffregen/MadgwickAHRS
99

1010
I just made small modifications in the way the libraries handled the timing between two measurements and melted them together
1111

12-
## Installation
13-
Use the arduino library manager or download directly from github
12+
13+
# Installation
14+
Use the arduino/platformIO library manager or download directly from github
15+
1416

1517
# About
1618

@@ -23,9 +25,7 @@ please note that the Roll is inverted
2325

2426
I am using an STM32F103F103 known as blue pill but also any Arduino board will work
2527

26-
The IMU is a cheap mpu9250, you could find it everywhere for about 2€ (eBay, Aliexpress, ecc), to use it I strongly suggest you [this library](https://github.com/bolderflight/MPU9250)
27-
28-
28+
The IMU is a cheap MPU9250, you could find it everywhere for about 2€ (eBay, Aliexpress, ecc), to use it I strongly suggest you [this library](https://github.com/bolderflight/MPU9250)
2929

30-
If you wish use "IMU_tester" to see how well you IMU works (needs Processing)
31-
Note: i am using also this very useful library: Streaming
30+
If you wish use `IMU_tester` in the extras folder to see how you IMU works (needs Processing)
31+
Note: I am using also this very useful library: [Streaming](https://github.com/geneReeves/ArduinoStreaming)

examples/MPU9250_SPI_SF/MPU9250_SPI_SF.ino

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
//the Serial needs this library https://github.com/geneReeves/ArduinoStreaming
2-
3-
#include "MPU9250.h"
1+
#include "MPU9250.h" // https://github.com/bolderflight/MPU9250
2+
#include "Streaming.h" // needed for the Serial output https://github.com/geneReeves/ArduinoStreaming
43
#include "SensorFusion.h"
5-
#include "Streaming.h"
4+
SF fusion;
5+
66
float gx, gy, gz, ax, ay, az, mx, my, mz, temp;
77
float pitch, roll, yaw;
88
float deltat;
9-
SF filter;
9+
1010
#define SS_PIN PB12
1111
SPIClass mySPI (2);
1212
MPU9250 IMU(mySPI, SS_PIN);
1313
int status;
1414

15-
//#define EULER_DATA
15+
#define EULER_DATA
1616
//#define RAW_DATA
17-
#define PROCESSING
17+
//#define PROCESSING
1818

1919

2020
void setup() {
@@ -56,22 +56,22 @@ void loop() {
5656
Serial << "TEMP:\t" << temp << newl << newl;
5757
#endif
5858

59-
deltat = filter.deltatUpdate();
60-
//filter.MahonyUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat); //mahony is suggested if there isn't the mag
61-
filter.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat); //else use the magwick
59+
deltat = fusion.deltatUpdate();
60+
//fusion.MahonyUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat); //mahony is suggested if there isn't the mag
61+
fusion.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat); //else use the magwick
6262

63-
roll = filter.getRoll();
64-
pitch = filter.getPitch();
65-
yaw = filter.getYaw();
63+
roll = fusion.getRoll();
64+
pitch = fusion.getPitch();
65+
yaw = fusion.getYaw();
6666

6767
#ifdef EULER_DATA
6868
Serial << "Pitch:\t" << pitch << "\t\tRoll:\t" << roll << "\t\tYaw:\t" << yaw << newl << newl;
6969
#endif
7070

7171
#ifdef PROCESSING
72-
roll = filter.getRollRadians();
73-
pitch = filter.getPitchRadians();
74-
yaw = filter.getYawRadians();
72+
roll = fusion.getRollRadians();
73+
pitch = fusion.getPitchRadians();
74+
yaw = fusion.getYawRadians();
7575
Serial << pitch << ":" << roll << ":" << yaw << newl;
7676
#endif
7777

examples/SensorFusion/SensorFusion.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// IMPORTANT
2-
// this is not a working example, this is just to show how to set the filter
3-
// if you need a working example please see: https://www.hackster.io/vincenzo-g/sensor-fusion-algorithms-made-simple-6c55f6
2+
// this is not a working example, this is just to show how to set the library
3+
// if you need a working example please see the other example
44

55

66
#include "SensorFusion.h" //SF
7-
SF filter;
7+
SF fusion;
88

99
float gx, gy, gz, ax, ay, az, mx, my, mz;
1010
float pitch, roll, yaw;
@@ -23,14 +23,14 @@ void loop() {
2323
// NOTE: the gyroscope data have to be in radians
2424
// if you have them in degree convert them with: DEG_TO_RAD example: gx * DEG_TO_RAD
2525

26-
deltat = filter.deltatUpdate(); //this have to be done before calling the filter update
26+
deltat = fusion.deltatUpdate(); //this have to be done before calling the fusion update
2727
//choose only one of these two:
28-
filter.MahonyUpdate(gx, gy, gz, ax, ay, az, deltat); //mahony is suggested if there isn't the mag and the mcu is slow
29-
//filter.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat); //else use the magwick, it is slower but more accurate
28+
fusion.MahonyUpdate(gx, gy, gz, ax, ay, az, deltat); //mahony is suggested if there isn't the mag and the mcu is slow
29+
//fusion.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat); //else use the magwick, it is slower but more accurate
3030

31-
pitch = filter.getPitch();
32-
roll = filter.getRoll(); //you could also use getRollRadians() ecc
33-
yaw = filter.getYaw();
31+
pitch = fusion.getPitch();
32+
roll = fusion.getRoll(); //you could also use getRollRadians() ecc
33+
yaw = fusion.getYaw();
3434

3535
Serial.print("Pitch:\t"); Serial.println(pitch);
3636
Serial.print("Roll:\t"); Serial.println(roll);
Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
/*
2-
This code is made for processing https://processing.org/
3-
*/
4-
5-
import processing.serial.*; // import the Processing serial library
6-
Serial myPort; // The serial port
7-
String my_port = "COM11"; //choose your port
8-
float xx, yy, zz;
9-
10-
void setup() {
11-
size(1024, 800, P3D);
12-
13-
myPort = new Serial(this, my_port, 115200);
14-
myPort.bufferUntil('\n');
15-
16-
smooth();
17-
}
18-
19-
void draw() {
20-
background(0);
21-
noStroke();
22-
translate(width/2, height/2);
23-
pushMatrix();
24-
rotateX(xx);//pitch
25-
rotateY(zz);//yaw
26-
rotateZ(yy);//roll
27-
box(100, 50, 600);
28-
popMatrix();
29-
}
30-
31-
32-
void serialEvent(Serial myPort) {
33-
34-
String myString = myPort.readStringUntil('\n');
35-
myString = trim(myString);
36-
float sensors[] = float(split(myString, ':'));
37-
38-
xx = sensors[0];
39-
yy = sensors[1];
40-
zz = sensors[2];
41-
42-
//println("roll: " + xx + " pitch: " + yy + " yaw: " + zz + "\n"); //debug
43-
1+
/*
2+
This code is made for processing https://processing.org/
3+
*/
4+
5+
import processing.serial.*; // import the Processing serial library
6+
Serial myPort; // The serial port
7+
String my_port = "COM11"; // choose your port
8+
float xx, yy, zz;
9+
10+
void setup() {
11+
size(1024, 800, P3D);
12+
13+
myPort = new Serial(this, my_port, 115200);
14+
myPort.bufferUntil('\n');
15+
16+
smooth();
17+
}
18+
19+
void draw() {
20+
background(0);
21+
noStroke();
22+
translate(width/2, height/2);
23+
pushMatrix();
24+
rotateX(xx);//pitch
25+
rotateY(zz);//yaw
26+
rotateZ(yy);//roll
27+
box(100, 50, 600);
28+
popMatrix();
29+
}
30+
31+
32+
void serialEvent(Serial myPort) {
33+
34+
String myString = myPort.readStringUntil('\n');
35+
myString = trim(myString);
36+
float sensors[] = float(split(myString, ':'));
37+
38+
xx = sensors[0];
39+
yy = sensors[1];
40+
zz = sensors[2];
41+
42+
//println("roll: " + xx + " pitch: " + yy + " yaw: " + zz + "\n"); //debug
43+
4444
}

extras/axis.png

11.8 KB
Loading

keywords.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@
33
#######################################
44
SF KEYWORD1
55

6-
76
#######################################
87
# Methods and Functions (KEYWORD2)
98
#######################################
109
MadgwickUpdate KEYWORD2
1110
MahonyUpdate KEYWORD2
1211
deltatUpdate KEYWORD2
13-
getPitch KEYWORD2
14-
getYaw KEYWORD2
15-
getRoll KEYWORD2
12+
getPitch KEYWORD2
13+
getYaw KEYWORD2
14+
getRoll KEYWORD2
1615
getRollRadians KEYWORD2
1716
getPitchRadians KEYWORD2
1817
getYawRadians KEYWORD2
1918

20-
2119
#######################################
2220
# Constants (LITERAL1)
2321
#######################################

0 commit comments

Comments
 (0)