第1步:所需零件
对于此项目,您将只需要一个arduino开发板和一个mems磁力计即可测量地磁场。我将使用包含mc5883l 3轴磁力计的gy – 80分支板。
在继续执行该项目的源代码之前,如果您需要更多详细信息,请参见mems磁力计如何工作以及如何通过i2c通信连接和使用gy-80接线板,
第2步:arduino源代码
我们首先需要做的是将草图上传到arduino板,该板将读取来自磁力计的数据,并将其发送到processing ide。这是arduino源代码:
/* arduino compass
*
* by dejan nedelkovski,
* www.howtomechatronics.com
*
*/
#include //i2c arduino library
#define magnetometer_mx0 0x03
#define magnetometer_mx1 0x04
#define magnetometer_mz0 0x05
#define magnetometer_mz1 0x06
#define magnetometer_my0 0x07
#define magnetometer_my1 0x08
int mx0, mx1, mx_out;
int my0, my1, my_out;
int mz0, mz1, mz_out;
float heading, headingdegrees, headingfiltered, declination;
float xm,ym,zm;
#define magnetometer 0x1e //i2c 7bit address of hmc5883
void setup(){
//initialize serial and i2c communications
serial.begin(115200);
wire.begin();
delay(100);
wire.begintransmission(magnetometer);
wire.write(0x02); // select mode register
wire.write(0x00); // continuous measurement mode
wire.endtransmission();
}
void loop(){
//---- x-axis
wire.begintransmission(magnetometer); // transmit to device
wire.write(magnetometer_mx1);
wire.endtransmission();
wire.requestfrom(magnetometer,1);
if(wire.available()《=1)
{
mx0 = wire.read();
}
wire.begintransmission(magnetometer); // transmit to device
wire.write(magnetometer_mx0);
wire.endtransmission();
wire.requestfrom(magnetometer,1);
if(wire.available()《=1)
{
mx1 = wire.read();
}
//---- y-axis
wire.begintransmission(magnetometer); // transmit to device
wire.write(magnetometer_my1);
wire.endtransmission();
wire.requestfrom(magnetometer,1);
if(wire.available()《=1)
{
my0 = wire.read();
}
wire.begintransmission(magnetometer); // transmit to device
wire.write(magnetometer_my0);
wire.endtransmission();
wire.requestfrom(magnetometer,1);
if(wire.available()《=1)
{
my1 = wire.read();
}
//---- z-axis
wire.begintransmission(magnetometer); // transmit to device
wire.write(magnetometer_mz1);
wire.endtransmission();
wire.requestfrom(magnetometer,1);
if(wire.available()《=1)
{
mz0 = wire.read();
}
wire.begintransmission(magnetometer); // transmit to device
wire.write(magnetometer_mz0);
wire.endtransmission();
wire.requestfrom(magnetometer,1);
if(wire.available()《=1)
{
mz1 = wire.read();
}
//---- x-axis
mx1=mx1《《8;
mx_out =mx0+mx1; // raw data
// from the datasheet: 0.92 mg/digit
xm = mx_out*0.00092; // gauss unit
//* earth magnetic field ranges from 0.25 to 0.65 gauss, so these are the values that we need to get approximately.
//---- y-axis
my1=my1《《8;
my_out =my0+my1;
ym = my_out*0.00092;
//---- z-axis
mz1=mz1《《8;
mz_out =mz0+mz1;
zm = mz_out*0.00092;
// ==============================
//calculating heading
heading = atan2(ym, xm);
// correcting the heading with the declination angle depending on your location
// you can find your declination angle at: http://www.ngdc.noaa.gov/geomag-web/
// at my location it‘s 4.2 degrees =》 0.073 rad
declination = 0.073;
heading += declination;
// correcting when signs are reveresed
if(heading 《0) heading += 2*pi;
// correcting due to the addition of the declination angle
if(heading 》 2*pi)heading -= 2*pi;
headingdegrees = heading * 180/pi; // the heading in degrees unit
// smoothing the output angle / low pass filter
headingfiltered = headingfiltered*0.85 + headingdegrees*0.15;
//sending the heading value through the serial port to processing ide
serial.println(headingfiltered);
delay(50);
}
步骤3:处理ide源代码
在我们上传了之前的arduino草图之后,我们需要将数据接收到processing ide中并绘制digital compass。指南针由背景图像,箭头的固定图像和指南针主体的旋转图像组成。因此,使用arduino计算出的耳磁场的值将用来旋转罗盘。
以下是processing ide的源代码:
/* arduino compass
*
* by dejan nedelkovski,
* www.howtomechatronics.com
*
*/
import processing.serial.*;
import java.awt.event.keyevent;
import java.io.ioexception;
serial myport;
pimage imgcompass;
pimage imgcompassarrow;
pimage background;
string data=“”;
float heading;
void setup() {
size (1920, 1080, p3d);
smooth();
imgcompass = loadimage(“compass.png”);
imgcompassarrow = loadimage(“compassarrow.png”);
background = loadimage(“background.png”);
myport = new serial(this, “com4”, 115200); // starts the serial communication
myport.bufferuntil(’ ‘);
}
void draw() {
image(background,0, 0); // loads the background image
pushmatrix();
translate(width/2, height/2, 0); // translates the coordinate system into the center of the screen, so that the rotation happen right in the center
rotatez(radians(-heading)); // rotates the compass around z - axis
image(imgcompass, -960, -540); // loads the compass image and as the coordinate system is relocated we need need to set the image at -960x, -540y (half the screen size)
popmatrix(); // brings coordinate system is back to the original position 0,0,0
image(imgcompassarrow,0, 0); // loads the compassarrow image which is not affected by the rotatez() function because of the popmatrix() function
textsize(30);
text(“heading: ” + heading,40,40); // prints the value of the heading on the screen
delay(40);
}
// starts reading data from the serial port
void serialevent (serial myport) {
data = myport.readstringuntil(’ ‘);// reads the data from the serial port and puts it into the string variable “data”。
heading = float(data); // convering the the string value into float value
}
IT资产跟踪系统是什么,它的特点是什么
芯启源科技®射频同轴连接器
在未来,自动化检测设备替代人工品检是必然的
不同角度探讨汽车产业面临的新变局与新挑战
上方电子成功研制的首台8.6代新型显示PVD装备,顺利完成发货
如何制作数字指南针
采用LED动态显示屏的计时器电路
十月,特斯拉国产版Model 3的产量实现翻倍
固特异智能无线充气泵内部拆解
一文了解CPU高速缓存
航顺芯片高性能HK32C030系列MCU简述
单片机对两位共阳数码管的驱动设计
PLC远程监控与数据采集方案
4G路由器无法上网时!该如何解决
开关电容高速比较器电路分析
三星的智能手机产量这个月削减了一半
RS485为什么用隔离
Molex与Infor合作:在复杂的航运中如何提高客户能见度和准时交货率
超声波脉冲开关电源发生器设计
PLC与Excel的DDE在测控系统中的应用