当前位置:网站首页>Ros2 knowledge (6): principle and practice of coordinate object TF

Ros2 knowledge (6): principle and practice of coordinate object TF

2022-06-23 12:00:00 Mr anhydrous

https://articulatedrobotics.xyz/ready-for-ros-6-tf/

One 、 Importance of transformation ?

         Coordinate transformation ( Or transform ) It plays an important role in robot mathematics . They are a mathematical tool , Used to obtain a point or measurement from an angle , And represent them in more useful ways . If you don't use transformation , We will need to use trigonometric functions to perform calculations , This can quickly become very complex for larger problems , Especially in 3D in .

         This tutorial is about ROS How to make it easy to use transformation . If you are particularly interested in more complex and interesting basic mathematics , I've written ( Most of the ) A series of tutorials about them , You can get... Here .

         The following shows two examples of robotic systems that help with the conversion . First , Two mobile robots are exploring , One of them found an object of interest . How does another robot know how to get to it ? In the second , The installed camera has found a target , The manipulator needs to move the gripper to the target . How do we know the correct movement from the fixture to the target ?

         Be careful : In the two cases above , One is two dynamics Frame stay world The relationship between coordinates ; The other is base-link And world Its relatively static profile .

         To solve these problems , We need to first assign a coordinate system or framework to the appropriate components of our system . Next , We need to define Frame Conversion between . Transformation tells us the translation and rotation required to convert one frame to another , And it can be easily reversed to another way .

         If we have a system , Each of these frames is composed of a ( And there is only one ) The relationship between other frames , This will create a tree structure , We will be able to convert known points in one frame into any other frame in the tree .

         Figuring out all the math and code behind this can be tricky , therefore ROS It provides us with a complete subsystem , Just to make the transition easier . In this tutorial , We will learn how to set up a transformation tree for some examples .

Two 、 Transformation Frame

        ROS A file named tf2(TransForm edition 2) To handle these transformations for us . Any node can use tf2 The library broadcasts the conversion from one frame to another . As mentioned above , These transformations will need to form a tree structure , Each of these frames consists of one of the other frames ( And there is only one ) Change definition , But any number of frames can depend on it . The following figure shows part of the tree we will explore later . In this tree ,base and camera Is relative to world Defined ,l3 Is relative to base Defined .

         Any node can also use tf2 Library to listen for transformations , Then use the transform to convert the point from any frame to any other frame , As long as they are connected in the tree .

         When a node broadcasts a particular transformation , It can be static ( Does not change over time ), It can also be dynamic ( May change over time , But it's not necessary ). The reason for this difference is that powerful systems need to know whether their information is outdated , And if the broadcaster hasn't updated the dynamic conversion for a while , You can mark the error . On the other hand , Static transformation can be broadcast once , And the assumption is correct before broadcasting the new transformation .

         Hereunder ,tf2 The library is still using themes (/tf and /tf_static) To handle all these communications , But because we don't actually publish to the topic ourselves , So we call it broadcast and listen, not publish and subscribe .

         If we are writing our own nodes , We can encode them to broadcast any transformation we like , But most of us are not ready to write custom nodes or compute transformations at first . To solve this problem ,ROS Some built-in nodes are attached to perform some common broadcast tasks .

3、 ... and 、 Single Frame radio broadcast

         The first way is to use static_transform_publisher Broadcast them manually . As you may have guessed , This tool cannot broadcast dynamic transformations , Only static transformations can be broadcast . This may seem a little useless , But it's about learning transformation 、 Perform rapid testing and act as a in larger systems “ adhesive ” Very helpful ( for example , If two different nodes expect a frame to have a different name , Or in a slightly different location ).

         The following command will broadcast the static transformation , And provide information from parent_frame To child_frame Translation and rotation of . Please note that , Rotation is in radians , And process after translation , Relative to the local coordinate system in sequence .

1
ros2 run tf2_ros static_transform_publisher x y z yaw pitch roll parent_frame child_frame

for example , We might want a framework robot_1 With 45°(0.785 radian ) The angle crosses the world frame and goes up . So , We will use the following command :

1
ros2 run tf2_ros static_transform_publisher 2 1 0 0.785 0 0 world robot_1

         Suppose we have a second robot , It always sits on the right side of our first robot ( Like a side car ). Use the second terminal , We can use the following command to create another static transformation to create a new framework ,robot_2, It is located in robot_1 Positive of x Direction 1m It's about .

1
ros2 run tf2_ros static_transform_publisher 1 0 0 0 0 0 robot_1 robot_2

To check if these are valid , We will use ROS Visualization tools RViz.

Four 、 stay RVIZ Look up Frame And transformation

stay ROS 2 in ,RViz(ROS Visualization tools ) be called rviz2, In a package with the same name . So we just need to run :

1
ros2 run rviz2 rviz2
 Please note that , You can run it directly  rviz2  As a shortcut , Instead of using  ros2 run.
RViz It can display different kinds of data . To display the  TF  data , We click... In the lower left corner “ add to ” Button , And then choose “TF”.

Our transformation will not happen yet , Because we haven't told RViz Which frame to use as a reference ( It defaults to map , It doesn't exist now ). In the upper left corner , We can choose the world as our fixed frame .

