0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

如何制作数字指南针

454398 来源:wv 2019-10-12 14:19 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

第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

    浏览量

    11119
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    ‌STMicroelectronics LPS28DFW 数字气压计技术深度解析与应用指南

    STMicroelectronics LPS28DFW绝对数字输出压力计是一款超紧凑型压阻式绝对压力传感器,可用作数字输出气压计。LPS28DFW将传感元件与通过I^2^C或MIPI I3CSM接口(从传感元件到应用程序)进行通信的IC接口结合在一起。
    的头像 发表于 10-31 11:24 277次阅读
    ‌STMicroelectronics LPS28DFW <b class='flag-5'>数字</b>气压计技术深度解析与应用<b class='flag-5'>指南</b>

    ‌ILPS22QS数字气压计技术深度解析与应用指南

    STMicroelectronics ILPS22QS绝对数字输出压力计是一款超紧凑型压阻式绝对压力传感器,可用作数字输出气压计。ILPS22QS支持高达4060hPa用户可选双满量程。ILPS22QS具有超低压力噪声和极低功耗。
    的头像 发表于 10-31 11:17 238次阅读
    ‌ILPS22QS<b class='flag-5'>数字</b>气压计技术深度解析与应用<b class='flag-5'>指南</b>

    如何制作字母数字键盘?

    制作字母数字键盘
    发表于 09-05 07:24

    《工业企业和园区数字化能碳管理中心建设指南》发布 安科瑞能碳管理系统助力绿色智造新时代 ——政策落

    一、政策引领:数字化能碳管理成工业转型核心任务 近日,工信部正式印发《工业企业和园区数字化能碳管理中心建设指南》(下称《指南》),明确提出构建“数据驱动、智能管控、全景可视”的能碳管理
    的头像 发表于 07-18 15:25 424次阅读
    《工业企业和园区<b class='flag-5'>数字</b>化能碳管理中心建设<b class='flag-5'>指南</b>》发布 安科瑞能碳管理系统助力绿色智造新时代 ——政策落

    东映携手奥拓刷新日本影视制作数字化标杆

    近日,由奥拓电子全程深度参与打造的“东映虚拟影棚”已正式发布启用。这座凝聚前沿科技的虚拟影棚,不仅以640㎡的规模成为日本之最,更凭借顶尖技术配置,刷新了日本影视制作数字化标杆。作为日本首个由电影
    的头像 发表于 06-04 15:21 871次阅读

    01V96V2数字调音台中文快速指南

    电子发烧友网站提供《01V96V2数字调音台中文快速指南.pdf》资料免费下载
    发表于 03-26 14:19 0次下载

    不到千元轻松入手!华为云 Flexus 数字制作简单、效果极佳

    数字化浪潮的席卷下,越来越多的数字人如雨后春笋般出现在大众视野中,数字人热度持续提升。然而,在这炙手可热的背后,是数字人便捷的制作流程和逼
    的头像 发表于 03-10 11:05 1051次阅读
    不到千元轻松入手!华为云 Flexus <b class='flag-5'>数字</b>人<b class='flag-5'>制作</b>简单、效果极佳

    不到千元体验最新数字人技术!华为云 Flexus 数字人效果领先更超值

    当下,数字人的应用已经非常广泛,彻底走进了我们的日常生活。在教育领域,可以看到数字人被用于制作教学视频,通过模拟真实的教师讲解,为学生提供生动、直观的学习材料;在政府服务大厅,有数字
    的头像 发表于 03-10 11:04 726次阅读
    不到千元体验最新<b class='flag-5'>数字</b>人技术!华为云 Flexus <b class='flag-5'>数字</b>人效果领先更超值

    三部门关于印发《制造业企业数字化转型实施指南》的通知

        制造业企业数字化转型实施指南 制造业数字化转型是运用数字技术对制造业研发生产 全流程和产业链供应链各环节进行改造升级和价值重塑的 过程,是制造业高质量发展的关键路径。 制造业企
    的头像 发表于 01-23 09:37 632次阅读
    三部门关于印发《制造业企业<b class='flag-5'>数字</b>化转型实施<b class='flag-5'>指南</b>》的通知

    dsPIC33CK512MP606数字电源接插模块(PIM)用户指南

    电子发烧友网站提供《dsPIC33CK512MP606数字电源接插模块(PIM)用户指南.pdf》资料免费下载
    发表于 01-21 14:51 1次下载
    dsPIC33CK512MP606<b class='flag-5'>数字</b>电源接插模块(PIM)用户<b class='flag-5'>指南</b>

    DAC8760的数字地和模拟地能否不连呢?

    of the device)。 虽然名称都是GND,但有数字地和模拟地之分。产品手册上的所有电路范例都是数字地和模拟地相连的。是否可以用两套互相隔离的电源,分别作数字电源和模拟电源(数字
    发表于 01-14 06:24

    GY/T 318-2018 地面数字电视广播单频网系统实施指南

    电子发烧友网站提供《GY/T 318-2018 地面数字电视广播单频网系统实施指南.pdf》资料免费下载
    发表于 01-13 13:57 0次下载

    带EQ和2频段DRC的25W数字输入放大器用户指南

    电子发烧友网站提供《带EQ和2频段DRC的25W数字输入放大器用户指南.pdf》资料免费下载
    发表于 12-23 14:30 1次下载
    带EQ和2频段DRC的25W<b class='flag-5'>数字</b>输入放大器用户<b class='flag-5'>指南</b>

    多种传感器集成,IMU助力无人机稳定飞行

    常见的传感器包括陀螺仪、加速度计、磁力计(指南针)、气压计(高度计)和GPS模块,大多数IMU只集成陀螺仪和加速度计。
    的头像 发表于 12-18 14:34 1736次阅读
    多种传感器集成,IMU助力无人机稳定飞行

    ISO784xx四通道数字隔离器EVM用户指南

    电子发烧友网站提供《ISO784xx四通道数字隔离器EVM用户指南.pdf》资料免费下载
    发表于 12-09 15:07 0次下载
    ISO784xx四通道<b class='flag-5'>数字</b>隔离器EVM用户<b class='flag-5'>指南</b>