ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

can't call ros2 service after adding one more publisher to other node

asked 2022-05-04 03:03:40 -0500

Saeed gravatar image

Hi everybody My project consists of 3 nodes:

  1. account service
  2. publisher
  3. trade service

when publisher has just one publisher, everything works fine and I can call services but after adding one more publisher to publisher node, services from account service node becomes unavailable(but I can see those services from ros2 service list), in other words I can not call them what I am doing wrong? here is the code for starting server:

def main():
  # ros2 initialize and create an executor for all nodes
  rclpy.init(args=None)
  executor = SingleThreadedExecutor()

  # creating account, publishing and service nodes
  publisher_node = Publisher(name='ig_broker_publisher')
  print('created publisher node')
  account_service_node = AccountService(name='account_service', publisher_node=publisher_node)
  print('created accoutn service node')
  trader_service_node = TradeService(name='trade_service', account_service=account_service_node)
  print('created trade service node')
  # account_service_node is the core and has accounts and database manager

  # add nodes to executor
  executor.add_node(account_service_node)
  executor.add_node(publisher_node)
  executor.add_node(trader_service_node)

  # execute until finish
  executor.spin()

and the client script which calls the services is like this:

class AccountClientAsync(Node):

def __init__(self, name: str):
    super().__init__(name)
    self.connect_client = self.create_client(RequestConnectAccount, 'connect_service')
    self.change_client = self.create_client(RequestConnectAccount, 'change_service')
    self.switch_client = self.create_client(RequestSwitchAccount, 'switch_service')
    while not self.connect_client.wait_for_service(timeout_sec=1.0):
        self.get_logger().info('service connect not available, waiting again...')
    while not self.change_client.wait_for_service(timeout_sec=1.0):
        self.get_logger().info('service change not available, waiting again...')
    while not self.switch_client.wait_for_service(timeout_sec=1.0):
        self.get_logger().info('service switch not available, waiting again...')

and this

def main(args=None):
rclpy.init(args=args)
account_client = AccountClientAsync(name='account_connect_client')

print('inside main function of client before making request')

if sys.argv[1] in ['CONNECT', 'CHANGE']:
    account_client.send_connect_or_change_request()
elif sys.argv[1] == 'SWITCH':
    account_client.send_switch_request()
else:
    print('invalid request')

print('inside main function of client after making request')
while rclpy.ok():
    rclpy.spin_once(account_client)
    if account_client.future.done():
        try:
            response = account_client.future.result()
        except Exception as e:
            account_client.get_logger().info(
                'Service call failed %r' % (e,))
        else:
            account_client.get_logger().info(response.result)
        break

account_client.destroy_node()
rclpy.shutdown()

my code is stuck on :

'service connect not available, waiting again...'

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-05-25 22:06:57 -0500

qilin_gundamenjoyer gravatar image

updated 2022-05-25 22:29:09 -0500

Based on your code, it doesn't show that you have imported the required libraries. For the service node file, ensure that you have from example_interfaces.srv import AddTwoInts, import sys, and import rclpy from rclpy.node written at the top of the file. For your client node file, ensure that you have from example_interfaces.srv import AddTwoInts, import sys, import rclpy from rclpy.node, and import Node written at the top of the file. Otherwise, the code that has sys., rcply., and rcply.node won't work as intended. If a setup.py file hasn't been created, it has to created in order for the service to know the entry point. Add 'service = your_pkg_name.service_member_function:main', between the 'console_scripts': brackets.

If it still doesn't work after that. Your code is stuck on service connect not available, waiting again...because the request cannot be received. Verify that the service is actually being advertised. You can check this with rosservice list command to check whether the service file name is present in your catkin workspace.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2022-05-04 02:51:06 -0500

Seen: 63 times

Last updated: May 25 '22