第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
}
-
指南針
+關注
關注
2文章
17瀏覽量
11022
發布評論請先 登錄
東映攜手奧拓刷新日本影視制作數字化標桿
不到千元體驗最新數字人技術!華為云 Flexus 數字人效果領先更超值

3D掃描與數字拓片:打造文化遺產的數字復本

用AIC3254來作數字拾音器,遇到的幾個疑問求解
電流傳感器的主要參數與選型
使用PCM5242連接到TMS320C5517上作數字量與模擬量的轉換,如何配置寄存器參數?
現代海上的電子指南針——艦艇慣導系統
醫療機器人的“指南針”:MT6701磁編碼IC實現精確導航
e2studio開發磁力計LIS2MDL(1)----輪詢獲取磁力計數據

關鍵指南針-NXP USB CDC_VCOM虛擬串口例程

評論