Flexiv RDK APIs  1.6.0
basics1_display_robot_states.cpp
1 
9 #include <flexiv/rdk/robot.hpp>
10 #include <flexiv/rdk/utility.hpp>
11 #include <spdlog/spdlog.h>
12 
13 #include <iostream>
14 #include <thread>
15 
16 using namespace flexiv;
17 
19 void PrintHelp()
20 {
21  // clang-format off
22  std::cout << "Required arguments: [robot_sn]" << std::endl;
23  std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
24  std::cout << "Optional arguments: None" << std::endl;
25  std::cout << std::endl;
26  // clang-format on
27 }
28 
30 void PrintRobotStates(rdk::Robot& robot)
31 {
32  while (true) {
33  // Print all robot states in JSON format using the built-in ostream operator overloading
34  spdlog::info("Current robot states:");
35  std::cout << robot.states() << std::endl;
36 
37  // Print digital inputs
38  spdlog::info("Current digital inputs:");
39  std::cout << rdk::utility::Arr2Str(robot.digital_inputs()) << std::endl;
40  std::this_thread::sleep_for(std::chrono::seconds(1));
41  }
42 }
43 
44 int main(int argc, char* argv[])
45 {
46  // Program Setup
47  // =============================================================================================
48  // Parse parameters
49  if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
50  PrintHelp();
51  return 1;
52  }
53  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
54  std::string robot_sn = argv[1];
55 
56  // Print description
57  spdlog::info(
58  ">>> Tutorial description <<<\nThis tutorial does the very first thing: check connection "
59  "with the robot server and print received robot states.\n");
60 
61  try {
62  // RDK Initialization
63  // =========================================================================================
64  // Instantiate robot interface
65  rdk::Robot robot(robot_sn);
66 
67  // Clear fault on the connected robot if any
68  if (robot.fault()) {
69  spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
70  // Try to clear the fault
71  if (!robot.ClearFault()) {
72  spdlog::error("Fault cannot be cleared, exiting ...");
73  return 1;
74  }
75  spdlog::info("Fault on the connected robot is cleared");
76  }
77 
78  // Enable the robot, make sure the E-stop is released before enabling
79  spdlog::info("Enabling robot ...");
80  robot.Enable();
81 
82  // Wait for the robot to become operational
83  while (!robot.operational()) {
84  std::this_thread::sleep_for(std::chrono::seconds(1));
85  }
86  spdlog::info("Robot is now operational");
87 
88  // Print States
89  // =========================================================================================
90  // Use std::thread to do scheduling so that this example can run on all OS, since not all OS
91  // support rdk::Scheduler
92  std::thread low_priority_thread(std::bind(PrintRobotStates, std::ref(robot)));
93 
94  // Properly exit thread
95  low_priority_thread.join();
96 
97  } catch (const std::exception& e) {
98  spdlog::error(e.what());
99  return 1;
100  }
101 
102  return 0;
103 }
Main interface with the robot, containing several function categories and background services.
Definition: robot.hpp:25
const RobotStates states() const
[Non-blocking] Current states data of the robot.
bool operational(bool verbose=true) const
[Non-blocking] Whether the robot is ready to be operated, which requires the following conditions to ...
const std::array< bool, kIOPorts > digital_inputs() const
[Non-blocking] Current reading from all digital input ports, including 16 on the control box plus 2 i...
void Enable()
[Blocking] Enable the robot, if E-stop is released and there's no fault, the robot will release brake...
bool fault() const
[Non-blocking] Whether the robot is in fault state.
bool ClearFault(unsigned int timeout_sec=30)
[Blocking] Try to clear minor or critical fault of the robot without a power cycle.