본문 바로가기

FOSCAR-(Autonomous Driving)/ROS 스터디

[2025 ROS 스터디] 한상민 #3주차 - ROS 기본 프로그래밍

반응형

1.ROS 프로그래밍 전에 알아둬야 할 사항

  • 표준 단위: SI 단위 사용 (meter, second, radian 등)
  • 좌표 표현: x(앞), y(왼쪽), z(위), 오른손 법칙 적용
  • 명명 규칙:
    • 패키지/토픽/변수: under_scored
    • 클래스/타입/열거형: CamelCased
    • 상수/매크로: ALL_CAPITALS

2. Topic 통신 (Publisher & Subscriber)

퍼블리셔 노드에서 메시지를 특정 토픽으로 주기적으로 발행하고, 서브스크라이버 노드에서는 해당 토픽의 메시지를 수신하여 처리한다.

ros::Publisher ros_tutorial_pub = nh.advertise<ros_tutorials_topic::MsgTutorial>("ros_tutorial_msg", 100); ros_tutorials_topic::MsgTutorial msg; msg.stamp = ros::Time::now(); msg.data = count; ros_tutorial_pub.publish(msg);

void msgCallback(const ros_tutorials_topic::MsgTutorial::ConstPtr& msg) { ROS_INFO("receive msg = %d", msg->data); } ros::Subscriber ros_tutorial_sub = nh.subscribe("ros_tutorial_msg", 100, msgCallback);

 

3. Service 통신 (Server & Client)

Service 서버는 요청을 받고 연산을 수행한 뒤 결과를 응답하고, 클라이언트는 값을 입력해 요청을 보낸다.

서비스 정의 파일 SrvTutorial.srv

int64 a

int64 b

int64 result

bool calculation(req, res) { res.result = req.a + req.b; return true; }

srv.request.a = 입력값1; srv.request.b = 입력값2; client.call(srv); ROS_INFO("result: %ld", srv.response.result);

 

4. 파라미터 활용

노드 실행 중에 파라미터를 읽거나 설정할 수 있으며, 이는 동적 설정이나 모드 변경 등에 유용하다.

nh.setParam("calculation_method", PLUS); nh.getParam("calculation_method", g_operator);

rosparam set /calculation_method 2 rosservice call /ros_tutorial_srv 10 5

5. roslaunch 사용법

여러 노드를 한 번에 실행하고 설정을 묶어서 관리할 수 있다.

roslaunch ros_tutorials_topic union.launch

 

반응형