วันเสาร์ที่ 15 ตุลาคม พ.ศ. 2559

โครงงาน หุ่นยนต์เดินตามเส้น 2 เซ็นเซอร์ Arduino



โปรเจค หุ่นยนต์เดินตามเส้น 2 เซ็นเซอร์ Arduino หุ่นยนต์และมนุษย์มีหลักการทำงานที่เหมือนกัน คือ หน่วยรับข้อมูลเข้า (Input Unit) หน่วยประมวลผล (Process Unit) และหน่วยแสดงผล (Output Unit) ดังนั้นการที่หุ่นยนต์จะเคลื่อนที่ไปให้ตรงเป้าหมาย หุ่นยนต์จะต้องมีอุปกรณ์ที่จะตรวจสอบตำแหน่งและส่งข้อมูลที่ได้ไปยังหน่วยประมวลผล เพื่อให้มอเตอร์ทำการแสดงผลโดยการไปยังเป้าหมายต่อไป อุปกรณ์พื้นฐานที่ใช้ในการตรวจสอบตำแหน่งนั้น คือ โมดูลเซ็นเซอร์แสงสำหรับตรวจจับวัตถุกีดขวาง


อุปกรณ์ที่ต้องใช้ก็คือ


     1. 2WD Smart Robot Car Chassis Kits
 
     2. 
Arduino UNO R3 - Made in italy   

     3. IR Infrared Obstacle Avoidance Sensor Module จำนวน 2 ชิ้น
   
   
     4. 
Motor Drive Module L298N   

     5. 
สาย Jumper Female to Male ยาว 20cm.   
   
     6. 
สาย Jumper Male to Male ยาว 20cm.   

     7. 
รางถ่านแบบ 18650 ใส่ถ่าน 2 ก้อน   
   
     8. แบตเตอรี่ลิเธียม 18650 จำนวน 2 ก้อน

   
     9. เสารองแผ่นพีซีบีโลหะแบบเหลี่ยม 6 mm.

      
    10. สกรูหัวกลมน็อตตัวเมีย ขนาด 3มม ยาว12 มม. 

    11. Mounting Bracket for HC-SR04 Ultrasonic Module แบบสั้น

    12. แผ่นอะคริลิค



การทำงานของ หุ่นยนต์เดินตามเส้น Arduino



เรียนรู้ การใช้งาน IR Infrared Obstacle Avoidance Sensor Module เพิ่มเติมที่

https://robotsiam.blogspot.com/2016/10/ir-infrared-obstacle-avoidance-sensor.html


เซ็นเซอร์แบบนี้จะมีช่วงในการทำงาน หรือ ระยะในการตรวจจับจะได้ใกล้กว่าแบบ Opposed mode ซึ่งในสภาวะการทำงานปกติ พื้นเเป็นสีขาว ตัวรับ Receiver จะสามารถรับสัญญาณแสงจากตัวส่ง  Emitter ได้ตลอดเวลา เนื่องจากลำแสง สะท้อนกลับมาได้ จะแสดงค่า เป็น 0



และ เมื่อพื้นเป็นสีดำ แสงสะท้อนกลับมาไม่ได้   จึงทำให้ตัวรับ Receiver ไม่สามารถรับลำแสงที่จะสะท้อนกลับมาได้ จะแสดงค่า เป็น 1



เซ็นเซอร์ตรวจสอบการสะท้อนแสงกลับมา ได้หรือไม่ แล้วส่งสัญญาณไปยัง Arduino Uno R3 แล้ว Arduino Uno R3 จึงไปสั่งให้มอเตอร์ทำงาน โดย Motor Drive Module L298N ให้เป็นไปตามการส่งค่ามาของเซ็นเซอร์


โปรเจค หุ่นยนต์เดินตามเส้น 2 เซ็นเซอร์ Arduino  นี้ เราจะใช้ 2 เซ็นเซอร์อินฟราเรด คือด้านซ้ายและด้านขวา  โดย เมื่อเซ็นเซอร์ ทั้งด้านซ้ายและด้านขวา ตรวจสอบแล้วเป็นพื้นสีขาว ลำแสง สามารถสะท้อนกลับมาได้ทั้งคู่  ( 0 , 0 ) ให้หุ่นยนต์เคลื่อนที่ไปข้างหน้า



หากเซ็นเซอร์ซ้ายมาพบพื้นสีดำ  และ เซ็นเซอร์ขวาพบพื้นสีขาว ( 1 , 0 ) ให้หุ่นยนต์เลี้ยวไปทางด้านซ้าย