“ Global status ” Should become healthy , Our two frameworks should appear , There is a line between them to indicate the conversion .

Please note that ,RViz Represent the transformation as an arrow from child to parent , Not in the following introduction and view_frames Father to son in .

         We can use the left panel to configure RViz Data shown in . for example , We can open it TF Visualize settings and enable display frame name .

         If we go back to our first terminal , And 90°(1.57 rad) Re run the command with a larger rotation of , We should RViz See the mark rotation of the first robot in , And the second robot also moves , Because its frame is relative to the definition of the first robot .

1
ros2 run tf2_ros static_transform_publisher 2 1 0 1.57 0 0 world robot_1

         It's a good thing to play with this and understand how the transformation works . Try fixing the frame ( top left corner ) Change to one of the robots instead of the world , Adjust the frame to the new position and rotation , Or add some new .

         Before proceeding to the next section , We need to make sure that all static_transform_publishers And shut down RViz, Otherwise they will interfere with .

5、 ... and 、 Broadcast a mobile robot

         up to now , We have learned some cool techniques , But we still insist on publishing static frames . We want our robots to move ! Before we solve this problem , We need to install several additional packages :

1
sudo apt install ros-foxy-xacro ros-foxy-joint-state-publisher-gui

         The first step is to make sure we have a robot URDF file . This is a configuration file , We can specify various physical properties of robot components in it , For example, their size 、 shape 、 Color, etc. . There are many tutorials available for writing URDF file , But now we can use this sample file , It is used for manipulators similar to those in the introduction .

stay URDF in , The robot consists of a series of rigid components called links , These links are linked together in a parent-child tree structure , The relationship between different links is called joints . Deja vu ?

         It's not hard to see the link / Joint patterns and frames / The transformation mode is very similar . Because they are closely related , So there is one called robot_state_publisher Of ROS node , It can receive a URDF File and automatically broadcast all transformations in it . It will also URDF The complete contents of the file are published to the topic /robot_description, So that any other node that needs it can use the same file .

         stay URDF In file , Each joint can be defined as “ fixed ”, Or one of several movable types ( for example , Continuous rotation 、 Finite rotation 、 Linear sliding ). For fixed joints ,robot_state_publisher You can publish only static transformations , But for moving joints , It needs an external value ( For example, angle or distance ) To calculate the dynamic transformation of the point in time . To get these values , It subscribes to a program called /joint_states The theme of , This topic will contain JointState news . These messages can contain information about the position of the joint 、 A message of speed or effort ( But now we only use location ).

         All of a sudden , Our work has become much easier ! There is no need to broadcast the entire conversion , What we need to do is release JointState news . Usually , These data will come from the actuator feedback sensor on the robot , Such as encoder or potentiometer ( And those that can be simulated in the simulation environment ). Now? , We will use the name joint_state_publisher_gui Tools to forge Federated States . This node will view the information provided by robot_state_publisher released /robot_description The theme , Find any joints that can move , And display a slider for them . It reads values from these sliders , And publish them to /joint_states.

