หน้าแรก ดูโปรเจกต์ทั้งหมด
Easy

โปรเจกต์ Obstacle Avoiding Robot

หุ่นยนต์หลบหลีกสิ่งกีดขวางตัวนี้ หรือเรียกอีกอย่างว่า NT1 จะหลบหลีกสิ่งกีดขวางที่อยู่ข้างหน้า โดยใช้ ultrasonic ranging sensor รุ่น HC-SR04

โปรเจกต์ Obstacle Avoiding Robot

วิดีโอสาธิต

Video

▶ กดเพื่อดูวิดีโอสาธิตโปรเจกต์

รายการอุปกรณ์และเครื่องมือ

2x DC motors with Wheels
-
1x Arduino UNO
-
1x L298N Motor Drive
-
1x HC-SR04 Ultrasonic Sensor
-
1x 9V Battery Clip Connector
-
1x Universal Wheel
-
1x SG90 Micro Servo Motor
-
2x 9V Batteries
-
1x Mini breadboard
-
1x 9V Battery Clip to Arduino Connector
-
1x F to F Jumpers and M to M Jumpers
-

รายละเอียดและวิธีทำ

หุ่นยนต์หลบหลีกสิ่งกีดขวางนี้ หรือที่รู้จักกันในชื่อ NT1 ทำหน้าที่หลบสิ่งกีดขวางในเส้นทางโดยใช้ Sensor วัดระยะแบบ Ultrasonic รุ่น HC-SR04 โดย Sensor นี้ช่วยให้หุ่นยนต์สามารถตรวจจับวัตถุที่อยู่ข้างหน้าได้ และผมได้ใช้ L298N H-Bridge เพื่อควบคุมล้อของหุ่นยนต์ เมื่อตรวจพบวัตถุในระยะใกล้ หุ่นยนต์จะหยุด ถอยหลัง และมองหาเส้นทางที่ว่างด้วยความช่วยเหลือจาก Sensor วัดระยะ ทั้งนี้ค่าพารามิเตอร์ของระยะทางและการตรวจจับทั้งหมดสามารถปรับเปลี่ยนได้ใน Code ของ Arduino เพื่อให้หุ่นยนต์ทำงานได้หลากหลายมากขึ้น

Key explanations:

L298N H-Bridge

  • ช่วยให้สามารถควบคุมความเร็วและทิศทางของ DC motors สองตัวได้พร้อมกัน
  • สามารถควบคุม DC motors ที่มีแรงดันไฟฟ้าตั้งแต่ 5 ถึง 35 V
  • มี Regulator 5 V ในตัวซึ่งสามารถใช้เป็น input หรือ output ได้ หาก VCC < 12 V สามารถเปิดใช้งาน Regulator 5 V เพื่อใช้เป็น output สำหรับจ่ายไฟให้บอร์ด Arduino แต่หาก VCC > 12 V แนะนำว่าไม่ควรใช้ Regulator 5 V เนื่องจาก VCC อาจทำให้มันเสียหายได้
  • มีแรงดันตกคร่อม 2V ที่เกิดจาก IC (integrated circuit) ดังนั้นแรงดันที่ขั้วมอเตอร์จะเป็น 10 V ซึ่งหมายความว่ามอเตอร์จะไม่สามารถทำงานได้ที่ความเร็วสูงสุด
  • Pin Enable A และ Enable B ใช้เพื่อควบคุมความเร็วของมอเตอร์
  • Pin Input 1, 2, 3 และ 4 ใช้ควบคุมสวิตช์ของ H-Bridge โดย Input 1 และ 2 ควบคุมมอเตอร์ตัวหนึ่ง และ Input 3 และ 4 ควบคุมมอเตอร์อีกตัวหนึ่ง
  • สวิตช์จะเปิดและปิดตาม Logic gates ตัวอย่างเช่น ถ้า Input 1 เป็น Low และ Input 2 เป็น High มอเตอร์จะหมุนไปข้างหน้า และในทางกลับกัน

Ultrasonic Sensor HC-SR04

  • ใช้ Sonar เพื่อระบุระยะทางถึงวัตถุ
  • มี 4 Pin ได้แก่ VCC, TRIG, ECHO และ GND
  • ตัวส่ง (Pin TRIG) จะส่งสัญญาณ เมื่อสัญญาณพบวัตถุ สัญญาณจะสะท้อนกลับและรับโดยตัวรับ (Pin ECHO)
  • Pin TRIG ต้องอยู่ในสถานะ High เป็นเวลา 10 us เพื่อส่งคลื่น Ultrasonic (40 kHz 8 cycle sonic bursts)
  • ความเร็วของคลื่น Ultrasonic คือ 340 m/s ในอากาศ
  • สูตรที่ใช้:
  • หากมีวัตถุอยู่ที่ระยะ 10 cm จาก Sensor การวัดเวลาที่คลื่นเสียงจะเดินทางจาก Pin TRIG ไปยังวัตถุ จะต้องใช้สูตรดังนี้:
  • t = d/v = time=distance/velocity = (10 cm)/(0.034 cm/s) = 294 us โดย Pin ECHO จะวัดเวลาเป็นสองเท่าเนื่องจากเป็นการวัดเวลาทั้งไปและกลับจากวัตถุ
  • หากคลื่นใช้เวลา 100us ในการสะท้อนกลับจากวัตถุ เราจะใช้สูตรต่อไปนี้เพื่อคำนวณระยะทางไปยังวัตถุ:
  • distance = time * (0.034 cm/s) / 2 = 100 us * (0.034 cm/s )/2 = 1.7 cm

