Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible SOLUTION - Index out of range error #12

Open
T-622 opened this issue Oct 12, 2024 · 2 comments
Open

Possible SOLUTION - Index out of range error #12

T-622 opened this issue Oct 12, 2024 · 2 comments

Comments

@T-622
Copy link

T-622 commented Oct 12, 2024

Hi everyone,

Background:
I was tinkering around with this library and encountered an error with an "Out Of Range" issue outputting self.sync_byte1 = raw_bytes[0] IndexError: index out of range. I combed through the new documentation for the "Cx, Ax, and Sx" series of firmware versions and discovered that this error is related to the sensor not reciving data.

Here is the documentation for V2.8 of the communication protocol:

LR001_SLAMTEC_rplidar_S&C series_protocol_v2.8_en.pdf

This is documentation for V2.3 of the communication protocol:

LR001_SLAMTEC_rplidar_protocol_v2.3_en.pdf

Notice that under pyrplidar_protocol.py included in the repo has the following on line 17: RPLIDAR_CMD_FORCE_SCAN = b'\x20'. Under V2.8 of the protocol, "FORCE_SCAN" no longer exists, and thus issuing 0x21 over serial to the LiDAR no longer works, thus we get an out of range error, since no data is returned; the sensor is confused by this command if it has a firmware version recent enough (I cannot tell what versions discontinued this function, just that it has been changed).

Possible Solution:
If you have this issue, you MUST check the baud rate, and set it to the correct one. Mine was 480600 with the C1 LiDAR module, but yours may differ. Examples below:
A1 / A1M8: 57600 baud
A2M8: 115200 baud
A2M12 / A3 / S1: 256000 baud
S2 / S3: 1000000 baud

You can verify that the communication is working and the baud rate works by checking the "check_connection.py" example included under the "examples" folder of this repo.

Once that works, and the example scans seem to be your issue, this is what i've discovered. Given my above reasonning, we can no longer issue 0x21 to the sensor for the "FORCE_SCAN", but we need to instead use "SCAN", which is issued with 0x21. To fix this issue, you can either change line 17:

From RPLIDAR_CMD_FORCE_SCAN = b'\x21'
To RPLIDAR_CMD_FORCE_SCAN = b'\x20'

Which will change the FORCE_SCAN command to the regular scan command. Else, you can change line 169 in pyrplidar.py

From self.send_command(RPLIDAR_CMD_FORCE_SCAN)
To self.send_command(RPLIDAR_CMD_SCAN)

And thus, the "force_scan()" command will issue a normal scan and spin up the motor. Please feel free to comment if you have found anything different, or if this work solves the issue for you! Note, this support issue exists both in the Adafruit Circuitpython library and Roboticia library from what I can tell, since they all issue the 0x21 command for forcing to scan.

For those interested:
The "FORCE_SCAN" command forces the lidar to output data for position even if the motor is not spinning.

@ralexy
Copy link

ralexy commented Oct 14, 2024

Hi T-622, thanks for the detailed response!

I'm having a really strange issue with my SLAMTEC C1 LiDAR on my Raspberry Pi 4. It works fine on my PC, but on the Pi, it causes the whole system to crash for a few seconds when I try to get scan data! I've tried your suggestions (baud rate, edit library), but no luck. Any ideas what could be causing such a dramatic crash? Here's the code and error:

from pyrplidar import PyRPlidar
import time
 
lidar = PyRPlidar()
lidar.connect(port="/dev/ttyUSB0", baudrate=460800)
# Linux   : "/dev/ttyUSB0"
# MacOS   : "/dev/cu.SLAB_USBtoUART"
# Windows : "COM5"
 
 
info = lidar.get_info()
print("info :", info)
 
health = lidar.get_health()
print("health :", health)
 
samplerate = lidar.get_samplerate()
print("samplerate :", samplerate)
 
lidar.set_motor_pwm(500)
 
scan_modes = lidar.get_scan_modes()
print("scan modes :")
for scan_mode in scan_modes:
    print(scan_mode)
 
time.sleep(2)
 
scan_generator = lidar.force_scan()   
 
 
#scan_generator = lidar.start_scan_express(4)
 
for count, scan in enumerate(scan_generator()):
    print(count, scan)
    if count == 20: break
 
 
lidar.set_motor_pwm(0)
lidar.stop()
 
lidar.disconnect()

