# การสื่อสารผ่านพอร์ตซีเรียลของ Raspberry Pi

{% hint style="warning" %}
หุ่นยนต์ไม่ต้องการ PI เพื่อเคลื่อนไหว&#x20;

คุณต้องถอดปลั๊ก USB Adpter 6 พินสำหรับ NYBoard ก่อนที่จะติดตั้ง PI ไปยังบอร์ด&#x20;
{% endhint %}

คุณสามารถประสานซ็อกเก็ต 2x5 บน Nyboard เพื่อเสียบ Raspberry Pi PI 3A+ เป็นแบบที่ดีที่สุดสำหรับ มิติของ Nyboard&#x20;

{% embed url="<https://youtu.be/iGYNq-T1fZc>" %}

### Nybble

![](/files/JsueB9MjyOpRoovN2R1P)

### Bittle

{% hint style="danger" %}
หลังจากที่คุณประสานบนซ็อกเก็ต คุณจะไม่สามารถติดตั้งฝาหลังด้านหลังของ Bittle ได้
{% endhint %}

![](/files/UC21fYUrY6ImhrBjNbyh)

ชิ้นส่วนสีแดง [Pi standoff](https://github.com/PetoiCamp/NonCodeFiles/blob/master/stl/Bittle_standoffPi.stl) สามารถใช้ 3D print ได้

![](/files/tMDBAIvoVdXTsoX3ktJ4)

ดังที่แสดงใน [serial protocol](https://docs.petoi.com/arduino-ide/serial-commands) แสดงว่าอาร์ดูโนไอดีเอสเป็นตัวกลางที่รองรับโทเค็นทั้งหมดที่ถูกเข้ารหัสเป็นตัวอักษร ASCII char string เพื่อให้มนุษย์สามารถอ่านได้ ในขณะที่คอมพิวเตอร์หลัก (เช่น RasPi) รองรับคำสั่งเพิ่มเติมที่เข้ารหัสเป็น binary string เพื่อให้การเข้ารหัสมีประสิทธิภาพมากขึ้น ตัวอย่างเช่น เมื่อเข้ารหัสมุม 65 องศา:

* Ascii: ใช้ 2 ไบต์เพื่อเก็บอักขระ Ascii '6' และ '5'
* Binary: ใช้ 1 byte เพื่อจัดเก็บค่า 65 ซึ่งเปรียบเสมือนกับตัวอักษร Ascii 'A'

{% hint style="info" %}
ถ้าเป็นค่า -113 แบบ Ascii string จะใช้พื้นที่หน่วยความจำ 4 bytes แต่จะเหลือเพียง 1 byte เมื่อเข้ารหัสแบบ binary โดยภายในจะไม่สามารถแสดงผลเป็นตัวอักษรได้ แต่ยังคงเก็บค่าได้เป็นจำนวนเต็ม 1 byte
{% endhint %}

แน่นอนว่าการเข้ารหัสแบบไบนารีจะมีประสิทธิภาพมากกว่าการเข้ารหัสแบบสตริง Ascii อย่างไรก็ตาม ข้อความที่ถูกส่งไปจะไม่สามารถอ่านได้โดยตรงโดยมนุษย์ ใน OpenCat repository ฉันได้ใส่สคริปต์ Python ที่เรียกว่า [ardSerial.py](https://github.com/PetoiCamp/OpenCat/blob/main/serialMaster/ardSerial.py) ที่สามารถจัดการการสื่อสารซีเรียลระหว่าง NyBoard และ Pi ได้ง่ายดาย

### 1. Config Raspberry Pi serial port

In Pi's terminal, type `sudo raspi-config`

ในตัวเลือก **Interface** ให้ค้นหา **Serial** แล้วปิดการเข้าสู่ระบบ **shell** ผ่านทางซีเรียลและเปิดใช้งานอินเทอร์เฟซซีเรียลเพื่อใช้พอร์ต UART หลัก:

1. Run **raspi-config** with **sudo** privilege: `sudo raspi-config`.
2. Find Interface Options -> Serial Port.
3. At the option `Would you like a login shell to be accessible over serial?` select 'No'.
4. At the option `Would you like the serial port hardware to be enabled?` select 'Yes'.
5. Exit raspi-config and reboot for changes to take effect.

{% hint style="danger" %}
You also need to DISABLE the [1-wire interface of Pi](https://www.raspberrypi-spy.co.uk/2018/02/enable-1-wire-interface-raspberry-pi/) to avoid repeating reset signals sent by Pi's GPIO 4.&#x20;
{% endhint %}

{% hint style="info" %}
[A good tutorial on the Pi Serial](https://www.raspberrypi.org/documentation/configuration/uart.md)
{% endhint %}

ถ้าคุณเชื่อมต่อ Pi เข้ากับช่อง 2x5 ของ NyBoard แล้ว พอร์ตซีเรียลของพวกเขาควรจะเชื่อมต่อโดยอัตโนมัติที่แรงดัน 3.3V อย่างไรก็ตามหากไม่ได้เชื่อมต่อแบบนี้ คุณควรใช้หลอดสายขนาดเล็กเพื่อเชื่อมต่อ Rx และ Tx ของชิป AI ของคุณกับ Tx และ Rx ของ NyBoard โดยให้แรงดันตรงกัน

{% hint style="info" %}
Note: If you installed **Ubuntu** **OS** on Raspberry Pi, please config it as follows:

* add `enable_uart=1` to `/boot/config.txt`
* remove `console=serial0,115200` from `/boot/firmware/cmdline.txt` on Ubuntu and similar to`/boot/cmdline.txt` on Raspberry Pi OS
* disable the serial console: `sudo systemctl stop serial-getty@ttyS0.service && sudo systemctl disable serial-getty@ttyS0.service`
* make sure you have `pyserial` installed if you're using the python serial library, not python-serial from apt.
* create the following udev file (I created `/etc/udev/rules.d/50-tty.rules`):

```
KERNEL=="ttyS0", SYMLINK+="serial0" GROUP="tty" MODE="0660"
KERNEL=="ttyAMA0", SYMLINK+="serial1" GROUP="tty" MODE="0660"
```

* reload your udev rules: `sudo udevadm control --reload-rules && sudo udevadm trigger`
* change the group of the new serial devices:

```
sudo chgrp -h tty /dev/serial0
sudo chgrp -h tty /dev/serial1
```

* The devices are now under the tty group. Need to add the user to the tty group and dialout group:

```
sudo adduser $USER tty
sudo adduser $USER dialout
```

* update the permissions for group read on the devices:

```
sudo chmod g+r /dev/ttyS0
sudo chmod g+r /dev/ttyAMA0
```

* reboot

Or just create a script that will do this automatically.
{% endhint %}

{% hint style="info" %}
ถ้าคุณใช้ระบบ Linux ทั่วไป หลังจากเชื่อมต่ออุปกรณ์แบบ uploader เข้ากับคอมพิวเตอร์ของคุณ คุณจะเห็น "ttyUSB#" ในรายการพอร์ตซีเรียล แต่อาจมีข้อผิดพลาดของพอร์ตซีเรียลเมื่อทำการอัปโหลด คุณจะต้องให้สิทธิ์การเชื่อมต่อพอร์ตซีเรียลก่อน โปรดไปที่ลิงค์นี้และทำตามคำแนะนำ: <https://playground.arduino.cc/Linux/All/#Permission>
{% endhint %}

### 2. Change the permission of [ardSerial.py](https://github.com/PetoiCamp/OpenCat/blob/main/serialMaster/ardSerial.py)

ถ้าคุณต้องการรันโปรแกรมเป็นคำสั่ง bash คุณต้องทำให้โปรแกรมสามารถรันได้:

`chmod +x ardSerial.py`

คุณอาจจะต้องเปลี่ยน path ที่ถูกต้องของ Python binary บนบรรทัดแรก:

`#!/user/bin/python`

### 3. Use **ardSerial.py** as the commander of robot

{% hint style="warning" %}
NyBoard มีพอร์ตซีเรียลเดียวเท่านั้น หากต้องการควบคุม Bittle ด้วยพอร์ตซีเรียลของ Raspberry Pi คุณจะต้องถอดอะแดปเตอร์ USB ออกก่อน
{% endhint %}

พิมพ์ `./ardSerial.py <args>` เทียบเท่ากับการพิมพ์ ใน Arduino's serial monitor โดยตัวอย่างเช่น ./ardSerial.py kcrF หมายความว่า "ทำการเคลื่อนที่ด้วยทักษะคลานไปข้างหน้า".

การทำงานของ **ardSerial.py** และการแยกวิเคราะห์ใน **OpenCat.ino** ต้องการการปรับปรุงเพิ่มเติมเพื่อรองรับคำสั่งซีเรียลทั้งหมดในโปรโตคอล<br>

{% hint style="warning" %}
For Nybble:

{% hint style="warning" %}
การเชื่อมต่อกับ Pi อาจทำให้ความสามารถในการเคลื่อนไหวลดลง! จำเป็นต้องใช้แบตเตอรี่ที่มีกำลังมากขึ้น
{% endhint %}

เมื่อเชื่อมต่อกับ Pi แล้ว Nybble อาจจะมีความสามารถในการเคลื่อนไหวที่ลดลง โดยเฉพาะการเคลื่อนไหวที่มีความเข้มข้น เช่นการเดินเหยียบ (โทเคน isktr) ระบบจะถูกจ่ายไฟด้วยแบตเตอรี่ 14500 สองเลเยอร์ คุณอาจจะคิดหาวิธีการจ่ายไฟที่ดีกว่า เช่น การใช้แบตเตอรี่ 7.4 ลิโพแบตเตอรี่สูงสุด หรือ 2S-18650 มีมากมายข้อควรพิจารณาในการร่วมมือซอฟต์แวร์และฮาร์ดแวร์เพื่อให้ได้ประสิทธิภาพที่สมดุลกัน ด้วยตัวขนาดเล็กของ Nybble นั้น จะดีกว่าใช้เป็นแพลตฟอร์มสำหรับเริ่มต้นกรอบการสื่อสารและต้นไม้พฤติกรรมมากกว่าเป็นสัตว์แข่งขัน
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.petoi.com/thai/api/raspberry-pi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
