Hi guys,
I am having 2 issues with part 1 of the rosject.
The first problem: I seem to be reading incorrect distances form the laser. From what I understand I should be checking the first values of ranges for the right and the values around 360 for the front. but when I do this I seem to get correct values for the right but not for the front.
The second problem: I don’t think I am publishing correctly to cmd_vel as when I launch the pkg the robot just moves straight even though it should be turning. The print messages in the code are being printed correctly.
Any help would be really great I have been stuck on this project for a while…
thanks in advance!
#! /usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
def callback(msg): # get the laser scan message
right_dist = min(min(msg.ranges[0:30]), 10)
front_dist = min(min(msg.ranges[350:370]), 10)
print("right dist is %f" % (right_dist))
print("front dist is %f" % (front_dist))
move = Twist()
if right_dist > 0.30 and front_dist > 0.50:
move.angular.z = -0.03
move.linear.x = 0.03
print("turning to wall")
elif right_dist < 0.20 and front_dist > 0.50:
move.angular.z = 0.03
move.linear.x = 0.03
print("turning away from wall")
elif front_dist > 0.50:
move.linear.x = 0.03
print("moving forward")
elif front_dist < 0.50:
move.angular.z = 0.04
move.linear.x = 0.03
print("turning with wall")
pub.publish(move)
rospy.init_node('Wall_follower') # we create a node called wall follower
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
right_dist = None
front_dist = None
sub = rospy.Subscriber('/scan', LaserScan, callback) # we subscribe to laser scan and put info in msg
rate = rospy.Rate(2)
move = Twist()
while not rospy.is_shutdown():
pub.publish(move)
rate.sleep()