패키지 프로그래밍

Hyuna·2024년 10월 18일
1

ROS2

목록 보기
5/15
post-thumbnail

📕 참고: ROS2로 시작하는 로봇 프로그래밍, topic_service_action_rclpy_example




목적별로 작성된 노드들을 서로 연동하여 하나의 계산기로 작동하는 패키지를 만들어보려고 한다.

arument

  • arithmetic_argument 토픽으로 현재 시간과 변수 a,b 발행

calculator

  • arithmetic_argument 토픽에서 발행된 시간과 변수 a,b를 구독
  • 서비스 클라이언트에게 연산자(+,-,*, /) 응답 요청
  • operator가 보낸 연산자로 변수 a,b 연산 후 다시 operator에게 수신
  • 연산결과를 arithmetic_checker 이름으로 액션 피드백을 checker로 전송

operator

  • arithmetic_operator 서비스를 통해 calculator에게 연산자(+,-,*, /)를 랜덤으로 보냄
  • calculator에게 받은 연산 결과를 터미널에 표시

checker

  • 연산값의 합이 액션 목표를 넘기면 최종 연산 결과를 터미널에 표시
  • 누적 한계치는 50으로 설정


인터페이스 패키지



인터페이스 파일 생성

먼저, 앞서 만들어뒀던 인터페이스 패키지ros_study_msgs에 이번 실습에 필요한 토픽, 서비스, 액션을 정의하는 파일을 작성해준다.

✔ ArithmeticArgument.msg

# Messages
builtin_interfaces/Time stamp # 시간 데이터 표현
float32 argument_a
float32 argumnet_b

✔ ArithmeticOperator.srv

# Constants
int8 PLUS = 1
int8 MINUS = 2
int8 MULTIPLY = 3
int8 DIVISION = 4

# Request
int8 arithmetic_operator
---

# Response
float32 arithmetic_result

✔ ArithmeticChecker.action

# Goal
float32 goal_sum
---

# Result
string[] all_formula #연산공식 저장
float32 total_sum
---

# Feedback
string[] formula

✔ CMakeLists.txt

set 명령어로 msg,srv,action 파일을 지정해준다.

set(msg_files
    "msg/ArithmeticArgument.msg"
)

set(srv_files
    "srv/ArithmeticOperator.srv"
)

set(action_files
    "action/ArithmeticChecker.action"
)

rosidl_generate_interfaces(${PROJECT_NAME}
    ${msg_files}
    ${srv_files}
    ${action_files}
    DEPENDENCIES builtin_interfaces
)



패키지 생성


ros2 pkg create ex_calculator --build-type ament_python --dependencies ros_study_msgs std_msgs rclpy
이렇게 하면 package.xml파일에 자동으로 의존성이 추가된다.


설정 파일 수정

✔ setup.py

console_scripts에 노드 실행 명령어를 추가해준다.


✔ param/arithmetic_config.yaml

노드에서 생성되는 랜덤 숫자의 범위를 지정한다.


/**: # namespace and node name
  ros__parameters:
    qos_depth: 30
    min_random_num: 0
    max_random_num: 9

qos_depth : 최대 저장 메시지 수
min_random_num : 노드에서 무작위로 숫자를 생성할 때 최소값
max_random_num : 노드에서 무작위로 숫자를 생성할 때 최대값



실행

argument-calculator

서브스크라이버 argument - 퍼블리셔 calculator 순서로 실행해준다.
퍼블리셔를 먼저 실행하면 메시지 처음부터 구독할 수 없기 때문이다.


operator

서비스 operator를 실행한다.
랜덤하게 선택된 연산자로 연산된 결과를 확인할 수 있다.


checker

액션 ckecker를 실행한다.
operator에서 어떤 연산자가 선택되어 계산된건지 확인할 수 있다.
누적한계치 50을 넘자 실행을 멈추었다. (Action result(total sum): 72.0)

0개의 댓글