anhy0923
HY's Embedded LAB
anhy0923
전체 방문자
오늘
어제
  • 분류 전체보기 (67)
    • UDS 진단통신 (1)
    • FreeRTOS (2)
    • ROS2 (5)
    • [EH전공] CAN 통신 (1)
    • C언어 튜터링: C로 로봇을 파헤쳐보자! (4)
    • [졸업작품] 외벽 균열 검사 로봇 (8)
      • 2021-2 <종합설계기획> (7)
      • 2022-1 <종합설계1> (1)
      • 2022-2 <종합설계2> (0)
    • TCP 기반 제어시스템 (7)
      • Robot Arm Control (5)
      • RPi CCTV Server (2)
    • Embedded System (6)
      • Linux (3)
      • Linux - Ubuntu (2)
      • RPi (1)
    • Drone FW using STM32CubeIDE (25)
      • 0. Intro (2)
      • 1. Debug (3)
      • 2. Sensor Interface (4)
      • 3. GPS (4)
      • 4. Transmitter_Receiver (3)
      • 5. Drone Body Asb (1)
      • 6. ESC Protocol (4)
      • 7. EEPROM (2)
      • 8. GCS (1)
      • PID Control (1)
    • Elec Academy (3)
    • HAL & LL Driver (1)
    • OpenCV - Lane Detection (1)

블로그 메뉴

  • 홈
  • 방명록

티스토리

hELLO · Designed By 정상우.
anhy0923

HY's Embedded LAB

ROS2

ROS2 Node 프로그래밍

2022. 7. 17. 00:57

setup.py

파이썬 파일 ros2 run으로 실행하려면 setup.py 파일에 entry_points 부분에 파일명 추가

'실행 시 사용될 이름 = 패키지 이름.파일 이름:main'

    entry_points={
        'console_scripts': [
            'example_node_1 = basic_topic_pkg.node_example_1:main',
            'example_node_2 = basic_topic_pkg.node_example_2:main',
            'example_node_3 = basic_topic_pkg.node_example_3:main',
            'example_node_4 = basic_topic_pkg.node_example_4:main',
            'example_node_5 = basic_topic_pkg.node_example_5:main',
            'example_node_6 = basic_topic_pkg.node_example_6:main',
            'topic_pub_node = basic_topic_pkg.topic_example_1_publisher:main',
            'topic_sub_node = basic_topic_pkg.topic_example_2_subscriber:main',
            'mimic_node     = basic_topic_pkg.topic_example_4_mimic:main',
        ],
    },

 

Timer

node.create_timer(주기, 콜백 함수 이름)

count = 0

def timer_callback():
	global count
	count += 1
	print(f'==== Hello ROS 2 : {count}====')

 

Spin: Node의 상태를 지속적으로 살핌

rclpy.spin(node)

 

Node Composition: Node의 생성을 객체지향으로 구현

생성자내에서 super().__init__('노드 이름')

class NodeClass(Node):
    def __init__(self):
        super().__init__('node_name')
        self.create_timer(0.2, self.timer_callback)
        self.count = 1
        
    def timer_callback(self):
        self.get_logger().info(f'==== Hello ROS2 : {self.count} ====')
        self.count += 1

def main(args=None):
    rclpy.init(args=args)
    node = NodeClass()
    node.get_logger().info('\n==== Create Node ====')
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()
    
if __name__ == '__main__':
    """main function"""
    main()

 

Logger Level: 콘솔 출력 글씨 색

get_logger().<   >();

info: 흰색

warn: 노란색

error, fatal: 빨간색

저작자표시 비영리 (새창열림)

'ROS2' 카테고리의 다른 글

6일차 & 7일차  (0) 2022.07.19
ROS2 Service 프로그래밍  (0) 2022.07.18
ROS2 Topic 프로그래밍  (0) 2022.07.17
ROS2 Launch file  (0) 2022.07.17
    'ROS2' 카테고리의 다른 글
    • 6일차 & 7일차
    • ROS2 Service 프로그래밍
    • ROS2 Topic 프로그래밍
    • ROS2 Launch file
    anhy0923
    anhy0923

    티스토리툴바