SkoltechRobotics / rplidar

Python module for RPLidar A1 and A2 rangefinder scanners
MIT License
185 stars 126 forks source link

rplidar_Modified_A2 #3

Closed luckywang95 closed 7 years ago

luckywang95 commented 7 years ago
newpavlov commented 7 years ago

Thank you! Though you could've committed changes directly into rplidar.py. This way it's easier to review them.

I've modified your code a bit, check changes if you are interested. It's mostly cleanups and compatibility changes for Python 2.

Modified code works with A1, but we need check it with A2 to be safe.

Also I've removed your changes to the iter_measurments, iter_scans and clear_input. Was there a particular reason for them?

luckywang95 commented 7 years ago

I didn't make any modification to the functions you mentioned. At least in the version i downloaded. I will update you after i tested it with my A2. I am currently still working on running my completed code on raspberry pi.

btw, i have just completed a code similar to animate.py but i failed to do it by using FuncAnimation and iter_measurment because it keep giving me "Incorrect descriptor starting bytes" error. I think it is due to the start() in the iter_measurement(). Do you mind tell me how to perform FuncAnimation if i need to use the iter_measurment() instead of iter_scan()? However, I have completed a similar code but using different approach.

newpavlov commented 7 years ago

I didn't make any modification to the functions you mentioned. At least in the version i downloaded.

Ah, so it was probably an older version of the module.

because it keep giving me "Incorrect descriptor starting bytes" error.

Most probably it was due to the unread leftovers in the input buffer. Try to call clear_input() before starting iter_scans() or add correct shutdown in the end of your script.

iter_measurments() yields points instead of the whole scans, so I doubt you want to use it.

luckywang95 commented 7 years ago

I think it is because i call iter_measurement() in my FuncAnimation() def lidar_plot(self): for measurement in lidar.iter_measurments_results_only(): lidar_plotting = animation.FuncAnimation(fig, lidar_plot, init_func = init, interval = 0, blit = True) so the scan has started but i still send command for get_health(), therefore, i always get the error. I dont know how to bypass that. i use iter_measurement because i need the startBit. Instead of displaying the data constantly, i reduced the plot update speed, e.g. i update the plot only after 2 or 3 startBit is detected and i only store scan data before fig.canvas.update() so I have less data to transmit later.

newpavlov commented 7 years ago

You don't need start flag for that. You can simply write:

i = 0
for scan in lidar.iter_scans():
    if i == 10:
        process(scan)
        i = 0
    i += 1

Also before skipping scans, test speed of the new animate.py.

Also as far as I remember you can't send commands in the measurement mode.

luckywang95 commented 7 years ago

iter_scans() give u a list of distance, angle, and quality? how about when distance == 0? because now i abandoned points with distance == 0. by abandon these points, i can reduce the number of data transmitted to my MCU later.

for the new animate.py, the speed has improved but still not realt-time. By skipping only 2 scans and at max speed, i can get relatively smooth and real-time display. if at default speed, skipping 1 scans is enough.

I think we can send command such as get_health() but just either the program will read the response scan data or a mixture of health status and scan data.

newpavlov commented 7 years ago

I think RPLidar does not return measurements with zero distance or quality. But it should be checked.

Regarding get_health() I meant what module wasn't written with such use case in mind. I don't think it makes much sense to check health status constantly.

luckywang95 commented 7 years ago

I did write the raw data to a txt file and analyze and it did give me 0 quality and 0 distance. at least for A2, it did. A1 didnt give data with 0 quality or 0 distance?

newpavlov commented 7 years ago

This is strange, because in the iter_scan() explicit filtering of points with zero quality implemented like this:

if data[1] > 0:
    scan.append(data[1:])

Maybe you used iter_measurments()? It does not perform any filtering and yields all points reported by the sensor, even invalid ones.

I think it's better to continue discussion not related to this pull request in the email. If you don't have any additions to this PR I will merge it.

luckywang95 commented 7 years ago

Dont merge yet because i am able to start the motor but not able to stop the motor nor set_pwm. i am currently looking at the code.

newpavlov commented 7 years ago

Thank you for your work!

I will need to make some changes (mainly documentation and support for express scan) before publishing new version to PyPI.