ROS2 + Ubuntu20.04
11 min readNov 17, 2020
Ubuntu20.04에 ROS2 foxy 설치 (네이버 오로카 카페, 표윤석박사 글에서 발췌)
sudo add-apt-repository ppa:linuxuprising/shutter
sudo apt update
sudo apt upgradesudo apt install -y \
build-essential \
cmake \
curl \
gimp \
git \
gnupg2 \
gparted \
htop \
iftop \
iperf \
kdenlive \
kolourpaint \
lsb-release \
mc \
ntpdate \
okular \
qtcreator \
shutter \
simplescreenrecorder \
terminator \
tig \
tmux \
vim \
vlc \
wget \
xclip
sudo apt update
sudo apt upgradesudo locale-gen en_US en_US.UTF-8sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8export LANG=en_US.UTF-8sudo apt update && sudo apt install curl gnupg2 lsb-releasecurl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://packages.ros.org/ros2/ubuntu focal main" > /etc/apt/sources.list.d/ros2-latest.list'sudo apt updatesudo apt install ros-foxy-desktop ros-foxy-rmw-cyclonedds-cppsource /opt/ros/foxy/setup.bashsudo apt install python3-argcompletesudo apt install python3-colcon-common-extensionsprintenv |grep -i ROS------------.bashrc 파일 추가 내용---------------------------------alias vr='vim ~/.bashrc'
alias nr='nano ~/.bashrc'
alias sr='source ~/.bashrc'
alias killgazebo='killall -9 gazebo & killall -9 gzserver & killall -9 gzclient'alias cw='cd ~/robot_ws'
alias cs='cd ~/robot_ws/src'alias cb='cd ~/robot_ws && colcon build --symlink-install'
alias cbp='cd ~/robot_ws && colcon build --symlink-install --packages-select'alias rt='ros2 topic list'
alias re='ros2 topic echo'
alias rn='ros2 node list'
alias af='ament_flake8'
alias ac='ament_cpplint'alias testpub='ros2 run demo_nodes_cpp talker'
alias testsub='ros2 run demo_nodes_cpp listener'
alias testpubimg='ros2 run image_tools cam2image'
alias testsubimg='ros2 run image_tools showimage'alias ros2_init='. /opt/ros/foxy/setup.bash'
alias all_init='. ~/robot_ws/install/local_setup.bash'export ROS_DOMAIN_ID=10export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
# export RMW_IMPLEMENTATION=rmw_fastrtps_cpp# export RCUTILS_CONSOLE_OUTPUT_FORMAT='[{severity} {time}] [{name}]: {message} ({function_name}() at {file_name}:{line_number})'
export RCUTILS_CONSOLE_OUTPUT_FORMAT='[{severity}] [{time}]: {message}'
export RCUTILS_COLORIZED_OUTPUT=1
# export RCUTILS_LOGGING_USE_STDOUT=1
export RCUTILS_LOGGING_BUFFERED_STREAM=1source /opt/ros/foxy/setup.bash
source ~/robot_ws/install/local_setup.bash
RO2 CLI (command line interface)
$ ros2 -h 또는 ros2 --help
사용할 수 있는 여러 command를 보여 준다. $ ros2 pkg -h
위에서 보여준 여러 commands중에 pkg에 대한 help 정보 보여 줌$ ros2 pkg list -h
사용 가능한 package list 를 보여 줌. $ ros2 pkg executables -h
지정한 패키지의 실행 가능한 파일 리스트$ ros2 pkg executables turtlesim
* ROS2 installation workspace (binary package install)
$ sudo apt install ros-foxy-
$ source /opt/ros/foxy/setup.bash* ROS2 local workspace (making your own packages or by downloading form internet)
$ source install/setup.bash* 내 package 생성 하기
$ mkdir robot_ws/src
$ cd robot_ws/src# Cmake package 생성
$ ros2 pkg create --build-type ament_cmake --node-name my_node my_package_cpp# python package 생성은
$ ros2 pkg create --build-type ament_python --node-name my_node my_package_python# 생성한 package build 하기
$ cd ~/robot_ws
$ colcon build
$ source install/setup.bash# build 된 package 실행하기
$ ros2 run my_package_cpp my_node
$ ros2 run my_package_python my_node
turtlesim github의 CMakeLists.txt파일을 보면 tutlesim package내에서 실행 가능한 파일 이름이 정의 된 것을 볼 수 있다. (https://github.com/ros/ros_tutorials/blob/foxy-devel/turtlesim/CMakeLists.txt )add_executable(turtlesim_node ${turtlesim_node_SRCS}
...
add_executable(turtle_teleop_key tutorials/teleop_turtle_key.cpp)
...
add_executable(draw_square tutorials/draw_square.cpp)
...
add_executable(mimic tutorials/mimic.cpp)
...위에서 보이는 정보는 terminal에서 다음 명령으로도 확인 가능하다. $ ros2 pkg executables turtlesim
https://github.com/ros/ros_tutorials/blob/foxy-devel/turtlesim/tutorials/teleop_turtle_key.cpp 를 보면
....int main(int argc, char** argv){
rclcpp::init(argc, argv);
TeleopTurtle teleop_turtle; // Class TeleopTurtle의 객체 teleop_turtle을 생성한다. 생성자(constructor)가 뭘하는지 살펴 보면.....class TeleopTurtle{
public:
TeleopTurtle(); // 생성자
int keyLoop();
....TeleopTurtle::TeleopTurtle(): // 생성자 하는 일
linear_(0),
angular_(0),
l_scale_(2.0),
a_scale_(2.0)
{
nh_ = rclcpp::Node::make_shared("teleop_turtle"); //teleop_turtle
// 이름의 node를 생성한다.
....
}..... teleop_key node를 실행 시켜 보자$ ros2 run turtlesim turtle_teleop_key$ ros2 node -h
$ ros2 node list
/teleop_turtle node가 실행 되고 있음을 볼 수 있다. 위 코드에서 생성한 node name과 같다. $ ros2 node info -h
$ ros2 node info /teleop_turtle
이 노드는 Subscriber, Publisher, Service server, Action client를 포함하고 있음을 볼 수 있다. $ rqt
plugins -> introspection -> node graph
$ ros2 node info /turtlesim 으로 정보 확인하면
/turtle1/cmd_vel 토픽을 {geometry_msgs/msg/Twist(메시지타입)} subscribe 하는 것을 알 수 있다. $ ros2 interface proto geometry_msgs/msg/Twist 로 확인$ ros2 topic -h
여러 명령 중에 메시지 publish를 위해 help를 한번 더 살펴 보자$ ros2 topic pub -h
$ ros2 topic pub -r 1 /turtle1/cmd_vel geometry_msgs/msg/Twist "linear:
> x:2.0
> y:0.0
> z:0.0
> angular:
> x:0.0
> y:0.0
> z:2.0
// 한번만 publish 하려면 -1 옵션,주기적 publish는 -r 10 이런 식으로 $ ros2 topic list -t // topic name과 message type을 같이 볼 수 있다.$ ros2 topic info /turtle1/cmd_vel
$ ros2 topic echo /turtle1/cmd_vel
$ ros2 topic echo /turtle1/pose$ ros2 topic hz /turtle1/pose
$ rqt
topic monitor 참고
Message Publisher 로 메시지 publish 가능
패키지 생성
$ ros2 pkg create --build-type ament_cmake <package_name># build package
$ colcon build
# 만약 command not found 에러가 나온다면
$ sudo apt install python3-colcon-common-extensions# source bash file
$ source install/setup.bash# run executable
$ ros2 run <package_name> <executable_name>