# TF and Python **Goal:** listen to frames, and publish fixed and moving frames from Python. **Package:** [tf2_python_capabilities_demo](https://github.com/ROS-Industrial-Session-B/tf2_python_capabilities_demo), short scripts with `tf2_ros`. **Before:** the [TF and rqt Frame Editor](01_tf_frame_editor.md) exercise, with the frames `object_1` (parent `world`) and `object_2` (parent `object_1`), the Frame Editor and RViz (TF display) running. ## 1. Build the package In the `src/` folder of your workspace: ```bash git clone https://github.com/ROS-Industrial-Session-B/tf2_python_capabilities_demo.git cd .. rosdep install --from-paths src/tf2_python_capabilities_demo --ignore-src -y colcon build --symlink-install --packages-select tf2_python_capabilities_demo source install/setup.bash ``` ## 2. Listener: where is a frame? Read [`listener.py`](https://github.com/ROS-Industrial-Session-B/tf2_python_capabilities_demo/blob/main/tf2_python_capabilities_demo/listener.py), then run it: ```bash ros2 launch tf2_python_capabilities_demo listener.launch.py target_frame:=world source_frame:=object_2 ``` Once per second, it prints where `object_2` is, expressed in `world`. - Move `object_1` or `object_2` with the Frame Editor: what changes in the output? - Start it with `source_frame:=object_3`, a frame that nobody publishes yet. What happens? ## 3. Static broadcaster: a fixed frame Add the frame `fixture`, below `world`: ```bash ros2 launch tf2_python_capabilities_demo static.launch.py ``` ![Static TF: fixture](../../../_static/manipulation/tf/static_tf_fixture.png) Read [`static_broadcaster.py`](https://github.com/ROS-Industrial-Session-B/tf2_python_capabilities_demo/blob/main/tf2_python_capabilities_demo/static_broadcaster.py): the transform is sent only once, on `/tf_static`. - Stop the process with Ctrl+C: RViz still shows `fixture`. Why? - Restart RViz (or run `ros2 run tf2_ros tf2_echo world fixture`): is `fixture` still there? Why must the node of a static frame keep running? ## 4. Dynamic broadcaster: a moving frame Add the frame `object_3`, turning around `object_2`: ```bash ros2 launch tf2_python_capabilities_demo dynamic.launch.py parent_frame:=object_2 child_frame:=object_3 ``` Read [`dynamic_broadcaster.py`](https://github.com/ROS-Industrial-Session-B/tf2_python_capabilities_demo/blob/main/tf2_python_capabilities_demo/dynamic_broadcaster.py): the transform is published again and again (10 Hz) on `/tf`, each time with a new timestamp. - Move `object_2` with the Frame Editor: `object_3` keeps turning around it. - Start the listener of part 2 with `source_frame:=object_3`: the output changes every second.