12.
สายไฟแดงดำ ขนาด 22AWG 13.
Wireless Joystick Playstation PS2 Controller 14.
PS2 Joystick Converter Adapter
เริ่มด้วย ต่อสายไฟสำหรับมอเตอร์ ทั้ง 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 ครั้ง และ เดินหน้าอีกหนึ่งครั้งแล้วจึงหยุด ถ้าไม่ถูกต้องให้แก้ไข เช่นการต่อขั้วของมอเตอร์ผิด เป็นต้น
ถ้าทุกอย่างถูกต้อง ทดลอง ยกลงวางที่พื้นแล้วทดสอบ อีกครั้ง ถอด แบตเตอรี่ออก (หรือ ปิดเปิด สวิทช์ไฟใหม่) แล้วใส่เข้าไปใหม่ ถ้าทุกอย่างถูกต้อง รถจะเคลื่อนที่ไปข้างหน้า-ถอยหลัง แล้ว เลี้ยวซ้าย แล้ว จึงกลับสู่ตำแหน่งเดิม จากนั้น ยึด PS2 Adapter
วิธีที่ 1. ต่อบอร์ด Arduino กับ PS2 Adapter
PS2 Adapter Arduino Board
DAT D 13
CMD D 11
GND GND
+5V +5V
SEL D 10
CLK D 12
วิธีที่ 2. ต่อบอร์ด Arduino กับ PS2 PS3 Adapter
PS2 PS3 Adapter Arduino Board
+5V +5V
DATA D 13
CMD D 11
CLK D 12
ATT D 10
GND GND
.
ประกอบ Receiver เข้า กับ อะแดปเตอร์แปลงหัว
การเพิ่มไลบรารี่ PS2X
ไลบรารี่ PS2X เป็น ไลบรารี่ ฟังก์ชัน ที่มีผู้พัฒนาเตรียมพร้อมไว้ให้เราแล้ว โดยให้ไปดาวน์โหลด ไลบรารี่ PS2X ได้ที่
Download library
หรือ
Unzip ไฟล์ที่ดาวน์โหลดมา PS2X_lib และนำไปเก็บไว้ที Library ของโปรแกรม Arduino
Upload โค้ด โปรเจค รถบังคับ ด้วย Wireless Joystick PS2 ไปยัง บอร์ด Arduino UNO R3
/* * By : RobotSiam.com */ #include <PS2X_lib.h> // เรียกใช้งานไลบรารีสำหรับ PS2 Controller #define PS2_DAT 13 // กำหนดขา Data เป็นขา 13 #define PS2_CMD 11 // กำหนดขา Command เป็นขา 11 #define PS2_SEL 10 // กำหนดขา Select เป็นขา 10 #define PS2_CLK 12 // กำหนดขา Clock เป็นขา 12 PS2X ps2x; // ประกาศตัวแปรสำหรับ PS2 Controller // กำหนดขาการต่อมอเตอร์ A int enableA = 3; int pinA1 = 6; int pinA2 = 7; // กำหนดขาการต่อมอเตอร์ B int enableB = 5; int pinB1 = 8; int pinB2 = 9; void setup() { delay(1000); // หน่วงเวลา 1 วินาทีเพื่อรอให้บอร์ดพร้อมทำงาน Serial.begin(57600); Serial.println("Connecting"); // แสดงข้อความเพื่อให้รู้ว่ากำลังทำการเชื่อมต่อกับ PS2 Controller while (true) // วนการทำงานเพื่อรอการเชื่อมต่อกับ PS2 Controller { // กำหนดขาที่จะเชื่อมต่กับ PS2 Controller โดยมีการเก็บค่าที่ส่งกลับมาเป็น Integer เพื่อรู้ได้ว่าเชื่อมต่อได้หรือไม่ int error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, false, false); if (error == 0) // กรณีที่เชื่อมต่อได้ ไม่มีปัญหาอะไร (Error = 0) { Serial.println("OK"); // แสดงข้อความว่าเชื่อมต่อกับ PS2 Controller เรียบร้อยแล้ว delay(1000); // หน่วงเวลา 1 วินาที break; // ออกจาก while(true) } delay(50); // หน่วงเวลา 50 มิลลิวินาทีเพื่อรอการเชื่อมต่อครั้งต่อไปในกรณีที่เชื่อมต่อไม่สำเร็จ } // กำหนดขาที่ควบคุมมอเตอร์ ให้เป็นขา เอาท์พุท pinMode(enableA, OUTPUT); pinMode(pinA1, OUTPUT); pinMode(pinA2, OUTPUT); pinMode(enableB, OUTPUT); pinMode(pinB1, OUTPUT); pinMode(pinB2, OUTPUT); enableMotors(); // ให้มอเตอร์ทำงาน analogWrite(enableA, 225); // ปรับความเร็วหุ่นยนต์ 64 ถึง 225 analogWrite(enableB, 225); // ปรับความเร็วหุ่นยนต์ 64 ถึง 225 } void loop() { ps2x.read_gamepad(false, false); // อ่านข้อมูลจาก PS2 Controller if (ps2x.Button(PSB_CIRCLE)) // ถ้าปุ่มวงกลมถูกกด coast(1); // หยุด 1 มิลลิวินาที else if (ps2x.Button(PSB_PAD_UP)) // ถ้าปุ่ม Up ถูกกด forward(1); // เดินหน้า 1 มิลลิวินาที else if (ps2x.Button(PSB_PAD_DOWN)) // ถ้าปุ่ม Down ถูกกด backward(1); // ถอยหลัง 1 มิลลิวินาที else if (ps2x.Button(PSB_PAD_LEFT)) // ถ้าปุ่ม Left ถูกกด turnLeft(1); // เลี้ยวซ้าย 1 มิลลิวินาที else if (ps2x.Button(PSB_PAD_RIGHT)) // ถ้าปุ่ม Right ถูกกด ให้เลี้ยวขวา turnRight(1); // เลี้ยวขวา 1 มิลลิวินาที delay(50); // หน่วงเวลา 50 มิลลิวินาที } //ส่วนคำสั่งควบคุมมอเตอร์ void enableMotors() { motorAOn(); motorBOn(); } void disableMotors() { motorAOff(); motorBOff(); } void forward(int time) { Serial.println("Forward"); motorAForward(); motorBForward(); delay(time); } void backward(int time) { Serial.println("Backward"); motorABackward(); motorBBackward(); delay(time); } void turnLeft(int time) { Serial.println("TurnLeft"); motorABackward(); motorBForward(); delay(time); } void turnRight(int time) { Serial.println("TurnRight"); motorAForward(); motorBBackward(); delay(time); } void coast(int time) { Serial.println("Coast"); 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); }
หลังจากนั้นสังเกตที่ Receiver ที่ไฟแสดงผล LED ไฟสีแดงจะติด และ ไฟสีเขียวกระพริบ แสดงว่าพร้อมใช้งาน
ใส่ถ่าน ขนาด AAA จำนวน 2 ก้อน ที่ Wireless Joystick Playstation PS2
เปิดสวิตช์ ของ Wireless Joystick Playstation PS2 ไปที่ ON สังเกตที่ Receiver ไฟสีเขียวที่กระพริบ จะเปลี่ยนเป็นสีเขียวไม่ กระพริบ แสดงว่าพร้อมใช้งาน
หมายเหตุ : ถ้าไฟสีเขียวยังกระพริบอยู่ ให้ทดลอง กดปุ่ม START
เปิดหน้าต่าง Serial Monitor ตั้งค่าความเร็วในการรับ-ส่งข้อมูล คือ 57600
จะแสดงสถานะ Connecting OK
ทดลองกดปุ่มควบคุมที่ด้านซ้ายมือ ของ จอยไร้สาย
ถ้าแสดงผลตามที่เรากดควบคุมแสดงว่า รถบังคับ ด้วย Wireless Joystick PS2 พร้อมทำงานแล้วครับการควบคุมหุ่นยนต์ ด้านซ้ายมือ เลี้ยวซ้าย , เลี้ยวขาว , เดินหน้า , ถอยหลัง และ ด้านขวามือ ปุ่มวงกลม คือ ให้หยุด ใส่ถ่านชาร์จ 18650 จำนวน 2 ก้อน
วีดีโอผลลัพธ์การทำงานของ โปรเจค รถบังคับ ด้วย Wireless Joystick PS2
VIDEO