|
| 1 | +// -- BEGIN LICENSE BLOCK ---------------------------------------------- |
| 2 | +// Copyright 2025 Universal Robots A/S |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions are met: |
| 6 | +// |
| 7 | +// * Redistributions of source code must retain the above copyright |
| 8 | +// notice, this list of conditions and the following disclaimer. |
| 9 | +// |
| 10 | +// * Redistributions in binary form must reproduce the above copyright |
| 11 | +// notice, this list of conditions and the following disclaimer in the |
| 12 | +// documentation and/or other materials provided with the distribution. |
| 13 | +// |
| 14 | +// * Neither the name of the {copyright_holder} nor the names of its |
| 15 | +// contributors may be used to endorse or promote products derived from |
| 16 | +// this software without specific prior written permission. |
| 17 | +// |
| 18 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | +// POSSIBILITY OF SUCH DAMAGE. |
| 29 | +// -- END LICENSE BLOCK ------------------------------------------------ |
| 30 | + |
| 31 | +#ifndef UR_CLIENT_LIBRARY_EXAMPLE_ROBOT_WRAPPER_H_INCLUDED |
| 32 | +#define UR_CLIENT_LIBRARY_EXAMPLE_ROBOT_WRAPPER_H_INCLUDED |
| 33 | + |
| 34 | +#include <ur_client_library/ur/dashboard_client.h> |
| 35 | +#include <ur_client_library/ur/ur_driver.h> |
| 36 | +#include <ur_client_library/log.h> |
| 37 | + |
| 38 | +namespace urcl |
| 39 | +{ |
| 40 | +/*! |
| 41 | + * \class ExampleRobotWrapper |
| 42 | + * \brief This class is a high-level abstraction around UrDriver and DashboardClient. It's main |
| 43 | + * purpose is to help us avoiding repetitive robot initialization code in our examples and tests. |
| 44 | + * |
| 45 | + * It is therefore not intended to be used in production code, but rather as a helper class for |
| 46 | + * developers. If you want to use this wrapper in your own code, please make sure to understand the |
| 47 | + * logic behind it and adjust it to your needs. |
| 48 | + * |
| 49 | + * Since this is mainly intended for internal use, don't count on the API being stable for this |
| 50 | + * class! |
| 51 | + */ |
| 52 | +class ExampleRobotWrapper |
| 53 | +{ |
| 54 | +public: |
| 55 | + inline static const std::string DEFAULT_ROBOT_IP = "192.168.56.101"; |
| 56 | + inline static const std::string SCRIPT_FILE = "resources/external_control.urscript"; |
| 57 | + |
| 58 | + ExampleRobotWrapper() = delete; |
| 59 | + /*! |
| 60 | + * \brief Construct a new Example Robot Wrapper object |
| 61 | + * |
| 62 | + * This will connect to a robot and initialize it. In headless mode the program will be running |
| 63 | + * instantly, in teach pendant mode the from \p autostart_program will be started. |
| 64 | + * |
| 65 | + * Note: RTDE communication has to be started separately. |
| 66 | + * |
| 67 | + * \param robot_ip IP address of the robot to connect to |
| 68 | + * \param output_recipe_file Output recipe file for RTDE communication |
| 69 | + * \param input_recipe_file Input recipe file for RTDE communication |
| 70 | + * \param headless_mode Should the driver be started in headless mode or not? |
| 71 | + * \param autostart_program Program to start automatically after initialization when not in |
| 72 | + * headless mode. This flag is ignored in headless mode. |
| 73 | + * \param script_file URScript file to send to the robot. That should be script code |
| 74 | + * communicating to the driver's reverse interface and trajectory interface. |
| 75 | + */ |
| 76 | + ExampleRobotWrapper(const std::string& robot_ip, const std::string& output_recipe_file, |
| 77 | + const std::string& input_recipe_file, const bool headless_mode = true, |
| 78 | + const std::string& autostart_program = "", const std::string& script_file = SCRIPT_FILE); |
| 79 | + ~ExampleRobotWrapper(); |
| 80 | + |
| 81 | + /** |
| 82 | + * @brief Initializes the robot in order to be able to start a program. |
| 83 | + * |
| 84 | + * The robot will be power-cycled once and end up switched on, breaks released. |
| 85 | + */ |
| 86 | + void initializeRobotWithDashboard(); |
| 87 | + |
| 88 | + /** |
| 89 | + * @brief Starts RTDE communication with the robot. |
| 90 | + * |
| 91 | + * @param consume_data Once the RTDE client is started, it's data has to be consumed. If you |
| 92 | + * don't actually care about that data, this class can silently consume RTDE data when `true` is |
| 93 | + * passed. This can be stopped and started at any time using the startConsumingRTDEData() and |
| 94 | + * stopConsumingRTDEData() methods. |
| 95 | + */ |
| 96 | + void startRTDECommununication(const bool consume_data = false); |
| 97 | + |
| 98 | + /** |
| 99 | + * @brief Start consuming RTDE data in the background. |
| 100 | + */ |
| 101 | + void startConsumingRTDEData(); |
| 102 | + |
| 103 | + /** |
| 104 | + * @brief Stop consuming RTDE data in the background. Note that data has to be consumed manually |
| 105 | + * using readDataPackage(). |
| 106 | + */ |
| 107 | + void stopConsumingRTDEData(); |
| 108 | + |
| 109 | + /** |
| 110 | + * @brief Get the latest RTDE package. |
| 111 | + * |
| 112 | + * Do not call this, while RTDE data is being consumed in the background. In doubt, call |
| 113 | + * stopConsumingRTDEData() before calling this function. |
| 114 | + * |
| 115 | + * @param[out] data_pkg The data package will be stored in that object |
| 116 | + * @return true on a successful read, false if no package can be read or when RTDE data is |
| 117 | + * already being consumed in the background. |
| 118 | + */ |
| 119 | + bool readDataPackage(std::unique_ptr<rtde_interface::DataPackage>& data_pkg); |
| 120 | + |
| 121 | + /** |
| 122 | + * @brief Blocks until there is a robot program connected to the driver's reverse interface or |
| 123 | + * until the timeout is hit. |
| 124 | + * |
| 125 | + * @param milliseconds How long to wait for a successful connection. |
| 126 | + * @return True on a successful connection, false if not connection could be detected before the |
| 127 | + * timeout. |
| 128 | + */ |
| 129 | + bool waitForProgramRunning(int milliseconds = 100); |
| 130 | + |
| 131 | + /** |
| 132 | + * @brief Blocks until there is a disconnection event from the driver's reverse interface |
| 133 | + * detected or until the timeout is hit. |
| 134 | + * |
| 135 | + * @param milliseconds How long to wait for a disconnection. |
| 136 | + * @return True on a disconnection event has been detected, false if no event could be detected before the |
| 137 | + * timeout. |
| 138 | + */ |
| 139 | + bool waitForProgramNotRunning(int milliseconds = 100); |
| 140 | + |
| 141 | + /** |
| 142 | + * @brief Depending on whether it is headless or not start autostart_program or call driver's resendRobotProgram |
| 143 | + * function. |
| 144 | + * |
| 145 | + * @return True on successful program start, false otherwise. |
| 146 | + */ |
| 147 | + bool resendRobotProgram(); |
| 148 | + |
| 149 | + /** |
| 150 | + * @brief Start the program \p program_file_name on the robot. |
| 151 | + * |
| 152 | + * The program has be be present on the robot, otherwise this call does not succeed. The robot |
| 153 | + * needs to be in remote_control mode for this to work properly. |
| 154 | + * |
| 155 | + * @param program_file_name Filename on the robot including the ".urp" extension. |
| 156 | + * @return True on successful program start, false otherwise. |
| 157 | + */ |
| 158 | + bool startRobotProgram(const std::string& program_file_name); |
| 159 | + |
| 160 | + std::shared_ptr<urcl::DashboardClient> dashboard_client_; /*!< Dashboard client to interact with the robot */ |
| 161 | + std::shared_ptr<urcl::UrDriver> ur_driver_; /*!< UR driver to interact with the robot */ |
| 162 | + |
| 163 | +private: |
| 164 | + void handleRobotProgramState(bool program_running); |
| 165 | + |
| 166 | + std::atomic<bool> rtde_communication_started_ = false; |
| 167 | + std::atomic<bool> consume_rtde_packages_ = false; |
| 168 | + std::mutex read_package_mutex_; |
| 169 | + std::unique_ptr<rtde_interface::DataPackage> data_pkg_; |
| 170 | + |
| 171 | + bool program_running_; |
| 172 | + std::condition_variable program_running_cv_; |
| 173 | + std::condition_variable program_not_running_cv_; |
| 174 | + std::mutex program_running_mutex_; |
| 175 | + std::mutex program_not_running_mutex_; |
| 176 | + |
| 177 | + std::thread rtde_consumer_thread_; |
| 178 | + |
| 179 | + bool headless_mode_; |
| 180 | + std::string autostart_program_; |
| 181 | +}; |
| 182 | +} // namespace urcl |
| 183 | + |
| 184 | +#endif |
0 commit comments