Closed ysshah closed 7 years ago
Actually, I just realized that the reason for my bugs might be that my computer is trying to execute lidar methods before the lidar is fully initialized. Calling time.sleep(0.1)
after initializing the lidar with lidar = RPLidar('/dev/ttyUSB0')
seems to have solved the problem. Nevertheless, the changes in this pull request might be required to support future versions of pySerial, where the setDTR
method might be completely removed.
Thank you for your contribution!
How about trying to use laser.stop()
and laser.clear_input()
? First command will stop measuring process, so sensor will stop sending data and the second one will clear the the serial port buffer. It seems what you have leftover data from previous run, current code is not able to handle such situations correctly. I'll think of how better to handle it, probably I will add checks if buffer empty or not.
I will try to check your changes in the following days.
apparently dtr is only for a1, but I think you should not send the command for a1 and a2. Better to add a self.model ='a2' and filter the start and stop command on the model
@jjehl Do you experience any problems with DTR on A2? Serial protocol wise it should not create any problems. I would like to not introduce any distinctions between A1 and A2 without a real need for it.
@ysshah Sorry it longer than I intended. I've merged your PR, will publish it on PyPI in the following week.
I didn't understand correctly your code. There isn't any problem with the DTR on my A2.
any idea why I get ths error please?
It works for a while but the gives error
Traceback (most recent call last):
File "python_osc_send.py", line 70, in
Sorry if its a bit untidy! I realise its to do with BlockingIOError and I have tried to enlighten myself from searching for a clue to solve it. Have you a fix/come across this issue?
Best,
James
The code is here:
"""Small example OSC client Extended to work with Lidar As part of work for final project MA Computational Art 2018 Version 0.0 james@tregaskis.org code adapted from https://pypi.org/project/python-osc/ and https://github.com/SkoltechRobotics/rplidar
This program sends values from Lidar sensorfrom www.coolcomponents.com /filter address, waiting for 1 seconds between each value. """ import argparse import random import time import sys from rplidar import RPLidar
from pythonosc import osc_message_builder from pythonosc import udp_client PORT_NAME = '/dev/ttyUSB0' lidar = RPLidar(PORT_NAME) lidar = RPLidar('/dev/ttyUSB0')
info = lidar.get_info() print(info)
health = lidar.get_health() print(health)
try: if name == "main": parser = argparse.ArgumentParser() parser.add_argument("--ip", default="192.168.0.40", help="The ip of the OSC server") parser.add_argument("--port", type=int, default=5005, help="The port the OSC server is listening on") args = parser.parse_args()
client = udp_client.SimpleUDPClient(args.ip, args.port)
maxAngle=0
# angle_old=[None]*360;
# angle_new=[None]*360;
distance_old=[None]*360;
distance_new=[None]*360;
for measurment in lidar.iter_measurments():
# strength of laser, angle of reading, distance
# print(measurment[1],measurment[2],measurment[3])
if int(measurment[1]) > 0:
# print (int(measurment[1]))
# print (int(measurment[2]))
# angle_new[int(measurment[2])]=int(measurment[2])
distance_new[int(measurment[2])]=int(measurment[3])
# if angle_new[int(measurment[2])] != angle_old[int(measurment[2])] and
if distance_new[int(measurment[2])] != distance_old[int(measurment[2])]:
# if angle_new[30]!=angle_old[30] or distance_new[30]!=angle_old[30]:
# st = (angle_new[int(measurment[2])])
an = int(measurment[2])
di = (distance_new[int(measurment[2])])
print(an,di)
# angle_old[int(measurment[2])]=angle_new[int(measurment[2])]
distance_old[int(measurment[2])]=distance_new[int(measurment[2])] client.send_message("/st", [ an, di])
# client.send_message("/di", di)
# print([st,an,di])
#client.send_message("/angle", angles)
#client.send_message("/strength", strengths)
#client.send_message("/distance", distances)
# print (angles)
#client.send_message("/distance", int(measurment[3]))
# else:
# if measurment[2]==None or measurment[2]=="":
# pass
# #strengths.append(int(measurment[1]))
# angles.append(int(measurment[2]))
# #distances.append(int(measurment[3]))
# maxAngle = maxAngle+1
time.sleep(1)
except KeyboardInterrupt: print('Stopping.')
lidar.stop() lidar.disconnect()
I am using Python 3 with the RPLIDAR A2. I don't know too much about Serial port communication, but often when I restarted the code or ran it multiple times, I would get an
Incorrect descriptor starting bytes
error:or a
Wrong body size
error:After some investigation, I visited the pySerial documentation and found that the
setDTR
method is deprecated. I think we need to use the dtr method instead. Then I noticed that in the init function, the flagdsrdtr
must be set toTrue
to "Enable hardware (DSR/DTR) flow control." With these edits to the code, my RPLIDAR A2 is able to work consistently. However, I still must callget_info()
and/orget_health()
before callingiter_scans()
for some reason - not sure if this is unintended or by design. Please review these edits and let me know what you think. Thank you!