PYNQ-Z2 Special Episode: ควบคุม Hardware ด้วย AI Agent (Nanobot)
ในตอนนี้เราจะมีเนื้อหาพิเศษ เนื่องจากช่วงนี้กระแส AI Agent กำลังมาแรง โดยเฉพาะโปรเจกต์อย่าง OpenClaw ที่ได้รับความสนใจอย่างมาก แต่สำหรับสาย Embedded เราต้องการตัวเลือกที่เบากว่าและใช้ทรัพยากรน้อยกว่า Nanobot เป็น AI Agent ที่ได้รับแรงบันดาลใจมาจาก OpenClaw แต่ถูกออกแบบมาให้ใช้ Memory น้อยกว่า เหมาะสำหรับการรันบนบอร์ดขนาดเล็ก ในบทความนี้เราจะลองนำ Nanobot มารันบนบอร์ด PYNQ-Z2 เพื่อทดลองควบคุมหน้าจอ LCD (ผ่าน Framebuffer) ที่เราสร้างขึ้นในตอนก่อนๆ เพื่อเป็นแนวทางในการนำ AI มาประยุกต์ใช้ควบคุม Hardware ในระดับ Low-level ครับ
ขั้นตอนการดำเนินการ
1. การติดตั้ง Debian Linux
เริ่มต้นด้วยการเตรียมระบบปฏิบัติการ Debian สำหรับ PYNQ-Z2
ดาวน์โหลด Debian Image:
ดาวน์โหลดจาก: rcn-ee.com (Minimal FS)
ในบทความนี้ผมใช้เวอร์ชัน Debian 12.1 (Bookworm)เตรียม SD Card:
ใช้เครื่องมืออย่างgpartedเพื่อแบ่ง Partition ดังนี้:- Partition 1: FAT32 (สำหรับ Bootloader และ Kernel)
- Partition 2: EXT4 (สำหรับ Root Filesystem)
copy ไฟล์ระบบ:
copy ไฟล์ที่ได้จากการ Build ด้วย PetaLinux ไปยัง Partition 1:$ sudo mount /dev/sdb1 /mnt/sdb1 $ cd ~/pynq_z2/images/linux/ $ sudo cp BOOT.BIN image.ub /mnt/sdb1/ $ sync $ sudo umount /mnt/sdb1แตกไฟล์ Debian RootFS ลงใน Partition 2:
$ tar xJf debian-12.1-minimal-armhf-2023-08-22.tar.xz $ sudo mount /dev/sdb2 /mnt/sdb2 $ sudo tar xfvp debian-12.1-minimal-armhf-2023-08-22/armhf-rootfs-debian-bookworm.tar -C /mnt/sdb2 $ sync $ sudo umount /mnt/sdb2ตั้งค่า U-Boot Environment:
เสียบ SD Card เข้ากับ PYNQ-Z2 แล้วตั้งค่าผ่าน Serial Console:setenv bootargs console=ttyPS0,115200 root=/dev/mmcblk0p2 rw rootwait saveenv
2. การจัดการ Framebuffer
2.1 ทดสอบการโหลด Driver
เราจะทำการโหลด Module ของหน้าจอ LCD (ILI9486) ที่เรา Build ไว้ก่อนหน้านี้:
debian@arm:~$ sudo insmod fb-ili9486.ko
fb_ili9486: loading out-of-tree module taints kernel.
fb_ili9486: V2-20251213
Console: switching to mono frame buffer device 40x30
# ทดสอบรัน Demo LVGL
debian@arm:~$ chmod +x demo_lvgl
debian@arm:~$ ./demo_lvgl
[DEBUG] LVGL multi-box speed test start
[DEBUG] Multi-box test running
[LVGL] running... 1772452985
2.2 ตั้งค่า Auto-load เมื่อ Startup
เพื่อให้ Driver โหลดโดยอัตโนมัติทุกครั้งที่เปิดเครื่อง ให้สร้าง Systemd Service:
sudo nano /etc/systemd/system/fbvideo.service
[Unit]
Description=Load fb_ili9486 Driver
After=multi-user.target
[Service]
Type=oneshot
ExecStart=/sbin/insmod /home/debian/fb-ili9486.ko
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
3. การติดตั้ง Nanobot
เนื่องจากเราใช้ Debian 12 ทำให้การติดตั้ง Python และ Library ต่างๆ ทำได้ง่ายผ่าน apt
- ติดตั้ง Dependencies:
sudo apt update sudo apt install python3-pip python3-venv python3-full -y - Clone และติดตั้ง Nanobot:
git clone https://github.com/HKUDS/nanobot.git cd nanobot python3 -m venv venv source venv/bin/activate pip install --upgrade pip pip install -e . - ตั้งค่า AI Model:
ในตัวอย่างนี้เราจะใช้ Gemini เป็นสมองหลักของ Agent ให้แก้ไขไฟล์ Config (สมมติว่ารันnanobot onboardเพื่อสร้างไฟล์เริ่มต้นก่อน):
nano ~/.nanobot/config.json
"agents": {
"defaults": {
"workspace": "~/.nanobot/workspace",
"model": "gemini/gemini-2.5-flash",
"provider": "gemini",
...
"gemini": {
"apiKey": "xxxxxxxxxxxxxxxxxxxxxxxxx",
"apiBase": null,
"extraHeaders": null
4. การสร้าง Skill ให้ Nanobot ควบคุม Framebuffer
จุดเด่นของ Nanobot คือเราสามารถสอน Skill ใหม่ๆ ให้มันได้ โดยการสร้างไฟล์ SKILL.md เพื่ออธิบายความสามารถให้ AI เข้าใจ
- สร้างโฟลเดอร์สำหรับ Skill:
mkdir -p ~/.nanobot/workspace/skills/led_control - สร้างไฟล์คู่มือสำหรับ AI (SKILL.md):
~/.nanobot/workspace/skills/led_control/SKILL.md
NOTE
LED Test Skill
This skill allows controlling an LED (simulated via framebuffer) by filling the screen with red or blue.
Actions
turn_on_red_led
Turns on the red LED (fills the screen with red).
default_api.exec(command="python3 fb_test_fill_red.py", working_dir="/home/debian/.nanobot/workspace/skills/led_test")turn_on_blue_led
Turns on the blue LED (fills the screen with blue).
default_api.exec(command="python3 fb_test_fill_blue.py", working_dir="/home/debian/.nanobot/workspace/skills/led_test")test_led
when user want to test led, please use below command
default_api.exec(command="./test_rgb", working_dir="/home/debian/.nanobot/workspace/skills/led_test")
- เขียน Python Script สำหรับควบคุม Hardware:
ตัวอย่างไฟล์fb_test_fill_red.pyสำหรับพ่นสีแดงลงบน/dev/fb0:
import os
import struct
import mmap
# Adjust these to match your framebuffer
WIDTH = 480
HEIGHT = 320
BYTES_PER_PIXEL = 2 # 16-bit
# RGB565 red = 0b1111100000000000 = 0xF800
RED = 0xF800
# Open framebuffer
fb = os.open("/dev/fb0", os.O_RDWR)
# Map framebuffer to memory
screen_size = WIDTH * HEIGHT * BYTES_PER_PIXEL
fb_mem = mmap.mmap(fb, screen_size, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ)
# Create one red pixel in RGB565
red_pixel = struct.pack("
ผลการทดสอบ
เมื่อทุกอย่างพร้อมแล้ว เราสามารถสั่งงานด้วยเสียงหรือพิมพ์สั่ง Nanobot เช่น "Turn on the red light" ตัว Agent จะไปอ่าน SKILL.md และตัดสินใจรันไฟล์ Python เพื่อเปลี่ยนสีหน้าจอ LCD ของเราได้ทันที!
ตัวอย่างการทำงานจริง