Hello guys,
I’ve been doing the 2nd part of the Rosject for the ROS Basics in 5 Days (Python) course and I am having serious issues. I don’t really know how to work with services and subscriptions at the same time, and I don’t know how the service should wait for the end of the sub node in order to proceed with its message. I paste here the code I made, so to have some suggestions. Btw, I also noticed that during the first rotation, the obstacles at the center of the simulation space are closer than the wall, and this could be an issue. How did you solve it? Thanks in advance!
#! /usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from scan_test.srv import FindWall, FindWallResponse
def callback2(msg):
if msg.ranges.index(min(msg.ranges)) not in range (357,363,1):
rospy.loginfo("Rotating...")
move.linear.x=0
move.angular.z=0.2
pub.publish(move)
elif msg.ranges.index(min(msg.ranges)) in range(357,363,1):
move.angular.z=0
rospy.loginfo("Robot is approaching the wall...")
while msg.ranges[360]>0.3:
move.linear.x=0.1
pub.publish(move)
move.linear.x=0
rospy.loginfo("Robot is rotating to start the algorithm...")
while msg.ranges[270] != min(msg.ranges):
move.angular.z=0.2
pub.publish(move)
def my_callback(request):
rospy.loginfo("Starting the service...")
sub=rospy.Subscriber('/scan', LaserScan, callback2)
rospy.loginfo("Wall found! Exiting service...")
response = FindWallResponse()
response.wallfound = True
rospy.spin()
return response
rospy.init_node('service_server')
my_service=rospy.Service('/find_wall', FindWall, my_callback)
pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)
move=Twist()
rospy.spin()