2016-11-16 3 views
0

私は、複数のエージェントがそれぞれの位置とそれぞれのIDを発行するトピックに登録されているROSノードを持っています。私はカメのようなエージェントを描き、それに応じてポジションを更新できるようにしたい。ROSコールバック関数からの入力に基づいてカメの配列を更新する方法

私は、次のしている:

class Visualizer(): 

def __init__(self): 
    self.turtles = list() 
    #Of the form: [[id, pos], ...] 
    #[[1, [2, 3]],[2, [6, 7]],[3, [1, 1]],[4, [9, 8]], ...] 
    self.logistics = [] 
    self.colors = [] 

    rospy.init_node('visualizer', anonymous=True) 
    rospy.Subscriber('/environment/agent_position', Protocol_Msg, self.draw_agents) 

    turtle.Screen() 

def is_it_new(self, data): 

    ----> check if broadcast received by new agent 

    if new: 
     self.turtles.append(turtle.Turtle()) 
     self.colors.append([random.random(), random.random(), random.random()]) 
     print self.colors 
     self.logistics.append([turtle_id, pos]) 

    else: 
     #only update position 
     for x in self.logistics: 
      if x[0] == turtle_id: 
       x[1] = pos 
    print self.logistics 

def draw_agents(self, data): 
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.content) 
    self.is_it_new(data) 
    for x in self.logistics: 
     if x[0] == int(data.sender): 
      turtles[self.logistics.index(x)].setx(x[1][0]) 
      turtles[self.logistics.index(x)].sety(x[1][1]) 


if __name__=='__main__': 
    try: 
     visualize = Visualizer() 
     rospy.spin() 
    except rospy.ROSInterruptException: 
    pass 

しかし、これは私を除くと

RuntimeError: main thread is not in main loop

を生産するが、私は更新が中に発生するようにコードを書き換えする方法がわからないですしないようメインスレッド。 (私は前にそのようなGUIで作業していない)。

+0

コードにもう少しコンテキストを追加してください。[最小限の完全で検証可能な例にしてください](http://stackoverflow.com/help/mcve)。今のところ、例えば、変数「カメ」がどこから来るのかはわかりません。また、エラーが発生した場所を正確に追加してください。 – luator

答えて

0

努力をいただきありがとうございます。私は次のようにして解決しました。

def __init__(self): 
     ..... 
     self.turtle_queue = Queue.Queue() 

    def put2queue(self, data): 
     self.turtle_queue.put(data) 

その後、私はそうのようにメインの描画関数を呼び出します:

if __name__=='__main__': 

try: 
    visualize = Visualizer() 
    while True: 
     data = visualize.turtle_queue.get() 
     visualize.draw_agents(data) 
except rospy.ROSInterruptException: 
    traceback.print_exc() 
    raise 

だからGUIが更新されないので、は次のようにコールバック関数で は、私が)キュー(新しいデータを入れますコールバックスレッドからではなく、メインスレッドから取得します。 :)

関連する問題