|
| 1 | +// -- BEGIN LICENSE BLOCK ---------------------------------------------- |
| 2 | +// Copyright 2026 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 | +#include <gtest/gtest.h> |
| 32 | + |
| 33 | +#include "test_utils.h" |
| 34 | +#include "ur_client_library/example_robot_wrapper.h" |
| 35 | + |
| 36 | +using namespace urcl; |
| 37 | +const std::string SCRIPT_FILE = "../resources/external_control.urscript"; |
| 38 | +const std::string OUTPUT_RECIPE = "resources/rtde_output_recipe.txt"; |
| 39 | +const std::string INPUT_RECIPE = "resources/rtde_input_recipe.txt"; |
| 40 | +std::string g_ROBOT_IP = "192.168.56.101"; |
| 41 | +bool g_HEADLESS = true; |
| 42 | + |
| 43 | +std::unique_ptr<ExampleRobotWrapper> g_my_robot; |
| 44 | + |
| 45 | +class ExternalControlProgramTest : public ::testing::Test |
| 46 | +{ |
| 47 | +protected: |
| 48 | + static void SetUpTestSuite() |
| 49 | + { |
| 50 | + if (!(robotVersionLessThan(g_ROBOT_IP, "10.0.0") || g_HEADLESS)) |
| 51 | + { |
| 52 | + GTEST_SKIP_("Running URCap tests for PolyScope X is currently not supported."); |
| 53 | + } |
| 54 | + } |
| 55 | + void SetUp() override |
| 56 | + { |
| 57 | + std::string modified_script_path = extendScript(SCRIPT_FILE); |
| 58 | + |
| 59 | + g_my_robot = std::make_unique<ExampleRobotWrapper>(g_ROBOT_IP, OUTPUT_RECIPE, INPUT_RECIPE, g_HEADLESS, |
| 60 | + "external_control.urp", modified_script_path); |
| 61 | + if (!g_my_robot->isHealthy()) |
| 62 | + { |
| 63 | + ASSERT_TRUE(g_my_robot->resendRobotProgram()); |
| 64 | + ASSERT_TRUE(g_my_robot->waitForProgramRunning(500)); |
| 65 | + } |
| 66 | + server_.reset(new TestableTcpServer(60005)); |
| 67 | + server_->start(); |
| 68 | + } |
| 69 | + |
| 70 | + void TearDown() override |
| 71 | + { |
| 72 | + server_.reset(); |
| 73 | + } |
| 74 | + |
| 75 | + std::string extendScript(const std::string& script_path) |
| 76 | + { |
| 77 | + char modified_script_path[] = "urscript.XXXXXX"; |
| 78 | +#ifdef _WIN32 |
| 79 | +# define mkstemp _mktemp_s |
| 80 | +#endif |
| 81 | + std::ignore = mkstemp(modified_script_path); |
| 82 | + |
| 83 | + std::ofstream ofs(modified_script_path); |
| 84 | + if (ofs.bad()) |
| 85 | + { |
| 86 | + std::cout << "Failed to create temporary files" << std::endl; |
| 87 | + throw std::runtime_error("Failed to create temporary files"); |
| 88 | + } |
| 89 | + std::ifstream in_file(script_path); |
| 90 | + std::string prog((std::istreambuf_iterator<char>(in_file)), (std::istreambuf_iterator<char>())); |
| 91 | + prog += "\nsocket_open(\"{{SERVER_IP_REPLACE}}\", 60005, \"test_socket\")\n"; |
| 92 | + prog += "\nsleep(0.6)\n"; |
| 93 | + prog += "\ntextmsg(\"sleeping done.\")\n"; |
| 94 | + ofs << prog; |
| 95 | + ofs.close(); |
| 96 | + |
| 97 | + return modified_script_path; |
| 98 | + } |
| 99 | + |
| 100 | + std::unique_ptr<TestableTcpServer> server_; |
| 101 | +}; |
| 102 | + |
| 103 | +TEST_F(ExternalControlProgramTest, program_halts_on_timeout) |
| 104 | +{ |
| 105 | + vector6d_t zeros = { 0, 0, 0, 0, 0, 0 }; |
| 106 | + g_my_robot->getUrDriver()->writeJointCommand(zeros, comm::ControlMode::MODE_IDLE, RobotReceiveTimeout::millisec(200)); |
| 107 | + EXPECT_FALSE(server_->waitForConnectionCallback(1000)); |
| 108 | +} |
| 109 | + |
| 110 | +TEST_F(ExternalControlProgramTest, stop_control_does_not_halt_program) |
| 111 | +{ |
| 112 | + vector6d_t zeros = { 0, 0, 0, 0, 0, 0 }; |
| 113 | + g_my_robot->getUrDriver()->writeJointCommand(zeros, comm::ControlMode::MODE_IDLE, RobotReceiveTimeout::off()); |
| 114 | + |
| 115 | + // Make sure that we can stop the robot control, when robot receive timeout has been set off |
| 116 | + g_my_robot->getUrDriver()->stopControl(); |
| 117 | + EXPECT_TRUE(server_->waitForConnectionCallback(1000)); |
| 118 | +} |
| 119 | + |
| 120 | +int main(int argc, char* argv[]) |
| 121 | +{ |
| 122 | + ::testing::InitGoogleTest(&argc, argv); |
| 123 | + |
| 124 | + for (int i = 0; i < argc; i++) |
| 125 | + { |
| 126 | + if (std::string(argv[i]) == "--robot_ip" && i + 1 < argc) |
| 127 | + { |
| 128 | + g_ROBOT_IP = argv[i + 1]; |
| 129 | + ++i; |
| 130 | + } |
| 131 | + if (std::string(argv[i]) == "--headless" && i + 1 < argc) |
| 132 | + { |
| 133 | + std::string headless = argv[i + 1]; |
| 134 | + g_HEADLESS = headless == "true" || headless == "1" || headless == "True" || headless == "TRUE"; |
| 135 | + ++i; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return RUN_ALL_TESTS(); |
| 140 | +} |
0 commit comments