ROS2 에서
self._sub_node_obj = rclpy.create_node(self.sub_node_info.node_name)
으로 노드 생성하고
def _node_obj_create_subscription(self) -> None:
"""subscribe 노드 생성시 사용 되는 함수"""
print("create_subscription on ", flush=True)
self._sub_node_obj.Subscription = self._sub_node_obj.create_subscription(
self.sub_node_info.msg_type,
self.sub_node_info.topic_name,
self.sub_node_info.callback,
self.sub_node_info.qos_profile
)
self._execute_list.append(self._sub_node_obj)
def _node_obj_create_publisher(self) -> None:
"""publisher 노드 생성시 사용 되는 함수"""
print("create_publisher on ", flush=True)
self._pub_node_obj.publisher = self._pub_node_obj.create_publisher(
self.pub_node_info.msg_type,
self.pub_node_info.topic_name,
self.pub_node_info.qos_profile
)
# self._execute_list.append(self._pub_node_obj)
로 노드를 정의하는 방법이 있다.
self._pub_node_obj : rclpy.node.Node() 인데
문제의 그 부분 -> "self._pub_node_obj.publisher = self._pub_node_obj.create_publisher( ~~~ )"
".create_publisher( )" 함수는 Node 클래스 안에 있다.
근데 ".publisher"이 뭔지를 알아보게 됨
간략하게 말하면 _pub_node_obj에 publisher 라는 네임스페이스? 같은 변수를 만들고
publisher 에 ".create_publisher( )" 의 리턴값 "publisher : Publisher 클래스"를 받아서
"노드이름.publisher.Publisher클래스함수명" 이 구조로 사용하는 것.
그래서
노드이름.@@@ -> @@@ 은 Node 클래스 를 사용
노드이름.publisher.@@@ -> @@@ 은 Publisher 클래스 사용
이런식으로 쓰더라..
이해를 돕기 위해 간단한 예제를 만들어 봤음
예제 코드
class Publisher:
def __init__(self, topic):
self.topic = topic
def publish(self):
print('Publisher.publish() : ', self.topic)
class A:
def __init__(self):
self.a = 0
self.b = 0
# self.publisher = None
def printt(self):
print(self.a)
print(self.b)
def create_publisher(self, topic):
publisher = Publisher(
topic
)
# print('publisher : ', type(publisher))
# publisher : <class '__main__.Publisher'>
return publisher
#--- 이런식으로 class A 의 멤버변수 접근 도 가능하고
aa = A()
aa.printt()
aa.a = 1
aa.printt()
#--- 이런식으로 return으로 다른 클래스를 받아서 사용하는 것도 가능하다.
aa.publisher = aa.create_publisher('/topic')
print(type(aa.publisher))
aa.publisher.publish()
#--- publisher 대신 다른 네이밍을 써줘도 가능
aa.bb = aa.create_publisher('/bb')
print(type(aa.bb))
aa.bb.publish()
'ros2_python > Ros2 msg,srv,action,parm' 카테고리의 다른 글
Ros2 비동기적으로 spin( ) (0) | 2022.09.15 |
---|---|
spin_until_future_complete 예제 (0) | 2022.09.15 |
python MultiThread( publisher 2개, suscriber 2개 ) (0) | 2022.09.01 |
ROS2_msg_pub_sub_multithread_python (0) | 2022.07.21 |
ros2_msg 토픽 example(executor사용==멀티쓰레드) (0) | 2022.07.05 |
댓글