def run(self): try: #start ROS rospy.init_node(self.name, anonymous=False) #what to do if shut down (e.g. ctrl + C or failure) rospy.on_shutdown(self.shutdown) # Publisher to manually control the robot (e.g. to stop it) self.cmd_vel_pub = rospy.Publisher('cmd_vel', Twist) #tell the action client that we want to spin a thread by default self.move_base = actionlib.SimpleActionClient( "move_base", MoveBaseAction) rospy.loginfo("wait for the action server to come up") #allow up to 5 seconds for the action server to come up self.move_base.wait_for_server(rospy.Duration(5)) rospy.loginfo("Connected to move base server") # A variable to hold the initial pose of the robot to be set by # the user in RViz initial_pose = PoseWithCovarianceStamped() # Get the initial pose from the user rospy.loginfo( "*** Click the 2D Pose Estimate button in RViz to set the robot's initial pose..." ) rospy.wait_for_message('initialpose', PoseWithCovarianceStamped) self.last_location = Pose() rospy.Subscriber('initialpose', PoseWithCovarianceStamped, self.update_initial_pose) # wiat for initial pose while initial_pose.header.stamp == "": rospy.sleep(1) rospy.loginfo("Starting navigation test") #end ROS # Create new receiver thread self.receiver = Receiver(self.name, threadLock) self.receiver.start() while not rospy.is_shutdown(): self.checkReceive() if self.auctionEndTime != 0 and self.auctionEndTime < current_sec_time( ): self.auctionEndTime = 0 winner = max(self.bidsDic, key=self.bidsDic.get) if winner == self.name: self.winTask(self.auctionMsg) else: self.winMsg = comm_msg_t() self.winMsg.receiver = winner self.sendMsg("winMsg") print self.bidsDic except Exception, e: print(self.name, " node terminated.", e)
def run(self): try: # Create new receiver thread self.receiver = Receiver(self.name, threadLock) self.receiver.start() while True: self.checkReceive() if self.auctionEndTime != 0 and self.auctionEndTime < current_sec_time(): self.auctionEndTime = 0 winner = max(self.bidsDic, key=self.bidsDic.get); if winner == self.name: self.winTask(self.auctionMsg) else: self.winMsg = comm_msg_t() self.winMsg.receiver = winner self.sendMsg("winMsg") print self.bidsDic except Exception, e: print(self.name, " node terminated.", e)
threadLock = threading.Lock() # Create new receiver thread receiver = Receiver("turtlebot1", threadLock) receiver.start() counter = 1 while True: time.sleep(1) print "main thread still alive" threadLock.acquire() if receiver.isReceived: receiver.isReceived = False if receiver.msg.receiver == robotName: print " I am the receiver " + receiver.msg.receiver threadLock.release() if counter > 0: msg = comm_msg_t() msg.sender = robotName msg.receiver = "supervisor" #turtlebot1" msg.position = (1, 2, 3) Sender(msg) counter -= 1 # wait for all child threads to die receiver.join() except: print("Receiver node terminated.")