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

    浏览量

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

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    芯科科技低功耗Wi-Fi开发指南

    Silicon Labs(芯科科技)整理并制作了低功耗Wi-Fi开发指南的网站资源,以帮助开发人员使用低功耗 Wi-Fi 6协议进行下一代物联网产品开发。
    的头像 发表于 04-21 10:09 289次阅读

    AD5232数字电位器:特性、应用与编程指南

    AD5232数字电位器:特性、应用与编程指南 在电子设计领域,数字电位器作为一种重要的电子元件,为工程师们提供了灵活、精确的电阻调节解决方案。今天,我们将深入探讨Analog Devices公司
    的头像 发表于 04-17 09:20 319次阅读

    技术实战:智慧医院院内导航系统的设计与落地,破解用户寻路难题

    外地图无缝贴合,实现 "医院门口→科室→检查室→药房" 全路径无断点导航 动态纠偏:内置指南针纠偏算法 + 惯性传感器
    的头像 发表于 04-08 17:37 1225次阅读
    技术实战:智慧医院院内导航系统的设计与落地,破解用户寻路难题

    探索Microchip数字电源启动套件:功能、特性与应用指南

    探索Microchip数字电源启动套件:功能、特性与应用指南 在电子工程领域,数字电源技术正变得越来越重要。Microchip的数字电源启动套件为工程师们提供了一个理想的平台,用于探索
    的头像 发表于 04-06 16:45 1089次阅读

    AGV搬运机器人怎么找寻正确的库位?

    在动辄数万平的仓库里,AGV如何毫米级精准寻位?激光导航构建高精地图,视觉识别二维码地标,多传感器协同判断库位状态,智能系统实时调度路径——揭秘无人仓储背后的科技"指南针"。
    的头像 发表于 04-03 09:55 356次阅读
    AGV搬运机器人怎么找寻正确的库位?

    LDC1001电感数字转换器:特性、应用与设计指南

    LDC1001电感数字转换器:特性、应用与设计指南 引言 在电子设计领域,对于金属目标的线性或角位置进行精确测量是许多应用中的关键需求。LDC1001电感数字转换器凭借其独特的特性和广泛的应用场
    的头像 发表于 02-11 17:00 743次阅读

    电容隔离技术优势解析与乾鸿微全系列数字隔离器产品指南

    电容隔离技术优势解析与乾鸿微全系列数字隔离器产品指南
    的头像 发表于 01-23 16:43 342次阅读
    电容隔离技术优势解析与乾鸿微全系列<b class='flag-5'>数字</b>隔离器产品<b class='flag-5'>指南</b>

    井下定向不再难!小型化快速寻北系统,赋能智慧矿山精准掘进

    、精准、可靠的自主定向,成为矿山智能化升级中一道亟待攻克的技术难关。 二、颠覆性突破:全固态MEMS寻北,无需外界信号的“地下指南针” ER-MNS-04A/B 快速对准寻北系统应运而生,凭借自主寻北、全固态抗振、小巧易集成三大特性
    的头像 发表于 01-15 16:05 281次阅读

    手持智能扫码器选型指南:功能参数 + 行业应用

    门店、制造车间等 8 大应用场景,附选型指南与实用案例,帮企业快速选型,解决盘点慢、数据错、效率低等问题,实现线下动作数字化追踪。
    的头像 发表于 01-09 15:50 876次阅读
    手持智能扫码器选型<b class='flag-5'>指南</b>:功能参数 + 行业应用

    GNSS 卫星导航信号模拟器:无人机精准飞行的 “隐形考官”​

    在无人机技术飞速发展的今天,从农业植保、电力巡检到物流配送,无人机的应用场景日益广泛。而精准的定位与导航,是无人机完成各项任务的核心保障。GNSS(全球导航卫星系统)作为无人机定位的 “指南针”,其
    的头像 发表于 01-07 16:32 410次阅读

    Amphenol数字红外探测器评估套件使用指南

    Amphenol数字红外探测器评估套件使用指南 在电子设计领域,红外探测器的应用越来越广泛。Amphenol的数字红外探测器评估套件(Digital IR EVM KIT,Part No.
    的头像 发表于 12-11 09:20 706次阅读

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

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

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

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

    如何制作字母数字键盘?

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

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

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