Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

txROS Updates #1158

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions docs/reference/axros/examples.rst
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
Examples
^^^^^^^^

The following example starts a sublisher and a subscriber which listens to the messages
sublished by the sublisher. After two seconds, it reports how many messages it was
The following example starts a publisher and a subscriber which listens to the messages
published by the publisher. After two seconds, it reports how many messages it was
able to successfully receive.

.. code-block:: python

import asyncio
from rosgraph_msgs.msg import Clock
from axros import NodeHandle, sublisher, Subscriber
from axros import NodeHandle, Publisher, Subscriber
import uvloop

async def subsub(nh: NodeHandle, sub: sublisher, sub: Subscriber):
async def pubsub(nh: NodeHandle, pub: Publisher, sub: Subscriber):
count = 0
try:
while True:
await asyncio.sleep(0)
msg = (Clock(nh.get_time()))
sub.sublish(msg)
pub.publish(msg)
recv = await sub.get_next_message()
if (recv == msg):
count += 1
Expand All @@ -30,13 +30,13 @@ able to successfully receive.
async def main():
nh = await NodeHandle.from_argv("node", "", anonymous = True)
async with nh:
sub = nh.advertise('clock2', Clock, latching = True)
pub = nh.advertise('clock2', Clock, latching = True)
sub = nh.subscribe('clock2', Clock)
await asyncio.gather(sub.setup(), sub.setup())
subsub_task = asyncio.create_task(subsub(nh, sub, sub))
await asyncio.gather(pub.setup(), sub.setup())
pubsub_task = asyncio.create_task(pubsub(nh, pub, sub))
print("Everything is setup. Waiting two seconds.")
await asyncio.sleep(2)
subsub_task.cancel()
pubsub_task.cancel()

if __name__ == "__main__":
uvloop.install()
Expand All @@ -62,28 +62,28 @@ The node handle can be used for general purposes, such as parameters and sleepin
This parameter does not exist!
>>> await nh.sleep(2) # Sleeps for 2 seconds

The node handle can also be used for sublishing and subscribing to topics. Note
that all sublishers and subscribers must be setup.
The node handle can also be used for publishing and subscribing to topics. Note
that all Publishers and subscribers must be setup.

.. code-block:: python

>>> import os
>>> import axros
>>> nh = await axros.NodeHandle("/test", "special_node", "localhost", os.environ["ROS_MASTER_URI"], {})
>>> from std_msgs.msg import Int32
>>> sub = nh.advertise("running_time", Int32)
>>> await sub.setup()
>>> async def sublish():
>>> pub = nh.advertise("running_time", Int32)
>>> await pub.setup()
>>> async def publish():
... try:
... count = 0
... while True:
... sub.sublish(Int32(count))
... pub.publish(Int32(count))
... count += 1
... await asyncio.sleep(1)
... except asyncio.CancelledError as _:
... # When task gets cancelled, stop sublishing
... # When task gets cancelled, stop publishing
... pass
>>> task = asyncio.create_task(sublish()) # Start sublishing!
>>> task = asyncio.create_task(publish()) # Start publishing!
>>> sub = nh.subscribe("running_time", Int32)
>>> await sub.setup()
>>> while True:
Expand Down
Loading