공부중

[ROS2] urdf 패키지 만들기 본문

프로그래밍/ROS

[ROS2] urdf 패키지 만들기

복습 2024. 10. 6. 00:24
728x90

ubuntu 22.04

humble

 

https://wiki.ros.org/urdf

 

urdf - ROS Wiki

Overview This package contains a number of XML specifications for robot models, sensors, scenes, etc. Each XML specification has a corresponding parser in one or more languages. Components New in Hydro A number of different packages and components make up

wiki.ros.org

 

 

1. 워크스페이스만들기

~$ mkdir -p ~/ros2_first_ws/src

 

 

2. urdf 패키지 생성 

$ cd ~/ros2_first_ws/src/
ros2_first_ws/src$ ros2 pkg create --build-type ament_python urdf_tutorial
going to create a new package
package name: urdf_tutorial
destination directory: /home/ye2202/rokey_workspace/ros2_first_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['ye2202 <ye2202@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_python
dependencies: []
creating folder ./urdf_tutorial
creating ./urdf_tutorial/package.xml
creating source folder
creating folder ./urdf_tutorial/urdf_tutorial
creating ./urdf_tutorial/setup.py
creating ./urdf_tutorial/setup.cfg
creating folder ./urdf_tutorial/resource
creating ./urdf_tutorial/resource/urdf_tutorial
creating ./urdf_tutorial/urdf_tutorial/__init__.py
creating folder ./urdf_tutorial/test
creating ./urdf_tutorial/test/test_copyright.py
creating ./urdf_tutorial/test/test_flake8.py
creating ./urdf_tutorial/test/test_pep257.py

[WARNING]: Unknown license 'TODO: License declaration'.  This has been set in the package.xml, but no LICENSE file has been created.
It is recommended to use one of the ament license identitifers:
Apache-2.0
BSL-1.0
BSD-2.0
BSD-2-Clause
BSD-3-Clause
GPL-3.0-only
LGPL-3.0-only
MIT
MIT-0

 

 

파일 정보 확인

ros2_first_ws/src/urdf_tutorial$ ls
package.xml  resource  setup.cfg  setup.py  test  urdf_tutorial

 

package.xml 확인 

$ cat package.xml 
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>urdf_tutorial</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="ye2202@todo.todo">ye2202</maintainer>
  <license>TODO: License declaration</license>

  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>

 

 

3. 기본 디렉토리 추가 생성 

/ros2_first_ws/src/urdf_tutorial$ 
위치에 ...

mkdir urdf

mkdir launch

mkdir rviz

mkdir world

mkdir conf

 

 

4. robot1.xacro 파일 생성 

$ cd ~/ros2_first_ws/src/urdf_tutorial/urdf

 

$ code robot1.xacro

 

<?xml version="1.0"?>
<robot xmlns:xacro="http://ros.org/wiki/xacro" name="urdf_test">

    <!--BASE-->
    <link name="base_link">
    </link>

    <!--JOINT LINK-->
    <joint name="base_joint" type="fixed">
        <parent link="base_link"/>
        <child link="body"/>
    </joint>

    <!--BODY-->
    <link name="body">
        <visual>
            <geometry>
                <box size="1 1 1"/>
            </geometry>
        </visual>
    </link>

</robot>

 

 

더보기

틀린거??

<?xml version="1.0"?>
<robot xmlns:xacro="http://ros.org/wiki/xacro" name="urdf_test" >

    <!--BASE-->
    <link name="base_link">
    </link>

    <!--BOOT LINK-->
    <joint name="base_link"/>
        <parent link="base_link"/>
        <child link="body"/>
    </joint>

    
    <link name="body">
        <visual>
            <geometry>
                <box size="1 1 1"/>
            </geometry>
        </visual>
    </link>

</robot>

 

 

5. lainch 파일 추가

/ros2_first_ws/src/urdf_tutorial$ code robot1.launch.py

 

ros1할때는 launch였는데 ros2는 파이썬 형식으로 많이 만드는듯

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.substitutions import LaunchConfiguration
from launch.actions import DeclareLaunchArgument
from launch_ros.actions import Node
import xacro

def generate_launch_description():
    # LaunchConfiguration 객체 생성
    use_sim_time = LaunchConfiguration("use_sim_time")
    
    # 패키지 경로와 xacro 파일 경로 설정
    pkg_path = os.path.join(get_package_share_directory("urdf_tutorial"))
    xacro_file = os.path.join(pkg_path, "urdf", "robot1.xacro")
    
    # xacro 파일 처리
    robot_description = xacro.process_file(xacro_file).toxml()
    params = {"robot_description": robot_description, "use_sim_time": use_sim_time}
    
    # LaunchDescription 리턴
    return LaunchDescription(
        [
            # 'use_sim_time' 인자 설정
            DeclareLaunchArgument(
                "use_sim_time", default_value="false", description="Use simulation time"
            ),
            
            # 로봇 상태 퍼블리셔 노드 실행
            Node(
                package="robot_state_publisher",
                executable="robot_state_publisher",
                output="screen",
                parameters=[params],
            ),
        ]
    )

 

 

 

 

6. setup.py 수정 

기존 파일 

from setuptools import find_packages, setup

package_name = 'urdf_tutorial'

setup(
    name=package_name,
    version='0.0.0',
    packages=find_packages(exclude=['test']),
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='ye2202',
    maintainer_email='ye2202@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
        ],
    },
)

 

아래와 같이 추가 

from setuptools import find_packages, setup
# 두줄 추가
import os
from glob import glob

package_name = 'urdf_tutorial'

setup(
    name=package_name,
    version='0.0.0',
    packages=find_packages(exclude=['test']),
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        # 두줄 투가
        (os.path.join('share', package_name, 'launch'), glob('launch/*.launch.py')),
        (os.path.join('share', package_name, 'urdf'), glob('urdf/*.xacro')),
        
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='ye2202',
    maintainer_email='ye2202@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
        ],
    },
)

 

 

 

 

자주 쓰는 명령어

ye2202@ye2202-HP-Laptop-14s-fq0xxx:~$ vi .bashrc
alias vib='code ~/.bashrc'
alias sb='source ~/.bashrc'
alias ccbs='source /opt/ros/humble/setup.bash && colcon build --symlink-install && source install/setup.bash'
ye2202@ye2202-HP-Laptop-14s-fq0xxx:~$ source .bashrc

 

/ros2_first_ws$ ccbs
Starting >>> urdf_tutorial
--- stderr: urdf_tutorial                   
/usr/lib/python3/dist-packages/setuptools/command/easy_install.py:158: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
---
Finished <<< urdf_tutorial [1.19s]

Summary: 1 package finished [1.50s]
  1 package had stderr output: urdf_tutorial

 

 

 

7. 패키지 소싱


가장 중요한 거... 

$ source ~/ros2_first_ws/install/setup.bash
$ echo "source ~/ros2_first_ws/install/setup.bash" >> ~/.bashrc

 

이건 영구적(?).. (지우기 전까지는 영구적이니까.. )으로 하는거고

 

아래는 일시적 느낌..

위치 잘 잡고 해야된다. 

$ source ./install/setup.bash

 

 

8. 런치파일 실행

$ ros2 launch urdf_tutorial robot1.launch.py

 

 

9; rviz

$ rviz2

 

add -> robotmodel 추가

 

위에 런치 파일 실행한 다음에 해야됨. 

 

Fixed Frame -> base_link

 

add -> TF 추가 

잘 안보이면 Marker Scale을 5 정도로 하면 보인다. 

 

 

 

 

10. gazebo

$ sudo apt itall ros-humble-gazebo-ros-pkgs
$ gazebo

 

$ gazebo --verbose /opt/ros/humble/share/gazebo_plugins/worlds/gazebo_ros_diff_drive_demo.world 
$ ros2 topic pub /demo/cmd_demo geometry_msgs/msg/Twist "{linear: {x: 0.1, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.1}}" -r 10

돌아간다.

Screencast from 10-06-2024 01:00:02 AM.webm
0.16MB

 

 

 

 

 

 

 

 

 

 

728x90