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

来自Microduino Wikipedia
跳转至: 导航搜索
Objective
调试
 
(未显示2个用户的8个中间版本)
第1行: 第1行:
 +
{{Language | Compass}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-
 
|
 
|
==Objective==
+
==目的==
  
The course will show you how to simulate a compass on Processing by presenting the data of magnetic field strength detected by Microduino-10DOF module.
+
本教程将教大家如何用Microduino-10DOF模块测到的磁场强度数据在Processing中显示一个指南针。
  
==Equipments==
+
==设备==
*'''[[Microduino-Core]]'''
+
*'''[[Microduino-Core/zh]]'''
*'''[[Microduino-FT232R]]'''
+
*'''[[Microduino-USBTTL/zh]]'''
*'''[[Microduino-10DOF]]'''
+
*'''[[Microduino-10DOF/zh]]'''
  
*Other Hardware Equipments
+
*其他硬件设备
**A USB cable
+
**USB数据连接线  一根
  
==Schematic Diagram==
+
==原理图==
  
The HMC5883L magnetic field strength sensor of Microduino-10DOF will be available.
+
直接使用Microduino-10DOF上的HMC5883L磁场强度传感器
  
==Program==
+
==程序==
  
Referring to  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]
  
==Debugging==
+
==调试==
  
Step 1:Building the hardware environment according to the schematic diagram, just like this:
+
步骤一:按着原理图搭建硬件环境,像这样:
 
[[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]
  
 +
步骤二:解释一下代码:
  
Step 2:Here is the code we need:
+
本例需要两端的代码,Processing端和Microduino端
  
The code of the two ends (Processing and Microduino
+
Microduino:
  
Microduino:
+
在setup()中主要是初始化HMC5883L
  
//The data of the magnetic field strength will be exported into the serial port to be displayed on Processing 
+
在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:
  
//After getting the data of the first serial port, defining them or caching them if there is a new line  // 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
 
  
//Presenting the data obtained from Microduino on the screen in the form of curves with different color and then marking them
+
//在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
 +
 
  
Function Description:
 
  
//Drawing a magnetic field pointer
+
//绘制指南针的方向表示,东西南北
arrow(int x1, int y1, int x2, int y2, color c)
+
  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);
  
//Judging whether can convert them into numbers
 
isNumeric(String str)
 
  
Step 3:Uploading the code and compiling them successfully.
+
//绘制指南针
 +
  //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);
  
Step 4:After the system goes smoothly, you can use a magnet to change the magnetic field and see if there is any change.
+
步骤三:下载代码并编译通过。
  
==Result==
+
步骤四:运行后,用一块磁铁改变一下磁场,看看指针是否变化。
  
There will be a simple compass displayed on the screen and the pointer will change along with the magnetic field. As follows:
+
==结果==
[[File:compass_simulatorResult.jpg|600px|center|thumb]]
 
  
 +
屏幕上会显示一个简单的指南针,指针会随着磁场走,像这样:
 +
[[File:compass_simulatorResult1.jpg|600px|center|thumb]]
  
==Video==
+
==视频==
  
  
 
|}
 
|}

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

视频