Let's start running all this .

First , We will run robot_state_publisher, The first run can be a bit confusing , Because of the introduction URDF The file is a bit tricky . URDF Using parameters robot_description, So the command will be similar to :

1
ros2 run robot_state_publisher robot_state_publisher --ros-args -p robot_description:=(something here)

         You may wish to robot_description Parameter is URDF Path to file , But it actually wants URDF The complete contents of the file are passed on the command line . To achieve this , We need to use URDF Upper xacro The software preprocesses it , In the child shell ($(...)) Run it in , And enclose it in quotation marks ("...") in , In this way, the blank space will not appear and break everything . So our run command looks more like :

1
ros2 run robot_state_publisher robot_state_publisher --ros-args -p robot_description:="$(xacro path/to/my/xacro/file.urdf.xacro)"

Please note that , Because running in this way robot_state_publisher It's very painful , So it is usually easiest to create a startup file to do this for us . An example is provided here .​
 

then , To publish joint status , We can run

1
ros2 run joint_state_publisher_gui joint_state_publisher_gui

         Last , We can start again RViz, add to TF visualization , And see our frame move as you adjust the slider . We can still do that RViz Add “RobotModel” visualization . Use the options to select /robot_description As a theme , A visual representation of the link should appear .

  Please note that , For mobile platforms , It's going to get a little complicated . robot_state_publisher It can broadcast all transformations in the robot , But you need other nodes to publish the transform , For example, the position of the robot in the environment . There are existing nodes to handle this kind of thing , But they will be covered in a later tutorial .

6、 ... and 、 Under the hood

         I hope the above example is effective , But sometimes when we use transformations , Things don't work properly , We need to find out why .

         As mentioned earlier , Transform data is published across two topics ,/tf and /tf_static. We can try it ros2 Theme echo listen to these themes directly , But this is sometimes hard to explain . To help ,ROS A file named view_frames Tools for ( We need to install tf2_tools Do you ?), It allows us to visualize the transformation tree , And information about how it was broadcast .

Please note that , stay ROS 1 One of them is called rqt\u tf\u tree Useful tools for , It can do this , But less trouble . However , At the time of writing , This has not been ported to ROS 2, So we have to use view\u frame .

        view\u frames The tool is actually a Python Script , It listens for the transition for a few seconds , Then generate a file named frames The file of .pdf( as well as frames.gv) In any directory where the terminal is located when executing the script ( For new terminals , This will be your home directory ).

The script is tf2\u tools Part of the package , So we just need to run :

1
ros2 run tf2_tools view_frames.py

         If we look at the generated file ( Open in the file browser , Or from with atril frames.pdf Terminal open for ), We can see the visual tree . Please note that , The arrow here is the same as RViz contrary ( Same as the arrows in the introduction ). We won't worry too much about the rest of the information displayed now .

If you are not familiar with it , So in order to “16314……” The big number at the beginning is the time , use Linux Common in the system “Unix Time ” Express , The same is true in robot platforms . of Unix Time ( Also known as Epoch Time ) For more information , Please check out Epoch converter . When running the simulation ,Gazebo Will generate your own time source , The number will be in Gazebo Seconds after startup .

7、 ... and Conclusion

        ROS The transformation system is a powerful tool , We just have a preliminary understanding of how to use it . How do we use TF The details of will depend on many factors , For example, our robot is mobile , Or manipulator ( Or move the manipulator ), And whether we are running simulations or on physical hardware . In future tutorials , We will study more carefully TF How to use for a specific project .

see , we’ll see how to write our own URDF files.

原网站

版权声明
本文为[Mr anhydrous]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231152017433.html