Raspberry Pi • AI • Donkey Car • Embedded Control • RC Car
Autonomous Driving Car V4.X: รถ RC AI ด้วย Raspberry Pi + Watchdog 500 ms
สร้างรถ RC ให้ Neural Network เลี้ยวตามภาพจากกล้องอาจดูเหมือนโจทย์ AI เป็นหลัก แต่ Autonomous Driving Car V4.X กลับเจอว่า Model ไม่ใช่ส่วนที่ใช้เวลามากที่สุด ปัญหาหนักจริงอยู่รอบ ๆ AI: Pin Mapping ผิด, PWM ที่ต่ำเกินไป, Controller เขียนค่า 0 ทับ และที่สำคัญที่สุดคือรถยังวิ่งต่อได้แม้ Software ที่ควบคุมมันหยุดทำงานแล้ว
V4.X จึงไม่ได้เพิ่ม Chip ใหม่ให้รถเก่งขึ้น แต่ย้อนกลับมาแก้ Architecture เดิมให้ตรวจสอบได้มากขึ้น และเพิ่ม Firmware Watchdog 500 ms บน XMC1100 เพื่อให้ Motor Brake และ Steering กลับ Centre หาก Steering หรือ Throttle Command หายไป
นี่จึงเป็น Project ที่น่าสนใจไม่ใช่แค่เรื่อง Neural Network แต่เป็นตัวอย่างว่าระบบ Autonomous ขนาดเล็ก ต้องคิดทั้ง Perception, Control, Version Compatibility, Calibration และ Failure Mode ไปพร้อมกัน
- Raspberry Pi 4 ทำ Perception และ Driving Policy ส่วน XMC1100 ทำ Actuation
- ระบบใช้ I2C Address 0x40 และเลียนแบบ PCA9685 Message Format แม้ไม่มี PCA9685 Chip จริง
- V4.X เพิ่ม Firmware Watchdog 500 ms เพื่อ Brake Motor และ Centre Steering เมื่อ Command หาย
- Camera Input ตาม Build คือ 160×120 ที่ Drive Loop 20 Hz
- Build ใช้ Donkey Car 5.3.0 บน Raspberry Pi OS Bookworm 64-bit
- Dataset จริงมี 17,521 Records จาก 4 Sessions
- Model KerasLinear Train 39 Epochs และ Best Validation Loss อยู่ที่ 0.0906
- Deployment บนรถใช้ไฟล์ TFLite ขนาด 3.3 MB
- Manual Drive, Recording, Training และ Watchdog ถูก Verify แล้ว
- Full Autonomous Laps ยังไม่ถูก Confirm End-to-End ใน Source
AI ไม่ใช่ส่วนที่ยากที่สุดของรถคันนี้
Source ระบุว่า Keras Model ที่ Train จาก 17,521 Recorded Frames Converge ได้ภายในช่วงบ่ายบน Laptop CPU
แต่สิ่งที่กินเวลาหลายวันกลับเป็น Integration รอบนอก เช่น:
- Motor ไม่หมุนเพราะใช้ Pin Map ของ Infineon Board คนละรุ่น
- ล้อแค่กระตุกเพราะ Throttle ที่ดูเหมาะสมจริง ๆ แปลงเป็น Duty ต่ำมาก
- Web Joystick ถูก Controller Part อีกตัวเขียนค่า 0 ทับทุก Loop
- Software Guide เก่าใช้ Package ที่ Bookworm ไม่มีแล้ว
- รถยังถือคำสั่ง Speed ล่าสุดต่อไป แม้ Raspberry Pi หรือ Python หยุดทำงาน
จาก V1 ถึง V4.X เปลี่ยนอะไร?
| Generation | สิ่งที่ Source ระบุ |
|---|---|
| V1 — 2019 | Raspberry Pi 3 + XMC1100 Boot Kit + BTN8982TA Motor Shield และเริ่มใช้วิธี Emulate PCA9685 |
| V2 — 2023 | ย้ายมา Raspberry Pi 4 และ DC-SHIELD_BTN9970LV |
| V3 — 2025 | เพิ่ม Donkey Car Simulator, Mobile App และใช้ Servo Signal จาก Pin 20 |
| V4.X | ไม่ได้เพิ่ม Chip ใหม่ แต่แก้ Mapping, Software Stack, Calibration และเพิ่ม 500 ms Watchdog |
Architecture: Raspberry Pi คิด แต่ XMC1100 เป็นคนขยับรถ
160 × 120 px ตาม Build
Camera → Donkey Car / TFLite → Steering + Throttle
Decode PCA9685-format Writes → Motor PWM + Servo Pulse + Watchdog
ควบคุม Brushed DC Motor และพวงมาลัย
Source ระบุ Drive Loop ที่: 20 Hz หรือประมาณหนึ่งรอบทุก 50 ms
การแบ่งหน้าที่แบบนี้ตั้งใจให้ Linux / Python ทำงานที่ต้องใช้ Image Processing และ Floating-point Compute ส่วน MCU ที่ไม่มี Operating System รับผิดชอบ Layer ที่ต้องหยุด Motor ให้ได้เมื่อ Command หาย
ไม่มี PCA9685 แล้วทำไม Donkey Car ยังเห็น PCA9685?
จุดนี้ทำให้หลายคนสับสน: Configuration ของ Donkey Car ยังมีชื่ออย่าง PCA9685.1:40.0 และ PCA9685.1:40.1 แต่รถคันนี้ไม่มี PCA9685 Board จริง
XMC1100 ตอบที่ I2C Address: 0x40 และยอมรับ Register Writes ใน Format ที่ Donkey Car ปกติใช้กับ PCA9685
ทำให้ Software ฝั่ง Raspberry Pi ใช้ Standard PWM_STEERING_THROTTLE ได้ตามเดิม และคำสั่งอย่าง donkey calibrate ไม่ต้องถูก Patch
Hardware หลักมีอะไรบ้าง?
| ส่วน | Hardware / Software จาก Source |
|---|---|
| Main Compute | Raspberry Pi 4 Model B |
| Camera | Raspberry Pi Camera Module V2 / OV5647 ตาม Architecture |
| Embedded Controller | XMC1100 Boot Kit |
| Motor Shield | Infineon DC-SHIELD_BTN9970LV |
| Steering | RC Steering Servo |
| Software | Donkey Car 5.3.0 + Raspberry Pi OS Bookworm 64-bit |
| AI Deployment | TensorFlow Lite Model |
Wiring และ Pin Mapping ที่ทำให้ Motor ไม่หมุนอยู่หลายวัน
I2C ระหว่าง Raspberry Pi กับ XMC1100 ใช้:
| Raspberry Pi | XMC1100 |
|---|---|
| Pin 3 — GPIO2 / SDA1 | P2.1 / Arduino Pin 15 |
| Pin 5 — GPIO3 / SCL1 | P2.0 / Arduino Pin 16 |
| Pin 6 — GND | GND |
Servo Signal ใน V4.X: P0.12 / Arduino Pin 8
ส่วน Pin Mapping ของ Motor Shield ที่ผู้สร้าง Verify จาก Source Code ของ Infineon Library คือ:
| Signal | IC1 BTN9990LV → OUT1 | IC2 BTN9970LV → OUT2 |
|---|---|---|
| IN / PWM | Pin 10 / P0.9 | Pin 9 / P0.8 |
| INH / Enable | Pin 3 / P0.0 | Pin 13 / P0.7 |
| IS / Current | A1 / P2.8 | A0 / P2.6 |
Power Wiring ที่ Source ย้ำ
- Motor ต่อออกที่ OUT1 / OUT2 ของ Shield
- Battery + เข้า VBAT ผ่าน Inline Fuse
- Battery - เข้า GND ของ Shield
- Motor Battery ต้องเข้าที่ Screw Terminal ไม่ผ่าน XMC Header
- Raspberry Pi ใช้ 5V USB-C Supply ของตัวเอง
- Pi GPIO รับได้ 3.3V เท่านั้น และ Source แนะนำให้วัด SDA/SCL ก่อนต่อครั้งแรก
Watchdog 500 ms: ถ้า Raspberry Pi หาย รถต้องหยุด
V4.X เพิ่ม Watchdog ไว้ใน Firmware ของ XMC1100 ไม่ใช่ใน Python
Donkey Car ส่ง Steering / Throttle ทุกประมาณ 50 ms ที่ Drive Loop 20 Hz ส่วน Watchdog ให้ Timeout ที่: 500 ms
ถ้าไม่มี Complete Steering หรือ Throttle Value เข้ามาภายในช่วงดังกล่าว:
- Motor ถูกสั่ง Brake
- Steering ถูกสั่งกลับ Centre ที่ค่าที่ Calibrate ไว้
- เมื่อ Command กลับมา Normal Control Resume ได้โดยไม่ต้อง Reset
const unsigned long CMD_TIMEOUT_MS = 500; // Donkey Car writes every 50 ms at 20 Hz
const int SERVO_CENTRE_POS = -100; // calibrated centre pulse 500, minus 600
volatile unsigned long lastCmdMs = 0;
void loop() {
noInterrupts();
unsigned long sinceCmdMs = millis() - lastCmdMs;
interrupts();
if (sinceCmdMs > CMD_TIMEOUT_MS) {
if (!watchdogTripped) {
Serial.println("watchdog: no commands, motor stopped");
watchdogTripped = true;
}
btn_motor_control.set_speed(0); // brake
servoMove(servo, SERVO_CENTRE_POS); // centre
delay(20);
return;
}
watchdogTripped = false;
// ... normal servo and motor control
}
donkey calibrate ส่งค่าเพียงครั้งเดียว ดังนั้น Watchdog จะหยุด Motor หลัง 500 ms ตามที่ออกแบบไว้ Source ระบุว่าการเพิ่ม Timeout ชั่วคราวใช้เฉพาะช่วง Calibration และต้อง Restore กลับก่อนนำรถไปขับSoftware Stack ที่ใช้จริงใน V4.X
Source มีข้อมูล OS สองจุดที่ไม่ตรงกันทั้งหมด: รายการ Things Used ด้านบนพูดถึง Raspbian Lite 32-bit แต่ขั้นตอน Build V4.X ที่ผู้สร้างทดสอบจริงใช้:
- Raspberry Pi OS Bookworm 64-bit
- Python 3.11.2 ตาม Environment ที่ Source ระบุ
- Donkey Car 5.3.0
- Picamera2 / Bookworm Camera Stack
Package รุ่นเก่าบางชื่อใช้ไม่ได้แล้วบน Bookworm เช่น:
-
python3-picamera→ ใช้python3-picamera2 -
libtiff5-dev→ ใช้libtiff-dev -
libhdf5-serial-dev→ ใช้libhdf5-dev
เช็ก Camera และ I2C ก่อนตั้งค่า Donkey Car
ก่อนเริ่ม Calibration Source แนะนำให้ Verify Hardware สองจุดหลัก
Camera
ใช้: rpicam-hello --list-cameras
หากไม่มี Camera Source ให้ Shutdown ก่อนตรวจและเสียบ CSI Ribbon ใหม่
I2C
ใช้: i2cdetect -y 1
ถ้า XMC1100 Flash Firmware แล้วและมี Power ควรเห็น: 40 บน I2C Grid
Calibration: ทำไม Throttle ดูเยอะแต่ล้อแค่สั่น?
Firmware Mapping สำหรับ Throttle ใน Build นี้คือ:
speed = pulse - 255
แล้วส่ง:
set_speed(speed × 0.4)
ตัวอย่างที่ผู้สร้างแกะย้อนจาก Firmware: Pulse 300 ให้ค่า Duty ประมาณ: 7%
ผลคือ Motor อาจเพียง Buzz หรือสั่น แต่ไม่สามารถเอาชนะแรงเสียดทานและ Cogging ได้
| Calibration | ค่าของ Build นี้ |
|---|---|
| Throttle Stop | 255 |
| Throttle Forward | 500 — Source คำนวณประมาณ 38% Duty |
| Throttle Reverse | 100 — Source คำนวณประมาณ 24% Duty |
| Steering Centre | 500 — 1400 µs ตามค่าที่วัดใน Build |
| Steering Left | 250 — 1150 µs |
| Steering Right | 750 — 1650 µs |
ผู้สร้างยังพบ Hard Ceiling ใกล้ Pulse 894 จาก 8-bit Overflow จึงเตือนให้ Keep Forward ต่ำกว่าประมาณ 890 ใน Firmware Mapping ชุดนี้
Steering Centre ต้องอยู่ตรงกลางของ Left / Right
Donkey Car Map Steering แบบ Linear ระหว่าง Left และ Right และใช้ Midpoint ของสองค่านี้เป็น Centre
แม้ผู้สร้างวัด Physical Limit ได้ 250 / 900 แต่ Midpoint จะกลายเป็น 575 ซึ่งไม่ตรงกับ Straight-ahead ที่วัดได้ 500
จึงใช้: 250 / 750 เพื่อให้ Midpoint อยู่ที่ 500 แม้ต้องยอมเสีย Right Lock บางส่วน
Dataset 17,521 Records: รถเรียนจากสิ่งที่เราขับให้ดู
Source แนะนำ Dataset ประมาณ: 10,000–20,000 Records
Build นี้ใช้:
- 17,521 Records
- 4 Recording Sessions
- Track ที่มีขอบชัด
- Lighting ที่สม่ำเสมอ
- การขับแบบ Smooth และ Centred
ดังนั้น Source แนะนำให้หยุด Recording ก่อน Crash หรือก่อนยกรถมา Reposition
Training Model: 39 Epochs และ Validation Loss 0.0906
Training ทำบน PC ไม่ใช่บน Raspberry Pi
Build ใช้:
- Python 3.11
- Donkey Car 5.3.0
- TensorFlow 2.15
- KerasLinear Model
- 80/20 Train / Validation Split
- Early Stopping Patience 5
| ผล Training | ค่าจาก Source |
|---|---|
| Records | 17,521 |
| Epochs | 39 |
| Best Epoch | 33 |
| Best Validation Loss | 0.0906 |
| Steering Loss | 0.0718 |
| Throttle Loss | 0.0187 |
| mypilot.h5 | 9.9 MB |
| mypilot.tflite | 3.3 MB |
ผู้สร้างตีความ Model นี้ว่า: “usable first model, not a precise one”
Deploy AI แบบทีละขั้น ไม่เปิด Full Auto ตั้งแต่ครั้งแรก
Raspberry Pi ใช้: mypilot.tflite ไม่ใช่ไฟล์ H5 สำหรับ Inference บนรถ
Source แนะนำ Hand Control ไปทีละ Stage:
-
Wheels off the ground + Auto Steer
ขยับรถด้วยมือให้ Camera เห็น Track และดูว่าล้อเลี้ยวตามทิศที่ควรหรือไม่ -
บน Track + Auto Steer
ให้ Model เลี้ยว แต่คนยังควบคุม Throttle -
Full Auto
ใช้เมื่อ Auto Steer Reliable แล้วเท่านั้น
อะไร Verify แล้ว และอะไรยังไม่?
| ส่วน | สถานะจาก Source |
|---|---|
| Manual Driving | Verified |
| Camera / Recording Pipeline | Verified |
| Model Training | Verified |
| Firmware Watchdog | Verified |
| Hold Test | Passed บน Build นี้ |
| Full Autonomous Laps | ยังไม่ Confirm End-to-End |
Troubleshooting ที่ผู้สร้างเจอจริง
| อาการ | สาเหตุที่ Source พบ | บทเรียน |
|---|---|---|
| Motor ไม่ขยับเลย | ใช้ Classic Infineon Pin Map ผิดกับ Shield นี้ | Library Source เป็น Pinout ที่ผู้สร้างใช้ Verify |
| ล้อ Buzz / สั่น | Duty ต่ำประมาณ 7% และ Battery 2S Sag ต่ำกว่าค่าที่ Shield ต้องการใน Test | คำนวณ Firmware Mapping ก่อนสงสัย Hardware เสีย |
| Web Joystick ไม่สั่งรถ | Xbox Controller Part เขียนค่า 0 ทับทุก Loop | ดู Startup Part List และตั้ง USE_JOYSTICK_AS_DEFAULT = False |
| I2C Read ได้ 0x01 ทุก Register | เป็น Behaviour ของ Firmware Emulator ไม่ใช่ PCA9685 จริง | อย่า Reverse-engineer จาก Expectation ของ Chip ที่ไม่ได้มีอยู่จริง |
| Camera หาย | CSI Ribbon หลวม | Shutdown แล้ว Reseat สายทั้งสองด้าน |
| Donkey Car 5.4.0 ติด Python Version | Build ใช้ Python 3.11 | Pin Donkey Car เป็น 5.3.0 ตาม Source |
| Calibration Value อยู่ได้ 0.5 s | Watchdog ทำงานถูกต้อง | อย่าตีความเป็น Motor Fault |
รถเห็น Track ได้ แต่ยังไม่รู้ Speed ตัวเอง
Source ระบุว่า V4.X ไม่มี:
- Wheel Encoder
- IMU
- Steering Feedback
รถจึงไม่รู้ Speed จริง มันรู้เพียง Duty Cycle ที่ตัวเองสั่ง
Control เป็น Feed-forward และ Physical Environment ปิด Loop กลับผ่าน Camera ที่ 20 Hz
นั่นหมายความว่า Command เดิม อาจให้ความเร็วจริงไม่เท่ากันเมื่อ:
- พื้นเปลี่ยนจากเรียบเป็นพรม
- รถขึ้นทางลาด
- Battery Voltage ลดลง
โดยระบบปัจจุบันไม่มี Vehicle-state Sensor คอยบอกว่าความเร็วจริงเปลี่ยนไปแล้ว
Safety ก่อนให้ AI ขับ
กฎที่ผู้สร้างย้ำหลังผ่านการ Debug จริง:
- Test แรกหลังเปลี่ยน Firmware หรือ Hardware ให้ยกล้อพ้นพื้น
- Battery Lead ต้องมี Inline Fuse ตาม Build
- Motor Battery ไม่ต่อเข้า Raspberry Pi
- Power Raspberry Pi แยกจาก Motor Battery ตาม Architecture นี้
- ทำ Watchdog Stop Test ใหม่หลัง Firmware Change ก่อนปล่อยให้ Model ขับ
- อย่าเดา I2C Write หรือ Motor Command แล้วส่งเข้า Controller ขณะ Battery ต่ออยู่
Source ยังเปิด Technical Debt ที่เหลืออยู่:
- Current Sensing ต่อไว้แต่ยังไม่ได้ใช้
- ไม่มี Stall Detection
- Servo Pulse ยังใช้
delayMicroseconds - I2C Interrupt สามารถทำให้ Servo Pulse ยืดเล็กน้อยและเกิด Steering Jitter
- Firmware ยังมี
Serial.println()ใน Interrupt Handler ซึ่งผู้สร้างมองว่าเป็น Latent Risk - 2S Battery ที่ใช้ใน Build ถูก Source ระบุว่ามีปัญหา Out-of-spec Under Load
โปรเจกต์นี้มีหลายขั้น แนะนำอ่านต้นฉบับควบคู่กัน
V4.X มีรายละเอียดตั้งแต่ Mechanical Assembly, Wiring, XMC Firmware, Bookworm Setup, Donkey Car, Calibration, Dataset Collection, Training และ Deployment รวมถึง Troubleshooting จากปัญหาที่เกิดขึ้นจริง จึงควรอ่าน Tutorial ต้นฉบับ หากต้องการทำ Build ตามทีละขั้น
FAQ: คำถามที่พบบ่อย
1. Autonomous Driving Car V4.X คืออะไร?
เป็นรถ RC ขนาด 1:10 ที่ใช้ Raspberry Pi ทำ Camera Processing และ AI Driving Policy พร้อม XMC1100 ควบคุม Motor / Steering และเพิ่ม Firmware Watchdog ใน V4.X
2. ใช้ Raspberry Pi รุ่นไหน?
Source ระบุ Raspberry Pi 4 Model B
3. ใช้กล้องอะไร?
ใช้ Raspberry Pi Camera Module V2 และ Architecture ระบุ Sensor OV5647
4. Camera Resolution ที่ใช้กับ Driving Loop เท่าไร?
Source ระบุ 160×120 pixels ที่ Camera / Drive Loop 20 Hz
5. ใช้ Donkey Car Version ไหน?
Build นี้ Pin ที่ Donkey Car 5.3.0
6. ทำไมไม่ใช้ Donkey Car 5.4.0?
Source ระบุ 5.4.0 ต้องการ Python 3.12+ ขณะที่ Bookworm Environment ของ Build นี้ใช้ Python 3.11
7. รถมี PCA9685 จริงหรือไม่?
ไม่มี XMC1100 จำลองเฉพาะ Register Behaviour ที่ Donkey Car ต้องใช้ และตอบที่ I2C Address 0x40
8. Watchdog 500 ms ทำอะไร?
หากไม่มี Complete Steering หรือ Throttle Command ภายใน 500 ms XMC1100 จะ Brake Motor และ Centre Steering
9. ถ้า Raspberry Pi ค้าง รถจะทำอย่างไร?
ถ้า Command หายเกิน Watchdog Timeout Firmware จะเข้าสู่ Brake + Steering Centre ตาม Design ของ V4.X
10. ทำไม Motor เคยไม่หมุน?
ผู้สร้างพบว่าใช้ Classic Infineon Pin Mapping ที่ไม่ตรงกับ DC-SHIELD_BTN9970LV ใน Build นี้
11. ทำไมล้อเคยแค่สั่น?
Source แกะ Firmware Arithmetic แล้วพบว่า Throttle ตัวอย่างหนึ่งให้ Duty เพียงประมาณ 7% ซึ่งต่ำเกินกว่าจะเอาชนะแรงเสียดทานและ Cogging ได้
12. Dataset ใช้กี่ Records?
Build นี้ใช้ 17,521 Records จาก 4 Recording Sessions
13. Model Train กี่ Epoch?
39 Epochs โดย Best Validation Loss อยู่ที่ Epoch 33
14. ใช้ GPU Train หรือไม่?
Source ระบุว่า Run นี้ Train บน Laptop CPU และไม่จำเป็นต้องใช้ GPU
15. บนรถใช้ไฟล์ .h5 หรือ .tflite?
Deployment บน Raspberry Pi ใช้ mypilot.tflite ผ่าน TensorFlow Lite Runtime
16. Full Autonomous Lap สำเร็จแล้วหรือยัง?
ยัง Source ระบุว่า Full Autonomous Laps ยังไม่ได้ Confirm End-to-End
17. รถรู้ Speed จริงของตัวเองหรือไม่?
ไม่ Source ระบุว่าไม่มี Wheel Encoder และระบบรู้เพียง Duty Cycle ที่สั่ง
18. มี IMU หรือ Steering Feedback หรือไม่?
ไม่มีใน Architecture ที่ Source ระบุ
19. มี Obstacle Detection หรือไม่?
Source ไม่ได้ระบุ Obstacle Detection, Pedestrian Detection หรือ Independent Safety Perception
20. ใช้ Architecture นี้กับรถจริงบนถนนได้ไหม?
Source ระบุชัดว่า Project นี้เป็น Experimental และ Educational Platform ไม่ใช่ Certified Automotive Autonomy และไม่มี Redundancy หรือ Functional-safety Process สำหรับ Road Vehicle
References / แหล่งข้อมูลต้นฉบับ
AI ขับรถได้ แต่ Embedded Controller ต้องรู้ว่าจะหยุดเมื่อไร
Autonomous Driving Car V4.X เป็นตัวอย่างที่ดีว่าการทำ Robotics และ AI Hardware ไม่ได้จบที่ Model Training: Camera, Communication, Motor Driver, Calibration, Firmware และ Fail-safe ล้วนเป็นส่วนหนึ่งของระบบเดียวกัน
หากกำลังทำ Raspberry Pi, Computer Vision, Robotics, Motor Control หรือ AI Hardware Project สามารถสอบถามทีม Globalbyte เรื่อง Development Board, Sensor และอุปกรณ์ที่เหมาะกับ Project ได้