การใช้งาน ZX-PSX ร่วมกับ POP-BOT32i Rover Kit Plus

ZX-PSX คือแผงวงจรเชื่อมต่อแกนบังคับเครื่องเล่นเกมเพลย์สเตชั่นแบบไร้สาย รุ่น 3 โวลต์ สำหรับบอร์ด POP-32i

คุณสมบัติทางเทคนิค

  • ใช้สำหรับติดต่อกับแผงวงจรไมโครคอนโทรลเลอร์ เพื่อรับส่งข้อมูลกับจอยสติ๊กไร้สายและแบบมีสาย ที่ทำงานเข้ากันได้กับ PS2 คอนโทรลเลอร์ ของ SONY
  • มีจุดต่อเพื่อเชื่อมต่อกับไมโครคอนโทรลเลอร์ 2 แบบคือ JST 2 มม. 3 ขา ตัวผู้ 4 ตัว และแบบ IDC 2.54 มม. 6 ขา ทั้งตัวผู้และตัวเมีย
  • ใช้งานกับบอร์ดไมโครคอนโทรลเลอร์ได้ทุกแบบ

วิธีการใช้งาน ZX-PSX ร่วมกับ POP-BOT32i : Rover Kit Plus

     1. เตรียมอุปกรณ์

    • ชุด POP-BOT32i : Rover Kit Plus และ ZX-PSX
    • สาย USB สำหรับเชื่อมต่อบอร์ดกับคอมพิวเตอร์
    • ซอร์ฟแวร์ Arduino IDE สำหรับเขียนและอัพโหลดโค้ด
    •  ไลบรารี่ที่เกี่ยวข้อง

     2. ขั้นตอนการติดตั้งไลบรารี่ PsxLib.h

    • เปิดโปรแกรม Arduino IDE และติดตั้งไลบลารี่ PsxLib.h โดยเข้าไปที่ Sketch > Include Library > Add .ZIP Library… ดังรูปที่ 2.1

รูปที่ 2.1 ติดตั้งไลบลารี่ PsxLib.h

    • เลือกไฟล์ไลบลารี่ที่ดาวน์โหลดไว้ก่อนหน้านี่ คือ PsxLib.zip
    • รอสักครู่หากติดตั้งสำเร็จให้สังเกตที่หน้าต่างส่วน Output จะมีข้อความว่า Library installed

รูปที่ 2.2 เลือกไฟล์ไลบรารี่ PsxLib.h

     3. ต่อวงจร ZX-PSX ร่วมกับ POP-32i

การต่อวงจรโดยใช้สาย JST3AA-8 ระหว่าง ZX-PSX และ POP-32i ดังต่อไปนี้

ZX-PSX POP-32i

Data

PA0

Commond

PA1

Select

PA2

Clock

PA3

ตัวอย่างการเชื่อมต่อสายระหว่าง ZX-PSX และ POP-32i
รูปที่ 3.1 เชื่อมต่อ ZX-PSX และ POP-32i

     4. ทดสอบอ่านค่าจากการกดปุ่มจอยสติ๊ก

    • เปิดโปรแกรมสำหรับอ่านค่าของปุ่มจอยสติ๊ก โดยไปที่ File > Examples > PsxLib > PSX_Example ดังรูปที่ 4.1
    • เชื่อมต่อบอร์ด POP-32i เข้ากับคอมพิวเตอร์ และอัปโหลดโค้ด PSX_Example ดังรูปที่ 4.2

รูปที่ 4.1 เปิดโปรแกรมตัวอย่างการอ่านค่าของปุ่มจอยสติ๊ก

รูปที่ 4.2 เชื่อมต่อบอร์ด POP-32 เข้ากับคอมพิวเตอร์

