$search
00001 #!/usr/bin/env python 00002 import roslib; roslib.load_manifest('interactive_marker_client') 00003 import rospy 00004 from threading import Lock 00005 from visualization_msgs.msg import * 00006 from interactive_marker_client.msg import * 00007 from interactive_marker_client.srv import * 00008 00009 class InteractiveMarkerClient: 00010 def __init__(self,topic): 00011 self.init = True 00012 self.stopFlag = True 00013 self.pub_postfix = '_web' 00014 self.pub_topic = topic + self.pub_postfix 00015 self.lock = Lock() 00016 00017 self.topic = topic 00018 self.topic_full = topic + '_full' 00019 00020 self.full_marker_list = [] 00021 self.full_marker_list_index = 0 00022 00023 def start(self): 00024 self.sub_update = rospy.Subscriber(self.topic,InteractiveMarkerUpdate,self.update) 00025 self.sub_update_full = rospy.Subscriber(self.topic_full,InteractiveMarkerInit, self.update_full) 00026 self.pub = rospy.Publisher(self.pub_topic,InteractiveMarkerUpdateWeb) 00027 00028 def stop(self): 00029 self.sub_update.unregister() 00030 self.sub_update_full.unregister() 00031 #self.pub.unregister() 00032 00033 def reinit(self): 00034 self.init = True 00035 00036 def update(self,msg): 00037 #print "Update = " + str(msg.seq_num) 00038 if self.init: 00039 m = self.getMatchedMarker(msg.seq_num,msg.server_id) 00040 if m == 0: # seq lower 00041 print "seq == lower " 00042 return 00043 elif m == 1: # seq higher 00044 print "seq == higher " 00045 rospy.sleep(0.1) 00046 return 00047 else: 00048 newMsg = self.copyMsg(msg) 00049 newMsg.type = 2 00050 newMsg.markers = m 00051 newMsg.poses = [] 00052 newMsg.erases = [] 00053 self.init = False 00054 else: 00055 newMsg = self.copyMsg(msg) 00056 00057 self.pub.publish(newMsg) 00058 00059 def update_full(self,msg): 00060 print "Seq = " + str(msg.seq_num) 00061 index = self.full_marker_list_index 00062 00063 self.full_marker_list.insert(index,[msg.seq_num,msg.server_id,msg.markers]) 00064 index = index + 1 00065 if index == 100: 00066 index = 0 00067 00068 00069 def copyMsg(self,msg): 00070 newMsg = InteractiveMarkerUpdateWeb() 00071 newMsg.server_id = msg.server_id 00072 newMsg.seq_num = msg.seq_num 00073 newMsg.type = msg.type 00074 newMsg.markers = msg.markers 00075 newMsg.poses = msg.poses 00076 newMsg.erases = msg.erases 00077 return newMsg 00078 00079 def getMatchedMarker(self,seq,serverid): 00080 val = 0 00081 for marker in self.full_marker_list: 00082 if marker[1] == serverid and marker[0] < seq: 00083 val = 1 00084 elif marker[1] == serverid and marker[0] > seq: 00085 val = 0 00086 elif marker[1] == serverid: 00087 val = marker[2] 00088 break 00089 return val 00090 00091 class NodeClientMaster: 00092 def __init__(self): 00093 self.requestName = 'interactive_marker_request'; 00094 self.initName = 'interactive_marker_init_request'; 00095 self.topicDict = {} 00096 00097 00098 def receiveStartRequest(self,req): 00099 print req 00100 topic = self.setTopic(req.topic) 00101 if topic in self.topicDict: 00102 if not req.on: 00103 self.topicDict[topic].stop() 00104 del self.topicDict[topic] 00105 rospy.loginfo(topic + ' is removed') 00106 return RequestInteractiveMarkerResponse(req.topic,True) 00107 return RequestInteractiveMarkerResponse(req.topic,False) 00108 else: 00109 if req.on: 00110 node = InteractiveMarkerClient(topic) 00111 node.start() 00112 self.topicDict[topic] = node 00113 rospy.loginfo(topic + ' is started') 00114 return RequestInteractiveMarkerResponse(req.topic,True) 00115 return RequestInteractiveMarkerResponse(req.topic, False) 00116 00117 def receiveInitRequest(self,req): 00118 topic = self.setTopic(req.topic) 00119 if topic in self.topicDict: 00120 self.topicDict[topic].reinit() 00121 rospy.loginfo(topic + ' : reinitialization requested') 00122 else: 00123 rospy.loginfo(topic + ' : does not exist') 00124 00125 return RequestInitResponse() 00126 00127 def setTopic(self,topic): 00128 start = 0 00129 end = len(topic) 00130 if topic[0] == '/': 00131 start = 1 00132 return topic[start:end-4] 00133 00134 00135 00136 def spin(self): 00137 self.requestService = rospy.Service(self.requestName,RequestInteractiveMarker,self.receiveStartRequest) 00138 self.initService = rospy.Service(self.initName,RequestInit,self.receiveInitRequest) 00139 00140 rospy.loginfo('Interactive Marker client Initialized') 00141 00142 rospy.spin() 00143 rospy.loginfo('byebye') 00144 00145 if __name__ == "__main__": 00146 try: 00147 rospy.init_node('interactive_marker_client') 00148 node = NodeClientMaster() 00149 node.spin() 00150 except rospy.ROSInterruptException: pass