Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import rospy
- from geometry_msgs.msg import *
- from sensor_msgs.msg import *
- from nav_msgs.msg import *
- import math
- import random
- ##########################
- # BEGIN Global Variable Definitions
- front_distance = -1.0
- wall_distance = -1.0
- flag = 1
- # END Global Variable Definitions
- ##########################
- ##########################
- # BEGIN ROS Topic Callback Function [DON'T MESS WITH THIS STUFF]
- ##########################
- def laserCallback(laser_data):
- #This function will set front_distance and wall_distance. Both of these work in the same way. If something is closer than the detection_distance for that sensor, the value will tell how close something is. If nothing is closer than the detection distance, the value of -1 will be used.
- global flag
- if flag == 1:
- print '***********************'
- print len(laser_data.ranges)
- print laser_data.angle_increment
- print laser_data.angle_min
- flag = 2
- global front_distance
- global wall_distance
- #HA : increaed as the sensor is able to get mesurments up to 3
- front_detection_distance = 2.9
- wall_detection_distance = 2.9
- wf_min_right = 10.0
- wf_min_front = 10.0
- #HA : we might need to change the cur_angle initializeation and the range for i as the actual range of the scan is 60 dgrees not 180 around y
- cur_angle = laser_data.angle_min
- #HA: cur_angle = laser_data.angle_min + (215 * laser_data.angle_increment)
- for i in range(len(laser_data.ranges)):
- #HA: for i in range(215,415):
- if abs(cur_angle + math.pi/2) < (math.pi / 8):
- #HA: if cur_angle > 1.91986:
- #Wall sensor ((-5/8)*math.pi <= cur_angle <= (-3/8)*math.pi)
- if laser_data.ranges[i] < wf_min_right:
- wf_min_right = laser_data.ranges[i]
- if abs(cur_angle) < (math.pi / 8):
- #HA: if abs(cur_angle) < 1.8326:
- #Front sensor ((-1/8)*math.pi <= cur_angle <= (1/8)*math.pi)
- if laser_data.ranges[i] < wf_min_front:
- wf_min_front = laser_data.ranges[i]
- cur_angle = cur_angle + laser_data.angle_increment
- #Set the sensor variables
- front_distance = -1
- wall_distance = -1
- if wf_min_front < front_detection_distance:
- front_distance = wf_min_front
- if wf_min_right < wall_detection_distance:
- wall_distance = wf_min_right
- ############################
- ##### END CALLBACK FUNCTION
- ############################
- if __name__ == '__main__':
- rospy.init_node('lab3', anonymous=True) #Initialize the ros node
- pub = rospy.Publisher('cmd_vel_mux/input/teleop', Twist) #Create our publisher to send drive commands to the robot
- rospy.Subscriber("/scan", LaserScan, laserCallback) #Subscribe to the laser scan topic
- rate = rospy.Rate(10) #10 Hz
- #SENSOR VARIABLES
- global front_distance #How close is something in front of the robot (-1 = nothing is close)
- global wall_distance #How close is something on the right side of the robot (-1 = nothing is close)
- #########################################
- # LAB 3 VARIABLE DECLARATION CODE : BEGIN
- #########################################
- # PART C CODE HERE:
- # Define and initialize variables that
- # you need inside the main loop
- current_state = 'WHERE_AM_I'
- safe_distance = 0.51
- old_front_distance = front_distance
- old_wall_distance = wall_distance
- #HA: we also need the old command to be able to recognize when we get into blind kinect zone
- old_twist_command = Twist()
- #######################################
- # LAB 3 VARIABLE DECLARATION CODE : END
- #######################################
- while not rospy.is_shutdown():
- #Declare the twist command that we will publish this time step
- twist = Twist()
- ###############################
- # LAB 3 INNER LOOP CODE : BEGIN
- ###############################
- # PART C CODE HERE:
- # Make sure that twist gets set with your drive command
- # print current_state, front_distance, wall_distance
- twist.angular.z = math.pi * 0.5
- ################# WHERE_AM_I #################
- if current_state == 'WHERE_AM_I':
- if front_distance > 0 and wall_distance == -1:
- #HA: if front_distance > safe_distance and wall_distance == -1:
- current_state = 'GET_TO_WALL'
- elif front_distance == -1 and wall_distance > 0:
- #HA: elif front_distance == -1 and wall_distance > safe_distance:
- current_state = 'FOLLOW_WALL'
- else:
- twist.linear.x = random.randint(0, 5)
- twist.angular.z = math.pi / random.randint(1, 10)
- ################# GET_TO_WALL #################
- elif current_state == 'GET_TO_WALL':
- # twist.linear.x = front_distance - safe_distance
- twist.angular.z = math.pi * 0.5
- if wall_distance > 0:
- #HA: if wall_distance > safe_distance:
- current_state = 'FOLLOW_WALL'
- # if front_distance == -1 and wall_distance == -1:
- # current_state = 'TURN_RIGHT'
- # elif front_distance <= safe_distance:
- # current_state = 'TURN_LEFT'
- ################# FOLLOW_WALL #################
- elif current_state == 'FOLLOW_WALL':
- twist.linear.x = safe_distance / 2
- #HA: twist.linear.x = 1
- if wall_distance > safe_distance * 2:
- twist.angular.z = math.pi * - 0.2
- else:
- #HA: double check if z value is too big
- twist.angular.z = math.pi * 10 * (old_wall_distance - wall_distance)
- # print '-----', twist.linear.x, twist.angular.z
- #HA: range is bigger than hallway dimensions this check need more conditions on both if and elif OR we can lower front_detection_distance back to 0.9 to be less than dimensions
- if front_distance == -1 and wall_distance == -1:
- current_state = 'TURN_RIGHT'
- elif ( front_distance < safe_distance * 1.5 and front_distance != -1 ) or ( wall_distance != -1 and wall_distance <= safe_distance) :
- current_state = 'TURN_LEFT'
- ################# TURN_LEFT #################
- elif current_state == 'TURN_LEFT':
- #HA: check if z value is too big
- twist.angular.z = math.pi * 0.5
- if front_distance == -1 or front_distance > safe_distance : #HA: removed * 1.5
- current_state = 'FOLLOW_WALL'
- ################# TURN_RIGHT #################
- elif current_state == 'TURN_RIGHT':
- #HA: check if z value is too big
- twist.angular.z = math.pi * -0.5
- # if front_distance == -1 and wall_distance == -1:
- # twist.linear.x = safe_distance / 2
- if front_distance > 0 and wall_distance == -1:
- current_state = 'GET_TO_WALL'
- elif front_distance == -1 and wall_distance > 0:
- current_state = 'FOLLOW_WALL'
- old_front_distance = front_distance
- old_wall_distance = wall_distance
- old_twist_command = twist
- #############################
- # LAB 3 INNER LOOP CODE : END
- #############################
- #HA : reduce the x command as the units are different than simulation
- twist.linear.x = twist.linear.x * 0.01
- #Publish drive command
- pub.publish(twist)
- rate.sleep() #Sleep until the next time to publish
- twist = Twist()
- pub.publish(twist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement