ROS2_01 - 8BitsCoding/RobotMentor GitHub Wiki

ROS2.0 node 실행

$ ros2 run <package_name> <executable_file>

ROS2.0 launch 구성

"""Launch a talker and a listener."""

from launch import LaunchDescription
import launch_ros.actions


def generate_launch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            package='teleop_twist_keyboard', node_executable='teleop_twist_keyboard', output='screen'),
    ])

설명

from launch import LaunchDescription
import launch_ros.actions

launch 파일 구성의 기본 import(받아들인다.)

def generate_launch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            package='teleop_twist_keyboard', node_executable='teleop_twist_keyboard', output='screen'),
    ])

generate_launch_description()함수를 정의함으로서 launch파일 구성

package=<package_name> : 패키지 이름

node_executable=<cpp_executable_name : 실행가능한 cpp파일 이름

output='screen' : 출력을 어떤식으로 할지


패키지 생성

$ source /opt/ros/crystal/setup.bash

ROS2.0 용 setup.bash 실행

$ ros2 pkg create <package_name> --build-type ament_cmake --dependencies <package_dependecies>

# Example
$ ros2 pkg create my_package --build-type ament_cmake --dependencies rclcpp

패키지 확인

$ ros2 pkg list
# OR
$ ros2 pkg list | grep <package_name>

패키지 컴파일

$ cd ~/ros2_ws
$ colcon build --symlink-install
# Or, 하나의 패키지만 빌드
$ colcon build --symlink-install --packages-select <package_name>
$ source ~/ros2_ws/install/setup.bash

빌드 후 소스필요!


node 생성해보기

cpp 생성

src 내부에 newnode.cpp 생성

#include "rclcpp/rclcpp.hpp"
// ROS2.0 cpp library

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  auto node = rclcpp::Node::make_shared("newnode");

  RCLCPP_INFO(node->get_logger(), "new node created!!");
  
  rclcpp::shutdown();
  return 0;
}

주의 cpp파일의 권한 변경을 해줘야함 $ chmod +x <cpp>

launch 수정

"""Launch a talker and a listener."""

from launch import LaunchDescription
import launch_ros.actions


def generate_launch_description():
    return LaunchDescription([
        launch_ros.actions.Node(
            package='my_package', node_executable='newnode', output='screen'),
    ])

CMake 수정

# 여기
add_executable(newnode src/newnode.cpp)
# 여기
ament_target_dependencies(newnode rclcpp)

# 여기
install(TARGETS
   newnode
   DESTINATION lib/${PROJECT_NAME}
 )

# Install launch files.
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

생성된 노드 확인

$ ros2 node list
# Or
$ ros node info /<node>
/<node>
  Subscribers:
    /~
  Publishers:
    /~
  Services:
    /~

(추가) 환경변수 확인

$ export | grep ROS
⚠️ **GitHub.com Fallback** ⚠️