在 Arduino 中校准 Adafruit LSM303
Posted
技术标签:
【中文标题】在 Arduino 中校准 Adafruit LSM303【英文标题】:Calibrating Adafruit LSM303 in Arduino 【发布时间】:2019-02-20 00:26:38 【问题描述】:我有一个连接到 Arduino Uno 的 Adafruit LSM303 加速度计和指南针。
我使用了 Adafruit 推荐的校准草图之一(见下文)并得到以下输出:
加速度最小值:-8.55 -10.16 -14.12 加速度最大值:9.89 11.26 15.26 Mag 最小值:-60.09 -57.55 -130.51 Mag 最大值:69.64 78.09 68.88如何使用 Adafruit 的 recommended code 获取校准输出并使用它来校准指南针?
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
/* Assign a unique ID to these sensors */
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(54321);
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(12345);
float AccelMinX, AccelMaxX;
float AccelMinY, AccelMaxY;
float AccelMinZ, AccelMaxZ;
float MagMinX, MagMaxX;
float MagMinY, MagMaxY;
float MagMinZ, MagMaxZ;
long lastDisplayTime;
void setup(void)
Serial.begin(9600);
Serial.println("LSM303 Calibration"); Serial.println("");
/* Initialise the accelerometer */
if(!accel.begin())
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
/* Initialise the magnetometer */
if(!mag.begin())
/* There was a problem detecting the LSM303 ... check your connections */
Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
while(1);
lastDisplayTime = millis();
void loop(void)
/* Get a new sensor event */
sensors_event_t accelEvent;
sensors_event_t magEvent;
accel.getEvent(&accelEvent);
mag.getEvent(&magEvent);
if (accelEvent.acceleration.x < AccelMinX) AccelMinX = accelEvent.acceleration.x;
if (accelEvent.acceleration.x > AccelMaxX) AccelMaxX = accelEvent.acceleration.x;
if (accelEvent.acceleration.y < AccelMinY) AccelMinY = accelEvent.acceleration.y;
if (accelEvent.acceleration.y > AccelMaxY) AccelMaxY = accelEvent.acceleration.y;
if (accelEvent.acceleration.z < AccelMinZ) AccelMinZ = accelEvent.acceleration.z;
if (accelEvent.acceleration.z > AccelMaxZ) AccelMaxZ = accelEvent.acceleration.z;
if (magEvent.magnetic.x < MagMinX) MagMinX = magEvent.magnetic.x;
if (magEvent.magnetic.x > MagMaxX) MagMaxX = magEvent.magnetic.x;
if (magEvent.magnetic.y < MagMinY) MagMinY = magEvent.magnetic.y;
if (magEvent.magnetic.y > MagMaxY) MagMaxY = magEvent.magnetic.y;
if (magEvent.magnetic.z < MagMinZ) MagMinZ = magEvent.magnetic.z;
if (magEvent.magnetic.z > MagMaxZ) MagMaxZ = magEvent.magnetic.z;
if ((millis() - lastDisplayTime) > 1000) // display once/second
Serial.print("Accel Minimums: "); Serial.print(AccelMinX); Serial.print(" ");Serial.print(AccelMinY); Serial.print(" "); Serial.print(AccelMinZ); Serial.println();
Serial.print("Accel Maximums: "); Serial.print(AccelMaxX); Serial.print(" ");Serial.print(AccelMaxY); Serial.print(" "); Serial.print(AccelMaxZ); Serial.println();
Serial.print("Mag Minimums: "); Serial.print(MagMinX); Serial.print(" ");Serial.print(MagMinY); Serial.print(" "); Serial.print(MagMinZ); Serial.println();
Serial.print("Mag Maximums: "); Serial.print(MagMaxX); Serial.print(" ");Serial.print(MagMaxY); Serial.print(" "); Serial.print(MagMaxZ); Serial.println(); Serial.println();
lastDisplayTime = millis();
【问题讨论】:
【参考方案1】:在您链接到的 adafruit 页面上,它说“我们可以使用 atan2() 函数来计算由 Y 轴和 X 轴读数定义的矢量的角度”我还找到了 instruction from Honeywell 关于如何计算罗盘航向的信息当给出一个 3 轴磁力计读数时,它证实上述代码的公式应如下:
float magFieldRad = atan2(magEvent.magnetic.x, magEvent.magnetic.y); // compass in Radians
float magFieldDeg = (magFieldRad * 180) / 3.1415; // compass in Degrees
【讨论】:
以上是关于在 Arduino 中校准 Adafruit LSM303的主要内容,如果未能解决你的问题,请参考以下文章