本文整理匯總了Python中rclpy.ok方法的典型用法代碼示例。如果您正苦於以下問題:Python rclpy.ok方法的具體用法?Python rclpy.ok怎麽用?Python rclpy.ok使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rclpy
的用法示例。
在下文中一共展示了rclpy.ok方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: main
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [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()
示例2: main
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [as 別名]
def main(args=None):
rclpy.init(args=args)
node = rclpy.create_node('minimal_publisher')
publisher = node.create_publisher(String, 'topic', 10)
msg = String()
i = 0
while rclpy.ok():
msg.data = 'Hello World: %d' % i
i += 1
node.get_logger().info('Publishing: "%s"' % msg.data)
publisher.publish(msg)
sleep(0.5) # seconds
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
node.destroy_node()
rclpy.shutdown()
示例3: get_msg_class
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [as 別名]
def get_msg_class(node, topic, blocking=False, include_hidden_topics=False):
msg_class = _get_msg_class(node, topic, include_hidden_topics)
if msg_class:
return msg_class
elif blocking:
print('WARNING: topic [%s] does not appear to be published yet' % topic)
while rclpy.ok():
msg_class = _get_msg_class(node, topic, include_hidden_topics)
if msg_class:
return msg_class
else:
sleep(0.1)
else:
print('WARNING: topic [%s] does not appear to be published yet' % topic)
return None
示例4: _rostopic_hz
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [as 別名]
def _rostopic_hz(node, topic, window_size=DEFAULT_WINDOW_SIZE, filter_expr=None, use_wtime=False):
"""
Periodically print the publishing rate of a topic to console until shutdown.
:param topic: topic name, ``list`` of ``str``
:param window_size: number of messages to average over, -1 for infinite, ``int``
:param filter_expr: Python filter expression that is called with m, the message instance
"""
# 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 = ROSTopicHz(node, window_size, filter_expr=filter_expr, use_wtime=use_wtime)
node.create_subscription(
msg_class,
topic,
functools.partial(rt.callback_hz, topic=topic),
qos_profile_sensor_data)
while rclpy.ok():
rclpy.spin_once(node)
rt.print_hz(topic)
node.destroy_node()
rclpy.shutdown()
示例5: __getattr__
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [as 別名]
def __getattr__(self, name):
if not rclpy.ok():
raise RuntimeError('!rclpy.ok()')
return getattr(self.node, name)
示例6: _rostopic_delay
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [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()
示例7: requester
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [as 別名]
def requester(service_pkg, service_name, namespace):
import rclpy
# Import the service
module = importlib.import_module(service_pkg + '.srv')
srv_mod = getattr(module, service_name)
req = srv_mod.Request()
resp = srv_mod.Response()
srv_fixtures = [[req, resp]]
service_name = 'test/service/' + service_name
rclpy.init(args=[])
try:
node = rclpy.create_node('requester', namespace=namespace)
try:
# wait for the service to be available
client = node.create_client(srv_mod, service_name)
tries = 15
while rclpy.ok() and not client.wait_for_service(timeout_sec=1.0) and tries > 0:
print('service not available, waiting again...')
tries -= 1
assert tries > 0, 'service still not available, aborting test'
print('requester: beginning request')
# Make one call to that service
for req, resp in srv_fixtures:
future = client.call_async(req)
rclpy.spin_until_future_complete(node, future)
assert repr(future.result()) == repr(resp), \
'unexpected response %r\n\nwas expecting %r' % (future.result(), resp)
print('received reply #%d of %d' % (
srv_fixtures.index([req, resp]) + 1, len(srv_fixtures)))
finally:
node.destroy_node()
finally:
rclpy.shutdown()
示例8: replier
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [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()
示例9: main
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [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()
示例10: main
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [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()
示例11: test_number_callbacks
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [as 別名]
def test_number_callbacks(period):
context = rclpy.context.Context()
rclpy.init(context=context)
try:
node = rclpy.create_node('test_timer_number_callbacks', context=context)
try:
executor = SingleThreadedExecutor(context=context)
try:
executor.add_node(node)
# The first spin_once() takes long enough for 1ms timer tests to fail
executor.spin_once(timeout_sec=0)
callbacks = []
timer = node.create_timer(period, lambda: callbacks.append(len(callbacks)))
try:
begin_time = time.time()
while rclpy.ok(context=context) and time.time() - begin_time < 4.5 * period:
executor.spin_once(timeout_sec=period / 10)
assert len(callbacks) == 4
finally:
node.destroy_timer(timer)
finally:
executor.shutdown()
finally:
node.destroy_node()
finally:
rclpy.shutdown(context=context)
示例12: publish_clock_messages
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [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)
示例13: publish_reversed_clock_messages
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [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)
示例14: set_use_sim_time_parameter
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [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
示例15: talker_main
# 需要導入模塊: import rclpy [as 別名]
# 或者: from rclpy import ok [as 別名]
def talker_main():
rclpy.init(args=None)
node = rclpy.create_node('ros2_talker_node')
pub = node.create_publisher(String, '/chatter')
msg = String()
i = 0
while rclpy.ok():
msg.data = 'Hello World: %d' % i
i += 1
node.get_logger().info('Publishing: "%s"' % msg.data)
pub.publish(msg)
sleep(0.5)