ROS2 Basics in 5 Days(python)

Hi:

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 find_wall_service.py:

import sys, time
path = "/opt/ros/noetic/lib/python3/dist-packages" 
if path in sys.path:
    sys.path.remove(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):
        super().__init__("find_wall_server")
        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)
        self.get_logger().info("Start!")


    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
    rclpy.init(args=args)
    # declare the node constructor
    find_wall = FindWallService()
        
    # pause the program execution, waits for a request to kill the node (ctrl+c)
    #rclpy.get_default_context().on_shutdown(wall_following.on_shutdown)
    try:
        rclpy.spin(find_wall)
    except KeyboardInterrupt:
        pass
    # Explicity destroy the node
   
    # shutdown the ROS communication
    rclpy.shutdown()

if __name__ == '__main__':
    main()

Can anyone tell me how to solve it? Thanks!

Hi,

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:

/cv_camera/set_camera_info
/find_wall
/find_wall_server/describe_parameters
/find_wall_server/get_parameter_types
/find_wall_server/get_parameters
/find_wall_server/list_parameters
/find_wall_server/set_parameters
/find_wall_server/set_parameters_atomically
/gazebo/clear_body_wrenches
/gazebo/clear_joint_forces
/gazebo/delete_light
/gazebo/delete_model
/gazebo/get_joint_properties
/gazebo/get_light_properties
/gazebo/get_link_properties
/gazebo/get_link_state
/gazebo/get_model_properties
/gazebo/get_model_state
/gazebo/get_physics_properties
/gazebo/get_world_properties
/gazebo/pause_physics

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.