07 Use JSON Commands to Control Slave Device

From Waveshare Wiki
Jump to: navigation, search

Using JSON Commands to Control the Lower Computer

This product is developed using a dual‑brain architecture. The upper computer sends JSON‑formatted commands to the lower computer via the serial port (the Raspberry Pi uses the GPIO serial port). Note: This chapter is a prerequisite for the later introduction of the lower computer JSON command set. Its content is similar to the previous chapter "Python Chassis Motion Control". If you are already familiar with that chapter, you can briefly understand the advantages of the JSON data format and then directly study the JSON command set.

Advantages of the JSON Data Format

JSON (JavaScript Object Notation) is a lightweight data interchange format that has become one of the standards for data transmission on the Internet. Here are some advantages of JSON:

  • Strong readability: JSON uses a text format that is easy for humans to understand and write, organizing data in key‑value pairs, making the data easier to read and understand during transmission and storage.
  • Lightweight: Compared to other data formats like XML, JSON's syntax is more concise and compact, making it lighter, reducing the size of transmitted data and network bandwidth usage, and improving transmission efficiency.
  • Easy to parse: JSON data structures are simple and clear, easy to parse and serialize. Almost all programming languages provide JSON parsing and generation libraries, making it convenient for developers to handle JSON data.
  • Good compatibility with various languages: JSON is supported in almost all programming languages, so it can be easily used for data exchange and communication across different platforms and systems.
  • Supports multiple data types: JSON supports various data types, including strings, numbers, booleans, arrays, and objects, allowing it to flexibly represent various types of data structures.
  • Seamless integration with web technologies: JSON originated from JavaScript, so it integrates very closely with web technologies. It has excellent compatibility with the JavaScript language and can be easily used in web applications.

Simple JSON Command Example to Control the Lower Computer

In the following example, we use the is_raspberry_pi5() function to determine the current Raspberry Pi model, because the GPIO serial device names are different between the Raspberry Pi 4B and Raspberry Pi 5. You need to use the correct GPIO device name and the same baud rate as the lower computer (default 115200).

Before running the following code block, you should lift the product so that all drive wheels are off the ground. When you run the code block, the robot will start moving. Be careful not to let the robot fall off the table.

from base_ctrl import BaseController
import time

# Function to detect the Raspberry Pi model
def is_raspberry_pi5():
    with open('/proc/cpuinfo', 'r') as file:
        for line in file:
            if 'Model' in line:
                if 'Raspberry Pi 5' in line:
                    return True
                else:
                    return False

# Determine the GPIO serial device name based on the Raspberry Pi model
if is_raspberry_pi5():
    base = BaseController('/dev/ttyAMA0', 115200)
else:
    base = BaseController('/dev/serial0', 115200)

# Make the wheels rotate at 0.2 m/s for 2 seconds, then stop
base.send_command({"T":1,"L":0.2,"R":0.2})
time.sleep(2)
base.send_command({"T":1,"L":0,"R":0})

By running the code block above, the Raspberry Pi first sends the command {"T":1,"L":0.2,"R":0.2} (we will discuss the command structure in detail later). The wheels start rotating. After two seconds, the Raspberry Pi sends the command {"T":1,"L":0,"R":0}, and the wheels stop. Note that even if you do not send the stop command, the wheels will still stop if no new command is sent. This is because the lower computer includes a heartbeat function: when the upper computer does not send any new command for a long time, the lower computer automatically stops the current movement command. This function prevents the robot from continuing to move if the upper computer crashes for some reason.

If you want the robot to keep moving continuously, the upper computer needs to repeatedly send motion control commands every 2‑4 seconds.