Jetson 07 Use JSON Commands to Control Slave Device

From Waveshare Wiki
Jump to: navigation, search

Use JSON Instructions to Control the Slave Device

This product is developed using the dual controller architecture, and the host controller sends JSON format commands to the slave device through the serial port (Jetson through the GPIO serial port). Note: This chapter is a precursor to the introduction of the JSON instruction set of the slave device, and the content is similar to the content of the previous Python chassis motion control chapter.

Advantages of JSON Data Format

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

  • Readable: JSON uses a text format that is easy for humans to understand and write, using key-value pairs to organize data, which makes the data easier to read and understand when it is transmitted and stored.
  • Lightweight: Compared with other data formats such as XML, JSON's syntax is more concise and compact, so it is more lightweight, which can reduce the size of data transmission and the occupation of network bandwidth, and improve transmission efficiency.
  • Easy to parse: JSON data structure is simple and clear, easy to parse and serialize, almost all programming languages provide JSON parsing and generation libraries, so that developers can easily process JSON data.
  • Good compatibility with various languages: JSON is supported in almost all programming languages, so it is easy to exchange and communicate data in different platforms and systems.
  • Support for multiple data types: JSON supports a variety of data types, including strings, numbers, booleans, arrays, and objects, etc., which makes it flexible to represent various types of data structures.
  • Seamless integration with web technologies: JSON was originally developed from JavaScript, so it is very tightly integrated with web technologies, and it is very compatible with the JavaScript language, which can be easily used in web applications. Convenient to use in web applications.

Simple JSON command to control the slave device demo

In the following demo, you need to use the correct GPIO device name and use the same baud rate as the slave device (115200 by default).

Before running the following code block, you need to raise the product and keep the drive wheels all off the ground, after calling the following code block, the robot will start to move, be careful not to let the robot fall off the desktop.

from base_ctrl import BaseController 
import time

base = BaseController('/dev/ttyTHS0', 115200)

# The wheel rotates at a speed of 0.2m/s for 2 seconds and then stops 
base.send_command({"T":1,"L":0.2,"R":0.2}) 
time.sleep(2) 
base.send_command({"T":1,"L":0,"R":0})

By calling the code block above, Jetson will first send {"T":1,"L":0.2,"R":0.2} (we will explain the composition of the command in detail in the following chapter), the wheel will start to turn, and after two seconds Jetson will send {"T":1,"L":0,"R":0} This command, the wheel will stop rotating, and it should be noted here that even if you don't send the later command to stop the wheel turning, if you don't send a new command, The wheel will still stop rotating, this is because the slave device contains a heartbeat function, the heartbeat function is used when the host controller has no new instructions sent to the slave device for a long time, the slave device automatically stops the current moving instructions, and the purpose of changing the function is to avoid the host controller from crashing for some reasons and causing the slave device to continue to move.

If you want the robot to continue to move continuously, the host computer needs to send motion control instructions every 2-4 seconds.