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!