โปรแกรมอ่านค่าของปุ่มจอยสติ๊ก PSX_Example
#include "PsxLib.h"
Psx Psx;
#define dataPin PA0
#define commandPin PA1
#define selectPin PA2
#define clockPin PA3
unsigned int data = 0;  // data stores the controller response
void setup() {
  // Defines what each pin is used
  Psx.setupPins(dataPin, commandPin, selectPin, clockPin, 10);  
  Serial.begin(9600);
  while (!Serial)
    ;
}
void loop() {
  // Psx.read() initiates the PSX controller and returns
  data = Psx.read();  
  Serial.print("key=");
  Serial.print(data);
  Serial.print("\tAnLx=");
  Serial.print(Psx.AnLx);
  Serial.print("\tAnLy=");
  Serial.print(Psx.AnLy);
  Serial.print("\tAnRx=");
  Serial.print(Psx.AnRx);
  Serial.print("\tAnRy=");
  Serial.println(Psx.AnRy);
  delay(20);
}
    • เปลี่ยนช่องการเชื่อมต่อของบอร์ด POP-32i เป็นช่อง HID (ช่อง USB Type-C ข้างล่างของบอร์ด POP-32i) และเลือกหมายเลข COM Port ที่ใช้งาน ดังรูปที่ 4.3
    • เปิด Serial Monitor ตั้งค่า baud rate เท่ากับ 9600 จากนั้นกดปุ่ม reset บอร์ด POP-32i ใหม่อีกครั้งรอสักครู่ หน้าต่างจะแสดงข้อความดังรูปที่ 4.4
  •  รูปที่ 4.3 เชื่อมต่อช่อง HID และเลือก COM Port

รูปที่ 4.4 ส่วนแสดงผล Serial Monitor

    • ทดสอบด้วยการกดปุ่มต่างๆ บนจอยสติ๊กโดยค่าประจำปุ่มจะแสดงที่ Key และปุ่มหมุนแอนาลอกซ้ายจะใช้ค่า AnLx และ AnLy ส่วนปุ่มหมุนแอนาลอกขวาจะใช้ AnRx และ AnRy เป็นตัวกำหนด

ตัวอย่างการประยุกต์ควบคุมหุ่นยนต์

ตัวอย่างจะควบคุมการทำงานของหุ่นยนต์ POP-BOT32i Rover Kit Plus โดยกำหนดการเคลื่อนที่โดยใช้ปุ่มต่างๆ ดังนี้

