2023年9月10日 星期日

利用紅外線循跡模組和L298N製作循軌自走車

材料:Arduino主板一個、杜邦線約20條、L298N電機驅動模組1個、18650電池盒1個(兩個電源接頭、麵包短版1個、ZK-2自走車底盤套件、紅外線循跡模組3組。
L298N電機驅動模組規格請參考:L298N
紅外線循跡模組規格請參考:TCRT5000

實作演練:利用L298N電機驅動模組和紅外線循跡模組控制小車跟著黑線走。

應用:無人駕駛自動循軌車(基礎板)。

成品外觀:



紅外線循跡模組參考電路接法:(圖片來源:http://twarm.com/commerce/product_info.php?products_id=4919)




程式碼:(此程式碼參考:楊明峰,ARDUINO自走車最佳入門與應用打造輪型機器人輕鬆學,碁峰)
const int negR=4;
const int posR=5;
const int negL=6;
const int posL=7;
const int pwmR=9;
const int pwmL=10;
const int irD1=A1;
const int irD2=A2;
const int irD3=A3;
const int Rspeed=200;
const int Lspeed=200;
byte IRstatus=0;
void setup()
{
  pinMode(posR,OUTPUT);
  pinMode(negR,OUTPUT);
  pinMode(posL,OUTPUT);
  pinMode(negL,OUTPUT);
  pinMode(irD1,INPUT_PULLUP);
  pinMode(irD2,INPUT_PULLUP);
  pinMode(irD3,INPUT_PULLUP);
}
void loop()
{
    int val;
    IRstatus=0;
    val=analogRead(irD1);
    if(val>=150)
      IRstatus=(IRstatus+4);           
    val=analogRead(irD2);
    if(val>=150)
      IRstatus=(IRstatus+2);     
    val=analogRead(irD3);
    if(val>=150)
      IRstatus=(IRstatus+1);     
    driveMotor(IRstatus);
}
void driveMotor(byte IRstatus)
{
  switch(IRstatus)
  {
    case 0: 
      forward(Rspeed,Lspeed);
      break;
    case 1:
      right(1,Rspeed,Lspeed);
      break;
    case 2:
      forward(Rspeed,Lspeed);
      break;
    case 3:
      right(0,Rspeed,Lspeed);
      break;
    case 4:
      left(1,Rspeed,Lspeed);
      break;
    case 5:
      pause(0,0);
      break;
    case 6:
      left(0,Rspeed,Lspeed);
      break;
    case 7:
      pause(0,0);
      break;
  } 
}
void forward(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,HIGH);
    digitalWrite(negR,LOW);       
    digitalWrite(posL,LOW);
    digitalWrite(negL,HIGH); 
}
void back(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,HIGH);       
    digitalWrite(posL,HIGH);
    digitalWrite(negL,LOW); 
}
void pause(byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    digitalWrite(posR,LOW);
    digitalWrite(negR,LOW);       
    digitalWrite(posL,LOW);
    digitalWrite(negL,LOW); 
}
void right(byte flag, byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    if(flag==1)    //fast
    {
      digitalWrite(posR,LOW);
      digitalWrite(negR,HIGH);       
      digitalWrite(posL,LOW);
      digitalWrite(negL,HIGH);
    }
    else         //slow
    {
      digitalWrite(posR,LOW);
      digitalWrite(negR,LOW);       
      digitalWrite(posL,LOW);
      digitalWrite(negL,HIGH);     
    }
}
void left(byte flag, byte RmotorSpeed, byte LmotorSpeed)
{
    analogWrite(pwmR,RmotorSpeed);
    analogWrite(pwmL,LmotorSpeed);
    if(flag==1)    //fast
    {
      digitalWrite(posR,HIGH);
      digitalWrite(negR,LOW);       
      digitalWrite(posL,HIGH);
      digitalWrite(negL,LOW);
    }
    else         //slow
    {
      digitalWrite(posR,HIGH);
      digitalWrite(negR,LOW);       
      digitalWrite(posL,LOW);
      digitalWrite(negL,LOW);     
    }
}

實作演練過程:

沒有留言:

張貼留言

熱門文章