1. 下载Arduino IDE
2. 在IDE 的 Board->Boardsmagner处下载Nano 33的依赖
- 在Tool->Manage LIbraries处下载LSM9DS1库,以及SensorFusioin库
4. 结合两个库的Example进行复制粘贴:
#include <Arduino_LSM9DS1.h> . // this is for IMU on BLE 33
#include "SensorFusion.h" //SF
SF fusion;
float gx, gy, gz, ax, ay, az, mx, my, mz;
float pitch, roll, yaw;
float deltat;
void setup() {
Serial.begin(115200);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
}
void loop() {
// now you should read the gyroscope, accelerometer (and magnetometer if you have it also)
// NOTE: the gyroscope data have to be in radians
// if you have them in degree convert them with: DEG_TO_RAD example: gx * DEG_TO_RAD
if (IMU.accelerationAvailable() && IMU.gyroscopeAvailable()&& IMU.magnetAvailable()) {
IMU.readAcceleration(ax, ay, az);
IMU.readGyroscope(gx, gy, gz);
IMU.readMagneticField(mx, my, mz);
float gyroScale =3.1415926f / 180;
gx = gx * gyroScale;
gy = gy * gyroScale;
gz = gz * gyroScale;
deltat = fusion.deltatUpdate(); //this have to be done before calling the fusion update
//choose only one of these two:
// fusion.MahonyUpdate(gx, gy, gz, ax, ay, az, deltat); //mahony is suggested if there isn t the mag and the mcu is slow
fusion.MadgwickUpdate(gx, gy, gz, ax, ay, az, mx, my, mz, deltat); //else use the magwick, it is slower but more accurate
pitch = fusion.getPitchRadians();
roll = -fusion.getRollRadians(); //you could also use getRollRadians() etc
yaw = fusion.getYawRadians();
Serial.print(pitch);
Serial.print(":");
Serial.print(roll);
Serial.print(":");
Serial.println(yaw);
}
}
- 复制粘贴,上传到Arduino
- 下载安装Processing, 复制粘贴SensorFusion库的代码就能运行啦~
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...