ปุ่ม การทำงาน
ขึ้น ↑ เดินหน้า
ลง ↓ ถอยหลัง
ซ้าย ← เลี้ยวซ้าย
ขวา → เลี้ยวขวา
L1 เพิ่มความเร็ว
L2 ลดความเร็ว
R1 Grapper-X ยกแขนขึ้น
R2 Gripper-X วางแขนลง
สามเหลี่ยม Δ Gripper-X จับวัตถุ
กากบาท ⊗ Gripper-X ปล่อยวัตถุ
จอยสติ๊ก(อนาล็อก) ฝั่งซ้าย ควบคุมการเคลื่อนที่ของมอเตอร์ทั้งหมด
โปรแกรมตัวอย่างการใช้งาน ZX-PSX
#include "POP32.h"
#include "PsxLib.h"
Psx Psx;
// Assign names to the pin connections used to connect to the joystick receiver.
#define dataPin PA0
#define commandPin PA1
#define selectPin PA2
#define clockPin PA3
// Assign a name to the servo angle.
#define sv1_up 10
#define sv1_down 90
#define sv2_release 50
#define sv2_hold 105
int speed = 50; // Set the starting speed to 50%.
unsigned int data = 0; // Define a variable to store the button press status.
void setup() {
  Psx.setupPins(dataPin, commandPin, selectPin, clockPin, 10);
  oled.textSize(5);
  oled.text(0, 0, " %d  ", speed);
  oled.show();
  Psx.read();
}
void loop() {
  data = Psx.read();
  // Speed ​​adjustment system (L1/L2)
  if(data & 1024){ // L1 button 
    speed = constrain(speed + 10, 10, 100); // Increase speed by 10
    oled.text(0, 0, " %d  ", speed); oled.show();
    delay(200);
    Psx.read();
  }
  if(data & 256){ // L2 button 
    speed = constrain(speed - 10, 10, 100); // Reduce speed to 10.
    oled.text(0, 0, " %d  ", speed); oled.show();
    delay(200);
    Psx.read();
  }
  // Left analog joystick
  int ly = Psx.AnLy; // Retrieve the Y-axis position value.
  int lx = Psx.AnLx; // Retrieve the X-axis position value.
  int moveY = 0;
  int moveX = 0;
  // Y-axis (forward, backward)
  if (ly < 117) { // Check, push up.
    moveY = map(ly, 117, 0, 0, 100); // Convert to forward speed.
  }else if(ly > 137){ // Check, push down.
    moveY = map(ly, 137, 255, 0, -100); // Convert to backward speed.
  }
  // X-axis (left-right)
  if (lx < 118) { // Check, Push left.
    moveX = map(lx, 118, 0, 0, -100); // Convert to left speed.
  }else if(lx > 138){ // Check, Push right.
    moveX = map(lx, 138, 255, 0, 100);  // Convert to right speed.
  }
  // Combining values ​​to drive the motor.
  float ratio = speed / 100.0;
  int L_raw = moveY + moveX; // Calculate the left wheel speed.
  int R_raw = moveY - moveX; // Calculate the right wheel speed.
  // Prevent values ​​from exceeding 100 and prevent abnormal reverse rotation.
  L_raw = constrain(L_raw, -100, 100);
  R_raw = constrain(R_raw, -100, 100);
  int L_final = L_raw * ratio;
  int R_final = R_raw * ratio;
  if (moveY != 0 || moveX != 0) { // Check the movement of the joystick.
    fd2(L_final, R_final); // Motor is operating as calculated.
  }
  // Motor Control
  else if(data & 16)  {fd(speed); } // UP Button to forward
  else if(data & 64)  {bk(speed); } // Down Button to Backward
  else if(data & 32)  {sr(speed); } // Right Button to SpinRight
  else if(data & 128) {sl(speed); } // Left Button to SpinLeft
  else { 
    AO();
  }
  // Servo Control
  if (data & 4096)  servo(2, sv2_hold);    // Triangle button to servo hold
  if (data & 16384) servo(2, sv2_release); // Cross button to servo release
  if (data & 2048)  servo(1, sv1_up);      // L1 button to servo up
  if (data & 512)   servo(1, sv1_down);    // L2 button to servo down
  delay(20);
}
อธิบายการทำงาน

เมื่อโปรแกรมเริ่มทำงาน ค่าความเร็วเริ่มต้นจะถูกกำหนดไว้ 50 เปอร์เซ็น และมีการแสดงออกหน้าจอ OLED ด้วย หากต้องการเพิ่มความเร็วให้กดปุ่ม L1 หรือหากต้องการลดความเร็วให้กดปุ่ม L2 โดยการเพิ่มหรือลดความเร็ว สามารถทำได้ครั้งละ 10 เปอร์เซ็นและจะกำหนดให้อยู่ในช่วง 10 ถึง 100 เปอร์เซ็นเท่านั้น เมื่อต้องการเดินหน้าให้กดปุ่ม ขึ้น , ถอยหลังกดปุ่ม ลง , เลี้ยวซ้ายกดปุ่ม ซ้าย , เลี้ยวขวากดปุ่ม ขวา และสามารถสั่งการทำงานของ Gripper-X โดยกดปุ่ม R1 จะยกแขนจับขึ้น กดปุ่ม R2 จะเป็นการเอาแขนจับลง เมื่อต้องการให้แขนจับหนีบวัตถุ ทำได้โดยการกดปุ่ม สามเหลี่ยม และปล่อยวัตถุโดยกดปุ่ม กากบาท นอกจากนี้ยังสามารถใช้งาน Analog Stick ในการควบคุมการเคลื่อนที่ของหุ่นยนต์ โดยเมื่อโยก Analog Stick ตามแกน X และ Y มอเตอร์แต่ละข้างจะทำงานตามระดับการโยกของ Analog Stick ทำให้สามารถปรับความเร็วของมอเตอร์แต่ละข้างได้ตามที่ต้องการ

ตัวอย่างการใช้งาน ZX-PSX ร่วมกับชุด POP-BOT32i Rover Kit Plus

Facebook Comments Box