Skip to content

Instantly share code, notes, and snippets.

@AdroitAnandAI
Created August 12, 2021 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdroitAnandAI/2393a0ae2a50ef26066a38809608fbcd to your computer and use it in GitHub Desktop.
Save AdroitAnandAI/2393a0ae2a50ef26066a38809608fbcd to your computer and use it in GitHub Desktop.
Find distnace to an object
def getObjectDistance (angle_min, angle_max):
minDist = 0
lidar = RPLidar(None, PORT_NAME)
try:
for scan in lidar_scans(lidar):
for (_, angle, distance) in scan:
scan_data[min([359, floor(angle)])] = distance
# fetching all non zero distance values between subtended angles
allDists = [scan_data[i] for i in range(360)
if i >= angle_min and i <= angle_max and scan_data[i] > 0]
# if half the distance values are filled in then break
if (2 * len(allDists) > angle_max - angle_min):
minDist = np.min(allDists)
lidar.stop()
lidar.disconnect()
return minDist
except KeyboardInterrupt:
print('Stoping LIDAR Scan')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment