Robofun 機器人論壇
標題:
4輪自走車+4個伺服馬達0&90度的轉換
[打印本頁]
作者:
f660229
時間:
2017-12-25 19:35
標題:
4輪自走車+4個伺服馬達0&90度的轉換
本帖最後由 f660229 於 2017-12-25 20:02 編輯
我希望的功能,如標題4輪的自走車+4個伺服馬達0&90度的轉換。
現在arduino程式一輸入4個伺服就一直不斷的抖動,自走車的控制也不知道是否正確。
下面是我arduino的程式
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
SoftwareSerial BT(0,1);
const int leftMotorIn1 = 4;
const int leftMotorIn2 = 5;
const int rightMotorIn3 = 6;
const int rightMotorIn4 = 7;
const int leftMotorIn5 = 8;
const int leftMotorIn6 = 9;
const int rightMotorIn7 = 10;
const int rightMotorIn8 = 11;
char command;
void setup()
{
pinMode(leftMotorIn1, OUTPUT);
pinMode(leftMotorIn2, OUTPUT);
pinMode(rightMotorIn3, OUTPUT);
pinMode(rightMotorIn4, OUTPUT);
pinMode(leftMotorIn5, OUTPUT);
pinMode(leftMotorIn6, OUTPUT);
pinMode(rightMotorIn7, OUTPUT);
pinMode(rightMotorIn8, OUTPUT);
Serial.begin(38400);
BT.begin(38400);
servo1.attach(2);
servo2.attach(3);
servo3.attach(12);
servo4.attach(13);
servo1.write(0);
servo2.write(0);
servo3.write(0);
servo4.write(0);
}
void loop()
{
if(BT.available() > 0)
{
command = BT.read();
Serial.println(command);
switch (command) {
case 'A':
forward();
break;
case 'B':
backward();
break;
case 'C':
left();
break;
case 'D':
right();
break;
case 'S':
motorstop();
break;
case 'E':
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
break;
case 'F':
servo1.write(0);
servo2.write(0);
servo3.write(0);
servo4.write(0);
break;
}
}
}
void motorstop()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, LOW);
digitalWrite(leftMotorIn5, LOW);
digitalWrite(leftMotorIn6, LOW);
digitalWrite(rightMotorIn7, LOW);
digitalWrite(rightMotorIn8, LOW);
}
void forward()
{
digitalWrite(leftMotorIn1, HIGH);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, HIGH);
digitalWrite(rightMotorIn4, LOW);
digitalWrite(leftMotorIn5, HIGH);
digitalWrite(leftMotorIn6, LOW);
digitalWrite(rightMotorIn7, HIGH);
digitalWrite(rightMotorIn8, LOW);
}
void backward()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, HIGH);
digitalWrite(leftMotorIn5, LOW);
digitalWrite(leftMotorIn6, HIGH);
digitalWrite(rightMotorIn7, LOW);
digitalWrite(rightMotorIn8, HIGH);
}
void right()
{
digitalWrite(leftMotorIn1, HIGH);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(rightMotorIn3, LOW);
digitalWrite(rightMotorIn4, HIGH);
digitalWrite(leftMotorIn5, HIGH);
digitalWrite(leftMotorIn6, LOW);
digitalWrite(rightMotorIn7, LOW);
digitalWrite(rightMotorIn8, HIGH);
}
void left()
{
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
digitalWrite(rightMotorIn3, HIGH);
digitalWrite(rightMotorIn4, LOW);
digitalWrite(leftMotorIn5, LOW);
digitalWrite(leftMotorIn6, HIGH);
digitalWrite(rightMotorIn7, HIGH);
digitalWrite(rightMotorIn8, LOW);
}
提一個額外的問題,為什麼RXT&TXD輸入跟輸出的時候要互換?
作者:
超新手
時間:
2017-12-25 20:12
1. 接那麼多,電流應該會吃很大
可以先拔掉8個馬達,先接一個
看看還會不會抖
2. 傳輸就是我送你收
所以我方輸出接到對方輸入,就是傳送
對方輸出接到對方輸出,就是接收
作者:
f660229
時間:
2017-12-25 20:46
我只有用4個伺服馬達但一上傳後就一直亂跳
這是伺服馬達的程式 按一個按鈕4個伺服都到90度
按另一個按鈕4個伺服都回到0度
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
SoftwareSerial BT(0,1);
char command;
void setup()
{
Serial.begin(38400);
BT.begin(38400);
servo1.attach(2);
servo2.attach(3);
servo3.attach(12);
servo4.attach(13);
servo1.write(0);
servo2.write(0);
servo3.write(0);
servo4.write(0);
}
void loop()
{
if(BT.available() > 0)
{
command = BT.read();
Serial.println(command);
switch (command)
{
case 'E':
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
break;
case 'F':
servo1.write(0);
servo2.write(0);
servo3.write(0);
servo4.write(0);
break;
}
}
}
作者:
超新手
時間:
2017-12-25 20:52
對。所以你有先拔掉3個,先接一個試試看嗎?
另外, 馬達是吃幾伏的電壓?
作者:
f660229
時間:
2017-12-25 21:08
馬達工作電壓4.8~6V
3個伺服可以 4個就不行ㄌ
作者:
超新手
時間:
2017-12-25 21:13
所以看來應該是電流不足
作者:
f660229
時間:
2017-12-25 22:08
請問有增加電流的方法嗎?
作者:
超新手
時間:
2017-12-26 18:10
1. 你給 Arduino Vin(6~12V) 是多少伏(V)多少安培(A)?
比較簡單的方式是直接換大電壓(或大電流)試看看
2. 你的伺服機到底接多少伏(V)?
作者:
f660229
時間:
2017-12-27 00:19
本帖最後由 f660229 於 2017-12-27 22:18 編輯
伺服是5V 馬達是Vin
換成大電壓(大電流)具體來說有什麼辦法嗎?
歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/)
Powered by Discuz! X3.2