12 #include <spdlog/spdlog.h>
23 const double kLoopPeriod = 0.001;
26 const double kSwingAmp = 0.1;
29 const double kSwingFreq = 0.025;
32 std::array<double, flexiv::rdk::kPoseSize> g_curr_tcp_pose;
35 uint64_t g_test_duration_loop_counts = 0;
38 const unsigned int kLogDurationLoopCounts = 10 * 60 * 1000;
43 std::array<double, flexiv::rdk::kPoseSize> tcp_pose;
44 std::array<double, flexiv::rdk::kCartDoF> tcp_force;
48 std::atomic<bool> g_stop = {
false};
51 void highPriorityTask(
52 flexiv::rdk::Robot& robot,
const std::array<double, flexiv::rdk::kPoseSize>& init_pose)
55 static uint64_t loop_counter = 0;
60 throw std::runtime_error(
61 "highPriorityTask: Fault occurred on the connected robot, exiting ...");
66 = init_pose[2] + kSwingAmp * sin(2 * M_PI * kSwingFreq * loop_counter * kLoopPeriod);
75 if (++loop_counter > g_test_duration_loop_counts) {
79 }
catch (
const std::exception& e) {
80 spdlog::error(e.what());
85 void lowPriorityTask()
88 uint64_t loop_counter = 0;
91 std::ofstream csv_file;
94 std::string csv_file_name;
97 unsigned int file_counter = 0;
100 std::this_thread::sleep_for(std::chrono::seconds(2));
105 std::this_thread::sleep_for(std::chrono::milliseconds(1));
109 if (loop_counter % kLogDurationLoopCounts == 0) {
110 if (csv_file.is_open()) {
112 spdlog::info(
"Saved log file: {}", csv_file_name);
119 csv_file_name =
"endurance_test_data_" + std::to_string(file_counter) +
".csv";
122 csv_file.open(csv_file_name);
123 if (csv_file.is_open()) {
124 spdlog::info(
"Created new log file: {}", csv_file_name);
126 spdlog::error(
"Failed to create log file: {}", csv_file_name);
131 if (csv_file.is_open()) {
133 csv_file << loop_counter <<
",";
134 for (
const auto& i : g_log_data.tcp_pose) {
135 csv_file << i <<
",";
137 for (
const auto& i : g_log_data.tcp_force) {
138 csv_file << i <<
",";
146 spdlog::info(
"Test duration has elapsed, saving any open log file ...");
148 if (csv_file.is_open()) {
150 spdlog::info(
"Saved log file: {}", csv_file_name);
161 std::cout <<
"Required arguments: [robot_sn] [test_hours]" << std::endl;
162 std::cout <<
" robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
163 std::cout <<
" test_hours: Duration of the test, can have decimals" << std::endl;
164 std::cout <<
"Optional arguments: None" << std::endl;
165 std::cout << std::endl;
169 int main(
int argc,
char* argv[])
173 if (argc < 3 || flexiv::rdk::utility::ProgramArgsExistAny(argc, argv, {
"-h",
"--help"})) {
179 std::string robot_sn = argv[1];
182 double test_hours = std::stof(argv[2]);
184 g_test_duration_loop_counts = (uint64_t)(test_hours * 3600.0 * 1000.0);
185 spdlog::info(
"Test duration: {} hours = {} cycles", test_hours, g_test_duration_loop_counts);
195 spdlog::warn(
"Fault occurred on the connected robot, trying to clear ...");
198 spdlog::error(
"Fault cannot be cleared, exiting ...");
201 spdlog::info(
"Fault on the connected robot is cleared");
205 spdlog::info(
"Enabling robot ...");
210 std::this_thread::sleep_for(std::chrono::seconds(1));
212 spdlog::info(
"Robot is now operational");
216 robot.
SwitchMode(flexiv::rdk::Mode::NRT_PLAN_EXECUTION);
221 std::this_thread::sleep_for(std::chrono::seconds(1));
222 }
while (robot.
busy());
225 robot.
SwitchMode(flexiv::rdk::Mode::RT_CARTESIAN_MOTION_FORCE);
229 spdlog::info(
"Initial TCP pose set to [position 3x1, rotation (quaternion) 4x1]: "
230 + flexiv::rdk::utility::Arr2Str(init_pose));
231 g_curr_tcp_pose = init_pose;
237 scheduler.
AddTask(std::bind(highPriorityTask, std::ref(robot), std::ref(init_pose)),
243 std::thread low_priority_thread(lowPriorityTask);
246 low_priority_thread.join();
248 }
catch (
const std::exception& e) {
249 spdlog::error(e.what());
Main interface with the robot, containing several function categories and background services.
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.
bool operational(bool verbose=true) const
[Non-blocking] Whether the robot is ready to be operated, which requires the following conditions to ...
void StreamCartesianMotionForce(const std::array< double, kPoseSize > &pose, const std::array< double, kCartDoF > &wrench={}, const std::array< double, kCartDoF > &velocity={}, const std::array< double, kCartDoF > &acceleration={})
[Non-blocking] Continuously stream Cartesian motion and/or force command for the robot to track using...
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...
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 Start()
[Blocking] Start all added tasks. A dedicated thread will be created for each added task and the peri...
std::array< double, kCartDoF > ext_wrench_in_world
std::array< double, kPoseSize > tcp_pose