본문 바로가기
통신 Study/ROS

[Robotics] ROS 다운로드 및 환경 설정(+python 예제 코드)

by 하람 Haram 2023. 5. 18.
728x90

매번 찾기도 귀찮고 그냥 한번에 끝내고 싶어서 이렇게 정리해본다

 

설명 없이 명령어만 나열 하겠다

http://wiki.ros.org/noetic/Installation/Ubuntu

 

noetic/Installation/Ubuntu - ROS Wiki

If you rely on these packages, please support OSRF. These packages are built and hosted on infrastructure maintained and paid for by the Open Source Robotics Foundation, a 501(c)(3) non-profit organization. If OSRF were to receive one penny for each downlo

wiki.ros.org

 

 

ROS (noetic) 설치

// Sources.list 설정
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

// Curl 설치 및 설정
udo apt install curl
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

// APT 목록 갱신
sudo apt update

// Ros Neotic Dekstop 설치
sudo apt install ros-noetic-desktop-full

// Bash 설정 (시작 시 자동 로딩)
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc

//zsh일 경우
echo "source /opt/ros/noetic/setup.zsh" >> ~/.zshrc
source ~/.zshrc

// 의존성 패키지 설정
sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
sudo rosdep init
rosdep update

를 하고

cd opt/ros/noetic
ls

다음과 같이 나오면 성공

 

 

ROS 환경설정 (Catkin_ws)

 

이제 workspace를 만들어 보자

#디렉토리 만들기
mkdir -p catkin_ws/src
cd catkin_ws/src

#패키지 만들기
catkin_create_pkg my_pkg rospy std_msgs roscpp
cd my_pkg

ls
#CMakeLists.txt  package.xml  src < rospy만 쓰면 include 없다
cd src

#소스코드 복붙하기
nano example.py

#실행권한 부여
chmod +x example.py

#workspace 디렉토리로 이동
cd ../../../

#컴파일
catkin_make

sudo nano ~/.bashrc

#add in ~/.bashrc
source ~/catkin_ws/devel/setup.bash

source ~/.bashrc

 

example code

#!/usr/bin/env python3
import rospy
from std_msgs.msg import Float32
from random import randint
#randint(start , stop ) start 이상  stop 이하 난 수 발생
 
def height_publisher():
    pub = rospy.Publisher('height', Float32, queue_size=10)
    rospy.init_node('height_publisher', anonymous=True)
    rate = rospy.Rate(100) # 100hz
    while not rospy.is_shutdown(): #-> c++에서 ros.ok() 느낌
        rand_height = randint(70,200) #70cm ~ 200cm 사이의 사람의 키가 인식되었다고 가정

        hello_str = str(rand_height)+"%s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(rand_height)
        rate.sleep() #100hz가 될때 까지 쉬기

if __name__ == '__main__':
    try:
        height_publisher()
    except rospy.ROSInterruptException:
        pass

 

#ctrl +Alt + T
roscore

#ctrl +Alt + T
cd ~/catkin_ws

#recommend to use tab(If it can't automatically complete -> something is wrong in environment)
rosrun my_pkg example.py

 

728x90