ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How do you load urdf files in rviz2 with ROS2?

asked 2020-06-08 14:56:20 -0600

rydb gravatar image

updated 2020-06-18 18:22:37 -0600

Edit 2: Following fergs's launch file code on https://github.com/mikeferguson/ubr_r... , I was able to create a working launch file which sets up robot_state_publisher which rviz2 picks up. However, now the problem is that, although the joints for the models load, the models do not... I do not have the 5 karma required to upload the picture, but here is the modified launch file im using, and the urdf file are below.

I also removed the error log mention in Edit 1 as well as the .rviz config file since they are no longer relevant to the current problem.

Edit: As stevemacenski pointed out, I was getting errors thrown about non-existent libraries was an rviz config problem. it throws an error with the old library names but it doesn't tell you that in rviz2, they renamed the libraries. So When when my laucnh file was loading, It threw an error because I was using Rviz1 library naming conventions instead of Rviz2 library naming conventions . I fixed the .rviz config file and the full working file has been moved into the question

I thought it would be a straight port of ros1 launch file to ros2 launch file to load but apprently not. I've now fixed the rviz config library naming convention to match ros2's, and I managed to get the launch file to load robot_state_publisher and got rviz2 to read it, the final problem seems to be that rviz2 loads the joints of urdf, but not the model(basic geometry and .dae file) of the robot. This urdf file I have will work in

Python launch file:

 #!/usr/bin/env python3

import os
import sys

from ament_index_python.packages import get_package_share_directory

import launch
import launch_ros.actions


def generate_launch_description():
    # Load the URDF into a parameter
    bringup_dir = get_package_share_directory('testpkg')
    urdf_path = os.path.join(bringup_dir, 'urdf', 'doggo.urdf')
    urdf = open(urdf_path).read()
  )

    return launch.LaunchDescription([),
        launch_ros.actions.Node(
            name='robot_state_publisher',
            package='robot_state_publisher',
            executable='robot_state_publisher',
            parameters=[{'robot_description': urdf}],
        ),
       ),
     )
    ])


def main(argv=sys.argv[1:]):
    """Run lifecycle nodes via launch."""
    ld = generate_launch_description()
    ls = launch.LaunchService(argv=argv)
    ls.include_launch_description(ld)
    return ls.run()


if __name__ == '__main__':
    main()

URDF file:

<?xml version="1.0"?>
<robot name="StlTest">
  <link name="base_link">
    <visual>
            <color rgba="0.1 0.1 0.1 1"/>
      <geometry>
        <cylinder length="0.01" radius="0.2"/>
      </geometry>
    </visual>
  </link>

    <link name="body_link">
        <visual>
        <origin xyz="0 0 0" rpy="0 0 0"/> 
            <geometry>
                <mesh filename="package://testpkg/meshes/doggo_body.dae"/>
            </geometry>
        </visual>
    </link>

    <joint name="base_to_right_leg" type="fixed">
      <parent link="base_link"/>
      <child link="body_link"/>
  </joint>
</robot>

Incase the filestructure is the issue, here it is.

  SourceBattleDoggo  
    ├── build
│   ├── COLCON_IGNORE
│   └── testpkg
│       ├── ament_cmake_core
│       ├── ament_cmake_environment_hooks
│       ├── ament_cmake_index
│       ├── ament_cmake_package_templates
│       ├── ament_cmake_symlink_install
│       ├── ament_cmake_uninstall_target
│       ├── ament_copyright
│       ├── ament_lint_cmake
│       ├── ament_xmllint
│       ├── cmake_args.last
│       ├── CMakeCache.txt
│       ├── CMakeFiles
│       ├── cmake_install.cmake
│       ├── colcon_build.rc
│       ├── colcon_command_prefix_build.sh
│       ├── colcon_command_prefix_build.sh.env
│       ├── CTestConfiguration.ini
│       ├── CTestCustom.cmake
│       ├── CTestTestfile.cmake
│       ├── install_manifest.txt
│       ├── Makefile
│       └── symlink_install_manifest.txt
├── install
│   ├── COLCON_IGNORE
│   ├── local_setup.bash
│   ├── local_setup.ps1 ...
(more)
edit retag flag offensive close merge delete

