Skip to content

Commit

Permalink
Simplified code to make laser_range and occdata 1-dimensional.
Browse files Browse the repository at this point in the history
Simplified code to check size of laser_range instead of using a try-catch block.
  • Loading branch information
syen123 committed Mar 1, 2020
1 parent 5ba98c1 commit 32edf2d
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions auto_nav.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
occdata = []
yaw = 0.0
rotate_speed = 0.1
linear_speed = 0.01
stop_distance = 0.25
linear_speed = 0.1
stop_distance = 0.5
occ_bins = [-1, 0, 100, 101]
front_angle = 30
front_angles = range(-front_angle,front_angle+1,1)
Expand All @@ -34,14 +34,14 @@ def get_laserscan(msg):
global laser_range

# create numpy array
laser_range = np.array([msg.ranges])
laser_range = np.array(msg.ranges)


def get_occupancy(msg):
global occdata

# create numpy array
occdata = np.array([msg.data])
occdata = np.array(msg.data)
# compute histogram to identify percent of bins with -1
occ_counts = np.histogram(occdata,occ_bins)
# calculate total number of bins
Expand Down Expand Up @@ -112,7 +112,6 @@ def rotatebot(rot_angle):
def pick_direction():
global laser_range

rospy.loginfo(['In pick_direction'])
# publish to cmd_vel to move TurtleBot
pub = rospy.Publisher('cmd_vel', Twist, queue_size=10)

Expand All @@ -123,10 +122,9 @@ def pick_direction():
time.sleep(1)
pub.publish(twist)

try:
if laser_range.size != 0:
lr2i = np.argmax(laser_range)
except ValueError:
# in case laser_range is empty
else:
lr2i = 0

rospy.loginfo(['Picked direction: ' + str(lr2i)])
Expand Down Expand Up @@ -165,7 +163,7 @@ def mover():

while not rospy.is_shutdown():
# check distances in front of TurtleBot
lr2 = laser_range[0,front_angles]
lr2 = laser_range[front_angles]
# distances beyond the resolution of the Lidar are returned
# as zero, so we need to exclude those values
lr20 = (lr2!=0).nonzero()
Expand Down

0 comments on commit 32edf2d

Please sign in to comment.