ROS2 Basics in 5 Days(python)


Now I start rosject “Work With a Real Robot” and trying to finish part_2: service.

But when I call my service, always get the message: waiting for service to become available…

Below is my code in

import sys, time
path = "/opt/ros/noetic/lib/python3/dist-packages" 
if path in sys.path:

import rclpy
# import the ROS2 python dependencies
from rclpy.node import Node
# import the Twist module from geometry_msgs dependencies
from geometry_msgs.msg import Twist
# import the LaserScan module from sensor_msgs dependencies
from sensor_msgs.msg import LaserScan
from rclpy.qos import ReliabilityPolicy, QoSProfile
from part_2.srv import FindWall

class FindWallService(Node):

    def __init__(self):
        self.laser_right = 0
        self.laser_front = 0
        self.laser_back = 0
        self.subscriber = self.create_subscription(LaserScan, '/scan', self.callback_find_wall_node, QoSProfile(depth=10, reliability=ReliabilityPolicy.BEST_EFFORT))
        self.server = self.create_service(FindWall, "find_wall", self.callback_find_wall_service)

    def callback_find_wall_node(self, msg):
        # Save the right laser scan info at 90°
        self.laser_right = msg.ranges[90] 
        self.laser_front = msg.ranges[180]
        self.laser_bask = msg.ranges[0]

    def callback_find_wall_service(self, request, response):
        #response.wallfound = False
        # print the data
        self.get_logger().info(f"Right: {self.laser_right}, front: {self.laser_front}")
        #response.wallfound = True
        return response

def main(args=None):
    # initialize the ROS communication
    # declare the node constructor
    find_wall = FindWallService()
    # pause the program execution, waits for a request to kill the node (ctrl+c)
    except KeyboardInterrupt:
    # Explicity destroy the node
    # shutdown the ROS communication

if __name__ == '__main__':

Can anyone tell me how to solve it? Thanks!


That probably means that your service server isn’t running correctly. Do you see it listed under

ros2 service list

Yes, below is the reply:


And I can use “rqt” to call the service “/find_wall” that can work.
I don’t know why can’t work while using command line in this rosject.

So if your server is working correctly and you can call it with ros2 service call, then I don’t understand your problem. When does the message appear? After launching your file you shared above, or when running another node that does the service call?

Here is my problem in this rosject:

If the service is working correctly, why just only can use rqt to call? But can not use command in terminal to call. Or just there are some problem in this rosject?

Your service is not working correctly. It is advertising it but you are not getting the response from the call in the terminal.

Check that your custom service is created correctly, and also try creating a server with an existing one with an empty call, you should be able to see that call returned.

I think I find the problem. I re-build my own package custom_interface

螢幕擷取畫面 2021-09-30 100852

There is a Hint within the rosject, but can not work.

螢幕擷取畫面 2021-09-30 100525

If I want to call my custom_interface, I need to use my file FindWall.srv .

Thanks for the remainder.