14 #include <spdlog/spdlog.h>
21 using namespace flexiv;
25 const std::vector<double> kFloatingDamping = {10.0, 10.0, 5.0, 5.0, 1.0, 1.0, 1.0};
28 std::atomic<bool> g_stop_sched = {
false};
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;
48 throw std::runtime_error(
49 "PeriodicTask: Fault occurred on the connected robot, exiting ...");
53 std::vector<double> target_torque(robot.
info().
DoF);
56 for (
size_t i = 0; i < target_torque.size(); ++i) {
57 target_torque[i] += -kFloatingDamping[i] * robot.
states().
dtheta[i];
64 }
catch (
const std::exception& e) {
65 spdlog::error(e.what());
70 int main(
int argc,
char* argv[])
75 if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {
"-h",
"--help"})) {
80 std::string robot_sn = argv[1];
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");
98 spdlog::warn(
"Fault occurred on the connected robot, trying to clear ...");
101 spdlog::error(
"Fault cannot be cleared, exiting ...");
104 spdlog::info(
"Fault on the connected robot is cleared");
108 spdlog::info(
"Enabling robot ...");
113 std::this_thread::sleep_for(std::chrono::seconds(1));
115 spdlog::info(
"Robot is now operational");
118 spdlog::info(
"Moving to home pose");
119 robot.
SwitchMode(rdk::Mode::NRT_PLAN_EXECUTION);
122 while (robot.
busy()) {
123 std::this_thread::sleep_for(std::chrono::seconds(1));
135 std::bind(PeriodicTask, std::ref(robot)),
"HP periodic", 1, scheduler.
max_priority());
140 while (!g_stop_sched) {
141 std::this_thread::sleep_for(std::chrono::milliseconds(1));
146 }
catch (
const std::exception& e) {
147 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.
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...
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