Flexiv RDK APIs  1.6.0
basics5_zero_force_torque_sensors.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 
29 int main(int argc, char* argv[])
30 {
31  // Program Setup
32  // =============================================================================================
33  // Parse parameters
34  if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
35  PrintHelp();
36  return 1;
37  }
38  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
39  std::string robot_sn = argv[1];
40 
41  // Print description
42  spdlog::info(
43  ">>> Tutorial description <<<\nThis tutorial zeros the robot's force and torque sensors, "
44  "which is a recommended (but not mandatory) step before any operations that require "
45  "accurate force/torque measurement.\n");
46 
47  try {
48  // RDK Initialization
49  // =========================================================================================
50  // Instantiate robot interface
51  rdk::Robot robot(robot_sn);
52 
53  // Clear fault on the connected robot if any
54  if (robot.fault()) {
55  spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
56  // Try to clear the fault
57  if (!robot.ClearFault()) {
58  spdlog::error("Fault cannot be cleared, exiting ...");
59  return 1;
60  }
61  spdlog::info("Fault on the connected robot is cleared");
62  }
63 
64  // Enable the robot, make sure the E-stop is released before enabling
65  spdlog::info("Enabling robot ...");
66  robot.Enable();
67 
68  // Wait for the robot to become operational
69  while (!robot.operational()) {
70  std::this_thread::sleep_for(std::chrono::seconds(1));
71  }
72  spdlog::info("Robot is now operational");
73 
74  // Zero Sensors
75  // =========================================================================================
76  // Get and print the current TCP force/moment readings
77  spdlog::info("TCP force and moment reading in world frame BEFORE sensor zeroing: "
78  + rdk::utility::Arr2Str(robot.states().ext_wrench_in_world) + "[N][Nm]");
79 
80  // Run the "ZeroFTSensor" primitive to automatically zero force and torque sensors
81  robot.SwitchMode(rdk::Mode::NRT_PRIMITIVE_EXECUTION);
82  robot.ExecutePrimitive("ZeroFTSensor", std::map<std::string, rdk::FlexivDataTypes> {});
83 
84  // WARNING: during the process, the robot must not contact anything, otherwise the result
85  // will be inaccurate and affect following operations
86  spdlog::warn(
87  "Zeroing force/torque sensors, make sure nothing is in contact with the robot");
88 
89  // Wait for primitive completion
90  while (robot.busy()) {
91  std::this_thread::sleep_for(std::chrono::seconds(1));
92  }
93  spdlog::info("Sensor zeroing complete");
94 
95  // Get and print the current TCP force/moment readings
96  spdlog::info("TCP force and moment reading in world frame AFTER sensor zeroing: "
97  + rdk::utility::Arr2Str(robot.states().ext_wrench_in_world) + "[N][Nm]");
98 
99  } catch (const std::exception& e) {
100  spdlog::error(e.what());
101  return 1;
102  }
103 
104  return 0;
105 }
Main interface with the robot, containing several function categories and background services.
Definition: robot.hpp:25