Components:

NT1 - Components

3D Printed Parts:

ในการออกแบบฐานที่พิมพ์จาก 3D printer ผมได้ใช้ Fusion 360 และพิมพ์โมเดลออกมาด้วย 3D printer ของผมเอง

3D Printed Bed

ไฟล์ STL อยู่บนเว็บไซต์ของผม ผมไม่สามารถโพสต์ไว้ที่นี่ได้ด้วยเหตุผลบางประการ

https://baltmaker.com/obstacle-avoiding-robot/

Fritzing Diagram:

Fritzing Diagram สำหรับ NT1

Step by Step Building Gallery:

  • ติดตั้งและทากาวล้ออิสระ (Universal wheel) ไว้ที่ใต้ฐานหุ่นยนต์
  • ติดตั้งส่วนประกอบที่เหลือไว้ที่อีกด้านหนึ่งของฐาน
  • ต่อล้อเข้ากับ DC motors
  • ติดตั้ง Sensor HC-SR04 เข้ากับขายึดและต่อเข้ากับ Servo motor

Arduino Code:

ก่อนอื่นเราต้องรวม Library NewPing เข้ามา โดยทำตามขั้นตอนดังนี้:

  • ดาวน์โหลด NewPing Library จากลิงก์ด้านล่าง:

https://playground.arduino.cc/Code/NewPing/

ย้าย Folder NewPing ไปยัง Folder libraries ซึ่งอยู่ภายใน Folder Arduino ในเครื่องคอมพิวเตอร์ของคุณ

#include <Servo.h> //Include the servomotorlibrary

#include <NewPing.h> //This library needs to be downloaded and included so that we can use the HC-SR04 sensor

//L298N (Motor driver)control pins

const int LeftMotorFwd = 7;

const int LeftMotorBwd = 6;

const int RightMotorFwd = 4;

const int RightMotorBwd = 5;

//ultrasonic sensor pins

#define trig_pin A1 //analog input 1

#define echo_pin A2 //analog input 2

#define maximum_distance 200

boolean goesFwd = false;

int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function

Servo servo_motor; //our servo name

void setup(){

pinMode(RightMotorFwd, OUTPUT);

pinMode(LeftMotorFwd, OUTPUT);

pinMode(LeftMotorBwd, OUTPUT);

pinMode(RightMotorBwd, OUTPUT);

servo_motor.attach(10); //our servo pin

servo_motor.write(115);

delay(2000);

distance = readPing();

delay(100);

distance = readPing();

delay(100);

distance = readPing();

delay(100);

distance = readPing();

delay(100);

}

void loop(){

int distanceRight = 0;

int distanceLeft = 0;

delay(50);

if (distance <= 20){

moveStop();

delay(300);

moveBackward();

delay(400);

moveStop();

delay(300);

distanceRight = lookRight();

delay(300);

distanceLeft = lookLeft();

delay(300);

if (distance >= distanceLeft){

turnRight();

moveStop();

}

else{

turnLeft();

moveStop();

}

}

else{

moveForward();

}

distance = readPing();

}

int lookRight(){

servo_motor.write(50);

delay(500);

int distance = readPing();

delay(100);

servo_motor.write(115);

return distance;

}

int lookLeft(){

servo_motor.write(170);

delay(500);

int distance = readPing();

delay(100);

servo_motor.write(115);

return distance;

delay(100);

}

int readPing(){

delay(70);

int cm = sonar.ping_cm();

if (cm==0){

cm=250;

}

return cm;

}

void moveStop(){

digitalWrite(RightMotorFwd, LOW);

digitalWrite(LeftMotorFwd, LOW);

digitalWrite(RightMotorBwd, LOW);

digitalWrite(LeftMotorBwd, LOW);

}

void moveForward(){

if(!goesForward){

goesForward=true;

digitalWrite(LeftMotorFwd, HIGH);

digitalWrite(RightMotorFwd, HIGH);

digitalWrite(LeftMotorBwd, LOW);

digitalWrite(RightMotorBwd, LOW);

}

}

void moveBackward(){

goesForward=false;

digitalWrite(LeftMotorBwd, HIGH);

digitalWrite(RightMotorBwd, HIGH);

digitalWrite(LeftMotorFwd, LOW);

digitalWrite(RightMotorFwd, LOW);

}

void turnRight(){

digitalWrite(LeftMotorFwd, HIGH);

digitalWrite(RightMotorBwd, HIGH);

digitalWrite(LeftMotorBwd, LOW);

digitalWrite(RightMotorFwd, LOW);

delay(500);

digitalWrite(LeftMotorFwd, HIGH);

digitalWrite(RightMotorFwd, HIGH);

digitalWrite(LeftMotorBwd, LOW);

digitalWrite(RightMotorBwd, LOW);

}

void turnLeft(){

digitalWrite(LeftMotorBwd, HIGH);

digitalWrite(RightMotorFwd, HIGH);

digitalWrite(LeftMotorFwd, LOW);

digitalWrite(RightMotorBwd, LOW);

delay(500);

digitalWrite(LeftMotorFwd, HIGH);

digitalWrite(RightMotorFwd, HIGH);

digitalWrite(LeftMotorBwd, LOW);

digitalWrite(RightMotorBwd, LOW);

}

Demo:

Code

🔒 ปลดล็อก Code

สนับสนุนเพื่อรับ Source Code หรือแอปพลิเคชันสำหรับโปรเจกต์นี้

รหัสอ้างอิงโปรเจกต์: obstacle-avoiding-robot-74afc1
299 บาท
PromptPay QR Code