“指南针”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(Created page with "{| style="width: 800px;" |- | ==目的== 本教程将教大家如何用Microduino-10DOF模块测到的磁场强度数据在Processing中显示一个指南针。 ==设备== *...")
 
调试
 
(未显示2个用户的12个中间版本)
第1行: 第1行:
 +
{{Language | Compass}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-
第7行: 第8行:
  
 
==设备==
 
==设备==
*'''[[Microduino-Core]]'''
+
*'''[[Microduino-Core/zh]]'''
*'''[[Microduino-FT232R]]'''
+
*'''[[Microduino-USBTTL/zh]]'''
'''[[Microduino-10DOF]'''
+
*'''[[Microduino-10DOF/zh]]'''
  
 
*其他硬件设备
 
*其他硬件设备
**USB数据连接线  一根  
+
**USB数据连接线  一根
 
 
  
 
==原理图==
 
==原理图==
第21行: 第21行:
 
==程序==
 
==程序==
  
compassMicroduino
+
[https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/compassMicroduino compassMicroduino]
  
compass_simulator
+
[https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/compass_simulator compass_simulator]
  
 
==调试==
 
==调试==
第29行: 第29行:
 
步骤一:按着原理图搭建硬件环境,像这样:
 
步骤一:按着原理图搭建硬件环境,像这样:
 
[[File:compass_simulatorConnectionDiagram.jpg|600px|center|thumb]]
 
[[File:compass_simulatorConnectionDiagram.jpg|600px|center|thumb]]
 
+
下载这个HMC5883L的库函数:[https://github.com/manifestinteractive/arduino/tree/master/Libraries/HMC5883L HMC5883L]
  
 
步骤二:解释一下代码:
 
步骤二:解释一下代码:
第37行: 第37行:
 
Microduino:
 
Microduino:
  
//得到磁场强度数据后输出到串口以便Processing显示
+
在setup()中主要是初始化HMC5883L
 +
 
 +
在loop()中计算出周围磁感应的角度
  
   void loop()
+
//Output()用来输出计算出的角度到串口
 +
  // Output the data down the serial port.
 +
   void Output(int RoundDegreeInt)
 
   {
 
   {
     mag.getHeading(&mx, &my, &mz);
+
     //Serial.println();
    Serial.print(mx);
+
     Serial.println(RoundDegreeInt);
     Serial.print(",");
+
     delay(150);
     Serial.println(my);  
 
 
   }
 
   }
  
 
Processing:
 
Processing:
  
//得到第一个串口的数据,并定义如果有换行就缓存
+
//得到第一个串口的数据.
  // is always my  Arduino, so I open Serial.list()[0].
 
  // Open whatever port is the one you're using.
 
 
   myPort = new Serial(this, Serial.list()[0], 9600);
 
   myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line
 
  
//把从Microduino接收到的数据取出来后以不同颜色的曲线图的形式显示在屏幕上,并标上标尺
+
//在draw()函数中,第一步得到串口传来的数据
 +
  while (myPort.available() > 0) {
 +
    myString = myPort.readStringUntil(lf);
 +
    if (myString != null) {
 +
  //print(myString);  // Prints String
 +
    angle=float(myString);  // Converts and prints float
 +
    println(angle);
 +
    }
 +
  }
 +
  translate(160, 50);
 +
 
 +
//绘制指南针的背景
 +
  // draw the compass background
 +
  ellipseMode(CENTER);
 +
  fill(50);
 +
  stroke(10);
 +
  strokeWeight(2);
 +
  ellipse(150,150,300,300);
 +
 
 +
 
 +
//绘制线和点
 +
  // draw the lines and dots
 +
  translate(150,150);  // translate the lines and dots to the middle of the compass
 +
  float CompassX = -angle;
 +
  rotate(radians(CompassX));
 +
  noStroke();
 +
  fill(51, 255, 51);
 +
  int radius = 120;
 +
  for( int degC = 5; degC < 360; degC += 10) //Compass dots
 +
  {
 +
    float angleC = radians(degC);
 +
    float xC = 0 + (cos(angleC)* radius);
 +
    float yC = 0 + (sin(angleC)* radius);
 +
    ellipse(xC,yC, 3, 3);
 +
  }
 +
  for( int degL = 10; degL < 370; degL += 10) //Compass lines
 +
  {
 +
    float angleL = radians(degL);
 +
    float x = 0 + (cos(angleL)* 145);
 +
    float y = 0 + (sin(angleL)* 145);
 +
    if( degL==90 || degL==180 || degL==270 || degL==360) {
 +
    stroke(51, 255, 51);
 +
    strokeWeight(4);
 +
    }
 +
    else {
 +
      stroke(234,144,7);
 +
      strokeWeight(2);
 +
    }
 +
    line(0,0, x,y);
 +
  }
 +
  fill(102, 102, 102);
 +
  noStroke();
 +
  ellipseMode(CENTER);
 +
  ellipse(0,0, 228,228); //draw a filled circle to hide the lines in the middle
  
函数说明:
 
  
//绘制磁场指针
 
arrow(int x1, int y1, int x2, int y2, color c)
 
  
//判断是否可转换为数字
+
//绘制指南针的方向表示,东西南北
isNumeric(String str)
+
  b = loadFont("Arial-BoldMT-48.vlw");
 +
  textAlign(CENTER);
 +
  // Draw the letters
 +
  fill(250);
 +
  textFont(b, 32);
 +
  text("N", 1, -90);
 +
  rotate(radians(90));
 +
  text("E", 0, -90);
 +
  rotate(radians(90));
 +
  text("S", 0, -90);
 +
  rotate(radians(90));
 +
  text("W", 0, -90);
 +
  rotate(radians(90));
 +
  textFont(b,40);
 +
  textAlign(CENTER);
 +
  //text((angle), 20, 20);
 +
  println(angle);
 +
 
 +
 
 +
//绘制指南针
 +
  //draw the needle
 +
  rotate(radians(-CompassX)); //make it stationary
 +
  stroke(234,144,7);
 +
  strokeWeight(3);
 +
  triangle(-10, 0, 10, 0, 0, -85);
 +
  fill(234,144,7);
 +
  triangle(-10, 0, 10, 0, 0, 60);
  
 
步骤三:下载代码并编译通过。
 
步骤三:下载代码并编译通过。
第72行: 第148行:
  
 
屏幕上会显示一个简单的指南针,指针会随着磁场走,像这样:
 
屏幕上会显示一个简单的指南针,指针会随着磁场走,像这样:
[[File:compass_simulatorResult.jpg|600px|center|thumb]]
+
[[File:compass_simulatorResult1.jpg|600px|center|thumb]]
 
 
  
 
==视频==
 
==视频==

2014年10月29日 (三) 07:20的最新版本

Language English

目的

本教程将教大家如何用Microduino-10DOF模块测到的磁场强度数据在Processing中显示一个指南针。

设备

  • 其他硬件设备
    • USB数据连接线 一根

原理图

直接使用Microduino-10DOF上的HMC5883L磁场强度传感器

程序

compassMicroduino

compass_simulator

调试

步骤一:按着原理图搭建硬件环境,像这样:

Compass simulatorConnectionDiagram.jpg

下载这个HMC5883L的库函数:HMC5883L

步骤二:解释一下代码:

本例需要两端的代码,Processing端和Microduino端

Microduino:

在setup()中主要是初始化HMC5883L

在loop()中计算出周围磁感应的角度

//Output()用来输出计算出的角度到串口

 // Output the data down the serial port.
 void Output(int RoundDegreeInt)
 {
   //Serial.println();
   Serial.println(RoundDegreeInt);
   delay(150);
 }

Processing:

//得到第一个串口的数据.

 myPort = new Serial(this, Serial.list()[0], 9600);

//在draw()函数中,第一步得到串口传来的数据

 while (myPort.available() > 0) {
   myString = myPort.readStringUntil(lf);
   if (myString != null) {
 //print(myString);  // Prints String
   angle=float(myString);  // Converts and prints float
   println(angle);
   }
 }
 translate(160, 50);

//绘制指南针的背景

 // draw the compass background
 ellipseMode(CENTER);
 fill(50);
 stroke(10);
 strokeWeight(2);
 ellipse(150,150,300,300);


//绘制线和点

 // draw the lines and dots
 translate(150,150);  // translate the lines and dots to the middle of the compass
 float CompassX = -angle;
 rotate(radians(CompassX));
 noStroke();
 fill(51, 255, 51);
 int radius = 120;
 for( int degC = 5; degC < 360; degC += 10) //Compass dots
 {
   float angleC = radians(degC);
   float xC = 0 + (cos(angleC)* radius);
   float yC = 0 + (sin(angleC)* radius);
   ellipse(xC,yC, 3, 3);
 }
 for( int degL = 10; degL < 370; degL += 10) //Compass lines
 {
   float angleL = radians(degL);
   float x = 0 + (cos(angleL)* 145);
   float y = 0 + (sin(angleL)* 145);
   if( degL==90 || degL==180 || degL==270 || degL==360) {
    stroke(51, 255, 51);
    strokeWeight(4);
   }
   else {
     stroke(234,144,7);
     strokeWeight(2);
   }
   line(0,0, x,y);
 }
 fill(102, 102, 102);
 noStroke();
 ellipseMode(CENTER);
 ellipse(0,0, 228,228); //draw a filled circle to hide the lines in the middle


//绘制指南针的方向表示,东西南北

 b = loadFont("Arial-BoldMT-48.vlw");
 textAlign(CENTER);
 // Draw the letters
 fill(250);
 textFont(b, 32);
 text("N", 1, -90);
 rotate(radians(90));
 text("E", 0, -90);
 rotate(radians(90));
 text("S", 0, -90);
 rotate(radians(90));
 text("W", 0, -90);
 rotate(radians(90));
 textFont(b,40);
 textAlign(CENTER);
 //text((angle), 20, 20);
 println(angle);


//绘制指南针

 //draw the needle
 rotate(radians(-CompassX)); //make it stationary
 stroke(234,144,7);
 strokeWeight(3);
 triangle(-10, 0, 10, 0, 0, -85);
 fill(234,144,7);
 triangle(-10, 0, 10, 0, 0, 60);

步骤三:下载代码并编译通过。

步骤四:运行后,用一块磁铁改变一下磁场,看看指针是否变化。

结果

屏幕上会显示一个简单的指南针,指针会随着磁场走,像这样:

Compass simulatorResult1.jpg

视频