Created
August 12, 2021 17:02
-
-
Save AdroitAnandAI/2393a0ae2a50ef26066a38809608fbcd to your computer and use it in GitHub Desktop.
Find distnace to an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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