หากเซ็นเซอร์ขวามาพบพื้นสีดำ  และ เซ็นเซอร์ซ้ายพบพื้นสีขาว ( 0 , 1 ) ให้หุ่นยนต์เลี้ยวไปทางด้านขวา


หากเซ็นเซอร์ทั้งสองมาอยู่บนเส้นสีดำทั้งคู่ ( 1 , 1 ) ให้หุ่นยนต์หยุด


โดยสรุป มี 4 เงื่อนไขที่ หุ่นยนต์เดินตามเส้น Arduino รับค่าจากเซ็นเซอร์ซ้ายและขวา
คือ (0 ,0) | (0 ,1) | (1 , 0) | (1 ,1 ) แสดงตามตารางด้านล่าง



อินพุต
เอาต์พุต
การเคลื่อนที่ของหุ่นยนต์
เซ็นเซอร์ซ้าย
เซ็นเซอร์ขวา
มอเตอร์ซ้าย

มอเตอร์ขวา
LS
RS
LM1
LM2
RM1
RM2

0
0
0
0
0
0
เดินหน้า
0
1
1
0
0
0
เลี้ยวขวา
1
0
0
0
1
0
เลี้ยวซ้าย
1
1
1
0
1
0
หยุด

เราจะสร้าง หุ่นยนต์เดินตามเส้น Arduino และ เขียนโปรแกรมตามเงื่อนไขที่แสดงในตารางด้านบน


เริ่มต้นด้วยการ ประกอบ Smart Robot Car Chassis Kit

ต่อวงจร Arduino  UNO กับ L298N  Motor Driver ตามรูปการต่อวงวงจร


นำไฟจากแบตเตอรี่ลิเธียม 18650 ไปต่อตรงกับ L298N  Motor Driver (ไม่ต่อกับ พอร์ต Power Supply ของ บอร์ด Arduino  UNO R3 ) และ นำไฟ +5 โวลต์ ที่ออกจาก L298N  Motor Driver ต่อออกไปที่  5V ของ Arduino  UNO R3 ตามรูปการต่อวงวงจร





*** VCC ของ Arduino UNO R3 คือ 5V ***



ต่อวงจร Arduino  UNO กับ Sensor ทั้ง 2 ตัว

เข้า ขา D2 และ D3 ตามรูป


Sensor <-> UNO

VCC (ซ้าย,ขวา) <-> 5V
GND 
(ซ้าย,ขวา) <-> GND
OUT (ซ้าย)       <-> D2
OUT (ขวา)       <-> D3




ตรง 5V ของ Arduino จะมี 3 สายใช้ร่วมกัน ให้เชื่อมต่อ บัตกรี รวมกันก่อนแล้วจึงต่อเข้าไปที่ 5V


ตัดแผ่นอะคริลิค ขนาด 3 x 10 เซ็นติเมตร 

ประกอบ 
เซ็นเซอร์ซ้ายและขวา




ยึดเข้ากับ Mounting Bracket for HC-SR04 Ultrasonic Module แบบสั้น




ยึดเข้ากับ ตัวหุ่นยนต์





การปรับแต่ง เซ็นเซอร์ให้ตรวจจับเส้น


เมื่อต่อวงจร หุ่นยนต์เดินตามเส้น เสร็จแล้ว ให้ต่อสาย USB จากคอมพิวเตอร์ เข้ากับ Arduino  UNO R3 ปรับ ระยะการตรวจจับของเซ็นเซอร์ โดยใช้ไขควง ปรับ ที่ VR (สี่เหลี่ยมสีฟ้า-ขาว) โดยใช้มือเคลื่อนที่หุ่นยนต์ผ่านเส้นสีดำและพื้นสีขาว

ให้ปรับเซ็นเซอร์ทั้ง 2 ตัว ดังนี้ ถ้าอยู่บนพื้นสีขาว ให้ไฟ LED ติด 2 ดวง และ ถ้าเข้าไปในบนเทปสีดำ ให้ ไฟ LED ติด 1 ดวง
 


ถาพรวมเมื่อต่อเสร็จแล้ว








อัพโหลดโปรแกรม



เปิดโปรแกรม Arduino (IDE) และ Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3



 /*-------definning Inputs------*/
int LS = 2;     // left sensor
int RS  = 3;      // right sensor

/*-------definning Outputs------*/
int LM1 = 4;      // left motor
int LM2 =  5;       // left motor
int RM1 =  6;       // right motor
int RM2 =  7;       // right motor

/*-------PWM ควบคุมความเร็วมอเตอร์-----*/
int PWM1 = 10;    // PWM left motor
int PWM2 = 11;    // PWM right motor
int SPEED = 90;  // Speed PWM สามารถปรับความเร็วได้ถึง 255

void setup()
{
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(LM2, OUTPUT);
  pinMode(RM1, OUTPUT);
  pinMode(RM2, OUTPUT);
  pinMode(PWM1, OUTPUT);
  pinMode(PWM2, OUTPUT);
}

void loop()

{

  if (!(digitalRead(LS)) && !(digitalRead(RS)))    // Move Forward
  {
    analogWrite(PWM1, SPEED);
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    analogWrite(PWM2, SPEED);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  }


  if (!(digitalRead(LS)) && digitalRead(RS))    // Turn right
  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, LOW);
    analogWrite(PWM2, SPEED);
    digitalWrite(RM1, HIGH);
    digitalWrite(RM2, LOW);
  }

  if (digitalRead(LS) && !(digitalRead(RS)))    // turn left
  {
    analogWrite(PWM1, SPEED);
    digitalWrite(LM1, HIGH);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, LOW);
  }

 if (digitalRead(LS) && digitalRead(RS))    // stop

  {
    digitalWrite(LM1, LOW);
    digitalWrite(LM2, LOW);
    digitalWrite(RM1, LOW);
    digitalWrite(RM2, LOW);
  }

}




ก่อนอัพโหลดให้ตรวจสอบ




1.ตรวจสอบการติดตั้งไดรเวอร์ ของ Arduino UNO



ไปที่หน้าจอคอมพิวเตอร์  
คลิกขวาที่ Computet -> Properties







คลิกที่ Device Manager




ไปที่ Ports (COM & LPT) จะพบ 
ไดร์เวอร์ ของ Arduino UNO ในตัวอย่างเป็น "COM3"




(ถ้าไม่พบให้ทำการติดตั้ง
ไดร์เวอร์ก่อน) 
การติดตั้งโปรแกรม Arduino (IDE) และ การติดตั้งไดร์เวอร์



2. ตรวจสอบการเลือก Board


ให้เลือก Board : เป็น Arduino/Genuino UNO



3. ตรวจสอบ ต้องมี Port :  ในตัวอย่างเป็น COM3

(ถ้ายังว่าง หรือ เป็น COM1 ให้ตรวจสอบการติดตั้งไดร์เวอร์ การต่อสาย USB ของ Arduino UNO)




ถ้าทุกอย่างถูกต้อง ให้คลิกเครื่องหมายตรงไป เพื่ออัพโหลด




4. รอกระทั่ง จนขึ้น Done uploading. 


แสดงว่า สามารถอัพโหลดโปรแกรมเข้า Arduino UNO สําเร็จแล้ว




วีดีโอผลลัพธ์การทำงานของ หุ่นยนต์เดินตามเส้น 2 เซ็นเซอร์ Arduino


วันอาทิตย์ที่ 25 กันยายน พ.ศ. 2559

code โปรเจค หุ่นยนต์หลบสิ่งกีดขวาง Arduino V2


หุ่นยนต์หลบสิ่งกีดขวาง Arduino V2

โดยเราจะเอา หุ่นยนต์หลบสิ่งกีดขวาง Arduino จกา เวอร์ชั่น 1 มาปรับปรุงดังนี้

หุ่นยนต์หลบสิ่งกีดขวาง เวอร์ชั่น 1 อุปกรณ์ที่ต้องใช้ก็คือ

     1. 4WD Smart Robot Car Chassis Kits

     2. Arduino UNO R3 - Made in italy

     3. Arduino Sensor Shield V5.0

     4. เซนเซอร์ Ultrasonic Module HC-SR04

     5. Mounting Bracket for HC-SR04 Ultrasonic Module แบบสั้น

     6. Motor Drive Module L298N

     7. สาย Jumper Female to Male ยาว 20cm.

     8. สาย Jumper Femalet to Female ยาว 20cm.

     9. รางถ่านแบบ 18650 ใส่ถ่าน 2 ก้อน

     10. แบตเตอรี่ลิเธียม 18650 จำนวน 2 ก้อน

     11. เสารองแผ่นพีซีบีโลหะแบบเหลี่ยม 6 mm 12 ชิ้น

     12. สกรูหัวกลมน็อตตัวเมีย ขนาด 3มม ยาว12มม. 10 ตัว

มาปรับปรุงเป็น เวอร์ชั่น 2 ดังนี้

