Workshop - ccst.hkfyg.org.hk · 進階工作坊流程 •Arduino使用(第二部分) •2017/18創意編程設計大賽 – 任務挑戰項目(ME)
Arduino 習作工坊 - Lesson 2 動力之夜
-
Upload
cavedu-education -
Category
Devices & Hardware
-
view
2.161 -
download
5
Transcript of Arduino 習作工坊 - Lesson 2 動力之夜
#2 馬達
馬達的種類
• 直流馬達 DC Motors
• 交流馬達 AC Motors
• 步進馬達 Step Motors
• 伺服馬達 RC Servo Motors
• 線性馬達 Linear Motors
SERVO
伺服機
http://pcbheaven.com/
1. 控制器(即控制電路)會將輸入的PWM訊號轉換成相對的參考電壓(解
碼,decoding),不同的參考電壓會對應到伺服馬達轉軸的不同位置。
2. 控制器藉由量測電位計的分壓得知目前伺服馬達轉軸的位置。
3. 控制器比較此兩電壓的差異(也就是位置的差異),並開始轉動,直到輸
入的參考電壓與實際的電位計分壓相同為止。
4. 因此,伺服馬達只要通電就會鎖死,無法由外力轉動,因為其會不斷地比
較位置變化(當然會有一個初始值),並嘗試修正到目標位置。
RC 伺服機
數位輸出? 類比輸出?
Pulse Width Modulation (PWM)脈衝寬度調變
• 數位輸出可以控制訊號的開和關,開和關同
時意味著通電與斷電
• 如果我們可以進一步控制通電的時間比例,
就能讓輸出的訊號產生變化,例如LED燈通
電時間為50%,就可以控制LED燈只有50%
的亮度
• 以數位訊號模擬類比訊號的技術
PWM 使用方法
• IDE code 為 analogWrite()
• 格式:analogWrite(pin, value)
• 在uno板子中只有pin 3,5,9,10,11腳位為PWM腳位
• Value: duty cycle,介於0-255,0為0%,127為50%,255為100%
伺服機(Servo)範例(File >> Example >> Servo >> Sweep)
程式 - Sweep
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
伺服機(Servo)範例(File >> Example >> Servo >> Knob)
程式-Knob
#include <Servo.h>
Servo myservo;
int potpin = 0;
int val;
void setup()
{
myservo.attach(9);
}
呼叫Servo函式庫
創立myservo物件
使用Pin9控制servo
程式-Knob
void loop()
{
val = analogRead(potpin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
}
轉換範圍從0~1023→0~179
程式-Knobvoid setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop()
{
val = analogRead(potpin); // reads the value of the
potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo
(value between 0 and 180)
println(val);
myservo.write(val); // sets the servo position according to the
scaled value
delay(15); // waits for the servo to get there
}
DC MOTOR
直流馬達
DC直流馬達
如何控制?
H 橋電路
一個典型的 H 橋 IC 主要是由四個電晶體組成,透過電晶體的開關控制電流流動的方向,因此可以用於馬達正反轉的控制上
原理說明
L293D 直流馬達驅動晶片
3
6
14
11
1以凹槽朝上,開始逆時針編號
8 9
4, 5, 12, 13與電池負極共接地
16: Arduino 5V
2 15
7 10
馬達電路-
橘色線用來控制轉速
3,6
14,11
ArduinoDirection
IC pin#
3 2
5 7
6 10
9 15
MOTOR IC pin#
MR+ 3
MR- 6
ML+ 11
ML- 14
ArduinoPWM
IC pin#
10 1
11 9電源正極 IC pin#
沒電池就用Arduino 5V 8
Arduino 5V 16
控制馬達轉動方向
int input1 = 3;
int input2 = 9;
void setup() {
pinMode(input1, OUTPUT);
pinMode(input2, OUTPUT);
}
void loop() {
digitalWrite(input1, HIGH);
digitalWrite(input2, LOW);
delay(1000);
digitalWrite(input1, LOW);
digitalWrite(input2, HIGH);
delay(1000);
}
控制馬達轉速Arduino #10 接到 IC pin 1
//透過 IC 的 EN腳位來//控制馬達轉速
int input1 = 3;int input2 = 9;void setup() {
pinMode(input1, OUTPUT); pinMode(input2, OUTPUT);
pinMode(10, OUTPUT);}
void loop() { analogWrite(10, 128);digitalWrite(input1, HIGH); digitalWrite(input2, LOW); delay(1000);
digitalWrite(input1, LOW); digitalWrite(input2, HIGH); delay(1000);
}
步進馬達
STEPPER
步進馬達
外側為電磁鐵的定子,內為NS交互磁化的永磁轉子(無齒型)
依轉子的構造來分
http://www.engineersgarage.com/
http://ming-shian.blogspot.tw/2013/05/blog-post_8.html
雙極就是驅動馬達的電流是雙向的,驅動控制時,需要改變電流方向。而單極的馬達,其電流就只需提供一個方向就好,改變提供的順序就可以達到驅動控制。
激磁方式
四相單極性步進馬達(五線)
http://robocraft.ru/
ULN2003APG
http://forum.allaboutcircuits.com/
http://www.instructables.com/
Stepper(int steps, pin1, pin2, pin3, pin4)建立一個步進馬達的物件。其中step是指轉一圈所需的步數,假使馬達定義每步的角度,用360去除,就會得到步數。例如:Stepper myStepper(100, 8, 9, 10, 11);
表示每一步為3.6度,轉一圈總共100步。
Stepper.setSpeed(long rpms)設定步進馬達每分鐘轉速 (RPMs) ,需為正數。這個函式並不會讓馬達轉動,只是設定好轉速,當呼叫Step()函式時才會開始轉動。
Stepper.step(int steps)啟動馬達行進steps步數。setSpeed()定義速度,正的表示一個方向, 負數表示反方向。
http://atceiling.blogspot.tw/
<Stepper.h>
程式:Stepmotor_step#include <Stepper.h>// initialize the stepper library on pins 8 through 11:
Stepper myStepper(200, 8,9,10,11);
//表示每一步為1.8度,轉一圈總共200步。
void setup() {
// nothing to do inside the setup
}
void loop() {
myStepper.setSpeed(50);
//轉速為50rpm(revolution per minutes每分鐘可以轉50圈)
myStepper.step(1); //一次走一步}
程式:Stepmotor2_control#include <Stepper.h>
const int stepsPerRevolution = 200;
// change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
int stepCount = 0; // number of steps the motor has taken
void setup() {
// nothing to do inside the setup
}
void loop() {
// read the sensor value:
int sensorReading = analogRead(A0); // map it to a range
from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution/100);
}
}
補充資料
• http://gcyrobot.blogspot.tw/2011/05/arduino-h-bridgel293d.html