Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import rospy
  3. # import functools
  4. from ackermann_msgs.msg import AckermannDriveStamped
  5. from sensor_msgs.msg import Range
  6. from std_msgs.msg import Float32,Bool
  7.  
  8. STEERING_ANGLE = Float32()
  9. STEERING_ANGLE.data = 0 # radians
  10. STEERING_ANGLE_VELOCITY = 0
  11. SPEED = 0.5 # m/s
  12. ACCELERATION = 1 # m/s^2
  13. JERK = 0.1 # m/s^3
  14. sonar_sensor = Float32()
  15. flag_Emergency = bool()
  16. flag_Overtaking = bool()
  17. flag_Overtaking_itHappens = bool()
  18. sonar_sensor.data = 3.75
  19. flag_Stop = Bool()
  20. flag_Stop.data = False
  21.  
  22.  
  23. def sonar_callback(sonar_data):
  24.     sonar_sensor.data = sonar_data.range
  25.     # rospy.loginfo("Sonar range: %lf" % sonar_sensor.data)
  26.     print("am intrat in callback sonar")
  27.    
  28. def steering_callback(stering_angle_data):
  29.     print("am intrat in callback steering")
  30.     STEERING_ANGLE.data = stering_angle_data.data
  31.     # rospy.loginfo("DATA CALLBACK_stering: %lf" % STEERING_ANGLE.data)
  32.    
  33.    
  34. def stop_line_callback(stop_line_data):
  35.     flag_Stop.data = stop_line_data.data
  36.     # rospy.loginfo("callback stopLine: %lf" % flag_Stop.data)
  37.     print("am intrat in callback stop line")
  38.     print(flag_Stop.data)
  39.    
  40. def listener_talker():
  41.     overaking_cnt = 0
  42.     publisher_obj = rospy.Publisher('/ackermann_vehicle/ackermann_cmd', AckermannDriveStamped, queue_size=10)
  43.     rospy.Subscriber('/steering_angle', Float32,steering_callback)
  44.     rospy.Subscriber('ackermann_vehicle/sonar', Range, sonar_callback)
  45.     rospy.Subscriber('/Stop_pub_topic',Bool,stop_line_callback)
  46.     rate = rospy.Rate(100) # 10hz
  47.    
  48.    
  49.     # !!! In Gazebo the scale is x2.5 to Real Simulation (ex. 1meter(Real Simulation) = 2.5m(Gazebo))
  50.    
  51.     while not rospy.is_shutdown():
  52.         cmd_to_publish = AckermannDriveStamped() # publisher object for ackermann_cmd
  53.         cmd_to_publish.drive.steering_angle = STEERING_ANGLE.data
  54.         cmd_to_publish.drive.steering_angle_velocity = STEERING_ANGLE_VELOCITY
  55.         cmd_to_publish.drive.speed = SPEED
  56.         cmd_to_publish.drive.acceleration = ACCELERATION
  57.         cmd_to_publish.drive.jerk = JERK
  58.          
  59.         publisher_obj.publish(cmd_to_publish) # publish Ackermann messagec
  60.        
  61.         rospy.loginfo("Soanar data: %lf " %sonar_sensor.data)  
  62.         print(flag_Stop)
  63.          
  64.         # if the obtacle is detected between 0.8-1.2m -> perform a overtaking manuver
  65.         if sonar_sensor.data <= 3:
  66.             rospy.loginfo("flag_Overtaking")
  67.             flag_Overtaking = True
  68.         else:
  69.             flag_Overtaking = False
  70.         rospy.loginfo("Overtaking: %lf " %flag_Overtaking)
  71.         rospy.loginfo("Soanr data: %lf " %sonar_sensor.data)
  72.  
  73.  
  74.         if sonar_sensor.data < 1.5 and flag_Overtaking == True:
  75.            
  76.             flag_Overtaking_itHappens = True
  77.             # cmd_to_publish.drive.speed = 0.3 #incetinesc la overtaking?
  78.  
  79.             start_time = rospy.get_rostime()
  80.             timeout1 = rospy.Duration(3)  # Timeout of 2 seconds
  81.             while(rospy.get_rostime() - start_time < timeout1):
  82.                 cmd_to_publish.drive.steering_angle = -0.21 #dreapta
  83.                 publisher_obj.publish(cmd_to_publish)
  84.            
  85.             start_time2 = rospy.get_rostime()
  86.             timeout2 = rospy.Duration(4)
  87.             while(rospy.get_rostime() - start_time2 < timeout2):
  88.                 cmd_to_publish.drive.steering_angle = 0.4 #stanga
  89.                 publisher_obj.publish(cmd_to_publish)
  90.            
  91.             start_time3 = rospy.get_rostime()
  92.             timeout3 =rospy.Duration(1)
  93.             while(rospy.get_rostime() - start_time3 < timeout3):
  94.                 cmd_to_publish.drive.steering_angle = -0.2
  95.                 publisher_obj.publish(cmd_to_publish)
  96.            
  97.             flag_Overtaking_itHappens = False
  98.            
  99.         # if the obtacle is detected at least 0.8m -> stop at 0.3m
  100.         if sonar_sensor.data <= 2 and flag_Overtaking == False:
  101.             flag_Emergency = True
  102.         else:
  103.             flag_Emergency = False
  104.         if sonar_sensor.data < 1.25 and flag_Emergency == True:  # stop at 0.5m
  105.             cmd_to_publish.drive.speed = 0
  106.             publisher_obj.publish(cmd_to_publish) # publish Ackermann message
  107.        
  108.         if flag_Stop.data == True:
  109.            
  110.             start_time_stop = rospy.get_rostime()
  111.             timeout_stop = rospy.Duration(2)
  112.             while(rospy.get_rostime() - start_time_stop < timeout_stop):
  113.                 cmd_to_publish.drive.speed = 0
  114.                 publisher_obj.publish(cmd_to_publish)
  115.        
  116.        
  117.        
  118.         rate.sleep()
  119.    
  120. if __name__ == '__main__':
  121.    
  122.    
  123.     try:
  124.         rospy.init_node('publisher_node', anonymous=True)
  125.        
  126.         listener_talker()          
  127.     except rospy.ROSInterruptException:
  128.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement