> For the complete documentation index, see [llms.txt](https://docs.petoi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.petoi.com/~/changes/MnaoHQ6wOvffal3XIPZR/block-based-programming/petoi-coding-blocks.md).

# Petoi Coding Blocks

## Prepare Mind+

* Download the latest version from [Mind+ official website](https://mindplus.cc/download-en.html)
  * Windows: **>= V1.7.0**
  * Ma&#x63;**:** version: **>= V1.7.3 RC2.0**

{% hint style="warning" %}
If you cannot download the software from Mind+'s official website, you can download a stable version from [the Google Drive folder](https://drive.google.com/drive/folders/1V9WSnNiEOKZznP05W_RPxUD0TqAFFK7T). However, we strongly recommend that you download and use the official latest version.
{% endhint %}

* After the installation is complete, you can open Mind+

{% hint style="info" %}
If the default installation language is Chinese, you can switch to **English** as follows:

<img src="/files/y8C9aqhcfz6ItOCnwh25" alt="" data-size="original">
{% endhint %}

## Watch the video tutorials

We provide [a series of video tutorials](https://www.youtube.com/playlist?list=PLHMFXft_rV6POrzm8O12Nybdy1-FS1ymg) on using Petoi Coding Blocks with [the free Scratch-like robotics coding curriculum](https://www.petoi.com/pages/free-quadruped-robotics-curriculum-scratch-coding).   Be sure to click next to go through all the videos.

{% embed url="<https://www.youtube.com/playlist?list=PLHMFXft_rV6POrzm8O12Nybdy1-FS1ymg>" %}

## Prepare Petoi Robot

{% hint style="info" %}
For the Bittle X, all functional blocks in Mind+ are supported by default. You can skip to the next section [Import Petoi Mind+ extension library](#import-petoi-mind-extension-library).&#x20;
{% endhint %}

* For [Nyboard](https://docs.petoi.com/nyboard/overview) products ([**Nybble**](https://nybble.petoi.com), [**Bittle**](https://bittle.petoi.com)), there are two ways to upload the firmware (Mind+ mode), which supports the Mind+ extension library, and
* [Using the Petoi Desktop App](/~/changes/MnaoHQ6wOvffal3XIPZR/desktop-app/firmware-uploader.md)<br>

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

  * If you just downloaded a new version of this Desktop App. You should click the **Upgrade the Firmware** button. You can select 'N' to preserve the calibration values.
  * If you have upgraded the firmware at least once after a new download, You can click the **Update the Mode Only** button. It's faster to only switch the modes without refreshing the parameters.
* [Using the Arduino IDE](/~/changes/MnaoHQ6wOvffal3XIPZR/arduino-ide/upload-sketch-for-nyboard.md)\
  Please download the latest code from [GitHub](https://github.com/PetoiCamp/OpenCat). Follow the steps for [uploading](https://docs.petoi.com/arduino-ide/upload-sketch-for-nyboard#upload). [Set up the configuration mode](https://docs.petoi.com/arduino-ide/upload-sketch-for-nyboard#2.-setup-the-configuration-mode) and activate this line of code in **OpenCat.ino**\
  \&#xNAN;**`#define MAIN_SKETCH`**

  **`#define GROVE_SERIAL_PASS_THROUGH`**

  Then, upload [the major functionalities sketch](https://docs.petoi.com/arduino-ide/upload-sketch-for-nyboard#10.-upload-the-major-functionalities-sketch) and power on the robot. Use the data cable and [USB uploader](https://docs.petoi.com/communication-modules/usb-downloader-ch340c) to connect with the computer or [the Bluetooth module](https://docs.petoi.com/communication-modules/dual-mode-bluetooth) and complete the pairing.
* Note that the gyroscope function is turned off with the Mind+ mode on NyBoard to save memory space.  The robot won't be able to self-balance and auto-recover.

{% hint style="warning" %}
If you don't use the "[Read or Write analog/digital pin](#write-analog-value)" function block, you can upload [the Standard mode sketch](https://docs.petoi.com/arduino-ide/upload-sketch-for-nyboard#11.-the-module-macro-in-the-code) for Bittle / Nybble and use the Mind+ extension library.\
**Note:**

When the robot is in the **Mind+ mode**, the gyroscope function will be turned off, and the robot will not be able to balance or auto recover.
{% endhint %}

* For [BiBoard](https://docs.petoi.com/biboard/biboard-v0) products (such as [**Bittle**](https://bittle.petoi.com/) and [**Bittle X**](https://bittle-x.petoi.com)), there is no need to modify any software code. By default, all functional blocks in Mind+ are supported. At the same time, the gyroscope function is turned on; that is, the robot can self-balance and auto-recover.

## Import Petoi Mind+ extension library

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

Paste the GitHub URL(<https://github.com/PetoiCamp/Petoi_MindPlusLib>) in the text box of the import interface:

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

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

{% hint style="info" %}
For macOS (**<=V1.7.2 RC3.0**), you need to download [PetoiRobot.zip](https://github.com/PetoiCamp/Petoi_MindPlusLib/raw/main/PetoiRobot.zip) and copy the extracted folder (PetoiRobot) to /Users/{your username}/Documents/mindplus-py/environment/Python3.6.5-64/lib/python3.6/site-packages/

![](/files/AZu2HURU7HI2m7vqwC2M)
{% endhint %}

## Programming and Running

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

{% hint style="warning" %}
Petoi Coding Blocks is a user-extended library of Mind+.&#x20;

If you open **Mind+** by double-clicking the icon![](/files/92YALGSXvECxpZe0MY4r), it will not automatically load this extension library, and you need to re-import it manually every time you open the app.&#x20;

If you open Mind+ by double-clicking the code file(suffix **mp** or **sb3**) that uses this extension library or load these code files after opening Mind+, Mind+ will automatically load this extension library.
{% endhint %}

## The principle and process

This extension library can control the robot without compiling and uploading the code to the robot's main board. Click the "Run" button directly to run the program on the Python level and send instructions to the robot's serial port. If you need to stop the program while running, you can click the "Stop" button anytime. The process of the program can be divided into three steps:

1. &#x20;Open the serial port
2. &#x20;Control the robot
3. &#x20;Close the serial port

## The instructions for blocks

### Open the serial port

There are two ways to open the serial port:

* Automatically identify and open the serial port\
  ![](/files/hSINs1yNcWlGsf0u4q7n)
* Enter the name of the serial port to open the serial port\
  ![](/files/S1iOlfySZBn7om16Mdku)

{% hint style="info" %}
If it fails to open the serial port, you can refer to the printed information in the terminal window to replace the name of the serial port:

![](/files/i0bQFkVv43dnbccZ6361)
{% endhint %}

### Deactivate Gyro

![](/files/MHbk71UQxnL0GeOQMoYS)

When the gyroscope function is turned on, the robot can balance its body in real-time. It may be seen that when the robot is doing preset actions (especially when performing more violent actions), the body will shake back and forth, and even the body will tip over. The robot will automatically perform recovery actions, which may disrupt your preset steps.

If the uploaded sketch is **Mind+ mode** sketch(**`#define GROVE_SERIAL_PASS_THROUGH`** this line is activated), the gyroscope function will be turned off, and the robot will not be able to balance or auto-recover, so there is no need to add this block.

{% hint style="info" %}
If the robot is in [**standard mode**](https://docs.petoi.com/arduino-ide/upload-sketch-for-nyboard#11.-the-module-macro-in-the-code), you'd better deactivate the Gyro after the serial port-opening block. It deactivates the gyroscope function to prevent the robot from performing balance feedback actions in real-time. For example:

![](/files/zfJ4hn5ZenxE6DuuCvvx)
{% endhint %}

### Perform inherent skills

<div align="left"><figure><img src="/files/7jKeQh2wGwuFwgDAkCLb" alt=""><figcaption></figcaption></figure></div>

Use this block to let the robot perform skills pre-built on the robot's main board. Skills from "**sit**" to "**zero**" are **postures** (containing only one action frame).  Skills from "**boxing**" to "**sniff**" are **behaviors** (containing multiple posture frames and are performed only once).  Skills from "**stepping**" to "**trotRight**" are **gaits** (containing multiple posture frames, and are repeated in periodical loops until stopped).&#x20;

After finishing the current block's task, the program will wait a short time (delay xx seconds) before moving to the next block.&#x20;

### Perform the last skill exported from the Skill Composer

<div align="left"><figure><img src="/files/QZ1G3oIJaRp0PrfyTotm" alt=""><figcaption></figcaption></figure></div>

Use this block to let the robot perform the last skill exported from the [Skill Composer](https://docs.petoi.com/desktop-app/skill-composer#export-the-skill).&#x20;

{% hint style="info" %}
It is equivalent to inputting the serial command 'T' in the [serial monitor](https://docs.petoi.com/arduino-ide/serial-monitor) and then delaying the preset time.
{% endhint %}

### Perform the skill in the file

<div align="left"><figure><img src="/files/N3EGVSD6wBrTkR0YWFCD" alt=""><figcaption></figcaption></figure></div>

Use this block to let the robot perform the skill in the skill files, which are in the following directory:

* **Windows**: C:\Users\\{your user name}\\.config\Petoi\SkillLibrary\\{model}
* **MacOS** : /Users/{your user name}/.config/Petoi/SkillLibrary/{model}
* **Linux**: /home/{your user name}/.config/Petoi/SkillLibrary/{model}

The folder name **{model}** is Bittle or Nybble. When [exporting](https://docs.petoi.com/desktop-app/skill-composer#export-the-skill) a skill file from the **Skill Composer**, it will automatically save the skill file to this directory.

{% hint style="info" %}
Tips: you can also copy & paste the [SkillLibrary](https://github.com/PetoiCamp/OpenCat/tree/main/SkillLibrary) folder from the source code of the OpenCat project on GitHub to the ***.config/Petoi*** directory. Therefore, you can use some sample skills for your Mind+ program, and there is no need to use the export function in the Skill Composer.

![](/files/lIGPo06pBWsggE5hDgwn)
{% endhint %}

{% hint style="info" %}
The folder **.config** is a hidden directory on MacOS/Linux but can be visited in the terminal or through a specific view setting:

* MacOS\
  open the directory /Users/{username} in Finder, then press the “**Command**” + “**Shift**” + “**.**” (period) keys at the same time.<br>

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

{% endhint %}

### Rotate joints in a sequence.

<div align="left"><figure><img src="/files/l3eF6NwhpeuazKTPEglh" alt=""><figcaption></figcaption></figure></div>

Use this block to control one joint or multiple joints to rotate in sequence. There are several ways to use the blocks for reference:

* Controls individual joint rotations to an absolute angle value.<br>

  <figure><img src="/files/xSXiZEeFCJfTYwtkw0KV" alt=""><figcaption></figcaption></figure>
* Controls individual joint rotations to a relative angle value.<br>

  <figure><img src="/files/yAUrkV4FKyn1Vl0KgfTM" alt=""><figcaption></figcaption></figure>
* Control multiple joints to rotate sequentially to **absolute** angle values or **relative** angle values.<br>

  <figure><img src="/files/G9fc47tePSrHTivPlfKq" alt=""><figcaption></figcaption></figure>
* Use the joint angle list to control multiple joints to rotate to absolute angle values in a sequence.<br>

  <div align="left"><figure><img src="/files/KZDShPYUizPcrOsFR8pd" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}

* <img src="/files/j5rjUZf7qLCmZdG0KBEH" alt="" data-size="original">, <img src="/files/9b6l2VUoNHYuxEhfffBz" alt="" data-size="original">represents a list consisting of a [joint index](https://app.gitbook.com/o/-M-_eWZUjFA4usjshHcZ/s/-MQ6a951Q6Jn1Zzt5Ajr-887967055/~/changes/285/petoi-robot-joint-index) and an angle value. For example, \[Head panning to 30 degrees] represents the list \[0, 30].
* <img src="/files/FyKxGaW0vddwqdYXceTc" alt="" data-size="original">\
  It consists of one or more pairs of[ joint index](https://app.gitbook.com/o/-M-_eWZUjFA4usjshHcZ/s/-MQ6a951Q6Jn1Zzt5Ajr-887967055/~/changes/285/petoi-robot-joint-index) + angle value, and the specific format is as follows: \
  \[joint index, angle value, joint index, angle value...]
  {% endhint %}

### Rotate joints simultaneously&#x20;

<div align="left"><figure><img src="/files/nCFY0sIXcipRPkDrjeRW" alt=""><figcaption></figcaption></figure></div>

Using this block can control multiple joints to rotate at the same time. There are several ways to use the blocks for reference:

* Control multiple joints to rotate to absolute angle values or relative angle values at the same time<br>

  <figure><img src="/files/soKQzuS4ylvcdCHG4L0R" alt=""><figcaption></figcaption></figure>
* Use the joint angle list to control the simultaneous rotation of multiple joints to absolute angle values.<br>

  <div align="left"><figure><img src="/files/Vh2XMoCWbfZ7yWfafXdX" alt=""><figcaption></figcaption></figure></div>

### Get the current angle value of a joint.

<div align="left"><figure><img src="/files/Brc9uT3zfvELerhTX5mN" alt=""><figcaption></figcaption></figure></div>

Use this block to get the current angle value of the selected joint. It is recommended to assign it to a variable first and then use the variable and algorithm to control other joints to rotate.

{% hint style="info" %}
The return value of this block is only an angle value, which cannot be filled in the "Turn sequentially" and "'Turn simultaneously" blocks alone.
{% endhint %}

Demo code:

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

{% file src="/files/dTxtuiDyT9RbbPkwqO4S" %}

### Transform to frame

<div align="left"><figure><img src="/files/hRZwOu0SW2rvJtJSCgbI" alt=""><figcaption></figcaption></figure></div>

Use this block to control all joints to rotate at the same time. Please use it with the "Action frame" block. As shown below:

<div align="left"><figure><img src="/files/9JGREsuYLcSu0SolRpyD" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
The "Action frame" block represents a list of 16 angle values. Each angle value corresponds to the absolute angle value to which the corresponding [joint index](https://app.gitbook.com/o/-M-_eWZUjFA4usjshHcZ/s/-MQ6a951Q6Jn1Zzt5Ajr-887967055/~/changes/285/petoi-robot-joint-index) servo rotates.
{% endhint %}

### Play a melody

![](/files/NqKpHKy7twOf1TuiguBg)

Use this block to control the robot to play music. There are several ways to use  blocks together for reference:

* A list made up of multiple "Tone + Duration" blocks<br>

  <div align="left"><figure><img src="/files/gOG2tmvrP0Xn1zS331B4" alt=""><figcaption></figcaption></figure></div>
* Using a tone duration list<br>

  <div align="left"><figure><img src="/files/3NUr3wQkTxyhf3Qrwd6D" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
![](/files/7KMmq9wgWd8om7CeTaO3)

Consists of one or more pairs of Tone + Duration, the specific format is as follows:

\[tone, duration, tone, duration, tone, duration...]
{% endhint %}

### Execute a serial command

<div align="left"><figure><img src="/files/sPOyyF6TzwjUHvSLaStM" alt=""><figcaption></figcaption></figure></div>

Use this block to send a serial command to the robot, which can provide you with more and more flexible control methods. For example, you can input "**kkcL**" (kick the left front leg), and "**khiR**" (raise the right front leg to say hello). For more serial port commands, please refer to [the serial protocol](https://docs.petoi.com/api/serial-protocol).&#x20;

### Write analog value

<div align="left"><figure><img src="/files/MqRx61qOySKFFMj7szCw" alt=""><figcaption></figcaption></figure></div>

Use this block to write an analog value to a specified pin. Analog value range: 0\~255

### Read analog value

![](/files/PlKNs3jjcUo0R2kf1M78)

Use this block to read an analog value from a specified pin.

### Write digital value

![](/files/HfD7apmmfLmGvZORMmaG)

Use this block to write a high/low-level value to the specified pin. High-level: 1; Low-level: 0.

### Read digital value

![](/files/WpiPm0lFfejX3m1gjTAA)

Use this block to read the high/low-level value of the specified pin.

### Read Ultrasonic sensor distance

<div align="left"><figure><img src="/files/l6XlAPafftMxUdjVJztQ" alt=""><figcaption></figcaption></figure></div>

Use this block to read the distance value from the ultrasonic sensor.

For the [Petoi RGB Ultrasonic Sensor](https://docs.petoi.com/extensible-modules/ultrasonic-sensor) (or **RUS-04**), you can set the two pins ( Trigger and Echo) like this:

* **NyBoard** (connects to the D6 and D7 pins)<br>

  <div align="left"><figure><img src="/files/Jc2cIVpIGzq7HzwkMLKq" alt=""><figcaption></figcaption></figure></div>
* **BiBoard** (connects to the Rx and Tx pins)<br>

  <div align="left"><figure><img src="/files/Jf90Y6ovj7a9NmRH1eAg" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
For other ultrasonic sensor models (e.g., **HC-SR04** connects to the D6 and D7 pins), you can set the two pins like this:

<img src="/files/wo5NE5VbCl2g8ZYjK0Gr" alt="" data-size="original">
{% endhint %}

### Close the serial port

![](/files/3CNpi9KxnwBBHuT5JaNg)

Generally, at the end of the program, it is recommended to use this block to close the serial port communication.

### Demos

We provide some demos to download for reference in the GitHub repository (Petoi\_MindPlusLib/examples).

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

### Free curricular

{% embed url="<https://drive.google.com/drive/folders/1vcdMiLY90WVypL1oHU-082e1qkAn8qW7>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.petoi.com/~/changes/MnaoHQ6wOvffal3XIPZR/block-based-programming/petoi-coding-blocks.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
