# 通过树莓派控制机器人

{% hint style="warning" %}
机器人的基本运动功能是不需要连接树莓派的，只有当您安装了树莓派时才需要阅读本章节。

如果想和树莓派的串口通信，就需要拔出NyBoard上的串口适配器。
{% endhint %}

您可以在 NyBoard 上焊接一个 2x5 插座以插入 Raspberry Pi。 Pi 3A+ 最适合 NyBoard 的尺寸。

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

### Nybble

![](/files/VQrQa9FNW1iv9fb4A2Sv)

### Bittle

{% hint style="danger" %}
注意：焊上了2x5的插座后，Bittle背上的盖子就盖不上了。
{% endhint %}

![](/files/bX3YUAY3Ga4GheCnIjVk)

红色的[树莓派卡扣](https://github.com/PetoiCamp/NonCodeFiles/blob/master/stl/Bittle%20%26%20BittleX/RaspberryPiStandOff/Pi3A_standOff.stl)可以3D打印。

![](/files/e47VfFZoDWXEpIELsBWF)

如[串口协议](https://docs.petoi.com/v/chinese/chuan-kou-xie-yi)所示，Arduino IDE的串口监视器支持的命令参数全部编码为AscII码字符串，主机（例如RasPi）支持额外的命令，大多数命令都编码为二进制字符串以进行有效编码。例如，当编码角度为65度时：

* Ascii: 2个字节的char  '6' and '5'
* Binary: 一个Byte的char，‘A’

{% hint style="info" %}
值 -113 如何处理？ 它需要四个字节作为 Ascii 字符串，但在二进制编码中仍然只需要一个字节，尽管内容将不再可作为字符打印。
{% endhint %}

显然，二进制编码比Ascii字符串有效得多。但是，传输的消息将不会直接被人类读取。在OpenCat存储库中，有一个简单的Python脚本[ardSerial.py](https://github.com/PetoiCamp/OpenCat/blob/main/serialMaster/ardSerial.py)，它可以处理NyBoard和Pi之间的串行通信。

### 1. 设置树莓派串口

在树莓派的终端输入`sudo raspi-config`

在**Interface**选项下找到 **Serial**，禁用串口登陆，开放串口界面（Disabled the serial login shell and enable the serial interface）：

1. 使用 sudo 权限命令运行 raspi-config：**sudo raspi-config**
2. 找到 Interface Options-> SerialPort
3. 选项 `Would you like a login shell to be accessible over serial? 选择`**`No`**
4. `选项 Would you like the serial port hardware to be enabled? 选择`**`Yes`**
5. 退出 raspi-config 并重新启动机器人（关闭、打开电池电源）以使更改生效。

{% hint style="danger" %}
你还需要禁用Pi的[1-wire接口](https://www.raspberrypi-spy.co.uk/2018/02/enable-1-wire-interface-raspberry-pi/)防止Pi的GPIO 4不断地发送重启信号。
{% endhint %}

{% hint style="info" %}
树莓派官方网站上有一个很好的[教程](https://www.raspberrypi.com/documentation/computers/configuration.html)。
{% endhint %}

只要把树莓派和NyBoard通过可选的2x5插座相连，它们的串口就自动连接起来了，逻辑电平是3.3V。如果您连接的是其他AI芯片，就要注意Rx和Tx的连接，以及它们的逻辑电平。外接板上的Rx要连接NyBoard的Tx，外接板上的Tx要连接NyBoard的Rx，还需要连接NyBoard的参考电平输入和外接板的逻辑电压。

{% hint style="info" %}
注意：如果您在树莓派上安装了 Ubuntu 操作系统，请进行如下配置：

* 在 `/boot/config.txt 添加 enable_uart=1`
* 在 Ubuntu系统的文件 `/boot/firmware/cmdline.txt中删除`&#x20;

`console=serial0,115200（类似于在树莓派系统中文件/boot/cmdline.txt）`

* 运行以下命令关闭串行控制台：

`sudo systemctl stop serial-getty@ttyS0.service && sudo systemctl disable serial-getty@ttyS0.service`

* 请确保您已安装python 串行库 pyserial，而不是来自 apt 的 python-serial。
* 创建 udev file (`/etc/udev/rules.d/50-tty.rules`),并添加以下内容：

  ```
  KERNEL=="ttyS0", SYMLINK+="serial0" GROUP="tty" MODE="0660"
  KERNEL=="ttyAMA0", SYMLINK+="serial1" GROUP="tty" MODE="0660"
  ```
* 运行以下命令重载 udev 规则：

`sudo udevadm control --reload-rules && sudo udevadm trigger`

* 运行以下命令更改新的串口设备的用户组：

sudo chgrp -h tty /dev/serial0

sudo chgrp -h tty /dev/serial1

* 这些设备现在位于 tty 组下。 运行以下命令将用户添加到 tty 组和dialout 组：

sudo adduser $USER tty

sudo adduser $USER dialout

* 运行以下命令更新设备上组读取的权限：

sudo chmod g+r /dev/ttyS0

sudo chmod g+r /dev/ttyAMA0

* 重启树莓派（关闭、打开机器人电池电源）

或者创建一个自动执行这些操作的脚本。
{% endhint %}

{% hint style="info" %}
如果是通用的Linux系统，将上载器连接到计算机后，您将在串行端口列表中看到“ ttyUSB＃”。但是上传时，您仍然可能会遇到串行端口错误。您将需要授予串行端口权限。请转到此链接并按照说明进行操作 <https://playground.arduino.cc/Linux/All/#Permission>
{% endhint %}

### 2. 修改ardSerial.py的权限

如果您想在bash里运行ardSerial.py，您还需要为它添加可执行权限：

`chmod +x ardSerial.py`

同时您可能需要把文件第一行的Python安装路径`#!/user/bin/python`改成您电脑上的设置。

### 3. 用 **ardSerial.py** 向机器人发送指令

在树莓派的终端输入`./ardSerial.py <args>` 就相当于之前在Arduino串口监视器里输入 \<args> 。比如，`./ardSerial.py kcr` 表示“执行 s**k**ill **cr**awl”。

Python脚本**ardSerial.py** 和**Nybble.ino**里对应的解析代码都比较简略，需要逐渐完善。

{% hint style="warning" %}
对于Nybble:

{% hint style="warning" %}
连接到 Pi 时可能会降低运动能力！ 需要更强的电池。
{% endhint %}

某些快速的步态，比如小跑（trot，指令是`ktr`），可能会导致系统重启。目前系统由两节14500电池串联供电，您可以升级供电方案，比如模型专用的锂聚合物动力电池。

在改装过程中需要综合考虑软硬件的协作。对于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/chinese/api/tong-guo-shu-mei-pai-kong-zhi-ji-qi-ren.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.
