def test_multi_threaded_executor_executes(self): self.assertIsNotNone(self.node.handle) executor = MultiThreadedExecutor(context=self.context) try: self.assertTrue(self.func_execution(executor)) finally: executor.shutdown()
def main(): rclpy.init(args=None) node = rclpy.create_node('minimal_action_node') executor = MultiThreadedExecutor() exit_future = rclpy.Future() asw = ActionServerWrapperThread(node, exit_future) asw.start() time.sleep( 0.5 ) # without this instruction this error is thrown at the end of the program: # PyCapsule_GetPointer called with invalid PyCapsule object #3 acw = ActionClientWrapperThread(node, exit_future) acw.start() while not exit_future.done(): rclpy.spin_until_future_complete(node, exit_future, executor=executor, timeout_sec=1) acw.destroy() asw.destroy() executor.shutdown() node.destroy_node() rclpy.shutdown()
def main(args=None): rclpy.init(args=args) #Executor to control all topic nodes executor = MultiThreadedExecutor() nodeList = [] #std_msgs/String topics -- topics = ['rosout', 'actions', 'status'] rosout_node = RosoutListener() executor.add_node(rosout_node) logger.info(f'creating node for rosout messages') nodeList.append(rosout_node) for topic in topics: #Node creation for all topics subscriber = TopicNode(topic) executor.add_node(subscriber) nodeList.append(subscriber) print(f'creating node for string topic /{topic} messages') try: executor.spin() finally: executor.shutdown() for subscriber in nodeList: subscriber.destroy_node() rclpy.shutdown()
def main(args=None): time.sleep(1) # to measure the load on instantiation rclpy.init(args=args) try: nodes = {} for i in range(int(os.environ["ROS2_TEST_NUM_PER_GROUP"])): nodes[f'pub{i:03d}'] = NodePublish(cli_args=[ "--ros-args", "-r", f"__node:=node_publish_{i:03d}", ]) nodes[f'sub{i:03d}'] = NodeSubscribe(cli_args=[ "--ros-args", "-r", f"__node:=node_subscribe_{i:03d}", ]) executor = MultiThreadedExecutor() [executor.add_node(node) for node in nodes.values()] try: executor.spin() finally: executor.shutdown() [node.destroy_node() for node in nodes.values()] finally: rclpy.shutdown()
def main(args=None): rclpy.init(args=args) pipeline_manager = PipelineManager() executor = MultiThreadedExecutor() executor.add_node(pipeline_manager) executor.spin() pipeline_manager.destroy_node() executor.shutdown() rclpy.shutdown()
def main(args=None): global executor rclpy.init(args=args) try: executor = MultiThreadedExecutor() manager_node = TaskSchedulerManager() executor.add_node(manager_node) try: executor.spin() finally: executor.shutdown() finally: rclpy.shutdown()
def main(): from rclpy.executors import MultiThreadedExecutor rclpy.init() node = BlockingWaitsForTransform() # this node blocks in a callback, so a MultiThreadedExecutor is required executor = MultiThreadedExecutor() executor.add_node(node) try: executor.spin() except KeyboardInterrupt: pass executor.shutdown() rclpy.shutdown()
def main(args=None): global executor rclpy.init(args=args) try: executor = MultiThreadedExecutor() executor.add_node(SubtaskRoomLightOn()) executor.add_node(SubtaskRoomLightOff()) try: executor.spin() finally: executor.shutdown() finally: rclpy.shutdown()
def main(args=None): rclpy.init(args=args) executor = MultiThreadedExecutor(num_threads=8) local_navigation = LocalNavigation() executor.add_node(local_navigation) executor.spin() executor.shutdown() # release resources (don't forget this) oalQuit()
def main(args=None): # Pythonクライアントライブラリの初期化 rclpy.init(args=args) executor = MultiThreadedExecutor() rcv = Reciever() executor.add_node(rcv) try: executor.spin() except KeyboardInterrupt: pass executor.shutdown() rcv.destroy_node()
def main(args=None): rclpy.init(args=args) try: executor = MultiThreadedExecutor() node_a = FakeLifecycleNode('A') node_b = FakeLifecycleNode('B') executor.add_node(node_a) executor.add_node(node_b) lc = LifecycleClient() try: lc.configure_system() executor.spin_once(timeout_sec=1) executor.spin_once(timeout_sec=1) sleep(2) lc.activate_system() executor.spin_once(timeout_sec=1) executor.spin_once(timeout_sec=1) executor.spin_once(timeout_sec=1) sleep(2) # give the system some time to converge lc.change_A_mode('AA') executor.spin_once(timeout_sec=1) executor.spin_once(timeout_sec=1) executor.spin_once(timeout_sec=1) executor.spin_once(timeout_sec=1) executor.spin_once(timeout_sec=1) sleep(3) # give the system some time to converge lc.change_A_mode( 'AA' ) # this is the tested aspect: call redundant, should be ignored executor.spin_once(timeout_sec=1) executor.spin_once(timeout_sec=1) sleep(2) # give the system some time to converge lc.change_A_mode('BB') executor.spin() finally: executor.shutdown() node_a.destroy_node() node_b.destroy_node() finally: rclpy.shutdown() return 0
def main(args=None): rclpy.init(args=args) try: calculator = Calculator() executor = MultiThreadedExecutor(num_threads=4) executor.add_node(calculator) try: executor.spin() except KeyboardInterrupt: calculator.get_logger().info('Keyboard Interrupt (SIGINT)') finally: executor.shutdown() calculator.arithmetic_action_server.destroy() calculator.destroy_node() finally: rclpy.shutdown()
def main(args=None): """ Executes the pipeline manager node Uses a multi threaded executor for the pipeline manager and continuously spins until stopped, then destroys the pipeline manager and shuts down the infastructure """ rclpy.init(args=args) pipeline_manager = PipelineManager() executor = MultiThreadedExecutor() executor.add_node(pipeline_manager) try: executor.spin() except KeyboardInterrupt: pass # To force exit code 0 executor.shutdown() rclpy.shutdown()
def main(args=None): global executor rclpy.init(args=args) try: executor = MultiThreadedExecutor() task_scheduler_manager = TaskSchedulerManager() executor.add_node(task_scheduler_manager) try: executor.spin() finally: executor.shutdown() task_scheduler_manager.destroy_node() finally: rclpy.shutdown()
def test_cancel_goal_accept(self): def execute_callback(goal_handle): # Wait, to give the opportunity to cancel time.sleep(3.0) self.assertTrue(goal_handle.is_cancel_requested) goal_handle.canceled() return Fibonacci.Result() def cancel_callback(request): return CancelResponse.ACCEPT executor = MultiThreadedExecutor(context=self.context) action_server = ActionServer( self.node, Fibonacci, 'fibonacci', callback_group=ReentrantCallbackGroup(), execute_callback=execute_callback, handle_accepted_callback=lambda gh: None, cancel_callback=cancel_callback, ) goal_uuid = UUID(uuid=list(uuid.uuid4().bytes)) goal_order = 10 goal_msg = Fibonacci.Impl.SendGoalService.Request() goal_msg.goal_id = goal_uuid goal_msg.goal.order = goal_order goal_future = self.mock_action_client.send_goal(goal_msg) rclpy.spin_until_future_complete(self.node, goal_future, executor) goal_handle = goal_future.result() self.assertTrue(goal_handle.accepted) cancel_srv = CancelGoal.Request() cancel_srv.goal_info.goal_id = goal_uuid cancel_srv.goal_info.stamp.sec = 0 cancel_srv.goal_info.stamp.nanosec = 0 cancel_future = self.mock_action_client.cancel_goal(cancel_srv) rclpy.spin_until_future_complete(self.node, cancel_future, executor) cancel_result = cancel_future.result() self.assertEqual(len(cancel_result.goals_canceling), 1) assert all( cancel_result.goals_canceling[0].goal_id.uuid == goal_uuid.uuid) action_server.destroy() executor.shutdown()
def main(): rclpy.init() hello_sub = HelloSub() try: executor = MultiThreadedExecutor(num_threads=2) executor.add_node(hello_sub) executor.spin() print('Added thread!') # original_spin(hello_sub) # does not work! # 2 lines below are for testing out multithreaded workload in ROS2 [Failed attempt] # for spinning nodes in the background # spin_thread1 = Thread(target=rclpy.spin, args=(hello_sub, )) # spin_thread1.start() except KeyboardInterrupt: pass executor.shutdown() hello_sub.destroy_node() rclpy.shutdown()
def main(args=None): '''start interaction with shell''' #TODO init parameter # print(args) shell = Shell() Process(target=write_string, args=(shell, )).start() rclpy.init(args=args) shell_read = ReadShell(shell) shell_write = WriteShell(shell) executor = MultiThreadedExecutor() executor.add_node(shell_read) executor.add_node(shell_write) executor.spin() executor.shutdown() shell_read.destroy_node() shell_write.destroy_node()
def main(args=None): rclpy.init(args=args) try: # MultiThreadedExecutor executes callbacks with a thread pool. If num_threads is not # specified then num_threads will be multiprocessing.cpu_count() if it is implemented. # Otherwise it will use a single thread. This executor will allow callbacks to happen in # parallel, however the MutuallyExclusiveCallbackGroup in DoubleTalker will only allow its # callbacks to be executed one at a time. The callbacks in Listener are free to execute in # parallel to the ones in DoubleTalker however. executor = MultiThreadedExecutor(num_threads=4) executor.add_node(DoubleTalker()) executor.add_node(Listener()) try: executor.spin() finally: executor.shutdown() finally: rclpy.shutdown()
def main(args=None): rclpy.init(args=args) loop = asyncio.new_event_loop() try: minimal_service = MinimalService(loop) wait_node = WaitNode() executor = MultiThreadedExecutor() # executor = CustomThreadedExecutor(loop) executor.add_node(minimal_service) executor.add_node(wait_node) try: executor.spin() finally: executor.shutdown() minimal_service.destroy_node() wait_node.destroy_node() finally: rclpy.shutdown()
def main(args=None): rclpy.init(args=args) # Referenced from robotpilot/ros2-seminar-examples # https://github.com/robotpilot/ros2-seminar-examples/blob/main/topic_service_action_rclpy_example/topic_service_action_rclpy_example/calculator/main.py try: maze_action_server = MazeActionServer() executor = MultiThreadedExecutor() executor.add_node(maze_action_server) try: executor.spin() except KeyboardInterrupt: maze_action_server.get_logger().info('Keyboard Interrupt (SIGINT)') finally: executor.shutdown() maze_action_server._action_server.destroy() maze_action_server.destroy_node() finally: rclpy.shutdown()
def thread_ros(self): print("Starting thread of ros threads") # executor = MultiThreadedExecutor(num_threads=4) # executor.add_node(self.node) # executor.spin() # When we will be solid: try: executor = MultiThreadedExecutor(num_threads=4) executor.add_node(self.node) try: executor.spin() except KeyboardInterrupt: act_on_simulation(ascii(STOP)) print('server stopped cleanly') finally: act_on_simulation(ascii(STOP)) executor.shutdown() self.node.destroy_node() finally: print("nothing")
def main_multi(args=None): # Pythonクライアントライブラリの初期化 rclpy.init(args=args) # minimal_publisherノードの作成 minimal_publisher = MinimalPublisher() minimal_subscriber = MinimalSubscriber() executor = MultiThreadedExecutor() executor.add_node(minimal_publisher) executor.add_node(minimal_subscriber) try: executor.spin() except KeyboardInterrupt: pass executor.shutdown() minimal_publisher.destroy_node() minimal_subscriber.destroy_node() # Pythonクライアントライブラリの終了 rclpy.shutdown()
def main(args=None): rclpy.init(args=args) try: manager_node = OdriveMotorManager() controller_node = OdriveMotorController(manager_node) executor = MultiThreadedExecutor() executor.add_node(manager_node) executor.add_node(controller_node) try: executor.spin() finally: executor.shutdown() manager_node.destroy_node() controller_node.destroy_node() finally: manager_node.reboot_odrive() rclpy.shutdown()
def main(args=None): global executor rclpy.init(args=args) try: executor = MultiThreadedExecutor() executor.add_node(SubtaskMove()) executor.add_node(SubtaskGrasp()) executor.add_node(SubtaskRelease()) executor.add_node(SubtaskOpen()) executor.add_node(SubtaskClose()) executor.add_node(SubtaskSensing()) executor.add_node(SubtaskRandomMove()) executor.add_node(SubtaskWait()) executor.add_node(SubtaskSpeakerAnnounce()) try: executor.spin() finally: executor.shutdown() finally: rclpy.shutdown()
def main(args=None): global executor rclpy.init(args=args) try: executor = MultiThreadedExecutor() executor.add_node(SubtaskBedLinkUp()) executor.add_node(SubtaskBedLinkDown()) executor.add_node(SubtaskBedHeadUp()) executor.add_node(SubtaskBedHeadDown()) executor.add_node(SubtaskBedFootUp()) executor.add_node(SubtaskBedFootDown()) executor.add_node(SubtaskBedHeightUp()) executor.add_node(SubtaskBedHeightDown()) try: executor.spin() finally: executor.shutdown() finally: rclpy.shutdown()
def main(args=None): rclpy.init(args=args) try: nodes = { 'cpu': CpuChecker(), 'mem': MemChecker(), 'net': NetChecker(), 'temp': TempChecker(), 'pub': NodePublish(), 'sub': NodeSubscribeRec(), } executor = MultiThreadedExecutor() [executor.add_node(node) for node in nodes.values()] try: executor.spin() finally: executor.shutdown() [node.destroy_node() for node in nodes.values()] finally: rclpy.shutdown()
class HelloExecutor: def __init__(self, nodes: list = []): self.nodes = nodes # self.executor = SingleThreadedExecutor() # commented out to test out MultiThreadedExecutor and see how it works self.executor = MultiThreadedExecutor( num_threads=2) # if num_threads is 1, it executes as single thread if len(nodes) is 0: self.executor.shutdown() rclpy.shutdown() raise ValueError('Not any node was given!') elif len(nodes) > 1: for node in nodes: self.executor.add_node(node) else: self.executor.add_node(node) def spin(self): try: self.executor.spin() finally: print('Killing executor and nodes') self.executor.shutdown() for node in self.nodes: node.destroy_node()
APP_NAME = "Crowd Thermometer" FACE_BB_COLOR = (255, 255, 255) # white EYES_BB_COLOR = (0, 255, 255) # yellow rclpy.init(args=None) # ROS publisher # node = rclpy.create_node("minimal_publisher") # publisher = node.create_publisher(String, "rgb_detections", 10) # ROS listener irclient = HeatmapClient() executor = MultiThreadedExecutor(num_threads=1) executor.add_node(irclient) t = Thread() t.run = executor.spin t.start() # Frame saver saving_executor = ThreadPoolExecutor(max_workers=1) try: camera_loop() finally: executor.shutdown() irclient.destroy_node() t.join() rclpy.shutdown()
class ActionServerWrapperProcess(multiprocessing.Process): def __init__(self, exit_future): multiprocessing.Process.__init__(self) self._node = rclpy.create_node('minimal_action_server') self._executor = MultiThreadedExecutor() self._exit_future = exit_future self._action_server = ActionServer( self._node, Fibonacci, 'fibonacci', execute_callback=self.execute_callback, callback_group=ReentrantCallbackGroup(), goal_callback=self.goal_callback, cancel_callback=self.cancel_callback) def goal_callback(self, goal_request): """Accepts or rejects a client request to begin an action.""" # This server allows multiple goals in parallel self._node.get_logger().info('Received goal request') return GoalResponse.ACCEPT def cancel_callback(self, goal_handle): """Accepts or rejects a client request to cancel an action.""" self._node.get_logger().info('Received cancel request') return CancelResponse.ACCEPT async def execute_callback(self, goal_handle): """Executes a goal.""" self._node.get_logger().info('Executing goal...') # Append the seeds for the Fibonacci sequence feedback_msg = Fibonacci.Feedback() feedback_msg.sequence = [0, 1] # Start executing the action for i in range(1, goal_handle.request.order): if goal_handle.is_cancel_requested: goal_handle.canceled() self._node.get_logger().info('Goal canceled') return Fibonacci.Result() # Update Fibonacci sequence feedback_msg.sequence.append(feedback_msg.sequence[i] + feedback_msg.sequence[i-1]) self._node.get_logger().info('Publishing feedback: {0}'.format(feedback_msg.sequence)) # Publish the feedback goal_handle.publish_feedback(feedback_msg) # Sleep for demonstration purposes #time.sleep(0.5) goal_handle.succeed() # Populate result message result = Fibonacci.Result() result.sequence = feedback_msg.sequence self._node.get_logger().info('Returning result: {0}'.format(result.sequence)) return result def run(self): print("Server is entering...") while not self._exit_future.done(): rclpy.spin_until_future_complete( self._node, self._exit_future, executor=self._executor, timeout_sec=1) self._action_server.destroy() self._executor.shutdown() self._node.destroy_node() print('Server is exiting...')