Flexiv RDK APIs  1.4
test_cart_motion_tracking.cpp
1 
2 #include <flexiv/rdk/robot.hpp>
4 #include <flexiv/rdk/utility.hpp>
5 #include <spdlog/spdlog.h>
6 
7 #include <iostream>
8 #include <fstream>
9 #include <cmath>
10 #include <thread>
11 #include <atomic>
12 
13 namespace {
15 constexpr size_t kLoopFreq = 1000;
16 
18 constexpr double kLoopPeriod = 0.001;
19 
21 constexpr double kSwingAmp = 0.1;
22 
24 constexpr double kSwingFreq = 0.3;
25 
27 std::atomic<bool> g_stop_sched = {false};
28 
29 // Data buffers written to log file after motion is finished
30 std::vector<unsigned int> loop_counter_buf = {};
31 std::vector<double> target_x_buf = {};
32 std::vector<double> actual_x_buf = {};
33 }
34 
36 void PrintHelp()
37 {
38  // clang-format off
39  std::cout << "Required arguments: [robot SN]" << std::endl;
40  std::cout << " robot SN: Serial number of the robot to connect to. "
41  "Remove any space, for example: Rizon4s-123456" << std::endl;
42  std::cout << std::endl;
43  // clang-format on
44 }
45 
47 void PeriodicTask(
48  flexiv::rdk::Robot& robot, const std::array<double, flexiv::rdk::kPoseSize>& init_pose)
49 {
50  // Local periodic loop counter
51  static uint64_t loop_counter = 0;
52 
53  try {
54  // Monitor fault on the connected robot
55  if (robot.fault()) {
56  throw std::runtime_error(
57  "PeriodicTask: Fault occurred on the connected robot, exiting ...");
58  }
59 
60  // Initialize target pose to initial pose
61  auto target_pose = init_pose;
62 
63  // Sine-sweep TCP along X axis
64  double w = 2 * M_PI * kSwingFreq;
65  double t = loop_counter * kLoopPeriod;
66  target_pose[0] = init_pose[0] + kSwingAmp * sin(w * t);
67 
68  // Calculate 2nd derivative of A*sin(wt) as feed-forward acceleration
69  std::array<double, flexiv::rdk::kCartDoF> target_acc = {};
70  target_acc[0] = -kSwingAmp * w * w * sin(w * t);
71 
72  // Zero target wrench
73  std::array<double, flexiv::rdk::kCartDoF> target_wrench = {};
74 
75  // Send command with target acc
76  robot.StreamCartesianMotionForce(target_pose, target_wrench, target_acc);
77 
78  // Save data @ 100Hz
79  loop_counter_buf.emplace_back(loop_counter);
80  target_x_buf.emplace_back(target_pose[0]);
81  actual_x_buf.emplace_back(robot.states().tcp_pose[0]);
82 
83  // Stop after 10s
84  if (++loop_counter > 10000) {
85  g_stop_sched = true;
86  }
87 
88  } catch (const std::exception& e) {
89  spdlog::error(e.what());
90  g_stop_sched = true;
91  }
92 }
93 
94 int main(int argc, char* argv[])
95 {
96  // Program Setup
97  // =============================================================================================
98  // Parse parameters
99  if (argc < 2 || flexiv::rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
100  PrintHelp();
101  return 1;
102  }
103  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
104  std::string robot_sn = argv[1];
105 
106  try {
107  // RDK Initialization
108  // =========================================================================================
109  // Instantiate robot interface
110  flexiv::rdk::Robot robot(robot_sn);
111 
112  // Clear fault on the connected robot if any
113  if (robot.fault()) {
114  spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
115  // Try to clear the fault
116  if (!robot.ClearFault()) {
117  spdlog::error("Fault cannot be cleared, exiting ...");
118  return 1;
119  }
120  spdlog::info("Fault on the connected robot is cleared");
121  }
122 
123  // Enable the robot, make sure the E-stop is released before enabling
124  spdlog::info("Enabling robot ...");
125  robot.Enable();
126 
127  // Wait for the robot to become operational
128  while (!robot.operational()) {
129  std::this_thread::sleep_for(std::chrono::seconds(1));
130  }
131  spdlog::info("Robot is now operational");
132 
133  // Move robot to home pose
134  spdlog::info("Moving to home pose");
135  robot.SwitchMode(flexiv::rdk::Mode::NRT_PRIMITIVE_EXECUTION);
136  robot.ExecutePrimitive("Home()");
137 
138  // Wait for the primitive to finish
139  while (robot.busy()) {
140  std::this_thread::sleep_for(std::chrono::seconds(1));
141  }
142 
143  // Start Pure Motion Control
144  // =========================================================================================
145 
146  // Switch to real-time mode for continuous motion control
147  robot.SwitchMode(flexiv::rdk::Mode::RT_CARTESIAN_MOTION_FORCE);
148 
149  // Set initial pose to current TCP pose
150  auto init_pose = robot.states().tcp_pose;
151  spdlog::info("Initial TCP pose set to [position 3x1, rotation (quaternion) 4x1]: "
152  + flexiv::rdk::utility::Arr2Str(init_pose));
153 
154  // Create real-time scheduler to run periodic tasks
155  flexiv::rdk::Scheduler scheduler;
156  // Add periodic task with 1ms interval and highest applicable priority
157  scheduler.AddTask(std::bind(PeriodicTask, std::ref(robot), std::ref(init_pose)),
158  "HP periodic", 1, scheduler.max_priority());
159  // Start all added tasks
160  scheduler.Start();
161 
162  // Block and wait for signal to stop scheduler tasks
163  while (!g_stop_sched) {
164  std::this_thread::sleep_for(std::chrono::milliseconds(1));
165  }
166  // Received signal to stop scheduler tasks
167  scheduler.Stop();
168 
169  // Write data to file
170  // =========================================================================================
171  // Create csv file
172  std::ofstream csv_file;
173  std::string csv_file_name = "test_data.csv";
174  csv_file.open(csv_file_name);
175  if (csv_file.is_open()) {
176  spdlog::info("Created new log file: {}", csv_file_name);
177  } else {
178  spdlog::error("Failed to create log file: {}", csv_file_name);
179  return 1;
180  }
181 
182  // Check vector size matches each other
183  if (loop_counter_buf.size() != target_x_buf.size()
184  || target_x_buf.size() != actual_x_buf.size()) {
185  spdlog::error(
186  "Size mismatch: loop_counter_buf.size() = {}, target_x_buf.size() = {}, "
187  "actual_x_buf.size() = {}",
188  loop_counter_buf.size(), target_x_buf.size(), actual_x_buf.size());
189  return 1;
190  }
191 
192  // Log data: [target_x, actual_x]
193  for (size_t i = 0; i < loop_counter_buf.size(); i++) {
194  csv_file << loop_counter_buf[i] << "," << target_x_buf[i] << "," << actual_x_buf[i]
195  << '\n';
196  }
197 
198  // Close file
199  csv_file.close();
200  spdlog::info("Saved {} data points to log file {}", target_x_buf.size(), csv_file_name);
201 
202  } catch (const std::exception& e) {
203  spdlog::error(e.what());
204  return 1;
205  }
206 
207  return 0;
208 }
Main interface with the robot, containing several function categories and background services.
Definition: robot.hpp:24
void StreamCartesianMotionForce(const std::array< double, kPoseSize > &pose, const std::array< double, kCartDoF > &wrench={}, const std::array< double, kCartDoF > &acceleration={})
[Non-blocking] Continuously stream Cartesian motion and/or force command for the robot to track using...
const RobotStates states() const
[Non-blocking] Access the current robot states.
bool operational(bool verbose=true) const
[Non-blocking] Whether the robot is normally operational, which requires the following conditions to ...
void ExecutePrimitive(const std::string &pt_cmd)
[Blocking] Execute a primitive by specifying its name and parameters, which can be found in the Flexi...
void SwitchMode(Mode mode)
[Blocking] Switch to a new control mode and wait until mode transition is finished.
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.
bool busy() const
[Non-blocking] Whether the robot is currently executing a task. This includes any user commanded oper...
Real-time scheduler that can simultaneously run multiple periodic tasks. Parameters for each task are...
Definition: scheduler.hpp:22
int max_priority() const
[Non-blocking] Get maximum available priority for user tasks.
void AddTask(std::function< void(void)> &&callback, const std::string &task_name, int interval, int priority, int cpu_affinity=-1)
[Non-blocking] Add a new periodic task to the scheduler's task pool. Each task in the pool is assigne...
void Stop()
[Blocking] Stop all added tasks. The periodic execution will stop and all task threads will be closed...
void Start()
[Blocking] Start all added tasks. A dedicated thread will be created for each added task and the peri...
std::array< double, kPoseSize > tcp_pose
Definition: data.hpp:148