https://cafe.naver.com/openrt -> Oroka ROS 토크를 따라가면서 배웠습니다.
https://blog.naver.com/staystays
https://soohwan-justin. -> 이것은 작가의 블로그입니다.
다룰 내용 (1)
- ROS 테마가 무엇이고 어떻게 사용하는지 알아보세요.
- 게시자란 무엇이며 게시자를 만드는 방법 알아보기
- 주제 메시지가 무엇이고 어떻게 작동하는지 알기
시뮬레이션 모델은 Turtlebot3 Waffle 모델을 사용하며 환경 변수를 추가해야 합니다.
gedit ~/.bashrc
맨 아래로 들어가
export TURTLEBOT3_MODEL=waffle
쓰기 및 저장
이 튜토리얼에서 사용하기 위해 my_examples_pkg라는 이름의 Rospackage 생성을 진행합니다.
cd ~/catkin_ws/src #cs로 저장해놨음
catkin_create_pkg my_examples_pkg rospy std_msgs
cd ~/catkin_ws&&catkin_make #cm으로 저장해놨음
rospack profile
roscd my_examples_pkg
mkdir scripts
스크립트 디렉토리에 샘플 Python 스크립트를 저장합니다.
cd scripts
gedit simple_topic_publisher.py
simple_topic_publisher.py라는 파일을 만들고 다음과 같이 편집합니다.
#!
/usr/bin/env python3
import rospy
from std_msgs.msg import Int32
rospy.init_node('topic_publisher')
pub = rospy.Publisher('/counter', Int32, queue_size=1)
rate = rospy.Rate(2)
count = Int32()
count.data = 0
while not rospy.is_shutdown():
pub.publish(count)
count.data += 1
rate.sleep()
편집 후 실행 권한을 부여하고 실행해 봅니다.
rosrun 명령어는 패키지의 실행 파일을 실행하는 명령어이고, roslaunch 명령어는 런치 파일을 실행하는 명령어이다.
$ roscore
$ sudo chmod +x simple_topic_publisher.py
$ rosrun my_examples_pkg simple_topic_publisher.py
** 이때 강의에는 설명이 없었는데 chmod에 대해 아는게 없어서 저도 찾아봤습니다.
(항상 그냥 그런줄 알았는데 중간에 모르는거 있으면 이제 찾아봐야겠다.
.)
Linux에서 파일로 할 수 있는 세 가지 주요 작업이 있습니다.
1. 파일에 저장된 데이터 읽기(r = 읽기)
2. 파일에 데이터 쓰기. (w = 쓰기)
3. 파일 실행(x = 실행)
그러나 Linux 시스템의 모든 파일을 종류나 용도에 관계없이 읽기, 쓰기 및 실행 가능하게 만들면 시스템 운영에 중요한 역할을 하는 파일의 내용이 잘못된 명령이나 사소한 사용을 통해 변경될 수 있습니다.
사용자의 실수 Linux 시스템의 모든 파일을 삭제할 수 있습니다.
이를 방지하기 위해 Linux에서는 각 파일 및 디렉터리에 대해 읽기(r), 쓰기(w) 및 실행(x) 권한을 개별적으로 설정할 수 있도록 했습니다.
또한 이 세 가지 권한은 각각 “파일을 소유한 사용자”, “특정 그룹에 속한 사용자” 및 “다른 사용자”에 대해 설정할 수 있습니다.
chmod 명령은 이러한 파일의 권한을 변경할 수 있는 명령입니다.
변화(change)와 패션(fashion)의 첫 글자를 조합한 것이다.
여기서 모드는 위에서 소개한 세 가지 권한(읽기, 쓰기 및 실행)과 각 권한이 할당되는 대상(파일 소유자, 그룹 및 기타 사용자)을 포함하여 파일의 속성을 나타냅니다.
즉, chmod 명령으로 지정된 읽기, 쓰기 및 실행 권한과 소유자, 그룹 및 기타 사용자에 대한 설정 값을 총칭하여 “모드”라고 합니다.
따라서 chmod 명령으로 파일의 모드를 변경하는 것은 파일의 권한을 변경하는 것과 같은 의미입니다.
시작하면 아무 일도 일어나지 않지만 맞습니다.
위의 코드는 단순히 /counter라는 주제를 생성하고 1씩 증가하는 int 유형의 변수를 지속적으로 게시합니다.
노드는 주제를 사용하여 특정 정보를 다른 노드에 게시합니다.
이 주제의 목록을 확인하고 싶다면 rostopic list 명령으로 확인할 수 있고, rostopic info로 주제에 대한 정보를 볼 수 있으며, 이 주제의 데이터를 보고 싶다면 rostopic echo 명령으로 볼 수 있습니다.
.
$ rostopic list
$ rostopic info /counter
$ rostopic echo /counter
출력 데이터는 std_msgs/Int32 데이터 유형으로 게시되며 이 정보를 게시하는 노드는 topic_publisher입니다.
아직 이 주제를 모니터링하는 노드가 없으므로 구독자 노드가 없습니다.
이제 위의 코드에 대한 자세한 설명을 보자.
#!
/usr/bin/env python3
import rospy # Import the Python library for ROS
from std_msgs.msg import Int32 # Import the Int32 message from the std_msgs package
rospy.init_node('topic_publisher') # Initiate a Node named 'topic_publisher'
pub = rospy.Publisher('/counter', Int32, queue_size=1)
# Create a Publisher object, that will publish on the /counter topic
# messages of type Int32
rate = rospy.Rate(2) # Set a publish rate of 2 Hz
count = Int32() # Create a var of type Int32
count.data = 0 # Initialize 'count' variable
while not rospy.is_shutdown(): # Create a loop that will go until someone stops the program execution
pub.publish(count) # Publish the message within the 'count' variable
count.data += 1 # Increment 'count' variable
rate.sleep() # Make sure the publish rate maintains at 2 Hz
예 1.1 끝
기본적으로 예제 1.1의 코드는 노드를 초기화하고 계속 증가하는 정수를 /counter라는 주제에 게시하는 게시자를 만듭니다.
요약하다
“게시자는 주제에 대한 뉴스를 지속적으로 게시하는 노드”입니다.
그렇다면 주제란 무엇인가?
주제는 ROS 노드가 정보를 게시하거나 읽을 수 있는 통로 역할을 하는 채널입니다.
rostopic echo 옵션에는 가장 최근 데이터만 표시하는 옵션이 있습니다.
rostopic echo 명령어로 데이터를 조회하면 데이터가 계속해서 출력되지만, 데이터가 복잡하거나 양이 많은 경우에는 데이터 조회가 번거로울 수 있습니다.
언제 사용할 명령
rostopic echo <topic_name> -n1
메시지
주제는 메시지에 대한 정보를 처리합니다.
다양한 유형의 메시지가 있으며 이 예에서는 std_msgs 패키지의 Int32 메시지입니다.
원하는 유형의 메시지를 생성할 수 있지만 가능하면 기본 제공 메시지를 사용하는 것이 좋습니다.
기타 다양한 보고서 http://wiki.ros.org/std_msgs 참조
Command 창에서 특정 메시지에 대한 정보를 확인하고 싶다면 다음과 같은 명령어를 사용하여 확인할 수 있습니다.
rosmsg show <message>
rosmsg show std_msgs/Int32
위 명령어를 실행하면
즉, 위의 데이터 유형에는 Int32 데이터 유형이 하나만 있습니다.
이제 예를 들어보자,,, 그 전에,
터틀봇 시뮬레이션 패키지를 사용한다고 하는데, emanual.robotis.com/docs/en/platform/turtlebot3/simulation/ 에서 다운로드
$ cd ~/catkin_ws/src/
$ git clone -b kinetic-devel https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
$ cd ~/catkin_ws && catkin_make
잘 되는 줄 알았는데 빨간 오류가 떴다.
CMake Error at /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
Could not find a package configuration file provided by "turtlebot3_msgs"
with any of the following names:
turtlebot3_msgsConfig.cmake
turtlebot3_msgs-config.cmake
Add the installation prefix of "turtlebot3_msgs" to CMAKE_PREFIX_PATH or
set "turtlebot3_msgs_DIR" to a directory containing one of the above files.
If "turtlebot3_msgs" provides a separate development package or SDK, be
sure it has been installed.
Call Stack (most recent call first):
turtlebot3_simulations/turtlebot3_fake/CMakeLists.txt:10 (find_package)
-- Configuring incomplete, errors occurred!
See also "/home/ksh/catkin_ws/build/CMakeFiles/CMakeOutput.log".
See also "/home/ksh/catkin_ws/build/CMakeFiles/CMakeError.log".
make: *** (Makefile:4100: cmake_check_build_system) Error 1
Invoking "make cmake_check_build_system" failed
그래서 검색했을 때 turtlebot3_msgs 패키지를 찾을 수 없다고 표시되었고 이 패키지는 turtlebot3_simulations 패키지의 종속성 중 하나이므로 turtlenot3_msgs 패키지를 설치할 수 있다고 말했습니다.
sudo apt-get install ros-kinetic-turtlebot3-msgs
나는 이것을했고 cm까지 완벽하게 작동했지만 여전히 하나의 버그가있었습니다.
Git Clone을 만들 때 Kinetic이 아닌 제가 사용하는 Noetic으로 변경했어야 하는데 그러지 않았습니다.
오류가 나타 났습니까?
암튼 기존에 설치되어 있던거 삭제하고 노에틱으로 재설치 했습니다.
cd ~/catkin_ws/src
rm -rf turtlebot3_simulations
cd ~/catkin_ws/src
git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
cd ~/catkin_ws
catkin_make
그랬더니,,,, 하고 싶었는데 시작하려고 하니 에러가 떴다.
... logging to /home/ksh/.ros/log/c3b861de-c84f-11ed-8df1-9154be721348/roslaunch-ksh-System-Product-Name-16049.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
Resource not found: turtlebot3_description
ROS path (0)=/opt/ros/noetic/share/ros
ROS path (1)=/home/ksh/catkin_ws/src
ROS path (2)=/opt/ros/noetic/share
The traceback for the exception was written to the log file
다시 검색 후 오류는 turtlebot3_description 패키지를 찾을 수 없으며 turtlebot3_simulations 패키지의 종속성 중 하나라고 합니다.
그래서 설치했습니다.
sudo apt-get install ros-noetic-turtlebot3-description
이제 이 사이트의 모든 시작 파일이 제대로 실행되고 있으므로 예제로 돌아가 보겠습니다.
…그 전에는 부트 파일이 실행되지 않아서 패키지를 설치했습니다.
$ roslaunch turtlebot3_fake turtlebot3_fake.launch
... logging to /home/ksh/.ros/log/c3b861de-c84f-11ed-8df1-9154be721348/roslaunch-ksh-System-Product-Name-18107.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.
Resource not found: turtlebot3_bringup
ROS path (0)=/opt/ros/noetic/share/ros
ROS path (1)=/home/ksh/catkin_ws/src
ROS path (2)=/opt/ros/noetic/share
The traceback for the exception was written to the log file
sudo apt-get install ros-noetic-turtlebot3-bringup
패키지를 설치하고 실행한 후 정상적으로 실행되었습니다.
이제 예를 들어 보겠습니다.
예 1.2)
1. 위 예시에서 작성한 simple_topic_publisher.py를 수정합니다.
– 로봇은 시뮬레이션으로 제어되며, 이 로봇을 제어하는 테마의 이름은 /cmd_vel입니다.
다음 명령으로 패키지를 실행합니다.
$ roslaunch turtlebot3_fake turtlebot3_fake.launch
위 명령어를 입력하면 rviz가 실행되면서 로봇이 등장합니다.
2. rosmsg show 및 rostopic info 명령을 사용하여 topic/cmd_vel의 정보를 확인하고 그에 따라 python 코드를 변경합니다.
이 주제인 geometry_msgs/Twist의 경우 변수를 다음과 같이 선언해야 합니다.
var=Twist() .
3. 이 로봇은 차동 구동 로봇입니다.
따라서 로봇의 움직임은 x축의 병진속도와 z축의 각속도만으로 제어된다.
로봇의 속도 단위는 m/sec 및 rad/sec이므로 속도 값으로 0에서 1 사이의 값을 할당합니다.
4. simple_topic_publisher.py를 실행하는 시작 파일을 작성하고 실행합니다.
사실 정답도 신경쓰지 않고 쓰려고 했는데,,,, 그것조차 쉽지가 않아서 적당히 고민하면서 쓰게 되었습니다.
#!
/usr/bin/env python3
import rospy
from geometry_msgs.msg import Twist
rospy.init_node('cmd_publisher')
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
rate = rospy.Rate(2)
var = Twist()
var.linear.x = 0.5
var.angular.z = 0.3
while not rospy.is_shutdown():
pub.publish(var)
rate.sleep()
그리고 부팅 파일을 만들어야 하는데,,, 어떻게 하셨어요…? 저도 몰라서 다시 찾아봤습니다.
https://uniggg.manage/newpost/?type=post&returnURL=%2Fmanage%2Fposts%2F# 참조
roslaunch는 여러 노드를 동시에 실행할 수 있는 .launch 확장자를 가진 파일입니다.
패키지를 처음 빌드할 때는 존재하지 않으며 마음대로 시작 파일을 만들 수 있습니다.
먼저 패키지 디렉토리에 실행 디렉토리를 생성하고 그 아래에 filename.launch 파일을 생성합니다.
<launch>
<node pkg="my_examples_pkg" type="simple_topic_publisher.py" name="cmd_pub_node" output="screen"/>
</launch>
출시일부터 시작합니다
내
위와 같이 구성되어 있습니다.
실행하려는 각 노드에 대해 이 작업을 수행해야 합니다.
또한 시작 파일의 다양한 설정에 대해 더 자세히 조사했습니다.
https://enssionaut.com/board_robotics/974 참조
어쨌든 시작 파일을 실행해 보았습니다.
하지만 작동하지 않아서 뭔가를 시도하고 모든 것을 끄고 다시 시작했고 작동했습니다.
로봇이 가만히 서 있습니다.
시작 파일 실행
잘 움직여
더 많은 ROS 기본 사항(1): 테마, 게시 종료!
!