Flexiv RDK APIs  1.6.0
basics9_global_variables.cpp
1 
8 #include <flexiv/rdk/robot.hpp>
9 #include <flexiv/rdk/utility.hpp>
10 #include <spdlog/spdlog.h>
11 
12 #include <iostream>
13 #include <thread>
14 
15 using namespace flexiv;
16 
18 void PrintHelp()
19 {
20  // clang-format off
21  std::cout << "Required arguments: [robot_sn]" << std::endl;
22  std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
23  std::cout << "Optional arguments: None" << std::endl;
24  std::cout << std::endl;
25  // clang-format on
26 }
27 
28 int main(int argc, char* argv[])
29 {
30  // Program Setup
31  // =============================================================================================
32  // Parse parameters
33  if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
34  PrintHelp();
35  return 1;
36  }
37  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
38  std::string robot_sn = argv[1];
39 
40  // Print description
41  spdlog::info(
42  ">>> Tutorial description <<<\nThis tutorial shows how to get and set global variables.\n");
43 
44  try {
45  // RDK Initialization
46  // =========================================================================================
47  // Instantiate robot interface
48  rdk::Robot robot(robot_sn);
49 
50  // Clear fault on the connected robot if any
51  if (robot.fault()) {
52  spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
53  // Try to clear the fault
54  if (!robot.ClearFault()) {
55  spdlog::error("Fault cannot be cleared, exiting ...");
56  return 1;
57  }
58  spdlog::info("Fault on the connected robot is cleared");
59  }
60 
61  // Enable the robot, make sure the E-stop is released before enabling
62  spdlog::info("Enabling robot ...");
63  robot.Enable();
64 
65  // Wait for the robot to become operational
66  while (!robot.operational()) {
67  std::this_thread::sleep_for(std::chrono::seconds(1));
68  }
69  spdlog::info("Robot is now operational");
70 
71  // Get existing global variables
72  // =========================================================================================
73  auto global_vars = robot.global_variables();
74  if (global_vars.empty()) {
75  spdlog::warn("No global variables available");
76  return 1;
77  } else {
78  spdlog::info("Existing global variables and their original values:");
79  for (const auto& var : global_vars) {
80  std::cout << var.first << ": " << rdk::utility::FlexivTypes2Str(var.second)
81  << std::endl;
82  }
83  }
84 
85  // Set global variables
86  // =========================================================================================
88  spdlog::info("Setting new values to existing global variables");
89  robot.SetGlobalVariables({
90  {"test_bool", 1},
91  {"test_int", 100},
92  {"test_double", 100.123},
93  {"test_string", "Flexiv"},
94  {"test_int_vec", std::vector<int> {1, 2, 3}},
95  {"test_double_vec", std::vector<double> {1.1, 2.2, 3.3}},
96  {"test_string_vec", std::vector<std::string> {"Go", "Flexiv", "Go!"}},
97  {"test_pose", std::vector<double> {0.1, -0.2, 0.3, -90, -45, 120}},
98  {"test_coord", rdk::Coord({0.1, -0.2, 0.3}, {-90, -45, 120}, {"WORLD", "WORLD_ORIGIN"},
99  {1, 2, 3, 4, 5, 6, 7}, {10, 20, 0, 0, 0, 0})},
100  {"test_coord_array",
101  std::vector<rdk::Coord> {rdk::Coord({1, 2, 3}, {4, 5, 6}, {"WORK", "WorkCoord0"}),
102  rdk::Coord({10, 20, 30}, {40, 50, 60}, {"WORLD", "WORLD_ORIGIN"},
103  {1, 2, 3, 4, 5, 6, 7}, {10, 20, 0, 0, 0, 0}),
104  rdk::Coord({3, 2, 1}, {180, 0, 180}, {"WORLD", "WORLD_ORIGIN"})}},
105  });
106 
107  // Get updated global variables
108  // =========================================================================================
109  global_vars = robot.global_variables();
110  if (global_vars.empty()) {
111  spdlog::warn("No global variables available");
112  return 1;
113  } else {
114  spdlog::info("Updated global variables:");
115  for (const auto& var : global_vars) {
116  std::cout << var.first << ": " << rdk::utility::FlexivTypes2Str(var.second)
117  << std::endl;
118  }
119  }
120 
121  spdlog::info("Program finished");
122 
123  } catch (const std::exception& e) {
124  spdlog::error(e.what());
125  return 1;
126  }
127 
128  return 0;
129 }
Main interface with the robot, containing several function categories and background services.
Definition: robot.hpp:25
Data structure representing the customized data type "COORD" in Flexiv Elements.
Definition: data.hpp:334