Jetson 06 Get Chassis Feedback
Get chassis feedback
After the product is turned on, the slave device will continue to feedback all kinds of information to the upper computer by default, and you can get the current working status of the product through these feedback information.
Normally, you need to continuously get the feedback from the slave device, but in this example, we only get a JSON message that is fed back by the slave device (comment out or delete the break line to get the feedback continuously).
Select the following code block, run the code block with Ctrl + Enter, when it gets the first complete JSON information with a T value of 1001, it will jump out of the loop and output feedback information, including the current wheel speed, IMU, pan-tilt angle (if installed), robot arm angle (if installed), power supply voltage, etc.
from base_ctrl
import BaseController import json
base = BaseController('/dev/ttyTHS0', 115200)
# Use an infinite loop to continuously listen to serial data
while True:
try:
# Read a line of data from the serial port, decode it into a string in 'utf-8' format, and try to convert it to a JSON object
data_recv_buffer = json.loads(base.rl.readline(). decode('utf-8'))
# Check whether the parsed data contains the 'T' key
if 'T' in data_recv_buffer:
# If the value of 'T' is 1001, then print the received data and jump out of the loop
if data_recv_buffer['T'] == 1001:
print(data_recv_buffer)
break
# If an exception occurs while reading or processing data, Ignore the exception and continue to listen for the next row of
except:
pass
Get the JSON information sent by the serial port in a non-blocked way
The following code is only used to understand the principle of reading JSON information from the underlying serial port, and the following code blocks cannot be executed.
class ReadLine:
# Constructor to initialize an instance of the ReadLine class
# s: The serial port object passed in to communicate with the serial port.
def __init__(self, s):
self.buf = bytearray() # Initialize a byte array to store the data read from the serial port but not yet processed
self.s = s # Save the incoming serial port object, which will be used to read the serial port data later
def readline(self):
i = self.buf.find(b"\n") # Find if there is a line break in the buffer
if i >= 0:
r = self.buf[:i+1] # If there is a line break, extract the data before the line break
self.buf = self.buf[i+1:] # Update the buffer to remove the processed data
return r
while True:
i = max(1, min( 512, self.s.in_waiting)) # Get the number of bytes that can be read, up to 512 bytes
data = self.s.read(i) # Read data from the serial port
i = data.find(b"\n") # Find a line break
if i >= 0:
r = self.buf + data[:i+1] # If a line break is found, merge the read data with the data in the buffer
self.buf[0:] = data[i+1:] # Update buffer to remove processed data
return r
else:
self.buf.extend(data) # If no newline is found, add data to buffer
- This method is used to read data from the serial port and return a full line of JSON data (separated by a newline character\n).
- If a complete row of data already exists in the buffer, that row of data is returned directly.
- If there is no complete row of data in the buffer, the number of bytes that can be read in the serial buffer is obtained by the in_waiting method, up to a maximum of 512 bytes.
- Read data from the serial port and merge it with the data in the buffer.
- Find if there is a line break in the newly read data, extract the full line of data if there is, and update the buffer.
- If there is no line break, the newly read data is added to the buffer and the reading continues until the line break is found. line character.
Function Features
- Non-blocking: This function uses a non-blocking reading method, even if there is no data to read on the serial port, it will not block the execution of the program, but wait for the data to be read.
- Efficient: A small buffer is used, with a maximum of 512 bytes per read, which reduces memory consumption and processes data in a timely manner to avoid buffer overflow.
- Flexibility: It can flexibly read data of any length, and automatically process data split by line breaks, which is very suitable for reading structured data such as JSON.
- Reliability: Various situations are considered in the process of processing reads, such as insufficient buffer data, no line breaks in the read data, etc., to ensure the accuracy and stability of the reads.
- This function is ideal for reading JSON data in a serial port in real time, especially if a non-blocking read is required.