PyRPlidar Info : device is connected info : {'model': 65, 'firmware_minor': 1, 'firmware_major': 1, 'hardware': 18, 'serialnumber': 'XYZ'} health : {'status': 0, 'error_code': 0} samplerate : {'t_standard': 200, 't_express': 200} {'name': 'Standard', 'max_distance': 4096, 'us_per_sample': 51200, 'ans_type': 'NORMAL'} {'name': 'DenseBoost', 'max_distance': 10240, 'us_per_sample': 51200, 'ans_type': 'DENSE_CAPSULED'} scan modes : {'name': 'Standard', 'max_distance': 4096, 'us_per_sample': 51200, 'ans_type': 'NORMAL'} {'name': 'DenseBoost', 'max_distance': 10240, 'us_per_sample': 51200, 'ans_type': 'DENSE_CAPSULED'} 0 {'start_flag': False, 'quality': 20, 'angle': 64.625, 'distance': 3220.75} 1 {'start_flag': False, 'quality': 8, 'angle': 146.59375, 'distance': 4177.0} 2 {'start_flag': False, 'quality': 20, 'angle': 166.25, 'distance': 7390.25} 3 {'start_flag': False, 'quality': 29, 'angle': 218.78125, 'distance': 843.5} Traceback (most recent call last): File "/home/alexy/Downloads/test_lidar.py", line 34, in <module> for count, scan in enumerate(scan_generator()): File "/home/alexy/Downloads/pyrplidar-master/pyrplidar.py", line 176, in scan_generator data = self.receive_data(discriptor) File "/home/alexy/Downloads/pyrplidar-master/pyrplidar.py", line 54, in receive_data raise PyRPlidarProtocolError() pyrplidar_protocol.PyRPlidarProtocolError PyRPlidar Info : device is disconnected

This one doesn't works too :

from pyrplidar import PyRPlidar
import time
 
lidar = PyRPlidar()
lidar.connect(port="/dev/ttyUSB0", baudrate=460800)
# Linux   : "/dev/ttyUSB0"
# MacOS   : "/dev/cu.SLAB_USBtoUART"
# Windows : "COM5"
 
 
info = lidar.get_info()
print("info :", info)
 
health = lidar.get_health()
print("health :", health)
 
samplerate = lidar.get_samplerate()
print("samplerate :", samplerate)
 
lidar.set_motor_pwm(500)
 
scan_modes = lidar.get_scan_modes()
print("scan modes :")
for scan_mode in scan_modes:
    print(scan_mode)
 
time.sleep(2)
 
#scan_generator = lidar.force_scan()   
 
 
scan_generator = lidar.start_scan_express(4)
 
for count, scan in enumerate(scan_generator()):
    print(count, scan)
    if count == 20: break
 
 
lidar.set_motor_pwm(0)
lidar.stop()
 
lidar.disconnect()

PyRPlidar Info : device is connected info : {'model': 65, 'firmware_minor': 1, 'firmware_major': 1, 'hardware': 18, 'serialnumber': 'XYZ'} health : {'status': 0, 'error_code': 0} samplerate : {'t_standard': 200, 't_express': 200} {'name': 'Standard', 'max_distance': 4096, 'us_per_sample': 51200, 'ans_type': 'NORMAL'} {'name': 'DenseBoost', 'max_distance': 10240, 'us_per_sample': 51200, 'ans_type': 'DENSE_CAPSULED'} scan modes : {'name': 'Standard', 'max_distance': 4096, 'us_per_sample': 51200, 'ans_type': 'NORMAL'} {'name': 'DenseBoost', 'max_distance': 10240, 'us_per_sample': 51200, 'ans_type': 'DENSE_CAPSULED'} Traceback (most recent call last): File "/home/alexy/Downloads/test_lidar.py", line 32, in <module> scan_generator = lidar.start_scan_express(4) File "/home/alexy/Downloads/pyrplidar-master/pyrplidar.py", line 138, in start_scan_express discriptor = self.receive_discriptor() File "/home/alexy/Downloads/pyrplidar-master/pyrplidar.py", line 42, in receive_discriptor discriptor = PyRPlidarResponse(self.lidar_serial.receive_data(RPLIDAR_DESCRIPTOR_LEN)) File "/home/alexy/Downloads/pyrplidar-master/pyrplidar_protocol.py", line 110, in __init__ self.sync_byte1 = raw_bytes[0] IndexError: index out of range PyRPlidar Info : device is disconnected

Please let me know if you have any ideas – I'm desperate! 🙏

@T-622
Copy link
Author

T-622 commented Oct 14, 2024

I will fire up my Pi 4 and give this code a try to see if I can reproduce your error, will give you an update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants