# Mind+中的Python代码模式

如果您熟悉Petoi编码积木块提供的API接口，可以直接用Python语言来编写程序代码，您可以在Mind+中切换到Code模式，如下图所示：

<figure><img src="/files/7vNVo3K3YbhcOhus1EN3" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/dsSKdDwfmbOYfbC92ABZ" alt=""><figcaption></figcaption></figure>

**代码**模式其实是一个Python3语言的开发环境。 您可以在其中使用Python3语言编写任意代码，并且可以调用Mind+导入的PetoiRobot库中的所有API接口。

您可以在以下目录中找到PetoiRobot库，其中包含所有与Petoi机器人相关的API接口定义：

* **Windows**\
  C:\Users\\{username}\AppData\Local\DFScratch\extensions\petoi-robot-thirdex\python\libraries\PetoiRobot.py
* **MacOS**\
  /Users/{username}/Library/DFScratch/extensions/petoi-robot-thirdex/python/libraries/PetoiRobot.py

以下是PetoiRobot库中支持的常用API接口函数。 您可以参考**模块**模式下自动生成的Python代码来了解其调用格式。

```python
# 用来打印调试信息
def printH(head, value)

# 关闭陀螺仪
def deacGyro()

# 获取当前所有关节的角度值列表
def getAngleList()
    return angleList

# 获取当前某个关节的角度值 
def getCurAng(index)

# 创建一个[关节序号，绝对角度值]组成的列表
def absValList(num1, num2)

# 创建一个[关节序号，符号，相对角度值]组成的列表
# index: 关节序号
# symbol: "+1" 或者 "-1"
# angle: 相对角度值
def relativeValList(index, symbol, angle)

# 依次/同时旋转关节
# token: 'I' 同时旋转, 'M' 依次旋转
# var: absValList(num1, num2) 或者 relativeValList(index, symbol, angle)
def rotateJoints(token, var, delayTime)

# 播放音乐旋律
def play(token, var, delayTime)

# 打开串口
# port: 串口名称
def openPort(port)

# 自动连接并打开串口
def autoConnect()

# 发送技能串口命令
def sendSkillStr(skillStr, delayTime)

# 调用技能文件中的技能
def loadSkill(fileName, delayTime):

# 发送串口指令
def sendCmdStr(cmdStr, delayTime)

# 发送长串口指令
def sendLongCmd(token, var, delayTime)

# 从模拟输入口读值
def readAnalogValue(pin)

# 从数字输入口读值
def readDigitalValue(pin)

# 向模拟输出口写值
def writeAnalogValue(pin, val)
 
# 向数字输出口写值
def writeDigitalValue(pin, val)

# 关闭串口
def closePort()
```

&#x20;示例代码：

```python
# 代码从此处开始
from PetoiRobot import *    # 如果使用PetoiRobot库中的API接口，必须使用导入库文件

# 首先连接打开串口
autoConnect()

# 调用API接口控制Petoi机器人
sendSkillStr('ksit', 0.5)
sendCmdStr('T', 0.5)
loadSkill("skillFileName", 0.2)

# 最后关闭串口
closePort()
```

您也可以在**模块**模式下的**自动生成**区域中复制代码，在**代码**模式下将其粘贴到代码文件中。 然后您可以编辑并运行代码程序。


---

# 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/tu-xing-hua-bian-cheng/mind+-zhong-de-python-dai-ma-mo-shi.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.
