“指南针”的版本间的差异
(→调试) |
|||
(未显示2个用户的9个中间版本) | |||
第1行: | 第1行: | ||
+ | {{Language | Compass}} | ||
{| style="width: 800px;" | {| style="width: 800px;" | ||
|- | |- | ||
| | | | ||
− | == | + | ==目的== |
− | + | 本教程将教大家如何用Microduino-10DOF模块测到的磁场强度数据在Processing中显示一个指南针。 | |
− | == | + | ==设备== |
− | *'''[[Microduino-Core]]''' | + | *'''[[Microduino-Core/zh]]''' |
− | *'''[[Microduino- | + | *'''[[Microduino-USBTTL/zh]]''' |
− | *'''[[Microduino-10DOF]]''' | + | *'''[[Microduino-10DOF/zh]]''' |
− | * | + | *其他硬件设备 |
− | ** | + | **USB数据连接线 一根 |
− | == | + | ==原理图== |
− | + | 直接使用Microduino-10DOF上的HMC5883L磁场强度传感器 | |
− | == | + | ==程序== |
− | + | [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] |
− | == | + | ==调试== |
− | + | 步骤一:按着原理图搭建硬件环境,像这样: | |
[[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] | ||
+ | 步骤二:解释一下代码: | ||
− | + | 本例需要两端的代码,Processing端和Microduino端 | |
− | + | Microduino: | |
− | + | 在setup()中主要是初始化HMC5883L | |
− | + | 在loop()中计算出周围磁感应的角度 | |
− | void | + | //Output()用来输出计算出的角度到串口 |
+ | // Output the data down the serial port. | ||
+ | void Output(int RoundDegreeInt) | ||
{ | { | ||
− | + | //Serial.println(); | |
− | + | Serial.println(RoundDegreeInt); | |
− | Serial. | + | delay(150); |
− | |||
} | } | ||
Processing: | Processing: | ||
− | // | + | //得到第一个串口的数据. |
− | |||
myPort = new Serial(this, Serial.list()[0], 9600); | 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); | ||
− | + | 步骤三:下载代码并编译通过。 | |
− | + | 步骤四:运行后,用一块磁铁改变一下磁场,看看指针是否变化。 | |
− | + | ==结果== | |
− | |||
+ | 屏幕上会显示一个简单的指南针,指针会随着磁场走,像这样: | ||
+ | [[File:compass_simulatorResult1.jpg|600px|center|thumb]] | ||
− | == | + | ==视频== |
|} | |} |
2014年10月29日 (三) 07:20的最新版本
Language | English |
---|
目的本教程将教大家如何用Microduino-10DOF模块测到的磁场强度数据在Processing中显示一个指南针。 设备
原理图直接使用Microduino-10DOF上的HMC5883L磁场强度传感器 程序调试步骤一:按着原理图搭建硬件环境,像这样: 下载这个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); 步骤三:下载代码并编译通过。 步骤四:运行后,用一块磁铁改变一下磁场,看看指针是否变化。 结果屏幕上会显示一个简单的指南针,指针会随着磁场走,像这样: 视频 |