Flexiv RDK APIs  1.6.0
intermediate4_realtime_joint_floating.cpp
1 
11 #include <flexiv/rdk/robot.hpp>
12 #include <flexiv/rdk/scheduler.hpp>
13 #include <flexiv/rdk/utility.hpp>
14 #include <spdlog/spdlog.h>
15 
16 #include <iostream>
17 #include <string>
18 #include <thread>
19 #include <atomic>
20 
21 using namespace flexiv;
22 
23 namespace {
25 const std::vector<double> kFloatingDamping = {10.0, 10.0, 5.0, 5.0, 1.0, 1.0, 1.0};
26 
28 std::atomic<bool> g_stop_sched = {false};
29 }
30 
32 void PrintHelp()
33 {
34  // clang-format off
35  std::cout << "Required arguments: [robot_sn]" << std::endl;
36  std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
37  std::cout << "Optional arguments: None" << std::endl;
38  std::cout << std::endl;
39  // clang-format on
40 }
41 
43 void PeriodicTask(rdk::Robot& robot)
44 {
45  try {
46  // Monitor fault on the connected robot
47  if (robot.fault()) {
48  throw std::runtime_error(
49  "PeriodicTask: Fault occurred on the connected robot, exiting ...");
50  }
51 
52  // Set 0 joint torques
53  std::vector<double> target_torque(robot.info().DoF);
54 
55  // Add some velocity damping
56  for (size_t i = 0; i < target_torque.size(); ++i) {
57  target_torque[i] += -kFloatingDamping[i] * robot.states().dtheta[i];
58  }
59 
60  // Send target joint torque to RDK server, enable gravity compensation and joint limits soft
61  // protection
62  robot.StreamJointTorque(target_torque, true, true);
63 
64  } catch (const std::exception& e) {
65  spdlog::error(e.what());
66  g_stop_sched = true;
67  }
68 }
69 
70 int main(int argc, char* argv[])
71 {
72  // Program Setup
73  // =============================================================================================
74  // Parse parameters
75  if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
76  PrintHelp();
77  return 1;
78  }
79  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
80  std::string robot_sn = argv[1];
81 
82  // Print description
83  spdlog::info(
84  ">>> Tutorial description <<<\nThis tutorial runs real-time joint floating with gentle "
85  "velocity damping, gravity compensation, and soft protection against position limits. This "
86  "example is ideal for verifying the system's whole-loop real-timeliness, accuracy of the "
87  "robot dynamics model, and joint torque control performance. If everything works well, all "
88  "joints should float smoothly.\n");
89 
90  try {
91  // RDK Initialization
92  // =========================================================================================
93  // Instantiate robot interface
94  rdk::Robot robot(robot_sn);
95 
96  // Clear fault on the connected robot if any
97  if (robot.fault()) {
98  spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
99  // Try to clear the fault
100  if (!robot.ClearFault()) {
101  spdlog::error("Fault cannot be cleared, exiting ...");
102  return 1;
103  }
104  spdlog::info("Fault on the connected robot is cleared");
105  }
106 
107  // Enable the robot, make sure the E-stop is released before enabling
108  spdlog::info("Enabling robot ...");
109  robot.Enable();
110 
111  // Wait for the robot to become operational
112  while (!robot.operational()) {
113  std::this_thread::sleep_for(std::chrono::seconds(1));
114  }
115  spdlog::info("Robot is now operational");
116 
117  // Move robot to home pose
118  spdlog::info("Moving to home pose");
119  robot.SwitchMode(rdk::Mode::NRT_PLAN_EXECUTION);
120  robot.ExecutePlan("PLAN-Home");
121  // Wait for the plan to finish
122  while (robot.busy()) {
123  std::this_thread::sleep_for(std::chrono::seconds(1));
124  }
125 
126  // Real-time Joint Floating
127  // =========================================================================================
128  // Switch to real-time joint torque control mode
129  robot.SwitchMode(rdk::Mode::RT_JOINT_TORQUE);
130 
131  // Create real-time scheduler to run periodic tasks
132  rdk::Scheduler scheduler;
133  // Add periodic task with 1ms interval and highest applicable priority
134  scheduler.AddTask(
135  std::bind(PeriodicTask, std::ref(robot)), "HP periodic", 1, scheduler.max_priority());
136  // Start all added tasks
137  scheduler.Start();
138 
139  // Block and wait for signal to stop scheduler tasks
140  while (!g_stop_sched) {
141  std::this_thread::sleep_for(std::chrono::milliseconds(1));
142  }
143  // Received signal to stop scheduler tasks
144  scheduler.Stop();
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
void ExecutePlan(unsigned int index, bool continue_exec=false, bool block_until_started=true)
[Blocking] Execute a plan by specifying its index.
const RobotStates states() const
[Non-blocking] Current states data of the robot.
const RobotInfo info() const
[Non-blocking] General information about the connected robot.
bool operational(bool verbose=true) const
[Non-blocking] Whether the robot is ready to be operated, which requires the following conditions to ...
void StreamJointTorque(const std::vector< double > &torques, bool enable_gravity_comp=true, bool enable_soft_limits=true)
[Non-blocking] Continuously stream joint torque command to the robot.
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::vector< double > dtheta
Definition: data.hpp:158