Hi,
I created the first rosject for the wall follower robot.
When I press CTRL+C the node exits but the robot does not stop. It continuous its working according to the last message in the topic /cmd_vel.
I decided to take an “emergency action”, so when my node exits, I would like to publish an all-zero Twist command.
Here is my launch file:
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import (ExecuteProcess,
LogInfo, RegisterEventHandler)
from launch.event_handlers import (OnShutdown)
from launch.substitutions import (FindExecutable)
def generate_launch_description():
stop_robot = ExecuteProcess(
cmd=[[
FindExecutable(name='ros2'),
' topic pub --once ',
' /cmd_vel ',
' geometry_msgs/msg/Twist ',
'"{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}"'
]],
shell=True
)
wall_follower_node = Node(
package='wall_follower',
executable='wall_following',
output='screen'
)
return LaunchDescription([
wall_follower_node,
RegisterEventHandler(
OnShutdown(
on_shutdown=[
LogInfo(msg='Wall follower exiting'),
LogInfo(msg='Stopping robot'),
stop_robot,
LogInfo(msg='Stopping done'),
]
)
)
])
When I press CTRL+C, the logging appears (“Stopping robot” and “Stopping done”) but the robot does not stop. I checked the /cmd_vel topic with ros2 topic echo and there is no all-zero Twist message in that, so the stop_robot command is not executed.
Unfortunately, I did not find any error messages.
What I suspect, is that the ros2 command was not found, so maybe I have to issue the well known “source …” command. But it is not needed in rosject, but maybe it is needed when the shell is not interactive.
Can someone help me?
Thank you in advance
Peter
You can see here that the shutdown hook is executed: the lines starting with “[INFO] [launch.user]”.
The console output:
[wall_following-1] [INFO] [1700113944.311203648] [wall_follower]: Forward distance: "0.81788569688797"
^C[WARNING] [launch]: user interrupted with ctrl-c (SIGINT)
[INFO] [launch.user]: Wall follower exiting
[INFO] [launch.user]: Stopping robot
[INFO] [launch.user]: Stopping done
[wall_following-1] Traceback (most recent call last):
[wall_following-1] File "/home/user/ros2_ws/install/wall_follower/lib/wall_follower/wall_following", line 33, in <module>
[wall_following-1] sys.exit(load_entry_point('wall-follower==0.0.0', 'console_scripts', 'wall_following')())
[wall_following-1] File "/home/user/ros2_ws/install/wall_follower/lib/python3.8/site-packages/wall_follower/wall_following.py", line 67, in main
[wall_following-1] rclpy.spin(wallFollower)
[wall_following-1] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/__init__.py", line 191, in spin
[wall_following-1] executor.spin_once()
[wall_following-1] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 703, in spin_once
[wall_following-1] handler, entity, node = self.wait_for_ready_callbacks(timeout_sec=timeout_sec)
[wall_following-1] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 689, in wait_for_ready_callbacks
[wall_following-1] return next(self._cb_iter)
[wall_following-1] File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 586, in _wait_for_ready_callbacks
[wall_following-1] _rclpy.rclpy_wait(wait_set, timeout_nsec)
[wall_following-1] KeyboardInterrupt
[ERROR] [wall_following-1]: process has died [pid 3263, exit code -2, cmd '/home/user/ros2_ws/install/wall_follower/lib/wall_follower/wall_following --ros-args']