After adding the stop_client in unit 4, colcon build --packages-select client_pkg produced the following warning:
WARNING:colcon.colcon_core.package_selection:ignoring unknown package ‘client_pkg’ in --packages-select
it worked fine when I only had the service_client code within the package. Here is the code…
stop_client.py
# import the empty module from std_servs Service interface
from std_srvs.srv import Empty
# import the ROS2 Python client libraries
import rclpy
from rclpy.node import Node
class ClientAsync(Node):
def __init__(self):
# Here you have the class constructor
super().__init__('stop_client')
self.client = self.create_client(Empty, 'stop')
while not self.client.wait_for_service(timeout_sec=1.0):
# if it is not available, a message is displayed
self.get_logger().info('service not available, waiting again...')
# create an Empty request
self.req = Empty.Request()
def send_request(self):
# send the request
self.future = self.client.call_async(self.req)
def main(args=None):
# initialize the ROS communication
rclpy.init(args=args)
# declare the node constructor
client = ClientAsync()
# run the send_request() method
client.send_request()
while rclpy.ok():
# pause the program execution, waits for a request to kill the node (ctrl+c)
rclpy.spin_once(client)
if client.future.done():
try:
# checks the future for a response from the Service
# while the system is running.
# If the Service has sent a response, the result will be written
# to a log message.
response = client.future.result()
except Exception as e:
# Display the message on the console
client.get_logger().info(
'Service call failed %r' % (e,))
else:
# Display the message on the console
client.get_logger().info(
'the robot is stopped' )
break
client.destroy_node()
# shutdown the ROS communication
rclpy.shutdown()
if __name__ == '__main__':
main()
Here is stop_client_launch_file.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='client_pkg',
executable='stop_client',
output='screen'),
])
Here is the setup file.
from setuptools import setup
import os
from glob import glob
package_name = 'client_pkg'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name), glob('launch/*.launch.py'))
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='user',
maintainer_email='user@todo.todo',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'service_client = client_pkg.service_client:main',
'stop_client = client_pkg.stop_client:main'
],
},
)