Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 2162|回復: 4
打印 上一主題 下一主題

自走車 程式延遲

[複製鏈接]
跳轉到指定樓層
1#
發表於 2017-12-20 18:41:57 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
我的自走車功能是有減速馬達&伺服馬達的組合   減速馬達用來驅動輪子的  伺服馬達有另外的功能    減速馬達&伺服馬達程式分開用的話動作正常   但何在一起後動作雖然一樣  但會有不定時的延遲    請問要怎麼解決???(下面是減速馬達+伺服馬達的程式)

#include<SoftwareSerial.h>
#include <Servo.h>
#include <Wire.h>
Servo UDservo;
Servo LRservo;
SoftwareSerial BT (11,12);
char command;
float ang = 90;
const int leftMotorIn1 = 4;
const int leftMotorIn2 = 5;
const int rightMotorIn3 = 6;      
const int rightMotorIn4 = 7;
bool isForward = true;

void setup()
{

  pinMode(leftMotorIn1, OUTPUT);
  pinMode(leftMotorIn2, OUTPUT);
  pinMode(rightMotorIn3, OUTPUT);
  pinMode(rightMotorIn4, OUTPUT);
  BT.begin(38400);
  Serial.begin(38400);
  UDservo.attach(2);
  LRservo.attach(3);
  UDservo.write(90);
  LRservo.write(90);
}
void loop()
{
if (BT.available() > 0)
{
command = BT.read();
Serial.println(command);
switch (command) {
case'A':
ang = ang + 3;
if (ang > 180) ang = 180;
UDservo.write(ang);
delay(40);
break;
case 'B':
ang = ang -3;
if (ang < 0) ang = 0;
UDservo.write(ang);
delay(40);
break;

case'C':
ang = ang -3;
if (ang < 0) ang = 0;
LRservo.write(ang);
delay(40);
break;
case'D':
ang = ang + 3;
if (ang > 180) ang = 180;
LRservo.write(ang);
delay(40);
break;
}
}
int inSize;
  char input;
  if( (inSize = (BT.available())) > 0) { //讀取藍牙訊息
      Serial.print("size = ");
      Serial.println(inSize);
      Serial.print("Input = ");
      Serial.println(input=(char)BT.read());
      
      if( input == 'E' ) {
        forward();
        isForward = true;
      }
      
      if( input == 'F' ) {
        backward();
        isForward = false;
      }
   
      if( input == 'H' ) {
        left();
      }
      
      if( input == 'G' ) {
        right();
      }
   
      if( input == 'I' ) {
        motorstop();
      }
  }
}

void motorstop()
{
  digitalWrite(leftMotorIn1, LOW);
  digitalWrite(leftMotorIn2, LOW);
  digitalWrite(rightMotorIn3, LOW);
  digitalWrite(rightMotorIn4, LOW);
}

void forward()
{
  digitalWrite(leftMotorIn1, HIGH);
  digitalWrite(leftMotorIn2, LOW);
  digitalWrite(rightMotorIn3, HIGH);
  digitalWrite(rightMotorIn4, LOW);
}

void backward()
{
  digitalWrite(leftMotorIn1, LOW);
  digitalWrite(leftMotorIn2, HIGH);
  digitalWrite(rightMotorIn3, LOW);
  digitalWrite(rightMotorIn4, HIGH);
}

void left()
{
  
    digitalWrite(leftMotorIn1, HIGH);
    digitalWrite(leftMotorIn2, LOW);
    digitalWrite(rightMotorIn3, LOW);
    digitalWrite(rightMotorIn4, LOW);
  
}

void right()
{

    digitalWrite(leftMotorIn1, LOW);
    digitalWrite(leftMotorIn2, LOW);
    digitalWrite(rightMotorIn3, HIGH);
    digitalWrite(rightMotorIn4, LOW);
  
}
2#
發表於 2017-12-20 19:04:12 | 只看該作者
本帖最後由 超新手 於 2017-12-21 08:15 編輯

因為你在兩個不同地方收藍牙命令
一個是command = BT.read(); 但是它只處理伺服機命令
另一個是input=(char)BT.read();只處理馬達命令
如果在command 那邊收到 馬達命令, 就會被忽略
或是在input那邊收到伺服機命令, 就會被忽略
所以兩個要整合在一起,才不會漏命令
也就是, 在loop() {...} 中,把
Int inSize;
char input;
以下的程式都拿掉(整合到上面的 switch中)
然後在 loop 中的 switch(command)   {...} 中增加
case 'E' : ~ case 'I':


另外, UDServo 和 LRServo 竟然共用一個變數 ang
有點奇怪
3#
 樓主| 發表於 2017-12-22 23:48:26 | 只看該作者
本帖最後由 f660229 於 2017-12-23 00:35 編輯

請問 ---在loop() {...} 中,把
Int inSize;
char input;
以下的程式都拿掉(整合到上面的 switch中)
然後在 loop 中的 switch(command)   {...} 中增加
case 'E' : ~ case 'I':---
是指把馬達的程式整個改掉嗎?還是改掉input=(char)BT.read();砍掉  然後把馬達的程式直接複製貼上?

UDservo & LRservo  我換成不同變數了 。請問設同一個變數會有什麼問題嗎??
4#
發表於 2017-12-23 02:34:57 | 只看該作者
1. 砍掉input=(char)BT.read()  然後把馬達的程式直接複製貼上。簡單來說,就是把兩個 switch 整合成一個

2. 假設剛開始, UD 和 LR 都是 90
你先下一堆命令,讓 UD 減到 0
此時 ang 等於 0
這時如果你改下一個命令,去減少 LR 的角度
這時候 LR 就會從 90 直接跳到 0
而不是 87 度
感覺怪怪的
5#
 樓主| 發表於 2017-12-23 23:48:17 | 只看該作者
本帖最後由 f660229 於 2017-12-23 23:52 編輯

謝謝     動作不會延遲了
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-4-25 07:53 , Processed in 0.196935 second(s), 6 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表