Skip to content

Commit 3b8c2fe

Browse files
committed
Add example documentation for primary_pipeline
1 parent 8cc0969 commit 3b8c2fe

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

doc/examples.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ starting point for your own applications.
1414
examples/force_mode
1515
examples/freedrive
1616
examples/instruction_executor
17+
examples/primary_pipeline
1718
examples/ur_driver

doc/examples/primary_pipeline.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
:github_url: https://github.com/UniversalRobots/Universal_Robots_Client_Library/blob/master/doc/examples/primary_pipeline.rst
2+
3+
.. _primary_pipeline_example:
4+
5+
Primary Pipeline example
6+
========================
7+
8+
This example shows how to use the ``Pipeline`` class for the robot's `primary interface
9+
<https://www.universal-robots.com/articles/ur/interface-communication/overview-of-client-interfaces/>`_.
10+
It reads all packages coming in from the robot't primary interface and prints their contents.
11+
12+
At the current time parsing primary interface data is very limited, so this example will print the
13+
raw binary data for most package types. The example serves to demonstrate the basic control flow
14+
used for reading data from the robot.
15+
16+
In this library, a "pipeline" uses a producer / consumer architecture. A producer is reading data
17+
from a *stream*, parses that data and puts it into a *queue*. A consumer reads data from the queue
18+
and can do whatever its purpose is.
19+
20+
Producer setup
21+
--------------
22+
23+
To setup the producer, we need to create a stream, a parser and create a producer with those:
24+
25+
.. literalinclude:: ../../examples/primary_pipeline.cpp
26+
:language: c++
27+
:caption: examples/primary_pipeline.cpp
28+
:linenos:
29+
:lineno-match:
30+
:start-at: // First of all, we need a stream
31+
:end-at: prod.setupProducer();
32+
33+
Consumer setup
34+
--------------
35+
36+
The consumer can be any consumer that is able to consume data produced by the producer, in this
37+
case ``urcl::primary_interface::PrimaryPackage``. Here, we use a ``ShellExecutor`` that will try to
38+
print each package's content to the shell output:
39+
40+
.. literalinclude:: ../../examples/primary_pipeline.cpp
41+
:language: c++
42+
:caption: examples/primary_pipeline.cpp
43+
:linenos:
44+
:lineno-match:
45+
:start-at: // The shell consumer
46+
:end-at: auto consumer = std::make_unique
47+
48+
Assemble the pipeline
49+
---------------------
50+
51+
Finally, we need to assemble the pipeline by connecting the producer to the consumer:
52+
53+
.. literalinclude:: ../../examples/primary_pipeline.cpp
54+
:language: c++
55+
:caption: examples/primary_pipeline.cpp
56+
:linenos:
57+
:lineno-match:
58+
:start-after: auto consumer = std::make_unique
59+
:end-at: pipeline.run()
60+
61+
You can setup a custom notifier that can handle start and stop events of the pipeline. In this
62+
example we use the basic ``INotifier`` which doesn't do anything.
63+
64+
With all that, we can create the ``pipeline`` by passing the producer, consumer, a name and the
65+
notifier to it's constructor.
66+
67+
From this point on, the producer will read the data coming on from the stream and that data will be
68+
processed by the consumer. We keep the example program alive for a while to see some data:
69+
70+
.. literalinclude:: ../../examples/primary_pipeline.cpp
71+
:language: c++
72+
:caption: examples/primary_pipeline.cpp
73+
:linenos:
74+
:lineno-match:
75+
:start-at: do
76+
:end-at: return 0
77+
78+

examples/primary_pipeline.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,20 @@ int main(int argc, char* argv[])
5555
second_to_run = std::stoi(argv[2]);
5656
}
5757

58-
// First of all, we need a stream that connects to the robot
58+
// First of all, we need a stream that connects to the robot's primary interface
5959
comm::URStream<primary_interface::PrimaryPackage> primary_stream(robot_ip, urcl::primary_interface::UR_PRIMARY_PORT);
6060

6161
// This will parse the primary packages
6262
primary_interface::PrimaryParser parser;
6363

6464
// The producer needs both, the stream and the parser to fully work
6565
comm::URProducer<primary_interface::PrimaryPackage> prod(primary_stream, parser);
66+
67+
// Connect to the stream
6668
prod.setupProducer();
6769

6870
// The shell consumer will print the package contents to the shell
69-
std::unique_ptr<comm::IConsumer<primary_interface::PrimaryPackage>> consumer;
70-
consumer.reset(new comm::ShellConsumer<primary_interface::PrimaryPackage>());
71+
auto consumer = std::make_unique<comm::ShellConsumer<primary_interface::PrimaryPackage>>();
7172

7273
// The notifer will be called at some points during connection setup / loss. This isn't fully
7374
// implemented atm.

0 commit comments

Comments
 (0)