หุ่นยนต์หลบสิ่งกีดขวาง เวอร์ชั่น 2 อุปกรณ์ที่ต้องเพิ่มเติม จาก เวอร์ชั่น 1 ก็คือ

13. Infrared Remote Control Kit

14. Servo Motor Servo Pro SG90 หรือ เซอร์โวมอเตอร์ MG996R พร้อมกับอุปกรณ์เสริม

ถ้าเลือกใช้ Servo Motor  Pro SG90 มีอุปกรณ์เสริม คือ Mounting Bracket for Servo SG90


เริ่มด้วยแก้ไข การเชื่อมต่อ บอร์ด Motor Drive Module L298N ดังรูป
ที่ขา ENA และ ENB ไม่ต้องเชื่อมต่อใดๆ และ ให้เอา Jumper ที่ถอดออกไป ใส่คืน สภาพเดิม
หมายเหตุ : ตรวจสอบการใส่ Jumper ให้ถูกต้องตามรูปด้วย



เพิ่ม Infrared Remote Control Kit เข้าไปดังรูป



และ ทดสอบการทำงาน การต่อวงจร ตามลิงค์นี้

http://robotsiam.blogspot.com/2016/09/infrared-remote-control-kit.html

จากนั้นเพิ่ม Servo Motor  "Pro SG90" หรือ "เซอร์โวมอเตอร์ MG996R" ความแตกต่างหลักๆของ 2 ตัวนี้ คือ รุ่น MG996R  จะเป็น เฟืองโลหะ ส่วน Pro SG90 จะเป็น เฟืองพลาสติก


ประกอบเข้ากับ เซนเซอร์ Ultrasonic Module HC-SR04 ตามรูป


จากการทดสอบ เมื่อเลือกใช้ รุ่น MG996R เซอร์โวมอเตอร์จะเป็น เฟืองโลหะ ต้องต่อ แหล่งจ่ายไฟให้กับ ซัพพลาย ของ Arduino UNO R3 ดังรูปด้วย







จากนั้นทดสอบ หุ่นยนต์หลบสิ่งกีดขวาง ได้โดย  Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3



#include "IRremote.h"
#include <Servo.h>

//Pins for motor A 
const int MotorA1 = 6;
const int MotorA2 = 7; 
//Pins for motor B 
const int MotorB1 = 8;
const int MotorB2 = 9;
//Pins for ultrasonic sensor
const int trigger=10;
const int echo=11;

int leftscanval, centerscanval, rightscanval, ldiagonalscanval, rdiagonalscanval;
char choice;
  
//Pin for IR control
int receiver = 12; // pin 1 of IR receiver to Arduino digital pin 12
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results; 
char contcommand;
int modecontrol=0;
int power=0;

const int distancelimit = 27; //Distance limit for obstacles in front           
const int sidedistancelimit = 12; //Minimum distance in cm to obstacles at both sides (the robot will allow a shorter distance sideways)

int distance;
int numcycles = 0;
char turndirection; //Gets 'l', 'r' or 'f' depending on which direction is obstacle free
const int turntime = 900; //Time the robot spends turning (miliseconds)
int thereis;
Servo head;

void setup(){
  head.attach(5);
  head.write(80);
  irrecv.enableIRIn(); // Start the IR receiver
  pinMode(MotorA1, OUTPUT); 
  pinMode(MotorA2, OUTPUT); 
  pinMode(MotorB1, OUTPUT); 
  pinMode(MotorB2, OUTPUT);
  pinMode(trigger,OUTPUT);
  pinMode(echo,INPUT);
  //Variable inicialization
  digitalWrite(MotorA1,LOW);
  digitalWrite(MotorA2,LOW);
  digitalWrite(MotorB1,LOW);
  digitalWrite(MotorB2,LOW);
  digitalWrite(trigger,LOW);
}

void go(){ 
   digitalWrite (MotorA1, HIGH);                              
   digitalWrite (MotorA2, LOW); 
   digitalWrite (MotorB1, HIGH); 
   digitalWrite (MotorB2, LOW);
}

void backwards(){
  digitalWrite (MotorA1 , LOW);                              
  digitalWrite (MotorA2, HIGH); 
  digitalWrite (MotorB1, LOW); 
  digitalWrite (MotorB2, HIGH);
}

int watch(){
  long howfar;
  digitalWrite(trigger,LOW);
  delayMicroseconds(5);                                                                              
  digitalWrite(trigger,HIGH);
  delayMicroseconds(15);
  digitalWrite(trigger,LOW);
  howfar=pulseIn(echo,HIGH);
  howfar=howfar*0.01657; //how far away is the object in cm
  return round(howfar);
}

