해당 실습 자료는 한양대학교 Road Balance - ROS 2 for G CampROS 2 Documentation: Foxy, 표윤석, 임태훈 <ROS 2로 시작하는 로봇 프로그래밍> 루피페이퍼(2022) 를 참고하여 작성하였습니다.


앞선 장에서 Service에 대해 배워보았습니다. 간단하게 정리하자면 node가 Service를 사용하여 통신할 때 데이터 요청(request)을 보내는 node를 Client Node라고 하고 요청에 응답(response)하는 Server Node라고 합니다. 요청과 응답의 구조는 .srv 파일에 의해 결정됩니다.

Service 프로그래밍


패키지 생성

$ ros2 pkg create py_srvcli --build-type ament_python  --dependencies rclpy example_interfaces

인터페이스

Untitled

Server Node 작성


from example_interfaces.srv import AddTwoInts

import rclpy
from rclpy.node import Node

class MinimalService(Node):

    def __init__(self):
        super().__init__('minimal_service')

        ### ============= 서버 설정 =============
        self.srv = self.create_service(
            AddTwoInts, ## srv 타입: 해당 클래스의 인터페이스로 서비스 요청에 해당되는 request
            'add_two_ints', ## 서비스 서버 명
            self.add_two_ints_callback) ## 콜백 함수
        ### ===========================================

    def add_two_ints_callback(self, request, response): ## Client Node로부터 클래스로 생성된 인터페이스로 서비스 요청에 해당되는 request 부분과 응답에 해당되는 response으로 구분
        response.sum = request.a + request.b ## 위의 AddTwoInts 인터페이스 정보 확인
        self.get_logger().info('Incoming request\\na: %d b: %d' % (request.a, request.b)) # cmd 창 출력

        return response ## 응답값 반환

def main(args=None):
    rclpy.init(args=args) # 초기화
    node = MinimalService() # MinimalService를 node라는 이름으로 생성
    try:
        rclpy.spin(node) # 생성한 노드를 spin하여 지정된 콜백 함수 실행 
    except KeyboardInterrupt:
        node.get_logger().info('Keyboard Interrupt (SIGINT)')
    finally:# 종료시 (ex `Ctrl + c`)
        node.destroy_node()  # 노드 소멸
        rclpy.shutdown() # rclpy.shutdown 함수로 노드 종료

if __name__ == '__main__':
    main()