4 Answers

Sort by » oldest newest most voted
4

answered 2020-06-23 16:20:03 -0600

rydb gravatar image

updated 2020-06-23 16:21:09 -0600

I figured out the problem on my own, if I had 5 karma, I'd show a picture of the model being rendered and my rviz setup. Here were the problems that were found and solved. If I had 10 karma, I'd also accept this awnser:

Problem 1: Rviz was throwing errors due to a plethora of undefined libraries:

Solution: I was using a custom .rviz config file copied from the ROS1 urdf tutorial on github, and it ended up throwing undefined library errors due to ROS1 and RO2 having different naming conventions. To fix this, I renamed "class:" declarations to either have rviz_common/(nameofpanel) and rviz_default_plugins/(name of panel) instead of just (name of panel). E.G, "rviz_common/Displays" instead of "/Displays".

I can't find where in the documentation when and why they changed them so someone please comment and link that here

Problem 2: My launch file I was using from urdf_tutorial was throwing: "[ERROR] [launch]: Caught exception in launch (see debug for traceback): The launch file may have a syntax error, or its format is unknown".

Solution: My launch file was in .xml, and it appears ROS2 launch files are now only in .cpp and python. Thanks to the help of fergs's linked launch file example, I was able to modify it to publish robot_state_publisher with the urdf file contents!

#!/usr/bin/env python3

import os
import sys

from ament_index_python.packages import get_package_share_directory

import launch
import launch_ros.actions


def generate_launch_description():
    # Load the URDF into a parameter
    bringup_dir = get_package_share_directory('testpkg')
    urdf_path = os.path.join(bringup_dir, 'urdf', 'doggo.urdf')
    urdf = open(urdf_path).read()
  )

    return launch.LaunchDescription([),
        launch_ros.actions.Node(
            name='robot_state_publisher',
            package='robot_state_publisher',
            executable='robot_state_publisher',
            parameters=[{'robot_description': urdf}],
        ),
       ),
     )
    ])


def main(argv=sys.argv[1:]):
    """Run lifecycle nodes via launch."""
    ld = generate_launch_description()
    ls = launch.LaunchService(argv=argv)
    ls.include_launch_description(ld)
    return ls.run()


if __name__ == '__main__':
    main()

I have no clue how it works, but it does.

Problem 3: rviz2 was not loading the urdf despite a robot_state_publisher node being published.

Solution: This was actually a collection of problems instead of one! I wish I was able to publish pictures to explain it, but I do not have enough karma. First rviz2 was throwing an error "Fixed Frame [map] does not exist", but what eclipsed me was that this frame is supposed to reference the fixed frame in your urdf file. I named mine base_link so I renamed "map" to "base_link" in the displays menu and that fixed that.

Next despite there being no visible errors, my urdf was not displaying. What did not understand was that you need to have a display ADD ON enabled called "robotmodel" enabled! This is not in the panels menu. In order to enable this, you need to click the "Add" button within the display panel menu, scroll down rviz_default_plugins to click RobotModel, then click ok to enable that. Robot model needs to be enabled in the displays panel in order to render urdf files.

Next, despite both RobotModel and rviz2 ... (more)

edit flag offensive delete link more

Comments

I can confirm that this solution worked. Note that this answer gets clipped since it is long, and you need to hit the "more" button to see all of it (I was missing the last step, which is the most important)

808brick gravatar image 808brick  ( 2021-03-29 18:28:00 -0600 )edit
1

answered 2020-06-08 21:36:24 -0600

This doesn't look like a URDF problem but your rviz config file issue. You have displays and plugins that don't exist or were renamed in ROS2. Most of those are just renamed to rviz_common or rviz_default_plugins.

