Start everything together
Goal: start the whole demo with one command instead of several terminals.
The idea
Your demo now has several nodes: one that prints the position and one that drives. Starting each one by hand, in its own terminal, is tiring and easy to get wrong.
A launch file is a Python file that lists the nodes to start, with their settings. You start everything with one command, and Ctrl+C stops everything together.
Look at it live
- Open two terminals (press Alt+E). Start
ros2 run starter_kit where_am_iin one andros2 run starter_kit ask_and_answerin the other. Then stop both with Ctrl+C. This is the work a launch file does for you. - Run
ros2 pkg executables starter_kit. It lists the programs you can start, aspackage executablepairs.
Read the code
A launch file has one function, generate_launch_description. It returns the list of things to start. Each node is a Node(...) with the package and the executable you would give to ros2 run.
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
driver = Node(
package='starter_kit',
executable='change_it_live',
output='screen', # show its prints in this terminal
parameters=[{'bonk_threshold': 0.4}], # set parameters at start
)
return LaunchDescription([driver])
output='screen'shows the node's messages in the terminal you launched from.parameterstakes a list of dictionaries: the same parameters you changed by hand in Challenge 4.
Your mission
Open launch/starter_kit_launch.py and complete the TODOs.
- TODO 1: create a
Nodeaction forwhere_am_iand one forask_and_answer. Addoutput='screen'so you can see their prints. - TODO 2: set the
bonk_thresholdparameter in the launch file, on theask_and_answernode. - TODO 3: add your nodes to the
LaunchDescriptionlist.
Start everything with one command. Then add conflict_demo:=true to also start move_the_robot. Watch the two drivers fight over /cmd_vel (the nodes even warn you), then explain why:
ros2 launch starter_kit starter_kit_launch.py
ros2 launch starter_kit starter_kit_launch.py conflict_demo:=true
Why only two nodes?
ask_and_answer already drives the robot (drive, BONK and E-Stop). Starting move_the_robot as well would put two publishers on /cmd_vel, and the robot would flicker between their commands.
What you should see
One command starts both nodes with no errors, and their output appears in the same terminal. The robot drives, brakes near obstacles at the threshold you set, and obeys the E-Stop from Challenge 5. With conflict_demo:=true the robot jitters, because two nodes publish to /cmd_vel, and the nodes print a warning.
Stuck?
Hint 1
Each Node(...) needs a package and an executable. The example for where_am_i is in the comments of the file.
Hint 2
parameters takes a list of dictionaries.
Hint 3
ros2 pkg executables starter_kit lists the executables you can launch.
Submit
When it works, open the Submit tab, choose 6 · Start Everything Together, and submit.