Flexiv RDK APIs  1.6.0
basics4_plan_execution.cpp
1 
11 #include <flexiv/rdk/robot.hpp>
12 #include <flexiv/rdk/utility.hpp>
13 #include <spdlog/spdlog.h>
14 
15 #include <iostream>
16 #include <thread>
17 
18 using namespace flexiv;
19 
21 void PrintHelp()
22 {
23  // clang-format off
24  std::cout << "Required arguments: [robot_sn]" << std::endl;
25  std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
26  std::cout << "Optional arguments: None" << std::endl;
27  std::cout << std::endl;
28  // clang-format on
29 }
30 
31 int main(int argc, char* argv[])
32 {
33  // Program Setup
34  // =============================================================================================
35  // Parse parameters
36  if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
37  PrintHelp();
38  return 1;
39  }
40  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
41  std::string robot_sn = argv[1];
42 
43  // Print description
44  spdlog::info(
45  ">>> Tutorial description <<<\nThis tutorial executes a plan selected by the user from a "
46  "list of available plans. A plan is a pre-written script to execute a series of robot "
47  "primitives with pre-defined transition conditions between 2 adjacent primitives. Users "
48  "can use Flexiv Elements to compose their own plan and assign to the robot, which will "
49  "appear in the plan list.\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  // Execute Plans
79  // =========================================================================================
80  // Switch to plan execution mode
81  robot.SwitchMode(rdk::Mode::NRT_PLAN_EXECUTION);
82 
83  while (true) {
84  // Monitor fault on the connected robot
85  if (robot.fault()) {
86  throw std::runtime_error("Fault occurred on the connected robot, exiting ...");
87  }
88 
89  // Get user input
90  spdlog::info("Choose an action:");
91  std::cout << "[1] Show available plans" << std::endl;
92  std::cout << "[2] Execute a plan by index" << std::endl;
93  std::cout << "[3] Execute a plan by name" << std::endl;
94 
95  std::string inputBuffer;
96  std::cin >> inputBuffer;
97  int userInput = std::stoi(inputBuffer);
98 
99  switch (userInput) {
100  // Get and show plan list
101  case 1: {
102  auto plan_list = robot.plan_list();
103  for (size_t i = 0; i < plan_list.size(); i++) {
104  std::cout << "[" << i << "] " << plan_list[i] << std::endl;
105  }
106  std::cout << std::endl;
107  } break;
108  // Execute plan by index
109  case 2: {
110  spdlog::info("Enter plan index to execute:");
111  int index;
112  std::cin >> index;
113  // Allow the plan to continue its execution even if the RDK program is closed or
114  // the connection is lost
115  robot.ExecutePlan(index, true);
116 
117  // Print plan info while the current plan is running
118  while (robot.busy()) {
119  spdlog::info("Current plan info:");
120  std::cout << robot.plan_info() << std::endl;
121  std::this_thread::sleep_for(std::chrono::seconds(1));
122  }
123  } break;
124  // Execute plan by name
125  case 3: {
126  spdlog::info("Enter plan name to execute:");
127  std::string name;
128  std::cin >> name;
129  // Allow the plan to continue its execution even if the RDK program is closed or
130  // the connection is lost
131  robot.ExecutePlan(name, true);
132 
133  // Print plan info while the current plan is running
134  while (robot.busy()) {
135  spdlog::info("Current plan info:");
136  std::cout << robot.plan_info() << std::endl;
137  std::this_thread::sleep_for(std::chrono::seconds(1));
138  }
139  } break;
140  default:
141  spdlog::warn("Invalid input");
142  break;
143  }
144  }
145 
146  } catch (const std::exception& e) {
147  spdlog::error(e.what());
148  return 1;
149  }
150 
151  return 0;
152 }
Main interface with the robot, containing several function categories and background services.
Definition: robot.hpp:25