arduino上使用QMC输出
通过IIC输出三轴磁力原始数据
通过MechaQMC5883库输出yaw角
我找了不少关于QMC磁力计的文章,发现不管是商家给的程序还是博客的其他文章,都是基于stm32的,而QMC在arduino上的应用却没多少,于是就一边看着数据文档,一边在arduino上实现QMC。
这里用的是arduino nano,
引脚连接:
nano -> QMC
5V -> VCC
A5 -> SCL
A4 -> SDA
GND -> GND
首先在例程中找到I2C_scanner,打开,连接QMC,上传。
如果没有这个例程,这里给出:
// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
#include <Wire.h>
void setup() {
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop() {
int nDevices = 0;
Serial.println("Scanning...");
for (byte address = 1; address < 127; ++address) {
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.print(address, HEX);
Serial.println(" !");
++nDevices;
} else if (error == 4) {
Serial.print("Unknown error at address 0x");
if (address < 16) {
Serial.print("0");
}
Serial.println(address, HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
} else {
Serial.println("done\n");
}
delay(5000); // Wait 5 seconds for next scan
}
上传后,打开串口监视器,可以看见QMC的地址
然后编写程序:
#include <Wire.h> //使用IIC库
#define address 0x0D //QMC5883L的设备地址设置
void setup(){
Serial.begin(9600); //初始化串口和I2C口
Wire.begin();
//初始化模块
Wire.beginTransmission(address);
Wire.write(0x09); //选择控制寄存器0X09
Wire.write(0x1D); //设置0x09寄存器为:OSR:512Hz,RNG:+/-8Gauss,ODG:200Hz,MODE:连续工作模式
Wire.endTransmission();
}
void loop(){
int x,y,z; //triple axis data
Wire.beginTransmission(address);//读写开始
Wire.write(0x00); //选择X,Y,Z所在数据储存寄存器
Wire.endTransmission();
//读取X、Y、Z三个方向的坐标值
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
//打印坐标到串口
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.println(z);
delay(100);
}
这里会输出三轴磁力数据:
但这些还没有转为角度,使用下面这个链接中的库转为yaw角:
https://github.com/keepworking/Mecha_QMC5883L
下载这个库,并保存至你的arduino库位置中:
…\Arduino\libraries
将文件名修改为MechaQMC5883
编写以下代码:
#include <Wire.h>
#include <MechaQMC5883.h>
MechaQMC5883 qmc;
void setup() {
Wire.begin();
Serial.begin(9600);
qmc.init();
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
}
void loop() {
int x,y,z;
int yaw;
qmc.read(&x,&y,&z);
yaw = qmc.azimuth(&y,&x);
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.print(" yaw: ");
Serial.print(yaw);
Serial.println();
delay(100);
}
上传,打开串口监视器:
已经输出yaw角!