13 #include <spdlog/spdlog.h>
20 using namespace flexiv;
24 constexpr
double kLoopPeriod = 0.001;
27 constexpr
double kSwingAmp = 0.1;
30 constexpr
double kSwingFreq = 0.3;
33 constexpr
double kPressingForce = 5.0;
36 constexpr
double kSearchVelocity = 0.02;
39 constexpr
double kSearchDistance = 1.0;
42 const std::array<double, rdk::kCartDoF> kMaxWrenchForContactSearch
43 = {10.0, 10.0, 10.0, 3.0, 3.0, 3.0};
46 std::atomic<bool> g_stop_sched = {
false};
53 std::cout <<
"Required arguments: [robot_sn]" << std::endl;
54 std::cout <<
" robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
55 std::cout <<
"Optional arguments: [--TCP] [--polish]" << std::endl;
56 std::cout <<
" --TCP: use TCP frame as reference frame for force control, otherwise use world frame" << std::endl;
57 std::cout <<
" --polish: run a simple polish motion along XY plane in world frame, otherwise hold robot motion in non-force-control axes"
64 void PeriodicTask(
rdk::Robot& robot,
const std::array<double, rdk::kPoseSize>& init_pose,
68 static uint64_t loop_counter = 0;
73 throw std::runtime_error(
74 "PeriodicTask: Fault occurred on the connected robot, exiting ...");
78 auto target_pose = init_pose;
82 if (force_ctrl_frame == rdk::CoordType::WORLD) {
84 }
else if (force_ctrl_frame == rdk::CoordType::TCP) {
87 std::array<double, rdk::kCartDoF> target_wrench = {0.0, 0.0, Fz, 0.0, 0.0, 0.0};
93 target_pose[1] = init_pose[1]
94 + kSwingAmp * sin(2 * M_PI * kSwingFreq * loop_counter * kLoopPeriod);
108 }
catch (
const std::exception& e) {
109 spdlog::error(e.what());
114 int main(
int argc,
char* argv[])
119 if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {
"-h",
"--help"})) {
124 std::string robot_sn = argv[1];
128 ">>> Tutorial description <<<\nThis tutorial runs real-time Cartesian-space unified "
129 "motion-force control. The Z axis of the chosen reference frame will be activated for "
130 "explicit force control, while the rest axes in the same reference frame will stay motion "
134 auto force_ctrl_frame = rdk::CoordType::WORLD;
135 if (rdk::utility::ProgramArgsExist(argc, argv,
"--TCP")) {
136 spdlog::info(
"Reference frame used for force control: robot TCP frame");
137 force_ctrl_frame = rdk::CoordType::TCP;
139 spdlog::info(
"Reference frame used for force control: robot world frame");
143 bool enable_polish =
false;
144 if (rdk::utility::ProgramArgsExist(argc, argv,
"--polish")) {
145 spdlog::info(
"Robot will run a polish motion along XY plane in robot world frame");
146 enable_polish =
true;
148 spdlog::info(
"Robot will hold its motion in all non-force-controlled axes");
159 spdlog::warn(
"Fault occurred on the connected robot, trying to clear ...");
162 spdlog::error(
"Fault cannot be cleared, exiting ...");
165 spdlog::info(
"Fault on the connected robot is cleared");
169 spdlog::info(
"Enabling robot ...");
174 std::this_thread::sleep_for(std::chrono::seconds(1));
176 spdlog::info(
"Robot is now operational");
179 spdlog::info(
"Moving to home pose");
180 robot.
SwitchMode(rdk::Mode::NRT_PLAN_EXECUTION);
183 while (robot.
busy()) {
184 std::this_thread::sleep_for(std::chrono::seconds(1));
189 robot.
SwitchMode(rdk::Mode::NRT_PRIMITIVE_EXECUTION);
191 robot.
ExecutePrimitive(
"ZeroFTSensor", std::map<std::string, rdk::FlexivDataTypes> {});
196 "Zeroing force/torque sensors, make sure nothing is in contact with the robot");
199 while (robot.
busy()) {
200 std::this_thread::sleep_for(std::chrono::seconds(1));
202 spdlog::info(
"Sensor zeroing complete");
209 spdlog::info(
"Searching for contact ...");
213 spdlog::info(
"Initial TCP pose set to [position 3x1, rotation (quaternion) 4x1]: "
214 + rdk::utility::Arr2Str(init_pose));
217 robot.
SwitchMode(rdk::Mode::NRT_CARTESIAN_MOTION_FORCE);
223 auto target_pose = init_pose;
224 target_pose[2] -= kSearchDistance;
231 bool is_contacted =
false;
232 while (!is_contacted) {
238 if (ext_force.norm() > kPressingForce) {
240 spdlog::info(
"Contact detected at robot TCP");
243 std::this_thread::sleep_for(std::chrono::milliseconds(1));
249 robot.
SwitchMode(rdk::Mode::RT_CARTESIAN_MOTION_FORCE);
258 std::array<bool, rdk::kCartDoF> {
false,
false,
true,
false,
false,
false});
273 std::array<double, rdk::kCartDoF> inf;
274 inf.fill(std::numeric_limits<double>::infinity());
283 scheduler.
AddTask(std::bind(PeriodicTask, std::ref(robot), std::ref(init_pose),
284 std::ref(force_ctrl_frame), enable_polish),
290 while (!g_stop_sched) {
291 std::this_thread::sleep_for(std::chrono::milliseconds(1));
296 }
catch (
const std::exception& e) {
297 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.
void SetMaxContactWrench(const std::array< double, kCartDoF > &max_wrench)
[Blocking] Set maximum contact wrench for the motion control part of the Cartesian motion-force contr...
const RobotStates states() const
[Non-blocking] Current states data of the robot.
void ExecutePrimitive(const std::string &primitive_name, const std::map< std::string, FlexivDataTypes > &input_params, const std::map< std::string, FlexivDataTypes > &properties={}, bool block_until_started=true)
[Blocking] Execute a primitive by specifying its name and parameters, which can be found in the Flexi...
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 SetForceControlFrame(CoordType root_coord, const std::array< double, kPoseSize > &T_in_root={0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0})
[Blocking] Set reference frame for force control while in the Cartesian motion-force control modes....
void SwitchMode(Mode mode)
[Blocking] Switch to a new control mode and wait until mode transition is finished.
void SetForceControlAxis(const std::array< bool, kCartDoF > &enabled_axes, const std::array< double, kCartDoF/2 > &max_linear_vel={1.0, 1.0, 1.0})
[Blocking] Set Cartesian axes to enable force control while in the Cartesian motion-force control mod...
void SendCartesianMotionForce(const std::array< double, kPoseSize > &pose, const std::array< double, kCartDoF > &wrench={}, double max_linear_vel=0.5, double max_angular_vel=1.0, double max_linear_acc=2.0, double max_angular_acc=5.0)
[Non-blocking] Discretely send Cartesian motion and/or force command for the robot to track using its...
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...
CoordType
Type of commonly-used reference coordinates.
std::array< double, kCartDoF > ext_wrench_in_world
std::array< double, kPoseSize > tcp_pose