ROS 2 개발 환경 구축

평범한컴공생·2023년 1월 4일
0

[ROS2]

목록 보기
3/19
post-thumbnail

배경

ROS 2 개발 환경 구축을 위한 IDE(VSCode) 설정과 확장 기능 설치, .json 파일을 통해 환경을 설정해 보자.

작업

1. VSCode 설치

IDE는 VSCode를 이용한다. VSCode 설치는 아래의 링크에서 .deb파일로 설치하면 된다.

https://code.visualstudio.com/Download

1-1. 확장 설치

VSCode의 확장 기능을 설치해보자.

Extension(Ctrl + Shift + X)으로 이동한 후, 아래의 기능을 설치한다.

이름코드명설명
C/C++ms-vscode.cpptoolsC/C++ Intellisense. 디버깅 및 코드 검색
CMaketwxs-cmakeCMake 언어 지원
CMake Toolsms-vscode.cmake-toolsCMake 언어 지원 및 기능 지원
Pythonms-python.python린팅, 디버깅, Intellisense, 코드 서식 지정, 리팩터링 등
이름코드명설명
ROSms-iot.vscode-rosROS 개발 지원
URDFsmilerobotics.urdfURDF/xacro 지원
Colcon Tasksdeitry.colcon-helperColcon 명령을 위한 VSCode Task
이름코드명설명
XML Toolsdotjoshjohnson.xmlXML, XQuery, XPath 지원
YAMLredhat.vscode-yamlYAML 지원
Markdown All in Oneyzhang.markdown-all-in-oneMarkdown 지원
이름코드명설명
Highlight Trailing White Spacesybaumes.highlight-trailing-white-spaces의미 없이 사용된 공백 문자 강조
EOF Maskmsfukui.eof-markEOF 없이 끝난 파일에 EOF 문자 추가
Better Commentsaaron-bond.better-commentsalert, informational, TODO 등 코멘트 기능 강화
indent-rainbowoderwat.indent-rainbow들여쓰기 별로 색을 구분하여 색상화

2. VSCode 개발 환경 구축

다음의 파일을 사용하여 VSCode의 개발 환경을 설정하도록 하자.

~/.config/Code/User/settings.json
~/dev_ws/.vscode/c_cpp_properties.json
~/dev_ws/.vscode/tasks.json
~/dev_ws/.vscode/launch.json

~/.config/Code/User/settings.json

해당 파일은 VSCode의 전역적인 설정을 지정하는 파일이다.

다음처럼 3가지 설정을 지정하면 된다. 즐겨하는 설정이 있다면 추가로 적어주면 된다.

{
  "ros.distro": "foxy",
  "colcon.provideTasks": true,
  "files.associations": {
    "*.repos": "yaml",
    "*.world": "xml",
    "*.xacro": "xml"
  }
}

~/dev_ws/.vscode/c_cpp_properties.json

C/C++ 관련 설정이며, 현재 워크스페이스에서만 영향을 준다.

C/C++ 컴파일러 버전, 컴파일러 경로, Intellisense 모드를 설정할 수 있다.

{
  "configurations": [
    {
      "browse": {
        "databaseFilename": "${default}",
        "limitSymbolsToIncludedHeaders": false
      },
      "includePath": [
        "${default}",
        "${workspaceFolder}/**",
        "/opt/ros/foxy/include/**"
      ],
      "name": "Linux",
      "intelliSenseMode": "linux-gcc-x64",
      "compilerPath": "/usr/bin/g++",
      "cStandard": "c99",
      "cppStandard": "c++20"
    }
  ],
  "version": 4
}

~/dev_ws/.vscode/tasks.json

VSCode에서는 외부 프로그램을 CLI를 사용해 연동하는 기능이 있는데 이를 Task라고 한다.

ROS 2를 빌드 할 때 사용되는 colcon build, test, clean 작업을 Task로 만들 수 있다.

이를 통해 Ctrl + Shift + B로 빌드 할 수 있다.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "colcon: build",
            "type": "shell",
            "command": "colcon build --cmake-args '-DCMAKE_BUILD_TYPE=Debug'",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "colcon: test",
            "type": "shell",
            "command": "colcon test && colcon test-result"
        },
        {
            "label": "colcon: clean",
            "type": "shell",
            "command": "rm -rf build install log"
        }
    ]
}

~/dev_ws/.vscode/launch.json

VSCode에서 Launch는 Run and Debug (Ctrl + Shift + D)에서 사용되는 실행 명령어로 언어 별, 디버거 별로 설정이 가능하고 세부 옵션으로 실행 이전에 수행되는 Task를 지정할 수 있다.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug-rclpy(debugpy)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Debug-rclcpp(gbd)",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/install/${input:package}/lib/${input:package}/${input:node}",
            "args": [],
            "preLaunchTask": "colcon: build",
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ],
    "inputs": [
        {
            "id": "package",
            "type": "promptString",
            "description": "package name",
            "default": "topic_service_action_rclcpp_example"
        },
        {
            "id": "node",
            "type": "promptString",
            "description": "node name",
            "default": "argument"
        }
    ]
}

이후 Ctrl + Shift + B로 빌드를 수행할 수 있다.

profile
학부 연구생(220627~)

0개의 댓글