void turnleft(int t){
  digitalWrite (MotorA1, LOW);                              
  digitalWrite (MotorA2, HIGH); 
  digitalWrite (MotorB1, HIGH); 
  digitalWrite (MotorB2, LOW);
  delay(t);
}

void turnright(int t){
  digitalWrite (MotorA1, HIGH);                              
  digitalWrite (MotorA2, LOW); 
  digitalWrite (MotorB1, LOW); 
  digitalWrite (MotorB2, HIGH);
  delay(t);
}  

void stopmove(){
  digitalWrite (MotorA1 ,LOW);                              
  digitalWrite (MotorA2, LOW); 
  digitalWrite (MotorB1, LOW); 
  digitalWrite (MotorB2, LOW);
}  

void watchsurrounding(){ //Meassures distances to the right, left, front, left diagonal, right diagonal and asign them in cm to the variables rightscanval, 
                         //leftscanval, centerscanval, ldiagonalscanval and rdiagonalscanval (there are 5 points for distance testing)
  centerscanval = watch();
  if(centerscanval<distancelimit){stopmove();}
  head.write(120);
  delay(100);
  ldiagonalscanval = watch();
  if(ldiagonalscanval<distancelimit){stopmove();}
  head.write(160); //Didn't use 180 degrees because my servo is not able to take this angle
  delay(300);
  leftscanval = watch();
  if(leftscanval<sidedistancelimit){stopmove();}
  head.write(120);
  delay(100);
  ldiagonalscanval = watch();
  if(ldiagonalscanval<distancelimit){stopmove();}
  head.write(80); //I used 80 degrees because its the central angle of my 160 degrees span (use 90 degrees if you are moving your servo through the whole 180 degrees)
  delay(100);
  centerscanval = watch();
  if(centerscanval<distancelimit){stopmove();}
  head.write(40);
  delay(100);
  rdiagonalscanval = watch();
  if(rdiagonalscanval<distancelimit){stopmove();}
  head.write(0);
  delay(100);
  rightscanval = watch();
  if(rightscanval<sidedistancelimit){stopmove();}

  head.write(80); //Finish looking around (look forward again)
  delay(300);
}

char decide(){
  watchsurrounding();
  if (leftscanval>rightscanval && leftscanval>centerscanval){
    choice = 'l';
  }
  else if (rightscanval>leftscanval && rightscanval>centerscanval){
    choice = 'r';
  }
  else{
    choice = 'f';
  }
  return choice;
}

