14.
เซอร์โวมอเตอร์ MG996R พร้อมกับอุปกรณ์เสริม 15.
Ultrasonic Sensor HC-SR04 16.
Mounting Bracket for HC-SR04 แบบสั้น 17.
Tilt U Bracket 18.
Jack 5.5 X 2.1mm สำหรับ เพาเวอร์ซัพพลาย ของ Arduino 19.
สายไฟแดงดำ ขนาด 22AWG เริ่มด้วย ต่อสายไฟสำหรับมอเตอร์ ทั้ง 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
*** VCC ของ Arduino Sensor Shield V5.0 คือ V ***
หลังจากนั้นให้ทดสอบเบื้องต้น ว่าการหมุนของล้อถูกต้องหรือไม่ โดย
เปิดโปรแกรม 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 ครั้ง และ เดินหน้าอีกหนึ่งครั้งแล้วจึงหยุด ถ้าไม่ถูกต้องให้แก้ไข เช่นการต่อขั้วของมอเตอร์ผิด เป็นต้น
ถ้าทุกอย่างถูกต้อง ทดลอง ยกลงวางที่พื้นแล้วทดสอบ อีกครั้ง ถอด แบตเตอรี่ออก (หรือ ปิดเปิด สวิทช์ไฟใหม่) แล้วใส่เข้าไปใหม่ ถ้าทุกอย่างถูกต้อง รถจะเคลื่อนที่ไปข้างหน้า-ถอยหลัง แล้ว เลี้ยวซ้าย แล้ว จึงกลับสู่ตำแหน่งเดิม ประกอบ Jack 5.5 X 2.1mm เข้ากับ เพาเวอร์ซัพพลาย ของ Arduino
โดย ขั้วบวก สายสีแดง เข้ากับ ไฟ+ สายสีแดง จาก รางถ่านแบบ 18650 หลัง ผ่านเพาเวอร์สวิตซ์สำหรับเปิดปิด (ON / OFF Rocker Switch) และ ขั้วลบ สีดำ เข้ากับ กราวด์ (G)
จากนั้นประกอบ เซอร์โวมอเตอร์ MG996R เข้ากับ บอร์ด Arduino Sensor Shield V5.0
โดยให้ สายสีส้ม -> D10 , สายสีแดง -> V , สายสีแดง , สายสีน้ำตาล -> G
ใช้สกรูหัวกลม+น็อตตัวเมีย ขนาด 3มม ยาว 40มม. 4 ตัว ยึด เซอร์โวมอเตอร์ MG996R เข้ากับ ด้านหน้าของหุ่นยนต์
ปรับองศา ของ เซอร์โวมอเตอร์ MG996R = 90 องศา โดย Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3
#include <Servo.h>
Servo myservo; //ประกาศตัวแปร myservo เรียกใช้งานคำสั่งจาก Library
void setup()
{
myservo.attach(10); //ให้ Digital Pin 10 เป็นขาของ Servo
}
void loop()
{
myservo.write(90); //หมุน 90 องศา
delay(1000);
}
แล้วจึงยึดอุปกรณ์เสริม ของเซอร์โวมอเตอร์ MG996R
ประกอบ Ultrasonic Sensor HC-SR04 เข้ากับ Mounting Bracket for HC-SR04 แบบสั้น
ใช้กาว ยึด Mounting Bracket for HC-SR04 แบบสั้น เข้า กับ Tilt U Bracket
จากนั้นประกอบ Ultrasonic Sensor HC-SR04 เข้ากับ บอร์ด Arduino Sensor Shield V5.0
โดยให้ TRIGGER -> A0 , ECHO -> A1 , VCC -> V , GND -> G
.ใช้สาย Jumper Female to Female ยาว 30cm ต่อจาก Arduino Sensor Shield V5.0 เข้า Ultrasonic Sensor HC-SR04
ยึด Tilt U Bracket เข้ากับ อุปกรณ์เสริม ของเซอร์โวมอเตอร์ MG996R
จากนั้นให้ทำการเพิ่ม ไลบรารี่ NewPing ให้กับ Arduino (IDE) ของเรา ตามรูป
เลือกไฟล์ ไลบรารี่ NewPing .ZIP ที่เรา ดาวน์โหลดมาอยู่ในคอมพิวเตอร์ของเรา จากนั้นคลิก Open โปรแกรม จะเพิ่ม ไลบรารี่ NewPing เข้าสู่ Arduino (IDE) ของเรา
เมื่อเข้าดูที่ Include Library จะพบ ไลบรารี่ NewPing เพิ่มเข้ามาด้านล่างสุด (สามารถใช้วิธีการเดียวกันนี้ ในการ การเพิ่ม ไลบรารี่ ฟังก์ชัน อื่นๆ ได้เช่นกัน )
ภาพรวมการต่อ หุ่นยนต์ติดตามวัตถุ Ultrasonic
จากนั้นทดสอบ หุ่นยนต์ติดตามวัตถุ Ultrasonic ได้โดย Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3
#include <Servo.h>
#include <NewPing.h>
// 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;
#define TRIGGER_PIN A0 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN A1 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 25 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 85;
int distance = 30;
int stepsize = 5;
int check_pos = 1;
int detect = 0;
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
pinMode(enableA, OUTPUT);
pinMode(pinA1, OUTPUT);
pinMode(pinA2, OUTPUT);
pinMode(enableB, OUTPUT);
pinMode(pinB1, OUTPUT);
pinMode(pinB2, OUTPUT);
myservo.attach(10); // attaches the servo on pin 10 to the servo object
myservo.write(pos);
}
void loop() {
enableMotors();
analogWrite(enableA, 150); // ปรับความเร็วหุ่นยนต์
analogWrite(enableB, 120); // ปรับความเร็วหุ่นยนต์
delay(30); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping();
// Send ping, get ping time in microseconds (uS).
distance = uS / US_ROUNDTRIP_CM;
if ((distance > 1) && (distance <= 20)) {
if (pos >= 80 && pos <= 110) {
forward(1);
}
if (pos > 1 && pos < 80) {
turnRight(100);
forward(1);
}
if (pos > 110 && pos <= 180) {
turnLeft(100);
forward(1);
}
}
else {
coast(2);
if (pos >= 179) {
check_pos = 0;
}
if (pos <= 2) {
check_pos = 1;
}
if (check_pos == 1 && pos < 179) {
pos++;
myservo.write(pos);
}
if (check_pos == 0 && pos > 2) {
pos--;
myservo.write(pos);
}
}
Serial.print(check_pos);
Serial.print(", ");
Serial.print(pos);
Serial.print(", ");
Serial.println(distance);
}
//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);
}
วีดีโอผลลัพธ์การทำงานของ โปรเจค หุ่นยนต์ติดตามวัตถุ Ultrasonic
VIDEO