【Follow me第二季第4期】-任务2:学习IMU基础知识,调试IMU传感器,通过串口打印...
[复制链接]
任务要求:打印加速度、角速度和温度
根据视频介绍,引入库LSM6DSOX.h
#include <Arduino_LSM6DSOX.h>
初始化之前,声明变量,存放相应数据:
float acc_x,acc_y,acc_z;
float gyro_x,gyro_y,gyro_z;
float temp;
int update_flag;
初始化中,对串口和陀螺仪进行初始化,并打印陀螺仪的采样频率:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if(!IMU.begin())
{
Serial.println("Falled to initialize IMU");
while(1);
}
Serial.print("Acc sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
Serial.print("Gyro sample rate = ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println("Hz");
}
循环函数中,为了更好的观察采样数据,循环打印采样频率,并打印三轴加速度、三轴角速度和温度
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Acc sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
Serial.print("Gyro sample rate = ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println("Hz");
if (IMU.accelerationAvailable())
{
IMU.readAcceleration(acc_x,acc_y,acc_z);
update_flag = 1;
}
if (IMU.gyroscopeAvailable())
{
IMU.readGyroscope(gyro_x,gyro_y,gyro_z);
update_flag = 1;
}
if (IMU.temperatureAvailable())
{
IMU.readTemperatureFloat(temp);
update_flag = 1;
}
if (update_flag)
{
update_flag=0;
Serial.print(acc_x);
Serial.print(',');
Serial.print(acc_y);
Serial.print(',');
Serial.print(acc_z);
Serial.print(',');
Serial.print(gyro_x);
Serial.print(',');
Serial.print(gyro_y);
Serial.print(',');
Serial.print(gyro_z);
Serial.print(',');
Serial.print(gyro_x);
Serial.print(',');
Serial.println(temp);
delay(500);
}
}
全部代码如下:
#include <Arduino_LSM6DSOX.h>
float acc_x,acc_y,acc_z;
float gyro_x,gyro_y,gyro_z;
float temp;
int update_flag;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if(!IMU.begin())
{
Serial.println("Falled to initialize IMU");
while(1);
}
Serial.print("Acc sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
Serial.print("Gyro sample rate = ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println("Hz");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Acc sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println("Hz");
Serial.print("Gyro sample rate = ");
Serial.print(IMU.gyroscopeSampleRate());
Serial.println("Hz");
if (IMU.accelerationAvailable())
{
IMU.readAcceleration(acc_x,acc_y,acc_z);
update_flag = 1;
}
if (IMU.gyroscopeAvailable())
{
IMU.readGyroscope(gyro_x,gyro_y,gyro_z);
update_flag = 1;
}
if (IMU.temperatureAvailable())
{
IMU.readTemperatureFloat(temp);
update_flag = 1;
}
if (update_flag)
{
update_flag=0;
Serial.print(acc_x);
Serial.print(',');
Serial.print(acc_y);
Serial.print(',');
Serial.print(acc_z);
Serial.print(',');
Serial.print(gyro_x);
Serial.print(',');
Serial.print(gyro_y);
Serial.print(',');
Serial.print(gyro_z);
Serial.print(',');
Serial.print(gyro_x);
Serial.print(',');
Serial.println(temp);
delay(500);
}
}
运行 结果如下:
|