我有一个与Arduino Uno相连的Adafruit LSM303加速度计和指南针。
我使用了Adafruit推荐的校准草图之一(见下文),并得到以下输出:
如何获取校准输出并使用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();
}
}
答案 0 :(得分:0)
在链接到的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