Aug-14-2023, 09:03 PM
(This post was last modified: Aug-14-2023, 10:38 PM by deanhystad.)
The arguments passed are wrong.
In write_command(), port is a Serial object, device is the LIDAR device(s) to receive the command, and command is the command you want the lidar device to execute. device and command are defined in the document: LDROBOT_LD07_Development manual_v1.0_en.pdf".
In your example, your serial device is called "device". I suggest you rename because "device" should be the lidar device. You want to send a command to device = 1 (you only have one LIDAR device), and the command you want to send is 2, PACK_GET_DISTANCE. To write a command to request distance information from you 1 lidar device you should call.:
Inside write_command(), it will compute a checksum = 3, pack a command of bytes = b'\xAA\xAA\xAA\xAA\x01\x02\x00\x00\x00\x00\x03' and wrote these bytes out the serial port.
It might be worthwhile defining some commands at the top of the file, like:
This lets you write a command like this in your code:
I would rewrite your code above to look (somewhat) like this:
I also found a bug while looking at the code.
I can't run any of this so there are likely other bugs. Heck, the byte order might not even be right for the pack/unpack calls.
In write_command(), port is a Serial object, device is the LIDAR device(s) to receive the command, and command is the command you want the lidar device to execute. device and command are defined in the document: LDROBOT_LD07_Development manual_v1.0_en.pdf".
In your example, your serial device is called "device". I suggest you rename because "device" should be the lidar device. You want to send a command to device = 1 (you only have one LIDAR device), and the command you want to send is 2, PACK_GET_DISTANCE. To write a command to request distance information from you 1 lidar device you should call.:
1 |
write_command(device, 1 , 2 ) |
It might be worthwhile defining some commands at the top of the file, like:
1 2 3 4 5 |
MY_LIDAR = 1 PACK_GET_DISTANCE = 0x2 PACK_STOP = 0XF PACK_GET_CDE = 0X12 PACK_ACL = 0x10 |
1 2 3 4 |
# Which is easier to understand? This: write_command(serial_port, MY_LIDAR, PACK_GET_DISTANCE) or this: write_command(device, 1 , 2 ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import serial import struct # define some LIDAR stuff MY_LIDAR = 1 PACK_GET_DISTANCE = 0x2 PACK_STOP = 0XF PACK_GET_CDE = 0X12 PACK_ACL = 0x10 serial_port = serial.Serial( '/dev/ttyUSB0' , baudrate = 921600 , timeout = 10.0 , bytesize = 8 , parity = 'N' , stopbits = 1 ) def write_command(port, device, command): checksum = (device + command) & 15 msg_bytes = struct.pack( "<LBBHHB" , 0xAAAAAAAA , device, command, 0 , 0 , checksum) port.write(msg_bytes) def read_data(port): # Read the header header = port.read(size = 10 ) if len (header) < 10 : raise serial.SerialTimeoutException( "Timed out reading header" ) # Verify the header is mostly correct start_chars, device, command, offset, size = struct.unpack( "<LBBHH" , header) if start_chars ! = 0xAAAAAAAA : raise serial.SerialException( "Start characters don't match" ) if size > 800 : raise serial.SerialException( "Data cannot exceed 800 bytes." ) # You could also check if the command code is something you recognize. # Read the data. Data is followed by the message checksum. data = port.read(size = size + 1 ) if len (data) < size + 1 : raise serial.SerialTimeoutException( "Timed out reading data" ) # Verify the checksum. Checksum is the sum of the entire message EXCEPT the 4 byte start character. and checksum. checksum = ( sum (header[ 4 :]) + sum (data[: - 1 ])) & 15 if checksum ! = data[ - 1 ]: raise serial.SeriaException( "Checksum is invalid" ) return device, command, data[: - 1 ] write_command(serial_port, MY_LIDAR, PACK_GET_DISTANCE) device, command, data = read_data(serial_port) print ( f 'reply from {device}.{command} = {data}' ) |
1 2 |
# data = port-read(size=size+1) data = port.read(size = size + 1 ) |