“直流电机控制”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
调试
程序
 
(未显示2个用户的6个中间版本)
第1行: 第1行:
 +
{{Language |DC_Motor_Control}}
 
{| style="width: 800px;"
 
{| style="width: 800px;"
 
|-
 
|-
第7行: 第8行:
  
 
==设备==
 
==设备==
*'''[[Microduino-Core]]'''
+
*'''[[Microduino-Core/zh]]'''
*'''[[Microduino-FT232R]]'''
+
*'''[[Microduino-USBTTL/zh]]'''
  
 
*其他硬件设备
 
*其他硬件设备
第19行: 第20行:
 
**二极管 一个
 
**二极管 一个
 
**NPN晶体管 一个
 
**NPN晶体管 一个
 
  
 
==原理图==
 
==原理图==
第27行: 第27行:
 
==程序==
 
==程序==
  
见 MicroduinoDCMotorControl
+
[https://github.com/Microduino/Microduino_Tutorials/tree/master/Microduino_Processing/ProcessingDCMotorControl ProcessingDCMotorControl]
 
 
ProcessingDCMotorControl
 
  
 
==调试==
 
==调试==
第43行: 第41行:
 
Microduino:
 
Microduino:
  
//得到串口数据,并输出值给电机
+
使用firmata的StandardFirmata程序。
 +
 
 +
Processing:
 +
 
 +
首先给你的processing添加个controlP5库文件:http://www.sojamo.de/libraries/controlP5/
  
  if(Serial.available())//if serial is available
+
下载后解压放到你的processing安装文件的\modes\java\libraries文件夹里,重启processing IDE
  {
 
    leitura=Serial.read();//read serial data
 
    Serial.println(leitura);
 
    vel=map(leitura,0,255,0,1023);//map
 
    if(leitura>0)//run motor by leitura
 
    {
 
      analogWrite(motorPin,vel);
 
      delay(1);
 
    }
 
    else//stop motor
 
    {
 
      analogWrite(motorPin,LOW);
 
    }
 
  }
 
  
Processing:
+
//定义串口
 +
  arduino = new Arduino(this, Arduino.list()[0], 57600); //your offset may vary
 +
  arduino.pinMode(motorPin, Arduino.OUTPUT); 
  
//列出所有串口列表,得到第一个串口的数据
+
//加载一个滑动条
   // List all the available serial ports in the output pane.
+
   cp5 = new ControlP5(this);
  // You will need to choose the port that the Wiring board is
+
   // add a vertical slider
  // connected to from this list. The first port in the list is
+
   cp5.addSlider("slider")
   // port #0 and the third port in the list is port #2.
+
    .setPosition(0, 580)
   println(Serial.list());
+
      .setSize(600, 20)
  // Open the port that the Wiring board is connected to (in this case #0)
+
        .setRange(0, 600)
  // Make sure to open the port at the same speed Wiring is using (9600bps)
+
          .setValue(25)
  port = new Serial(this, Serial.list()[0], 9600);
+
            ;
  
//绘制更新
+
//在draw()中绘制圆环进度条,并控制Microduino串口pwm
   void draw()  
+
  background(0);
 +
  noFill();
 +
  stroke(255);
 +
  smooth();
 +
  arc(width/2, height/2, 300, 300, turn, radians(map(speed, 0, 600, 0, 360))+turn);
 +
  //Specify font to be used
 +
  textFont(f, 48);
 +
  //Display Text
 +
   text ((int)speed, width/2-30, height/2+20);
 +
  vel=(int)map(speed, 0, 600, 0, 255);//map
 +
  if (speed>0)//run motor by speed
 +
  {
 +
    arduino.analogWrite(motorPin, vel);
 +
    delay(1);
 +
  }
 +
  else//stop motor
 
   {
 
   {
     background(0);
+
     arduino.analogWrite(motorPin, Arduino.LOW);
    update(mouseX/10);
 
    println(mouseX);
 
 
   }
 
   }
  
//串口输出鼠标x坐标值,并绘制当前值
+
//底部滑动条移动事件处理
   void update(int x)  
+
   void slider(float speedValue) {
  {
+
     speed=speedValue;
     port.write(x);
+
     println("Motor Speed:"+speed);
     stroke(255); 
 
    line(mouseX, 0, mouseX, 160); 
 
    text (mouseX, mouseX, 180);
 
 
   }
 
   }
  
 
步骤三:下载代码并编译通过。
 
步骤三:下载代码并编译通过。
  
步骤四:运行后,在processing中左右移动鼠标,看电机的转速变化。
+
步骤四:运行后,在processing中左右移动底部滑动条,看电机的转速变化。
  
 
==结果==
 
==结果==
  
屏幕上会显示一个简单的速度指示剂,跟着你的鼠标x轴变化,电机的转速也随之变化。
+
屏幕上会显示一个圆环进度条,拖拽下方的滑动条,圆环显示不同速率,电机的转速也随之变化。
[[File:processingDCMotorResult.jpg|600px|center|thumb]]
+
[[File:processingDCMotorResult1.jpg|600px|center|thumb]]
 
 
  
 
==视频==
 
==视频==

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

Language English

目的

本教程将教大家如何用processing来控制直流电机的转速。

设备

  • 其他硬件设备
    • USB数据连接线 一根
    • 面包板跳线 一盒
    • 面包板 一块
    • DC电机 一个
    • 电位器 一个
    • 2.2K欧姆电阻 一个
    • 二极管 一个
    • NPN晶体管 一个

原理图

ProcessingDCMotorSchematics.jpg

程序

ProcessingDCMotorControl

调试

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

ProcessingDCMotorConnectionDiagram.jpg


步骤二:解释一下代码:

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

Microduino:

使用firmata的StandardFirmata程序。

Processing:

首先给你的processing添加个controlP5库文件:http://www.sojamo.de/libraries/controlP5/

下载后解压放到你的processing安装文件的\modes\java\libraries文件夹里,重启processing IDE

//定义串口

 arduino = new Arduino(this, Arduino.list()[0], 57600); //your offset may vary
 arduino.pinMode(motorPin, Arduino.OUTPUT);  

//加载一个滑动条

 cp5 = new ControlP5(this);
 // add a vertical slider
 cp5.addSlider("slider")
   .setPosition(0, 580)
     .setSize(600, 20)
       .setRange(0, 600)
         .setValue(25)
           ;

//在draw()中绘制圆环进度条,并控制Microduino串口pwm

 background(0);
 noFill();
 stroke(255);
 smooth();
 arc(width/2, height/2, 300, 300, turn, radians(map(speed, 0, 600, 0, 360))+turn);
 //Specify font to be used
 textFont(f, 48); 
 //Display Text
 text ((int)speed, width/2-30, height/2+20);
 vel=(int)map(speed, 0, 600, 0, 255);//map
 if (speed>0)//run motor by speed
 {
   arduino.analogWrite(motorPin, vel);
   delay(1);
 }
 else//stop motor
 {
   arduino.analogWrite(motorPin, Arduino.LOW);
 }

//底部滑动条移动事件处理

 void slider(float speedValue) {
   speed=speedValue;
   println("Motor Speed:"+speed);
 }

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

步骤四:运行后,在processing中左右移动底部滑动条,看电机的转速变化。

结果

屏幕上会显示一个圆环进度条,拖拽下方的滑动条,圆环显示不同速率,电机的转速也随之变化。

ProcessingDCMotorResult1.jpg

视频