구상: Reciver.py에서 문자열 리스트 (원소 4개 , "First", "Second",."Third","Fourth") 와 인덱스역할을 하면서 flag역할을 하는 idx를 활용해 pub(First, Second, Third, Fourth)의 토픽을 순차적으로 sub(Reciver)이 받게끔 했다.
sr_order.launch
<launch>
<node pkg="msg_send" type="First.py" name="m_First" />
<node pkg="msg_send" type="Second.py" name="m_Second" />
<node pkg="msg_send" type="Third.py" name="m_Third" />
<node pkg="msg_send" type="Fourth.py" name="m_Fourth" />
<node pkg="msg_send" type="Receiver.py" name="m_Receiver" output="screen"/>
</launch>
First.py
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
rospy.init_node("first")
pub = rospy.Publisher("First",String,queue_size=10)
rate = rospy.Rate(2)
while not rospy.is_shutdown():
pub.publish("First")
rate.sleep()
Second.py
!/usr/bin/env python
import rospy
from std_msgs.msg import String
rospy.init_node("second")
pub = rospy.Publisher("Second",String,queue_size=10)
rate = rospy.Rate(2)
while not rospy.is_shutdown():
pub.publish("Second")
rate.sleep()
Third.py
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
rospy.init_node("third")
pub = rospy.Publisher("Third",String,queue_size=10)
rate = rospy.Rate(2)
while not rospy.is_shutdown():
pub.publish("Third")
rate.sleep()
Fourth.py
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
rospy.init_node("fourth")
pub = rospy.Publisher("Fourth",String,queue_size=10)
rate = rospy.Rate(2)
while not rospy.is_shutdown():
pub.publish("Fourth")
rate.sleep()
Reciever.py
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
rospy.init_node("receiver")
idx =0 # this role is index and flag
compare_msg = ["First","Second","Third","Fourth"]
def isSame(tar, my_msg):
if tar==my_msg:
return True
else: return False
def callback(msg):
global idx
my_msg= compare_msg[idx]
if isSame(msg.data, my_msg):
print msg.data
idx+=1
if idx == 4: idx = 0
while 1:
sub = rospy.Subscriber(compare_msg[idx], String,callback)
print idx
rospy.spin()
ros 형식들이 기억이 안나거나 (pub 생성 시 매개변수 순서들) 파이썬 2와 파이썬3이 문법에서 존재유무 차이들, 그리고 리눅스 vim 사용 시 vs code처럼 빌드 하기 전 (실행전) 빨간 줄로 오타를 안잡아주어서 (gedit, vim) 디버그를 많이 하게 되고( 대체로 print로 디버깅함), 블로그에서 기록한 ros 형식들을 계속 찾아보게 된다.
아무래도 문법들이 손에 익을 정도로 많은 연습이 필요로 할듯하다.
'프로그래머스 > ROS' 카테고리의 다른 글
| 카메라 패키지 관련 예제 코드 (with xycar usb cam) (0) | 2023.01.18 |
|---|---|
| 파이썬 2.0 에서 한글 사용 (0) | 2022.11.11 |
| 2-3 (3) 누락 풀이 (0) | 2022.11.10 |
| 2-3 (2) 전송속도 _ 풀이 (0) | 2022.11.10 |
| ros노드 통신프로그래밍은 이걸로 대체하기 (나만의 메시지 자료구조 ) // 핵심// 이걸로 기록지 만들기 (0) | 2022.11.09 |