edit flag offensive delete link more

Comments

The .rviz config problem and the working rviz has been placed in the question

rydb gravatar image rydb  ( 2020-06-09 13:21:05 -0600 )edit
0

answered 2020-06-11 23:37:40 -0600

fergs gravatar image

updated 2020-06-16 14:55:38 -0600

The major change here is that there is no longer a centralized parameter server. Therefore, you need to provide the URDF to the robot_state_publisher as a parameter named "robot_description". The robot_state_publisher is then very kind and publishes that URDF on the "robot_description" topic. In RVIZ2, you'll find that the RobotModel plugin has the option to load the URDF from either a topic (which is what I use, since robot_state_publisher is already publishing the topic) or you can have RVIZ2 load the file directly from a provided file name in the Displays panel.

I'm using Python, rather than XML, for launch - but here's an example of a full robot: https://github.com/mikeferguson/ubr_r...

edit flag offensive delete link more

Comments

I tried using a modified version of your launch file and I did get a topic giving an output in the console, but I don't see any part of it uploading the topic information to rviz? I also looked at the other launch file in the same directory and it doesn't make any mention of rviz. Also you said it was an option in rviz to load a urdf through the options panel but I cant find any "options" panel in rivz2. I dont have enough karma to share a picture but my options I get through Panels > Add new panels are:

Display, Help, Selection, Tool properties, transformations, Views

rydb gravatar image rydb  ( 2020-06-12 14:23:38 -0600 )edit

Your modified launch file im suing:

#!/usr/bin/env python3

import os
import sys

from ament_index_python.packages import get_package_share_directory

import launch
import launch_ros.actions


def generate_launch_description():
    # Load the URDF into a parameter
    bringup_dir = get_package_share_directory('testpkg')
    urdf_path = os.path.join(bringup_dir, 'urdf', 'doggo.urdf')
    urdf = open(urdf_path).read()
  )

    return launch.LaunchDescription([),
        launch_ros.actions.Node(
            name='robot_state_publisher',
            package='robot_state_publisher',
            executable='robot_state_publisher',
            parameters=[{'robot_description': urdf}],
        ),
       ),
     )
    ])


def main(argv=sys.argv[1:]):
    """Run lifecycle nodes via launch."""
    ld = generate_launch_description()
    ls = launch.LaunchService(argv=argv)
    ls.include_launch_description(ld)
    return ls.run()


if __name__ == '__main__':
    main()
rydb gravatar image rydb  ( 2020-06-12 14:25:54 -0600 )edit

I've updated the answer to use "Displays" rather than options - since that is the name it has. Basically, if you expand the RobotModel display that you've added, you'll see some settings.

My launch file does not include RVIZ - since I launch that separately.

fergs gravatar image fergs  ( 2020-06-16 14:56:54 -0600 )edit
0

answered 2020-10-18 16:20:23 -0600

I rencently try to do this and actually is quite easy but you have differents steps in differents files if you are in python you need to first add your urdf files to the share directory in the data files of your setup.py

        (os.path.join('share', package_name), glob('urdf/*')),

and then you need to get the path of this file in your launch file

urdf_file_name = 'robot.urdf.xml'

urdf = os.path.join(
    get_package_share_directory('mypackage'),
    urdf_file_name)

once that done you can launch the node with this file as argument

Node(
        package='robot_state_publisher',
        node_executable='robot_state_publisher',
        name='robot_state_publisher',
        output='screen',
        arguments=[urdf])

finally you can add the topic description in you rviz file in the RobotModel plugin

    Description File: ""
    Description Source: Topic
    Description Topic: 
    Depth: 5
    Durability Policy: Volatile
    History Policy: Keep Last
    Reliability Policy: Reliable
    Value: "/robot_description"
edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2020-06-08 14:56:20 -0600

Seen: 6,675 times

Last updated: Jun 23 '20