Flexiv RDK APIs  1.6.0
basics8_update_robot_tool.cpp
1 
10 #include <flexiv/rdk/robot.hpp>
11 #include <flexiv/rdk/tool.hpp>
12 #include <flexiv/rdk/utility.hpp>
13 #include <spdlog/spdlog.h>
14 
15 #include <iostream>
16 #include <string>
17 #include <thread>
18 
19 using namespace flexiv;
20 
22 void PrintHelp()
23 {
24  // clang-format off
25  std::cout << "Required arguments: [robot_sn]" << std::endl;
26  std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
27  std::cout << "Optional arguments: None" << std::endl;
28  std::cout << std::endl;
29  // clang-format on
30 }
31 
32 int main(int argc, char* argv[])
33 {
34  // Program Setup
35  // =============================================================================================
36  // Parse parameters
37  if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
38  PrintHelp();
39  return 1;
40  }
41  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
42  std::string robot_sn = argv[1];
43 
44  // Print description
45  spdlog::info(
46  ">>> Tutorial description <<<\nThis tutorial shows how to online update and interact with "
47  "the robot tools. All changes made to the robot tool system will take effect immediately "
48  "without needing to reboot. However, the robot must be put into IDLE mode when making "
49  "these changes.\n");
50 
51  try {
52  // RDK Initialization
53  // =========================================================================================
54  // Instantiate robot interface
55  rdk::Robot robot(robot_sn);
56 
57  // Clear fault on the connected robot if any
58  if (robot.fault()) {
59  spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
60  // Try to clear the fault
61  if (!robot.ClearFault()) {
62  spdlog::error("Fault cannot be cleared, exiting ...");
63  return 1;
64  }
65  spdlog::info("Fault on the connected robot is cleared");
66  }
67 
68  // Enable the robot, make sure the E-stop is released before enabling
69  spdlog::info("Enabling robot ...");
70  robot.Enable();
71 
72  // Wait for the robot to become operational
73  while (!robot.operational()) {
74  std::this_thread::sleep_for(std::chrono::seconds(1));
75  }
76  spdlog::info("Robot is now operational");
77 
78  // Update Robot Tool
79  // =========================================================================================
80  // Make sure the robot is in IDLE mode
81  robot.SwitchMode(rdk::Mode::IDLE);
82 
83  // Instantiate tool interface
84  rdk::Tool tool(robot);
85 
86  // Get and print a list of already configured tools currently in the robot's tools pool
87  spdlog::info("All configured tools:");
88  auto tool_list = tool.list();
89  for (size_t i = 0; i < tool_list.size(); i++) {
90  std::cout << "[" << i << "] " << tool_list[i] << std::endl;
91  }
92  std::cout << std::endl;
93 
94  // Get and print the current active tool
95  spdlog::info("Current active tool: {}", tool.name());
96 
97  // Set name and parameters for a new tool
98  std::string new_tool_name = "ExampleTool1";
99  rdk::ToolParams new_tool_params;
100  new_tool_params.mass = 0.9;
101  new_tool_params.CoM = {0.0, 0.0, 0.057};
102  new_tool_params.inertia = {2.768e-03, 3.149e-03, 5.64e-04, 0.0, 0.0, 0.0};
103  new_tool_params.tcp_location = {0.0, -0.207, 0.09, 0.7071068, 0.7071068, 0.0, 0.0};
104 
105  // If there's already a tool with the same name in the robot's tools pool, then remove it
106  // first, because duplicate tool names are not allowed
107  if (tool.exist(new_tool_name)) {
108  spdlog::warn(
109  "Tool with the same name [{}] already exists, removing it now", new_tool_name);
110  // Switch to other tool or no tool (Flange) before removing the current tool
111  tool.Switch("Flange");
112  tool.Remove(new_tool_name);
113  }
114 
115  // Add the new tool
116  spdlog::info("Adding new tool [{}] to the robot", new_tool_name);
117  tool.Add(new_tool_name, new_tool_params);
118 
119  // Get and print the tools list again, the new tool should appear at the end
120  spdlog::info("All configured tools:");
121  tool_list = tool.list();
122  for (size_t i = 0; i < tool_list.size(); i++) {
123  std::cout << "[" << i << "] " << tool_list[i] << std::endl;
124  }
125  std::cout << std::endl;
126 
127  // Switch to the newly added tool, i.e. set it as the active tool
128  spdlog::info("Switching to tool [{}]", new_tool_name);
129  tool.Switch(new_tool_name);
130 
131  // Get and print the current active tool again, should be the new tool
132  spdlog::info("Current active tool: {}", tool.name());
133 
134  // Switch to other tool or no tool (Flange) before removing the current tool
135  tool.Switch("Flange");
136 
137  // Clean up by removing the new tool
138  std::this_thread::sleep_for(std::chrono::seconds(2));
139  spdlog::info("Removing tool [{}]", new_tool_name);
140  tool.Remove(new_tool_name);
141 
142  spdlog::info("Program finished");
143 
144  } catch (const std::exception& e) {
145  spdlog::error(e.what());
146  return 1;
147  }
148 
149  return 0;
150 }
Main interface with the robot, containing several function categories and background services.
Definition: robot.hpp:25
Interface to online update and interact with the robot tools. All updates will take effect immediatel...
Definition: tool.hpp:42
Data structure containing robot tool parameters.
Definition: tool.hpp:20
std::array< double, kPoseSize > tcp_location
Definition: tool.hpp:33
std::array< double, 3 > CoM
Definition: tool.hpp:25
std::array< double, 6 > inertia
Definition: tool.hpp:28