|
| 1 | +#include <ur_client_library/primary/primary_client.h> |
| 2 | +#include <chrono> |
| 3 | + |
| 4 | +using namespace urcl; |
| 5 | + |
| 6 | +std::string g_DEFAULT_ROBOT_IP = "192.168.56.101"; |
| 7 | + |
| 8 | +int main(int argc, char* argv[]) |
| 9 | +{ |
| 10 | + // Set the loglevel to info to print info logs |
| 11 | + urcl::setLogLevel(urcl::LogLevel::INFO); |
| 12 | + |
| 13 | + // Parse the ip arguments if given |
| 14 | + std::string robot_ip = g_DEFAULT_ROBOT_IP; |
| 15 | + if (argc > 1) |
| 16 | + { |
| 17 | + robot_ip = std::string(argv[1]); |
| 18 | + } |
| 19 | + auto notif = comm::INotifier(); |
| 20 | + auto client = primary_interface::PrimaryClient(robot_ip, notif); |
| 21 | + client.start(10); |
| 22 | + |
| 23 | + // --------------- INITIALIZATION END ------------------- |
| 24 | + |
| 25 | + // Make sure the robot is running |
| 26 | + client.commandBrakeRelease(); |
| 27 | + |
| 28 | + if (!client.safetyModeAllowsExecution()) |
| 29 | + { |
| 30 | + URCL_LOG_ERROR("Robot is not in a safety state where script execution is possible. Exiting."); |
| 31 | + return 1; |
| 32 | + } |
| 33 | + |
| 34 | + // The sendScriptBlocking accepts script code, and will return true or false, |
| 35 | + // depending on whether the script is successfully executed |
| 36 | + const std::string fully_defined_script = R"""( |
| 37 | +# This is a fully defined script, function definition and all |
| 38 | +# All comments in this script will be stripped before sending the script to the robot |
| 39 | +
|
| 40 | +# Any whitespace-only lines will also be removed |
| 41 | +def example_fun(): |
| 42 | + movej([0,-1.2,1.2,-0.1,1.57,0]) |
| 43 | + sleep(0.1) |
| 44 | + current_pose = get_target_tcp_pose() |
| 45 | + relative_move = p[0,-0.1,0,0,0,0] |
| 46 | + movel(pose_trans(current_pose, relative_move), t=1) |
| 47 | +end)"""; |
| 48 | + |
| 49 | + if (client.sendScriptBlocking(fully_defined_script)) |
| 50 | + { |
| 51 | + // The function definition can also be omitted |
| 52 | + // A function name will then be auto generated |
| 53 | + client.sendScriptBlocking(R"(textmsg("Successful program execution"))"); |
| 54 | + } |
| 55 | + // A script-function name can also be passed to the method |
| 56 | + // A timeout can also be given to limit the wait for the passed function to start. If timeout = 0, it will |
| 57 | + // wait indefinitely. |
| 58 | + client.sendScriptBlocking(R"(textmsg("hello"))", "cool_function_name", std::chrono::milliseconds(0)); |
| 59 | + // There is no feedback on secondary programs, so it will return successful as soon as the script is sent to the |
| 60 | + // robot (Behavior is the same the sendScript function, except that robot state is checked before script is sent) |
| 61 | + // Note that secondary scripts have to be "fully defined" by the user. |
| 62 | + std::string secondary_script = R"( |
| 63 | +sec sec_script(): |
| 64 | + textmsg("Named secondary program") |
| 65 | +end |
| 66 | +)"; |
| 67 | + client.sendScriptBlocking(secondary_script); |
| 68 | + |
| 69 | + // Sending wrong script code will result in a clear error |
| 70 | + const std::string bad_script_code = R"""( |
| 71 | +def bad_code(): |
| 72 | + current_pose = get_target_tcp_pose() |
| 73 | + movel(current_pos) # note pose vs pos |
| 74 | +end)"""; |
| 75 | + URCL_LOG_INFO("Sending bad script code..."); |
| 76 | + bool success = client.sendScriptBlocking(bad_script_code); |
| 77 | + { |
| 78 | + std::stringstream ss; |
| 79 | + ss << "Execution of bad code successful? " << std::boolalpha << success; |
| 80 | + URCL_LOG_INFO("%s", ss.str().c_str()); |
| 81 | + } |
| 82 | + |
| 83 | + // We can also send script code without any checks |
| 84 | + URCL_LOG_INFO("Executing motion without feedback"); |
| 85 | + client.sendScript("movej([0.1,-0.9,0.9,0,0,0])"); |
| 86 | + // But we won't know when that is done or even if our code was correct. |
| 87 | + // E.g. sending the bad script here will not give us any information |
| 88 | + // The return value will only tell us that the script code has been sent to the robot. |
| 89 | + URCL_LOG_INFO("Sending bad script code without feedback..."); |
| 90 | + success = client.sendScript(bad_script_code); |
| 91 | + { |
| 92 | + std::stringstream ss; |
| 93 | + ss << "Bad code sent to robot successfully? " << std::boolalpha << success; |
| 94 | + URCL_LOG_INFO("%s", ss.str().c_str()); |
| 95 | + } |
| 96 | +} |
0 commit comments