在這篇文章中,我們將學(xué)習(xí)如何使用操縱桿和Arduino控制伺服電機。我們將看到有關(guān)操縱桿,其銷,其結(jié)構(gòu)和工作的概述。我們將從操縱桿中提取有用的數(shù)據(jù),這些數(shù)據(jù)將成為控制伺服電機的基礎(chǔ)。
現(xiàn)在讓我們看一下操縱桿。
操縱桿是一種輸入設(shè)備,由杠桿組成,杠桿可以在 X 軸和 Y 軸上沿多個方向移動。杠桿的運動用于控制電機或任何外圍電子設(shè)備。
從遙控玩具到Boing飛機,操縱桿都用于執(zhí)行類似的功能。此外,游戲和較小的操縱桿在Z軸上有一個按鈕,可以對其進行編程以執(zhí)行許多有用的操作。
操縱桿通常是電子設(shè)備,因此我們需要施加電源。杠桿的移動會在輸出引腳上產(chǎn)生電壓差。電壓電平由微控制器處理,以控制電機等輸出設(shè)備。
圖示的操縱桿與此類似,可以在PlayStation和Xbox控制器中找到。您無需破壞這些控制器即可挽救一個。這些模塊在當(dāng)?shù)仉娮由痰旰碗娮由虅?wù)網(wǎng)站上隨時可用。
現(xiàn)在讓我們看看這個操縱桿的構(gòu)造。
它有兩個 10 千歐姆電位器,位于帶有彈簧的 X 軸和 Y 軸上,因此當(dāng)用戶從杠桿釋放力時,它會返回到其原始位置。它在 Z 軸上有一個按下 ON
按鈕。
它具有 5 個引腳、5 伏 Vcc、GND、可變 X、可變 Y 和 SW(Z 軸開關(guān))。當(dāng)我們施加電壓并將操縱桿留在其原始杠桿位置時。X 和 Y
引腳將產(chǎn)生施加電壓的一半。
當(dāng)我們移動杠桿時,X 和 Y 輸出引腳的電壓會發(fā)生變化?,F(xiàn)在讓我們實際將操縱桿連接到Arduino。
示意圖:
引腳連接詳細信息在電路旁邊給出。連接完成的硬件設(shè)置并上傳代碼。
程序:
//---------------Program Developed by R.Girish--------------//
int X_axis = A0;
int Y_axis = A1;
int Z_axis = 2;
int x = 0;
int y = 0;
int z = 0;
void setup()
{
Serial.begin(9600);
pinMode(X_axis, INPUT);
pinMode(Y_axis, INPUT);
pinMode(Z_axis, INPUT);
digitalWrite(Z_axis, HIGH);
}
void loop()
{
y = analogRead(Y_axis);
z = digitalRead(Z_axis);
Serial.print(“X axis = ”);
Serial.println(x);
Serial.print(“Y axis = ”);
Serial.println(y);
Serial.print(“Z axis = ”);
if(z == HIGH)
{
Serial.println(“Button not Pressed”);
}
else
{
Serial.println(“Button Pressed”);
}
Serial.println(“----------------------------”);
delay(500);
}
//---------------Program Developed by R.Girish--------------//
Open the Serial monitor you can see the voltage level at the X and Y axes
pins and the status of the Z axis i.e. push button as illustrated below.
These X, Y, Z axes values are used to interpret the position of the lever.
As you can see the values are from 0 to 1023.
That’s because Arduino has built in ADC converter which convert the voltage
0V - 5V to 0 to 1023 values.
You can witness from the serial monitor that when the lever is left
untouched the lever stays at mid position of both X and Y axes and shows half
value of 1023.
You can also see it is not exact half of the 1023 that’s because
manufacturing these joysticks never been perfect.
By now, you would have got some technical knowledge about joysticks.
Now let’s see how to control two servo motors using one joystick.
Circuit Diagram:
兩個伺服電機由一個操縱桿控制;當(dāng)您沿 X 軸移動操縱桿時,連接在引腳 #7 處的伺服器會根據(jù)操縱桿位置順時針和逆時針移動。
如果將操縱桿水平保持在特定位置,也可以將伺服執(zhí)行器保持在某個位置。
與在引腳 #6 處連接的伺服電機類似,您可以沿 Y 軸移動杠桿。
當(dāng)您沿 Z 軸按下杠桿時,兩個電機將執(zhí)行 180 度掃描。
您可以將 arduino 連接到 9v 電池或計算機。如果將Arduino連接到計算機,則可以打開串行監(jiān)視器并查看伺服執(zhí)行器的角度和電壓電平。
伺服電機控制程序:
//---------------Program Developed by R.Girish--------------//
#include《Servo.h》
Servo servo_X;
Servo servo_Y;
int X_angleValue = 0;
int Y_angleValue = 0;
int X_axis = A0;
int Y_axis = A1;
int Z_axis = 2;
int x = 0;
int y = 0;
int z = 0;
int pos = 0;
int check1 = 0;
int check2 = 0;
int threshold = 10;
void setup()
{
Serial.begin(9600);
servo_X.attach(7);
servo_Y.attach(6);
pinMode(X_axis, INPUT);
pinMode(Y_axis, INPUT);
pinMode(Z_axis, INPUT);
digitalWrite(Z_axis, HIGH);
}
void loop()
{
x = analogRead(X_axis);
y = analogRead(Y_axis);
z = digitalRead(Z_axis);
if(z == LOW)
{
Serial.print(“Z axis status = ”);
Serial.println(“Button Pressed”);
Serial.println(“Sweeping servo actuators”);
for (pos = 0; pos 《= 180; pos += 1)
{
servo_X.write(pos);
delay(10);
}
for (pos = 180; pos 》= 0; pos -= 1)
{
servo_X.write(pos);
delay(15);
}
for (pos = 0; pos 《= 180; pos += 1)
{
servo_Y.write(pos);
delay(10);
}
for (pos = 180; pos 》= 0; pos -= 1)
{
servo_Y.write(pos);
delay(15);
}
Serial.println(“Done?。?!”);
}
if(x 》 check1 + threshold || x 《 check1 - threshold)
{
X_angleValue = map(x, 0, 1023, 0, 180);
servo_X.write(X_angleValue);
check1 = x;
Serial.print(“X axis voltage level = ”);
Serial.println(x);
Serial.print(“X axis servo motor angle = ”);
Serial.print(X_angleValue);
Serial.println(“ degree”);
Serial.println(“------------------------------------------”);
}
if(y 》 check2 + threshold || y 《 check2 - threshold)
{
Y_angleValue = map(y, 0, 1023, 0, 180);
servo_Y.write(Y_angleValue);
check2 = y;
Serial.print(“Y axis voltage level = ”);
Serial.println(y);
Serial.print(“Y axis servo motor angle = ”);
Serial.print(Y_angleValue);
Serial.println(“ degree”);
Serial.println(“------------------------------------------”);
}
}
-
伺服電機
+關(guān)注
關(guān)注
85文章
2060瀏覽量
58254 -
Arduino
+關(guān)注
關(guān)注
188文章
6477瀏覽量
187952 -
操縱桿
+關(guān)注
關(guān)注
0文章
69瀏覽量
8588
發(fā)布評論請先 登錄
相關(guān)推薦
小白求教,怎么實現(xiàn)操縱桿控制電機的旋轉(zhuǎn)角度
如何使用Arduino UNO、操縱桿模塊和NRF24L01模塊控制伺服電機?
基于arduino的超級無尾卡丁車操縱桿
怎樣用Arduino和操縱桿控制伺服電機
使用Arduino Uno制作操縱桿控制的PC鼠標(biāo)
![使用<b class='flag-5'>Arduino</b> Uno制作<b class='flag-5'>操縱桿</b><b class='flag-5'>控制</b>的PC鼠標(biāo)](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
如何使用Arduino和操縱桿控制BLDC電機
![如何使用<b class='flag-5'>Arduino</b>和<b class='flag-5'>操縱桿</b><b class='flag-5'>控制</b>BLDC<b class='flag-5'>電機</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
如何使用Arduino創(chuàng)建操縱桿控制器
![如何使用<b class='flag-5'>Arduino</b>創(chuàng)建<b class='flag-5'>操縱桿</b><b class='flag-5'>控制</b>器](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
Arduino之使用PS2操縱桿控制LED
![<b class='flag-5'>Arduino</b>之使用PS2<b class='flag-5'>操縱桿</b><b class='flag-5'>控制</b>LED](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
基于Arduino Pro Mini的操縱桿手柄和DIY相機滑塊
NRF24L01帶操縱桿的無線伺服電機控制
![NRF24L01帶<b class='flag-5'>操縱桿</b>的無線<b class='flag-5'>伺服</b><b class='flag-5'>電機</b><b class='flag-5'>控制</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
Arduino基于網(wǎng)絡(luò)的操縱桿
![<b class='flag-5'>Arduino</b>基于網(wǎng)絡(luò)的<b class='flag-5'>操縱桿</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
一個使用操縱桿控制器控制伺服電機的電路
![一個使用<b class='flag-5'>操縱桿</b><b class='flag-5'>控制</b>器<b class='flag-5'>控制</b><b class='flag-5'>伺服</b><b class='flag-5'>電機</b>的電路](https://file.elecfans.com/web2/M00/AD/1D/pYYBAGSMK92AEAc6AADmSLW9TRI825.png)
評論