驴友花雕
发表于 2021-9-29 16:19:44
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之六:测试方向
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之六:测试方向
实验接线:
QMC5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#include <QMC5883LCompass.h>
QMC5883LCompass compass;
void setup() {
Serial.begin(9600);
compass.init();
}
void loop() {
compass.read();
byte a = compass.getAzimuth();
char myArray;
compass.getDirection(myArray, a);
Serial.print(myArray);
Serial.print(myArray);
Serial.print(myArray);
Serial.println();
delay(250);
}
驴友花雕
发表于 2021-9-29 16:23:15
实验串口返回情况
驴友花雕
发表于 2021-9-29 16:34:55
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之七:三轴XYZ实时数据
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之七:三轴XYZ实时数据
实验接线:
5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#include <QMC5883LCompass.h>
QMC5883LCompass compass;
void setup() {
Serial.begin(9600);
Serial.print("5883L准备就绪");
compass.init();
/*
调用 setSmoothing(STEPS, ADVANCED);
STEPS = int 平滑结果的步数。有效 1 到 10。
更高的步骤等于更平滑但更长的处理时间。
ADVANCED = bool 打开或关闭高级平滑。True 将从每个步骤中删除最大值和最小值,然后正常处理。
启用此功能将导致更加平滑,但需要更长的处理时间。
*/
compass.setSmoothing(10, true);
}
void loop() {
int x, y, z;
// Read compass values
compass.read();
// Return XYZ readings
x = compass.getX();
y = compass.getY();
z = compass.getZ();
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.print(z);
Serial.println();
delay(250);
}
驴友花雕
发表于 2021-9-29 16:58:04
实验串口返回情况
驴友花雕
发表于 2021-9-29 16:59:55
实验串口绘图器返回情况
驴友花雕
发表于 2021-9-29 17:15:16
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之八:简单的QMC5883L 指南针演示
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之八:简单的QMC5883L 指南针演示
实验接线:
5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#include <QMC5883L.h>
#include <Wire.h>
QMC5883L compass;
void setup() {
Wire.begin();
compass.init();
compass.setSamplingRate(50);
Serial.begin(9600);
Serial.println("QMC5883L 指南针演示");
Serial.println("向各个方向转动罗盘来校准....");
}
void loop(){
int heading = compass.readHeading();
if (heading == 0) {
/* Still calibrating, so measure but don't print */
} else {
Serial.println(heading);
}
delay(250);
}
驴友花雕
发表于 2021-9-29 17:17:09
实验串口返回情况
驴友花雕
发表于 2021-9-29 21:35:09
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之九:动态四组数据,xyz+方位角a
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之九:动态四组数据,xyz+方位角
实验接线:
5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#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 azimuth;
//float azimuth; //is supporting float too
qmc.read(&x, &y, &z, &azimuth);
//azimuth = qmc.azimuth(&y,&x);//you can get custom azimuth
Serial.print("x: ");
Serial.print(x);
Serial.print(" y: ");
Serial.print(y);
Serial.print(" z: ");
Serial.print(z);
Serial.print(" a: ");
Serial.print(azimuth);
Serial.println();
delay(800);
}
驴友花雕
发表于 2021-9-29 21:47:19
实验串口返回情况
驴友花雕
发表于 2021-9-29 21:49:25
实验串口绘图器返回情况
驴友花雕
发表于 2021-9-30 04:58:09
模块实验接线示意图
驴友花雕
发表于 2021-9-30 05:16:39
磁偏角(magnetic declination)
地磁偏角是指地球上任一处的磁北方向和正北方向之间的夹角。当地磁北向实际偏东时,地磁偏角为正,反之为负。地磁偏角在历史上最早由中国北宋科学家沈括记录在著作《梦溪笔谈》中:“方家以磁石磨针锋,则能指南,然常微偏东,不全南也。”而西方最早的记录则在此之后约400年。在地球上不同的地方,地磁偏角一般也不相同。在同一个地方,地磁偏角随着时间的推移也在不断变化。发生磁暴时和在磁力异常地区,如磁铁矿和高压线附近,地磁偏角将会产生急剧变化。在中国大陆的大部分地区,地磁偏角在-10°~+2°之间。在台湾则是-4°~-3°左右。正北(真北TN):
驴友花雕
发表于 2021-9-30 05:18:55
方格北(图北GN):以方格北线所测之方位角为方格方位角。
磁北(MN):地磁偏角:当地磁北向实际偏东时,地磁偏角为正,反之为负。
驴友花雕
发表于 2021-9-30 05:20:55
指北针指的方向是磁北(MN),假定磁偏角为偏东14度(14°E) ,正北(TN)会在磁北(MN)右边14度。xx°E:正北在磁北右边xx度。xx°W:正北在磁北左边xx度。
驴友花雕
发表于 2021-9-30 05:36:51
磁偏角是根据您当前位置应用的校正
为了从磁北得到真北,它因地而异。
例子:
基督城,东经 23° 35'
惠灵顿 , 22° 14' EAST
但尼丁,东经 25° 8'
奥克兰,东经 19° 30'
您所在地区的磁偏角可以从 http://www.magnetic-declination.com/ 获得
驴友花雕
发表于 2021-9-30 05:52:10
实际测量几个地方的磁偏角
驴友花雕
发表于 2021-9-30 05:54:02
驴友花雕
发表于 2021-9-30 06:39:35
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之十:根据当前位置来校正磁偏角
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目之十:根据当前位置来校正磁偏角
实验接线:
5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>
// Create a compass
HMC5883L_Simple Compass;
void setup() {
Serial.begin(9600);
Wire.begin();
// Magnetic Declination is the correction applied according to your present location
// in order to get True North from Magnetic North, it varies from place to place.
//
// The declination for your area can be obtained from http://www.magnetic-declination.com/
// Take the "Magnetic Declination" line that it gives you in the information,
//
// Examples:
// Christchurch, 23° 35' EAST
// Wellington, 22° 14' EAST
// Dunedin , 25° 8'EAST
// Auckland , 19° 30' EAST
//
Compass.SetDeclination(-0, 23, 'W');
// The device can operate in SINGLE (default) or CONTINUOUS mode
// SINGLE simply means that it takes a reading when you request one
// CONTINUOUS means that it is always taking readings
// for most purposes, SINGLE is what you want.
Compass.SetSamplingMode(COMPASS_CONTINUOUS);
// The scale can be adjusted to one of several levels, you can probably leave it at the default.
// Essentially this controls how sensitive the device is.
// Options are 088, 130 (default), 190, 250, 400, 470, 560, 810
// Specify the option as COMPASS_SCALE_xxx
// Lower values are more sensitive, higher values are less sensitive.
// The default is probably just fine, it works for me.If it seems very noisy
//(jumping around), incrase the scale to a higher one.
Compass.SetScale(COMPASS_SCALE_250);
// The compass has 3 axes, but two of them must be close to parallel to the earth's surface to read it,
// (we do not compensate for tilt, that's a complicated thing) - just like a real compass has a floating
// needle you can imagine the digital compass does too.
//
// To allow you to mount the compass in different ways you can specify the orientation:
// COMPASS_HORIZONTAL_X_NORTH (default), the compass is oriented horizontally, top - side up. when pointing North the X silkscreen arrow will point North
// COMPASS_HORIZONTAL_Y_NORTH, top-side up, Y is the needle,when pointing North the Y silkscreen arrow will point North
// COMPASS_VERTICAL_X_EAST, vertically mounted (tall) looking at the top side, when facing North the X silkscreen arrow will point East
// COMPASS_VERTICAL_Y_WEST, vertically mounted (wide) looking at the top side, when facing North the Y silkscreen arrow will point West
Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
}
// Our main program loop.
void loop() {
float heading = Compass.GetHeadingDegrees();
Serial.print("Heading: \t");
Serial.println( heading );
delay(500);
}
驴友花雕
发表于 2021-9-30 06:50:14
实验串口返回情况
项目之十实验说明:这是第二次无法完成磁偏角的校准
第一次是使用QMC5883L的库,这次是使用HMC5883L的库,而实验采用模块的芯片是国产的DB5883L
这是三种有一定差异的不同产家的芯片,估计是在校准程序上有些不能兼容,其他功能还没有发现不兼容的情况。
驴友花雕
发表于 2021-9-30 07:10:03
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目十一:使用Adafruit库的HMC5883 磁力计测试
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
实验一百五十八:QMC5883L电子指南针罗盘模块 三轴磁场传感器GY-271
项目十一:使用Adafruit库的HMC5883 磁力计测试
实验接线:
5883L-------------- UNO
VCC------------------- 5V
GND------------------- GND
SCL ------------------- A5
SDA------------------- A4
DRDY------------------ N/C
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(666666);
void displaySensorDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print("Sensor: "); Serial.println(sensor.name);
Serial.print("Driver Ver: "); Serial.println(sensor.version);
Serial.print("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void setup(void)
{
Serial.begin(9600);
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
/* Initialise the sensor */
if (!mag.begin())
{
/* There was a problem detecting the HMC5883 ... check your connections */
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while (1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
}
void loop(void)
{
/* Get a new sensor event */
sensors_event_t event;
mag.getEvent(&event);
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(""); Serial.println("uT");
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
// Calculate heading when the magnetometer is level, then correct for signs of axis.
float heading = atan2(event.magnetic.y, event.magnetic.x);
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
float declinationAngle = 0.1;
heading += declinationAngle;
// Correct for when signs are reversed.
if (heading < 0)
heading += 2 * PI;
// Check for wrap due to addition of declination.
if (heading > 2 * PI)
heading -= 2 * PI;
// Convert radians to degrees for readability.
float headingDegrees = heading * 180 / M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
delay(500);
}
驴友花雕
发表于 2021-9-30 07:12:33
实验串口返回情况