Skip to content

Commit

Permalink
Waiting for initial byte sequence(platform+version info) has been com…
Browse files Browse the repository at this point in the history
…promised with time delay because initial byte sequence is only observed duing type c connection and not through convertor port
  • Loading branch information
lakshmanmallidi committed May 20, 2019
1 parent b601551 commit fb0bb95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
28 changes: 21 additions & 7 deletions PyLidar3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ def Connect(self):
if(not self._is_connected):
self._s=Serial(self._port, self._baudrate)
self._is_connected = True
sleep(3)
self._s.reset_input_buffer()
if("YdLidarX4" in str(type(self))):
self._Stop_motor()
while(self._s.inWaiting()==0):
sleep(0.5)
self._s.reset_input_buffer()
if(self.GetHealthStatus()):
return True
else:
raise Exception('Device status error.Try reconnecting device')
else:
raise Exception("Already connected")
except Exception as e:
print(e)
return False

def _Start_motor(self):
Expand Down Expand Up @@ -141,7 +141,7 @@ def StopScanning(self):
if(self._is_scanning):
self._is_scanning = False
self._s.write(b"\xA5\x65")
sleep(2)
sleep(1)
self._s.reset_input_buffer()
if("YdLidarX4" in str(type(self))):
self._Stop_motor()
Expand All @@ -156,6 +156,8 @@ def GetHealthStatus(self):
if(self._is_connected):
if self._is_scanning == True:
self.StopScanning()
self._s.reset_input_buffer()
sleep(0.5)
self._s.write(b"\xA5\x91")
sleep(0.5)
data = self._s.read(10)
Expand All @@ -171,6 +173,8 @@ def GetDeviceInfo(self):
if(self._is_connected):
if self._is_scanning == True:
self.StopScanning()
self._s.reset_input_buffer()
sleep(0.5)
self._s.write(b"\xA5\x90")
sleep(0.5)
data = self._s.read(27)
Expand Down Expand Up @@ -240,8 +244,10 @@ def GetLowPowerModeStatus(self):
if(self._is_connected):
if self._is_scanning == True:
self.StopScanning()
self._s.write(b"\xA5\x05")
self._s.reset_input_buffer()
sleep(0.5)
self._s.write(b"\xA5\x05")
sleep(1)
data = self._s.read(8)
if(data[7]!=1):
return True
Expand Down Expand Up @@ -284,10 +290,15 @@ def GetCurrentFrequency(self):
if(self._is_connected):
if self._is_scanning == True:
self.StopScanning()
self._s.reset_input_buffer()
sleep(0.5)
self._s.write(b"\xA5\x0D")
sleep(0.5)
data = self._s.read(11)
return (self._HexArrToDec(data[-4:]))/100
if(data[0]==165):
return (self._HexArrToDec(data[-4:]))/100.0
else:
return (self._HexArrToDec(data[:4]))/100.0
else:
raise Exception("Device is not connected")

Expand Down Expand Up @@ -332,8 +343,10 @@ def GetCurrentRangingFrequency(self):
if(self._is_connected):
if self._is_scanning == True:
self.StopScanning()
self._s.write(b"\xA5\xD1")
self._s.reset_input_buffer()
sleep(0.5)
self._s.write(b"\xA5\xD1")
sleep(1)
data = self._s.read(8)
if(data[-1]==0):
return 4
Expand All @@ -351,6 +364,7 @@ def Disconnect(self):
self.StopScanning()
if(self.GetLowPowerModeStatus()==False):
self.EnableLowPowerMode()
sleep(2)
self._s.close()
self._is_connected=False
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
setuptools.setup(
name="PyLidar3",
python_requires=">=3.4",
version="1.3",
version="1.4",
author="Lakshman mallidi",
author_email="[email protected]",
description="Library for Lidar. Currently supports YdLidar(X4,G4) from http://www.ydlidar.com",
Expand Down

0 comments on commit fb0bb95

Please sign in to comment.