홈>
두 가지 기능을 가진 코드가 있습니다. 'send_thread'의 콜백 인 함수 'send_thread'및 함수 'receive_thread'. 내가하고 싶은 것은 'send_thread'를 실행하는 것입니다. 이는 'receive_thread'를 활성화하고 끝났 으면 다시 반복하십시오. 그렇게하기 위해 아래 코드를 생각해 냈습니다. 'send_thread'가 다시 호출되지만 더 이상 콜백을 활성화하지 않기 때문에 원하는 결과를 제공하지 않습니다. 도움을 주셔서 감사합니다.
함수는 receive_thread의 끝에서 호출되어 send_thread (rospy.sleep ())에서 대기하는 시간 동안 실행된다는 것을 알았습니다. 그래도 처음 시도한 후에는 콜백을 다시 활성화하지 않습니다.
import rospy
import pepper_2d_simulator
import threading
class TROS(object):
def __init__(self):
self.cmd_vel_pub = rospy.Publisher('cmd_vel',Twist)
self.event = threading.Event()
def send_thread(self):
#send commmand
self.event.set()
sequence = [[1,0,0.05],[0,0,0],[0,0,0.1292]]
for cmd in sequence:
rospy.Rate(0.5).sleep()
msg = Twist()
msg.linear.x = cmd[0]
msg.linear.y = cmd[1]
msg.angular.z = cmd[2]
t = rospy.get_rostime()
self.cmd_vel_pub.publish(msg)
self.event.clear()
rospy.sleep(1)
def receive_thread(self,msg):
#if something is being send, listen to this
if self.event.isSet():
frame_id = msg.header.frame_id
self.x_odom = msg.pose.pose.position.x
self.y_odom = msg.pose.pose.position.y
self.z_odom = msg.pose.pose.position.z
self.pos_odom = [self.x_odom,self.y_odom,self.z_odom,1]
self.ang_odom = msg.pose.pose.orientation.z
self.time = msg.header.stamp.secs + msg.header.stamp.nsecs
#some transformations here to get self.trans...
else:
#after self.event() is cleared, rename and run again
self.x_odom = self.trans_br_x
self.y_odom = self.trans_br_y
self.ang_odom = self.rot_br_ang
self.send_thread()
def init_node(self):
rospy.init_node('pepper_cmd_evaluator',anonymous = True)
rospy.Subscriber('odom',Odometry,self.receive_thread)
if __name__ == '__main__':
thinking = Thinking()
thinking.init_node()
thinking.send_thread()
The expected result is that I am able to loop this two function so that I call send_thread, this activates receive thread. Then send_thread stops, receive_thread stops and activates the send_thread again. I want to do this 10 times.
- 답변 # 1
관련 자료
- python - 다른 함수 내에서 호출되는 모의 클래스 메서드
- javascript - Puppeeter를 사용하여 클래스 내에서 div 선택
- 네임 스페이스 Unity C # 경로 찾기 내 클래스에 액세스
- javascript - 다른 클래스의 클래스에서 메서드를 호출하는 방법은 무엇입니까? 속성 '테스트'JS를 읽을 수 없습니다
- javascript - for 루프 내의 모든 요소에 클래스 추가
- oop - 파이썬에서 부모 클래스의 각 자식 클래스의 메서드에 데코레이터를 할당하는 방법
- parallel processing - 파이썬에서 클래스 메서드를 병렬화하는 방법은 무엇입니까?
- python - unboundlocalerror - 클래스 메서드를 호출 할 때
- 다른 클래스 Java의 객체로 매개 변수를 사용하여 메서드를 호출하는 방법은 무엇입니까?
- 클래스 (루비)의 메서드에 이름을 표시하는 방법은 무엇입니까?
- c++ - 템플릿에 전달 된 유형이 int 인 경우에만 클래스 템플릿에 정렬 메서드가 있어야합니다
- 객체를 생성하지 않고 자바 액세스 공용 클래스 메소드
- scala - 클래스가 선언 될 때 자동으로 메서드를 호출하는 방법은 무엇입니까?
- python - 다른 클래스 메서드의 '헤더'에있는 클래스 메서드를 기본 변수로 어떻게 참조 할 수 있습니까?
- 클래스 기반 뷰에 get_context_data 메서드를 추가하면 django-tables2가 중단됩니다
- 클래스 내에서 개인 ArrayList를 어떻게 만듭니 까? (자바)
- php - phpunit - 다른 정적 클래스 메서드를 사용하는 클래스 메서드 테스트 방법
- java - javafx 프로젝트 내에서 음악을 재생하는 방법으로 인해 음악이 재생되지 않음
- java 상속 - 서브 클래스 인스턴스가 생성 될 때 수퍼 클래스 메소드를 호출하는 방법은 무엇입니까?
- java - 메서드 호출 후 클래스 변수가 업데이트되지 않음
트렌드
- OpenCv의 폴더에서 여러 이미지 읽기 (python)
- 파이썬 셀레늄 모든 "href"속성 가져 오기
- html - 자바 스크립트 - 클릭 후 변경 버튼 텍스트 변경
- git commit - 자식 - 로컬 커밋 된 파일에 대한 변경을 취소하는 방법
- JSP에 대한 클래스를 컴파일 할 수 없습니다
- javascript - 현재 URL에서 특정 div 만 새로 고침/새로 고침
- jquery - JavaScript로 현재 세션 값을 얻으시겠습니까?
- javascript - swiperjs에서 정지, 재생 버튼 추가
- JavaScript 변수를 HTML div에 '출력'하는 방법
- python - 문자열에서 특정 문자 제거
이제 방법을 알아 냈습니다. 다른 사람이 비슷한 문제를 겪을 경우를 대비하여 솔루션을 게시 할 것입니다. 내가 생각해 낸 솔루션은 매우 간단합니다. self.flag 변수를 만들고 대안으로 send_thread 및 콜백에서 각각 True 및 False로 설정했습니다. 코드 :