Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! usr#!/usr/bin/env python
- import rospy
- from sensor_msgs.msg import LaserScan
- from geometry_msgs.msg import Twist
- def callback(msg):
- print('=========================')
- print('s1 [0]')
- print(msg.ranges[0]) #distance at angle 0, which is directly in front of robot
- print('s2 [90]')
- print(msg.ranges[90]) #distance at angle 90 degrees
- print('s3 [180]')
- print(msg.ranges[180]) #distance at angle 180 degrees
- print('s4 [270]')
- print(msg.ranges[270]) #distance at angle 270 degrees
- print('s5 [359]')
- print(msg.ranges[359]) #distance at angle 359 degrees
- c1 = 0
- c2 = 0
- a1 = 345
- a2 = 0
- while a1 < 359:
- a1 += 1
- r = msg.ranges[a1] > 0.3
- if r == False:
- c1 += 1
- while a2 < 15:
- a2 += 1
- r = msg.ranges[a2] > 0.3
- if r == False:
- c2 += 1
- if c1 > 0 or c2 > 0:
- move.linear.x = 0.0
- move.angular.z = 0.0
- print('Obstacle in front')
- print("Turning")
- move.linear.x = 0.0
- move.angular.z = 0.22
- if msg.ranges[90] > 0.5:
- move.linear.x = 0.0
- move.angular.z = -0.22
- else:
- move.linear.x = 0.22
- move.angular.z = 0.0
- print('Moving Forward')
- pub.publish(move)
- rospy.init_node('obstacle_avoidance') #Initiate node
- sub = rospy.Subscriber('/scan', LaserScan, callback) #Create subscriber to laser/scan topic
- pub = rospy.Publisher('/cmd_vel', Twist)
- move = Twist()
- rospy.spin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement