|
| 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 | + |
0 commit comments