Thanks @luckierdodge! Just add more methods. Both examples below start a sim window and add another turtle2
.
Method 1
Based on @luckierdodge answer:
from launch import LaunchDescription
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
from launch_ros.actions import Node
def generate_launch_description():
ld = LaunchDescription(
[
Node(
package="turtlesim",
executable="turtlesim_node",
name="turtlesim",
),
]
)
ld.add_action(
ExecuteProcess(
cmd=[
[
FindExecutable(name="ros2"),
" service call ",
"/spawn ",
"turtlesim/srv/Spawn ",
"\"{x: 2.0, y: 2.0, theta: 0.0, name: 'turtle2'}\"",
]
],
shell=True,
)
)
return ld
Method 2
Based on https://docs.ros.org/en/humble/Tutorials/Intermediate/Launch/Using-Event-Handlers.html
from launch import LaunchDescription
from launch.actions import ExecuteProcess, LogInfo, RegisterEventHandler
from launch.event_handlers import OnProcessStart
from launch.substitutions import FindExecutable, LaunchConfiguration
from launch_ros.actions import Node
def generate_launch_description():
turtlesim_node = Node(
package="turtlesim",
executable="turtlesim_node",
name="sim",
)
spawn_turtle = ExecuteProcess(
cmd=[
[
FindExecutable(name="ros2"),
" service call ",
"/spawn ",
"turtlesim/srv/Spawn ",
"\"{x: 2.0, y: 2.0, theta: 0.0, name: 'turtle2'}\"",
]
],
shell=True,
)
return LaunchDescription(
[
turtlesim_node,
RegisterEventHandler(
OnProcessStart(
target_action=turtlesim_node,
on_start=[
LogInfo(msg="Turtlesim started, spawning turtle"),
spawn_turtle,
],
)
),
]
)
Originally posted by Hongbo Miao with karma: 70 on 2023-01-01
This answer was NOT ACCEPTED on the original site
Post score: 1