void translateIR() { //Used when robot is switched to operate in remote control mode
  switch(results.value)
  {
  case 0xFF629D: //Case 'FORWARD'
    go();
    break;
  case 0xFF22DD: //Case 'LEFT'
    turnleft(turntime); 
    stopmove();  
    break;
  case 0xFF02FD: //Case 'OK'
    stopmove();   
    break;
  case 0xFFC23D: //Case 'RIGHT'
    turnright(turntime);
    stopmove(); 
    break;
  case 0xFFA857: //Case 'REVERSE'
    backwards();
    break;
  case 0xFF42BD:  //Case '*'
    modecontrol=0; stopmove(); // If an '*' is received, switch to automatic robot operating mode
    break;
  default: 
    ;
  }// End Case
  delay(500); // Do not get immediate repeat

void loop(){
  
  if (irrecv.decode(&results)){ //Check if the remote control is sending a signal
    if(results.value==0xFF6897){ //If an '1' is received, turn on robot
      power=1; }
    if(results.value==0xFF4AB5){ //If a '0' is received, turn off robot
      stopmove();
      power=0; }
    if(results.value==0xFF42BD){ //If an '*' is received, switch operating mode from automatic robot to remote control (press also "*" to return to automatic robot mode)
      modecontrol=1; //  Activate remote control operating mode
      stopmove(); //The robot stops and starts responding to the user's directions
    }
    irrecv.resume(); // receive the next value
  }
  
  while(modecontrol==1){ //The system gets into this loop during the remote control mode until modecontrol=0 (with '*')
    if (irrecv.decode(&results)){ //If something is being received
      translateIR();//Do something depending on the signal received
      irrecv.resume(); // receive the next value
     }
  }
  if(power==1){
  go();  // if nothing is wrong go forward using go() function above.
  ++numcycles;
  if(numcycles>130){ //Watch if something is around every 130 loops while moving forward 
    watchsurrounding();
    if(leftscanval<sidedistancelimit || ldiagonalscanval<distancelimit){
      turnright(turntime);
    }
    if(rightscanval<sidedistancelimit || rdiagonalscanval<distancelimit){
      turnleft(turntime);
    }
    numcycles=0; //Restart count of cycles
  }
  distance = watch(); // use the watch() function to see if anything is ahead (when the robot is just moving forward and not looking around it will test the distance in front)
  if (distance<distancelimit){ // The robot will just stop if it is completely sure there's an obstacle ahead (must test 25 times) (needed to ignore ultrasonic sensor's false signals)
      ++thereis;}
  if (distance>distancelimit){
      thereis=0;} //Count is restarted
  if (thereis > 25){
    stopmove(); // Since something is ahead, stop moving.
    turndirection = decide(); //Decide which direction to turn.
    switch (turndirection){
      case 'l':
        turnleft(turntime);
        break;
      case 'r':
        turnright(turntime);
        break;
      case 'f':
        ; //Do not turn if there was actually nothing ahead
        break;
    }
    thereis=0;
  }
 }
}

ทดสอบการทำงาน ที่รีโมท ด้วยการกด 0 และตามด้วย กด 1 ถ้าต่อวงจรได้ถูกต้อง หุ่นยนต์หลบสิ่งกีดขวาง Arduino เวอร์ชั่น 2 ของเราจะเริ่มทำงานครับ


วีดีโอผลลัพธ์การทำงานของ หุ่นยนต์หลบสิ่งกีดขวาง Arduino เวอร์ชั่น 2 




code โปรเจค หุ่นยนต์หลบสิ่งกีดขวาง Arduino V1


หุ่นยนต์หลบสิ่งกีดขวาง  V1

หุ่นยนต์หลบสิ่งกีดขวาง อุปกรณ์ที่ต้องใช้ก็คือ

     1. 4WD Smart Robot Car Chassis Kits

     2. Arduino UNO R3 - Made in italy

     3. Arduino Sensor Shield V5.0

     4. เซนเซอร์ Ultrasonic Module HC-SR04

     5. Mounting Bracket for HC-SR04 Ultrasonic Module แบบสั้น

     6. Motor Drive Module L298N

     7. สาย Jumper Female to Male ยาว 20cm.

     8. สาย Jumper Femalet to Female ยาว 20cm.

     9. รางถ่านแบบ 18650 ใส่ถ่าน 2 ก้อน

     10. แบตเตอรี่ลิเธียม 18650 จำนวน 2 ก้อน

     11. เสารองแผ่นพีซีบีโลหะแบบเหลี่ยม 6 mm 12 ชิ้น

     12. สกรูหัวกลมน็อตตัวเมีย ขนาด 3มม ยาว12มม. 10 ตัว

เริ่มด้วย ต่อสายไฟสำหรับมอเตอร์ ทั้ง 4 ตัว ที่ 4WD smart car chassis ให้มา โดยให้ ขั้วลบ สายไฟสีดำ อยู่ด้านบน เหมือนกันทั้ง 4 ตัว


ประกอบเข้ากับ โครง ชิ้นที่ 1 ของ 4WD smart car chassis ดังรูป



ประกอบ น็อต 6 จุด สำหรับ วาง โครง ชิ้นที่ 2


ประกอบ บอร์ด Motor Drive Module L298N ลงที่ โครง ชิ้นที่ 1


เชื่อมสายไฟ ของ มอเตอร์ ด้านบน สายสีดำ ขั้ว -  ของมอเตอร์ ตัวที่1 เข้า กับมอเตอร์ ตัวที่2 และ สายสีแดง ขั้ว+  ของมอเตอร์ ตัวที่1 เข้า กับมอเตอร์ ตัวที่2 และ เช่นกันทำที่ มอเตอร์ คู่ด้านล่างด้วย เพื่อให้เหมือนมีมอเตอร์ จาก 4 ตัว เหลือ 2 ตัว

จากนั้น ประกอบสายไฟ ของมอเตอร์ ทั้ง 2 ตัว เข้ากับ บอร์ด L298N ดังรูป



ประกอบ โครงชิ้นที่ 2 เข้าที่ ด้านบนของ โครงชิ้นที่ 1 และ ประกอบ บอร์ด Arduino UNO R3 ลงที่โครงชิ้นที่ 2 ซึ่งอยู่ชั้นบน


ประกอบ บอร์ด Arduino Sensor Shield V5.0 ลงที่ บอร์ด Arduino UNO R3



ประกอบ รางถ่านแบบ 18650 และ เชื่อมต่อ วงจรตามรูป



หมายเหตุ : ถ้ามี Jumper อยู่ที่ขา ENA และ ENB ของ บอร์ด L298N  ให้ถอดออก

การเชื่อมต่อระหว่าง บอร์ด Arduino Sensor Shield V5.0 กับ บอร์ด L298N  


.เชื่อมต่อ เซ็นเซอร์ HC-SR04 กับ Sensor Shield V5.0 ดังรูป



ประกอบ HC-SR04  เข้ากับ Mounting Bracket for HC-SR04 Ultrasonic Module แบบสั้น และ ประกอบ Bracket  เข้ากลับ โครงรถ2 ตามรูป





และทดสอบการเชื่อมต่อว่าถูกต้องหรือไม่ อุปกรณ์ใช้งานได้หรือไม่ ตามตัวอย่าง ลิงค์ด้านล่าง

http://robotsiam.blogspot.com/2016/09/hc-sr04-sensor-shield-v50.html


หลังจากนั้นให้ทดสอบการเชื่อมต่อ ระหว่าง บอร์ด Arduino Sensor Shield V5.0 กับ บอร์ด L298N  และ การหมุนของล้อถูกต้องหรือไม่ โดย

เปิดโปรแกรม Arduino (IDE) และ Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3



// Motor A pins (enableA = enable motor, pinA1 = forward, pinA2 = backward)
int enableA = 3;
int pinA1 = 6;
int pinA2 = 7;

//Motor B pins (enabledB = enable motor, pinB2 = forward, pinB2 = backward)
int enableB = 5;
int pinB1 = 8;
int pinB2 = 9;

//This lets you run the loop a single time for testing
boolean run = true;

void setup() {
 pinMode(enableA, OUTPUT);
 pinMode(pinA1, OUTPUT);
 pinMode(pinA2, OUTPUT);

 pinMode(enableB, OUTPUT);
 pinMode(pinB1, OUTPUT);
 pinMode(pinB2, OUTPUT);
}
void loop() {
  if (run) {
    delay(2000);
    enableMotors();
    //Go forward
    forward(200);
    //Go backward
    backward(200);
    //Turn left
    turnLeft(400);
    coast(200);
    //Turn right
    turnRight(400);
    coast(200);
    //This stops the loop
    run = false; 
  }
}

//Define high-level H-bridge commands

void enableMotors()
{
 motorAOn();
 motorBOn();
}

void disableMotors()
{
 motorAOff();
 motorBOff();
}

void forward(int time)
{
 motorAForward();
 motorBForward();
 delay(time);
}

void backward(int time)
{
 motorABackward();
 motorBBackward();
 delay(time);
}

void turnLeft(int time)
{
 motorABackward();
 motorBForward();
 delay(time);
}

void turnRight(int time)
{
 motorAForward();
 motorBBackward();
 delay(time);
}

void coast(int time)
{
 motorACoast();
 motorBCoast();
 delay(time);
}

void brake(int time)
{
 motorABrake();
 motorBBrake();
 delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
 digitalWrite(enableA, HIGH);
}

void motorBOn()
{
 digitalWrite(enableB, HIGH);
}

 //disable motors
void motorAOff()
{
 digitalWrite(enableB, LOW);
}

void motorBOff()
{
 digitalWrite(enableA, LOW);
}

 //motor A controls
void motorAForward()
{
 digitalWrite(pinA1, HIGH);
 digitalWrite(pinA2, LOW);
}

void motorABackward()
{
 digitalWrite(pinA1, LOW);
 digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
 digitalWrite(pinB1, HIGH);
 digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
 digitalWrite(pinB1, LOW);
 digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
 digitalWrite(pinA1, LOW);
 digitalWrite(pinA2, LOW);
}

void motorABrake()
{
 digitalWrite(pinA1, HIGH);
 digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
 digitalWrite(pinB1, LOW);
 digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
 digitalWrite(pinB1, HIGH);
 digitalWrite(pinB2, HIGH);
}

ถอดสาย USB ระหว่าง Arduino กับ คอมพิวเตอร์ออก และ หาอุปกรณ์ที่สามารถยกตัวรถ 4WD smart car chassis ขึ้นแล้ว ล้อไม่แตะพืน เพือทดสอบการหมุนของล้อว่าถูกต้องหรือไม่



ใส่ แบตเตอรี่ลิเธียม 18650 จำนวน 2 ก้อน ไปที่ รางถ่าน และ ตรวจสอบขั้วของแบตเตอรี่ ใส่ถุกต้องหรือไม่



โปรแกรมนี้จะทำงานเพียง 1 ครั้ง ถ้าต้องการทดลองใหม่ให้ถอด แบตเตอรี่ออก (หรือ ปิดเปิด สวิทช์ไฟใหม่) แล้วใส่เข้าไปใหม่ เมื่อล้อหมุน ตรวจสอบการหมุน ขอล้อต่างๆถูกต้องหรือไม่ ถ้าต่อวงจรถูกต้อง ล้อ ทั้งสองข้างจะหมุนไปข้างหน้า 1ครั้ง กลับหลัง 1 ครั้ง และ เดินหน้าอีกหนึ่งครั้งแล้วจึงหยุด ถ้าไม่ถูกต้องให้แก้ไข เช่นการต่อขั้วของมอเตอร์ผิด เป็นต้น

ถ้าทุกอย่างถูกต้อง ทดลอง ยกลงวางที่พื้นแล้วทดสอบ อีกครั้ง ถอด แบตเตอรี่ออก (หรือ ปิดเปิด สวิทช์ไฟใหม่) แล้วใส่เข้าไปใหม่ ถ้าทุกอย่างถูกต้อง รถจะเครื่อนไปข้างหน้า แล้ว เลี้ยวขวา แล้ว จึงกลับสู่ตำแหน่งเดิม

ทดสอบ หุ่นยนต์หลบสิ่งกีดขวาง ได้โดย  Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3

#include <NewPing.h>

//Tell the Arduino where the sensor is hooked up
NewPing sonar(10, 11);

int enableA = 3;
int pinA1 = 6;
int pinA2 = 7;

int enableB = 5;
int pinB1 = 8;
int pinB2 = 9;

long inches;

void setup() {
pinMode(enableA, OUTPUT);
pinMode(pinA1, OUTPUT);
pinMode(pinA2, OUTPUT);

pinMode(enableB, OUTPUT);
pinMode(pinB1, OUTPUT);
pinMode(pinB2, OUTPUT);
}

void loop() {

//Run the motors at slightly less than full power
analogWrite(enableA, 200);
analogWrite(enableB, 200);

//Ping the sensor and determine the distance in inches
inches = sonar.ping_in();

//If the robot detects an obstacle less than four inches away, it will back up, then turn left; if no obstacle is detected, it will go forward
if (inches < 4) {
analogWrite(enableA, 255);
analogWrite(enableB, 255);
backward(600);
coast(200);
turnLeft(600);
coast(200);}
else {
forward(1);

}
}

//Define high-level H-bridge commands

void enableMotors()
{
motorAOn();
motorBOn();
}

void disableMotors()
{
motorAOff();
motorBOff();
}

void forward(int time)
{
motorAForward();
motorBForward();
delay(time);
}

void backward(int time)
{
motorABackward();
motorBBackward();
delay(time);
}

void turnLeft(int time)
{
motorABackward();
motorBForward();
delay(time);
}

void turnRight(int time)
{
motorAForward();
motorBBackward();
delay(time);
}

void coast(int time)
{
motorACoast();
motorBCoast();
delay(time);
}

void brake(int time)
{
motorABrake();
motorBBrake();
delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
digitalWrite(enableA, HIGH);
}

void motorBOn()
{
digitalWrite(enableB, HIGH);
}

//disable motors
void motorAOff()
{
digitalWrite(enableB, LOW);
}

void motorBOff()
{
digitalWrite(enableA, LOW);
}

//motor A controls
void motorAForward()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, LOW);
}

void motorABackward()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
digitalWrite(pinA1, LOW);
digitalWrite(pinA2, LOW);
}

void motorABrake()
{
digitalWrite(pinA1, HIGH);
digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
digitalWrite(pinB1, LOW);
digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
digitalWrite(pinB1, HIGH);
digitalWrite(pinB2, HIGH);
}


ทดลอง ยกลงวางที่พื้นแล้ว หาสิ่งกีดขวาง ทดสอบ อีกครั้ง จากรถจะกลายเป็น "หุ่นยนต์หลบสิ่งกีดขวาง" ทำงานได้ดีพอสมควรครับ


By : http://www.robotsiam.com/

Code โปรเจค Arduino เปิดปิดไฟ ด้วย Wireless Joystick

PS2 Joystick Playstation Adapter for Arduino อะแดปเตอร์แปลงหัว PS2 เป็นขาต่อแบบ DIP สำหรับ Arduino คอนเนคเตอร์สำหรับแปลงจาก PS2 เป็นขา DI...