ROSject part II

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()

Hi @pierpaolo.fucile ,

Some points there:

  • You should setup both subscriber and service during the initialization of the node
  • Do not use rospy.spin inside a function, it will hang the execution of the rest
  • Do not create a subscriber inside the service, because a new subscriber will be created everytime it is called. Unless you are handling this subscription and terminating it, it is better to not do so.

A template could be like:

#! /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 subscriber_callback(msg):
    # do subscriber stuff here
 
def service_callback(request):
    # do service stuff here
    return response


rospy.init_node('service_server')
sub=rospy.Subscriber('/scan', LaserScan, callback2)
my_service=rospy.Service('/find_wall', FindWall, my_callback)
pub=rospy.Publisher('/cmd_vel', Twist, queue_size=1)
move=Twist()
rospy.spin()

You can use globals to have to to variables outside callbacks scope or go further and use classes.
In which project are you working on?

Regards

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.