|
| 1 | +:github_url: https://github.com/UniversalRobots/Universal_Robots_Client_Library/blob/master/doc/examples/script_command_interface.rst |
| 2 | + |
| 3 | +Script command interface |
| 4 | +======================== |
| 5 | + |
| 6 | +The :ref:`script_command_interface` allows sending predefined commands to the robot while there is |
| 7 | +URScript running that is connected to it. |
| 8 | + |
| 9 | +An example to utilize the script command interface can be found in the `freedrive_example.cpp <https://github.com/UniversalRobots/Universal_Robots_Client_Library/blob/master/examples/script_command_interface.cpp>`_. |
| 10 | + |
| 11 | +In order to use the ``ScriptCommandInterface``, there has to be a script code running on the robot |
| 12 | +that connects to the ``ScriptCommandInterface``. This happens as part of the big |
| 13 | +`external_control.urscript <https://github.com/UniversalRobots/Universal_Robots_Client_Library/blob/master/resources/external_control.urscript>`_. In order to reuse that with this example, we will create a full |
| 14 | +``UrDriver`` and leverage the ``ScriptCommandInterface`` through this. |
| 15 | + |
| 16 | +At first, we create a ``ExampleRobotWrapper`` object in order to initialize communication with the |
| 17 | +robot. |
| 18 | + |
| 19 | +.. literalinclude:: ../../examples/script_command_interface.cpp |
| 20 | + :language: c++ |
| 21 | + :caption: examples/script_command_interface.cpp |
| 22 | + :linenos: |
| 23 | + :lineno-match: |
| 24 | + :start-at: g_my_robot = |
| 25 | + :end-at: std::thread script_command_send_thread(sendScriptCommands); |
| 26 | + |
| 27 | +The script commands will be sent in a separate thread which will be explained later. |
| 28 | + |
| 29 | +Since the connection to the script command interface runs as part of the bigger external_control |
| 30 | +script, we'll wrap the calls alongside a full ``ExampleRobotWrapper``. Hence, we'll have to send |
| 31 | +keepalive signals regularly to keep the script running: |
| 32 | + |
| 33 | +.. literalinclude:: ../../examples/script_command_interface.cpp |
| 34 | + :language: c++ |
| 35 | + :caption: examples/script_command_interface.cpp |
| 36 | + :linenos: |
| 37 | + :lineno-match: |
| 38 | + :start-at: std::chrono::duration<double> time_done(0); |
| 39 | + :end-at: g_my_robot->getUrDriver()->stopControl(); |
| 40 | + |
| 41 | +Sending script commands |
| 42 | +----------------------- |
| 43 | + |
| 44 | +Once the script is running on the robot, a connection to the driver's script command interface |
| 45 | +should be established. The ``UrDriver`` forwards most calls of the ``ScriptCommandInterface`` and |
| 46 | +we will use that interface in this example. To send a script command, we can e.g. use |
| 47 | +``g_my_robot->getUrDriver()->zeroFTSensor()``. |
| 48 | + |
| 49 | +In the example, we have wrapped the calls into a lambda function that will wait a specific timeout, |
| 50 | +print a log output what command will be sent and then call the respective command: |
| 51 | + |
| 52 | +.. literalinclude:: ../../examples/script_command_interface.cpp |
| 53 | + :language: c++ |
| 54 | + :caption: examples/script_command_interface.cpp |
| 55 | + :linenos: |
| 56 | + :lineno-match: |
| 57 | + :start-at: run_cmd( |
| 58 | + :end-before: URCL_LOG_INFO("Script command thread finished."); |
| 59 | + |
| 60 | +The lambda itself looks like this: |
| 61 | + |
| 62 | +.. literalinclude:: ../../examples/script_command_interface.cpp |
| 63 | + :language: c++ |
| 64 | + :caption: examples/script_command_interface.cpp |
| 65 | + :linenos: |
| 66 | + :lineno-match: |
| 67 | + :start-at: auto run_cmd = |
| 68 | + :end-before: // Keep running all commands in a loop |
| 69 | + |
| 70 | +For a list of all available script commands, please refer to the ``ScriptCommandInterface`` class |
| 71 | +`here <https://github.com/UniversalRobots/Universal_Robots_Client_Library/blob/master/include/ur_client_library/control/script_command_interface.h>`_. |
0 commit comments