本文整理汇总了Python中rclpy.spin_once方法的典型用法代码示例。如果您正苦于以下问题:Python rclpy.spin_once方法的具体用法?Python rclpy.spin_once怎么用?Python rclpy.spin_once使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rclpy
的用法示例。
在下文中一共展示了rclpy.spin_once方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _rostopic_bw
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def _rostopic_bw(node, topic, window_size=DEFAULT_WINDOW_SIZE):
"""Periodically print the received bandwidth of a topic to console until shutdown."""
# pause bw until topic is published
msg_class = get_msg_class(node, topic, blocking=True, include_hidden_topics=True)
if msg_class is None:
node.destroy_node()
return
rt = ROSTopicBandwidth(node, window_size)
node.create_subscription(
msg_class,
topic,
rt.callback,
qos_profile_sensor_data,
raw=True
)
print(f'Subscribed to [{topic}]')
timer = node.create_timer(1, rt.print_bw)
while rclpy.ok():
rclpy.spin_once(node)
node.destroy_timer(timer)
node.destroy_node()
rclpy.shutdown()
示例2: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def main(args=None):
global g_node
rclpy.init(args=args)
g_node = rclpy.create_node('minimal_subscriber')
subscription = g_node.create_subscription(String, 'topic', chatter_callback, 10)
subscription # prevent unused variable warning
while rclpy.ok():
rclpy.spin_once(g_node)
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
g_node.destroy_node()
rclpy.shutdown()
示例3: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def main(args=None):
rclpy.init(args=args)
minimal_client = MinimalClientAsync()
minimal_client.send_request()
while rclpy.ok():
rclpy.spin_once(minimal_client)
if minimal_client.future.done():
try:
response = minimal_client.future.result()
except Exception as e:
minimal_client.get_logger().info(
'Service call failed %r' % (e,))
else:
minimal_client.get_logger().info(
'Result of add_two_ints: for %d + %d = %d' %
(minimal_client.req.a, minimal_client.req.b, response.sum))
break
minimal_client.destroy_node()
rclpy.shutdown()
示例4: test_remove_node
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def test_remove_node(self):
self.assertIsNotNone(self.node.handle)
executor = SingleThreadedExecutor(context=self.context)
got_callback = False
def timer_callback():
nonlocal got_callback
got_callback = True
try:
tmr = self.node.create_timer(0.1, timer_callback)
try:
executor.add_node(self.node)
executor.remove_node(self.node)
executor.spin_once(timeout_sec=0.2)
finally:
self.node.destroy_timer(tmr)
finally:
executor.shutdown()
assert not got_callback
示例5: call_get_states
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def call_get_states(*, node, node_names):
clients = {}
futures = {}
# create clients
for node_name in node_names:
clients[node_name] = \
node.create_client(GetState, f'{node_name}/get_state')
# wait until all clients have been called
while True:
for node_name in [n for n in node_names if n not in futures]:
# call as soon as ready
client = clients[node_name]
if client.service_is_ready():
request = GetState.Request()
future = client.call_async(request)
futures[node_name] = future
if len(futures) == len(clients):
break
rclpy.spin_once(node, timeout_sec=1.0)
# wait for all responses
for future in futures.values():
rclpy.spin_until_future_complete(node, future)
# return current state or exception for each node
states = {}
for node_name, future in futures.items():
if future.result() is not None:
response = future.result()
states[node_name] = response.current_state
else:
states[node_name] = future.exception()
return states
示例6: call_change_states
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def call_change_states(*, node, transitions):
clients = {}
futures = {}
# create clients
for node_name in transitions.keys():
clients[node_name] = node.create_client(
ChangeState, f'{node_name}/change_state')
# wait until all clients have been called
while True:
for node_name in [n for n in transitions.keys() if n not in futures]:
# call as soon as ready
client = clients[node_name]
if client.service_is_ready():
request = ChangeState.Request()
request.transition = transitions[node_name]
future = client.call_async(request)
futures[node_name] = future
if len(futures) == len(clients):
break
rclpy.spin_once(node, timeout_sec=1.0)
# wait for all responses
for future in futures.values():
rclpy.spin_until_future_complete(node, future)
# return success flag or exception for each node
results = {}
for node_name, future in futures.items():
if future.result() is not None:
response = future.result()
results[node_name] = response.success
else:
results[node_name] = future.exception()
return results
示例7: __init__
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def __init__(self, args, *, node_name=None):
timeout_reached = False
def timer_callback():
nonlocal timeout_reached
timeout_reached = True
argv = getattr(args, 'argv', [])
rclpy.init(args=argv)
node_name_suffix = getattr(
args, 'node_name_suffix', '_%d' % os.getpid())
start_parameter_services = getattr(
args, 'start_parameter_services', False)
if node_name is None:
node_name = NODE_NAME_PREFIX + node_name_suffix
self.node = rclpy.create_node(
node_name,
start_parameter_services=start_parameter_services)
timeout = getattr(args, 'spin_time', DEFAULT_TIMEOUT)
timer = self.node.create_timer(timeout, timer_callback)
while not timeout_reached:
rclpy.spin_once(self.node)
self.node.destroy_timer(timer)
示例8: _rostopic_delay
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def _rostopic_delay(node, topic, window_size=DEFAULT_WINDOW_SIZE):
"""
Periodically print the publishing delay of a topic to console until shutdown.
:param topic: topic name, ``str``
:param window_size: number of messages to average over, ``unsigned_int``
:param blocking: pause delay until topic is published, ``bool``
"""
# pause hz until topic is published
msg_class = get_msg_class(node, topic, blocking=True, include_hidden_topics=True)
if msg_class is None:
node.destroy_node()
return
rt = ROSTopicDelay(node, window_size)
node.create_subscription(
msg_class,
topic,
rt.callback_delay,
qos_profile_sensor_data)
timer = node.create_timer(1, rt.print_delay)
while rclpy.ok():
rclpy.spin_once(node)
node.destroy_timer(timer)
node.destroy_node()
rclpy.shutdown()
示例9: replier
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def replier(service_pkg, service_name, number_of_cycles, namespace):
import rclpy
module = importlib.import_module(service_pkg + '.srv')
srv_mod = getattr(module, service_name)
rclpy.init(args=[])
node = rclpy.create_node('replier', namespace=namespace)
req = srv_mod.Request()
resp = srv_mod.Response()
srv_fixtures = [[req, resp]]
chatter_callback = functools.partial(
replier_callback, srv_fixtures=srv_fixtures)
node.create_service(
srv_mod, 'test/service/' + service_name, chatter_callback)
spin_count = 0
print('replier: beginning loop')
while rclpy.ok() and spin_count < number_of_cycles:
rclpy.spin_once(node, timeout_sec=2)
spin_count += 1
# print('spin_count: ' + str(spin_count))
node.destroy_node()
rclpy.shutdown()
示例10: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def main(args=None):
global g_node
rclpy.init(args=args)
g_node = rclpy.create_node('minimal_service')
srv = g_node.create_service(AddTwoInts, 'add_two_ints', add_two_ints_callback)
while rclpy.ok():
rclpy.spin_once(g_node)
# Destroy the service attached to the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
g_node.destroy_service(srv)
rclpy.shutdown()
示例11: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def main(args=None):
rclpy.init(args=args)
node = rclpy.create_node('minimal_client_async')
cli = node.create_client(AddTwoInts, 'add_two_ints')
req = AddTwoInts.Request()
req.a = 41
req.b = 1
while not cli.wait_for_service(timeout_sec=1.0):
node.get_logger().info('service not available, waiting again...')
future = cli.call_async(req)
while rclpy.ok():
rclpy.spin_once(node)
if future.done():
try:
result = future.result()
except Exception as e:
node.get_logger().info('Service call failed %r' % (e,))
else:
node.get_logger().info(
'Result of add_two_ints: for %d + %d = %d' %
(req.a, req.b, result.sum))
break
node.destroy_node()
rclpy.shutdown()
示例12: timed_spin
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def timed_spin(self, duration):
start_time = time.time()
while (time.time() - start_time) < duration:
rclpy.spin_once(self.node, executor=self.executor, timeout_sec=0.1)
示例13: publish_clock_messages
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def publish_clock_messages(self):
clock_pub = self.node.create_publisher(rosgraph_msgs.msg.Clock, CLOCK_TOPIC, 1)
cycle_count = 0
time_msg = rosgraph_msgs.msg.Clock()
while rclpy.ok(context=self.context) and cycle_count < 5:
time_msg.clock.sec = cycle_count
clock_pub.publish(time_msg)
cycle_count += 1
executor = rclpy.executors.SingleThreadedExecutor(context=self.context)
rclpy.spin_once(self.node, timeout_sec=1, executor=executor)
# TODO(dhood): use rate once available
time.sleep(1)
示例14: publish_reversed_clock_messages
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def publish_reversed_clock_messages(self):
clock_pub = self.node.create_publisher(rosgraph_msgs.msg.Clock, CLOCK_TOPIC, 1)
cycle_count = 0
time_msg = rosgraph_msgs.msg.Clock()
while rclpy.ok(context=self.context) and cycle_count < 5:
time_msg.clock.sec = 6 - cycle_count
clock_pub.publish(time_msg)
cycle_count += 1
executor = rclpy.executors.SingleThreadedExecutor(context=self.context)
rclpy.spin_once(self.node, timeout_sec=1, executor=executor)
time.sleep(1)
示例15: set_use_sim_time_parameter
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import spin_once [as 别名]
def set_use_sim_time_parameter(self, value):
self.node.set_parameters(
[Parameter('use_sim_time', Parameter.Type.BOOL, value)])
executor = rclpy.executors.SingleThreadedExecutor(context=self.context)
cycle_count = 0
while rclpy.ok(context=self.context) and cycle_count < 5:
use_sim_time_param = self.node.get_parameter('use_sim_time')
cycle_count += 1
if use_sim_time_param.type_ == Parameter.Type.BOOL:
break
rclpy.spin_once(self.node, timeout_sec=1, executor=executor)
time.sleep(1)
return use_sim_time_param.value == value