Flexiv RDK APIs  1.6.0
test_timeliness_monitor.cpp
1 
12 #include <flexiv/rdk/robot.hpp>
13 #include <flexiv/rdk/scheduler.hpp>
14 #include <flexiv/rdk/utility.hpp>
15 #include <spdlog/spdlog.h>
16 
17 #include <iostream>
18 #include <string>
19 #include <cmath>
20 #include <thread>
21 #include <atomic>
22 
23 namespace {
25 std::atomic<bool> g_stop_sched = {false};
26 }
27 
28 // callback function for realtime periodic task
29 void PeriodicTask(flexiv::rdk::Robot& robot, const std::vector<double>& init_pos)
30 {
31  // Loop counter
32  static unsigned int loop_counter = 0;
33 
34  try {
35  // Monitor fault on the connected robot
36  if (robot.fault()) {
37  throw std::runtime_error(
38  "PeriodicTask: Fault occurred on the connected robot, exiting ...");
39  }
40  // Hold position
41  std::vector<double> target_vel(robot.info().DoF);
42  std::vector<double> target_acc(robot.info().DoF);
43  robot.StreamJointPosition(init_pos, target_vel, target_acc);
44 
45  if (loop_counter == 5000) {
46  spdlog::warn(">>>>> Adding simulated loop delay <<<<<");
47  }
48  // simulate prolonged loop time after 5 seconds
49  else if (loop_counter > 5000) {
50  std::this_thread::sleep_for(std::chrono::microseconds(995));
51  }
52 
53  loop_counter++;
54 
55  } catch (const std::exception& e) {
56  spdlog::error(e.what());
57  g_stop_sched = true;
58  }
59 }
60 
61 void PrintHelp()
62 {
63  // clang-format off
64  std::cout << "Required arguments: [robot_sn]" << std::endl;
65  std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
66  std::cout << "Optional arguments: None" << std::endl;
67  std::cout << std::endl;
68  // clang-format on
69 }
70 
71 int main(int argc, char* argv[])
72 {
73  // Parse Parameters
74  //==============================================================================================
75  if (argc < 2 || flexiv::rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
76  PrintHelp();
77  return 1;
78  }
79 
80  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
81  std::string robot_sn = argv[1];
82 
83  try {
84  // RDK Initialization
85  //==========================================================================================
86  // Instantiate robot interface
87  flexiv::rdk::Robot robot(robot_sn);
88 
89  // Clear fault on the connected robot if any
90  if (robot.fault()) {
91  spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
92  // Try to clear the fault
93  if (!robot.ClearFault()) {
94  spdlog::error("Fault cannot be cleared, exiting ...");
95  return 1;
96  }
97  spdlog::info("Fault on the connected robot is cleared");
98  }
99 
100  // enable the robot, make sure the E-stop is released before enabling
101  spdlog::info("Enabling robot ...");
102  robot.Enable();
103 
104  // Wait for the robot to become operational
105  while (!robot.operational()) {
106  std::this_thread::sleep_for(std::chrono::seconds(1));
107  }
108  spdlog::info("Robot is now operational");
109 
110  // set mode after robot is operational
111  robot.SwitchMode(flexiv::rdk::Mode::RT_JOINT_POSITION);
112 
113  // Set initial joint positions
114  auto init_pos = robot.states().q;
115  spdlog::info("Initial joint positions set to: {}", flexiv::rdk::utility::Vec2Str(init_pos));
116  spdlog::warn(">>>>> Simulated loop delay will be added after 5 seconds <<<<<");
117 
118  // Periodic Tasks
119  //==========================================================================================
120  flexiv::rdk::Scheduler scheduler;
121  // Add periodic task with 1ms interval and highest applicable priority
122  scheduler.AddTask(std::bind(PeriodicTask, std::ref(robot), std::ref(init_pos)),
123  "HP periodic", 1, scheduler.max_priority());
124  // Start all added tasks
125  scheduler.Start();
126 
127  // Block and wait for signal to stop scheduler tasks
128  while (!g_stop_sched) {
129  std::this_thread::sleep_for(std::chrono::milliseconds(1));
130  }
131  // Received signal to stop scheduler tasks
132  scheduler.Stop();
133 
134  } catch (const std::exception& e) {
135  spdlog::error(e.what());
136  return 1;
137  }
138 
139  return 0;
140 }
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.
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 SwitchMode(Mode mode)
[Blocking] Switch to a new control mode and wait until mode transition is finished.
void StreamJointPosition(const std::vector< double > &positions, const std::vector< double > &velocities, const std::vector< double > &accelerations)
[Non-blocking] Continuously stream joint position, velocity, and acceleration command to the robot....
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.
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 > q
Definition: data.hpp:136