This tutorial runs real-time Cartesian-space unified motion-force control. The Z axis of the chosen reference frame will be activated for explicit force control, while the rest axes in the same reference frame will stay motion controlled.
#include <spdlog/spdlog.h>
#include <iostream>
#include <cmath>
#include <thread>
#include <atomic>
#include <algorithm>
using namespace flexiv;
namespace {
constexpr double kLoopPeriod = 0.001;
constexpr double kSwingAmp = 0.1;
constexpr double kSwingFreq = 0.3;
constexpr double kPressingForce = 5.0;
constexpr double kSearchVelocity = 0.02;
constexpr double kSearchDistance = 1.0;
const std::array<double, rdk::kCartDoF> kMaxWrenchForContactSearch
= {10.0, 10.0, 10.0, 3.0, 3.0, 3.0};
std::atomic<bool> g_stop_sched = {false};
}
void PrintHelp()
{
std::cout << "Required arguments: [robot_sn]" << std::endl;
std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Enlight-L-123456" << std::endl;
std::cout << "Optional arguments: [--TCP] [--polish]" << std::endl;
std::cout << " --TCP: use TCP frame as reference frame for force control, otherwise use world frame" << std::endl;
std::cout << " --polish: run a simple polish motion along XY plane in world frame, otherwise hold robot motion in non-force-control axes"
<< std::endl
<< std::endl;
}
void PeriodicTask(rdk::Robot& robot,
const std::map<
rdk::JointGroup, std::array<double, rdk::kPoseSize>>& all_init_pose,
rdk::CoordType force_ctrl_frame,
bool enable_polish)
{
static uint64_t loop_counter = 0;
try {
if (robot.fault()) {
throw std::runtime_error(
"PeriodicTask: Fault occurred on the connected robot, exiting ...");
}
double Fz = 0.0;
if (force_ctrl_frame ==
rdk::CoordType::WORLD) {
Fz = kPressingForce;
}
else if (force_ctrl_frame ==
rdk::CoordType::TCP) {
Fz = -kPressingForce;
}
std::array<double, rdk::kCartDoF> target_wrench = {0.0, 0.0, Fz, 0.0, 0.0, 0.0};
std::map<rdk::JointGroup, rdk::RtCartesianCmd> rt_cmds;
for (const auto& [group, init_pose] : all_init_pose) {
auto target_pose = init_pose;
if (enable_polish) {
target_pose[1]
= init_pose[1]
+ kSwingAmp * sin(2 * M_PI * kSwingFreq * loop_counter * kLoopPeriod);
}
rt_cmds[group] = rdk::RtCartesianCmd(target_pose, target_wrench);
}
robot.StreamCartesianMotionForce(rt_cmds);
loop_counter++;
} catch (const std::exception& e) {
spdlog::error(e.what());
g_stop_sched = true;
}
}
int main(int argc, char* argv[])
{
if (argc < 2 ||
rdk::utility::ProgramArgsExistAny(argc, argv, {
"-h",
"--help"})) {
PrintHelp();
return 1;
}
std::string robot_sn = argv[1];
spdlog::info(
">>> Tutorial description <<<\nThis tutorial runs real-time Cartesian-space unified "
"motion-force control. The Z axis of the chosen reference frame will be activated for "
"explicit force control, while the rest axes in the same reference frame will stay motion "
"controlled.\n");
auto force_ctrl_frame = rdk::CoordType::WORLD;
if (
rdk::utility::ProgramArgsExist(argc, argv,
"--TCP")) {
spdlog::info("Reference frame used for force control: robot TCP frame");
force_ctrl_frame = rdk::CoordType::TCP;
} else {
spdlog::info("Reference frame used for force control: robot world frame");
}
bool enable_polish = false;
if (rdk::utility::ProgramArgsExist(argc, argv, "--polish")) {
spdlog::info("Robot will run a polish motion along XY plane in robot world frame");
enable_polish = true;
} else {
spdlog::info("Robot will hold its motion in all non-force-controlled axes");
}
try {
rdk::Robot robot(robot_sn);
if (robot.fault()) {
spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
if (!robot.ClearFault()) {
spdlog::error("Fault cannot be cleared, exiting ...");
return 1;
}
spdlog::info("Fault on the connected robot is cleared");
}
spdlog::info("Servo on the robot ...");
robot.ServoOn();
while (!robot.operational()) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
spdlog::info("Robot is now operational");
spdlog::info("Moving to home pose");
robot.Home();
const auto& single_arm_groups = robot.info().single_arm_groups;
if (single_arm_groups.empty()) {
throw std::runtime_error("No single-arm joint group found on the connected robot");
}
robot.SwitchMode(
rdk::Mode::NRT_PRIMITIVE_EXECUTION);
std::map<rdk::JointGroup, rdk::PrimitiveArgs> pt_args;
for (const auto& [group, _] : single_arm_groups) {
pt_args[group] = rdk::PrimitiveArgs("ZeroFTSensor", {});
}
robot.ExecutePrimitive(pt_args);
spdlog::warn(
"Zeroing force/torque sensors, make sure nothing is in contact with the robot");
while (!
rdk::utility::PrimitiveStateTrueForGroups(robot.primitive_states(),
"terminated")) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
spdlog::info("Sensor zeroing complete");
spdlog::info("Searching for contact ...");
std::map<rdk::JointGroup, std::array<double, rdk::kPoseSize>> all_init_pose;
const auto robot_states = robot.states();
for (const auto& [group, _] : single_arm_groups) {
all_init_pose[group] = robot_states.at(group).tcp_pose;
spdlog::info("[{}] Initial TCP pose [position 3x1, rotation (quaternion) 4x1]: {}",
rdk::kJointGroupNames.at(group),
rdk::utility::Arr2Str(all_init_pose.at(group)));
}
robot.SwitchMode(
rdk::Mode::NRT_CARTESIAN_MOTION_FORCE);
for (const auto& [group, _] : single_arm_groups) {
robot.SetMaxContactWrench(group, kMaxWrenchForContactSearch);
}
std::map<rdk::JointGroup, rdk::NrtCartesianCmd> nrt_cmds;
for (const auto& [group, init_pose] : all_init_pose) {
auto target_pose = init_pose;
target_pose[2] -= kSearchDistance;
nrt_cmds[group] = rdk::NrtCartesianCmd(target_pose, {}, {}, kSearchVelocity);
}
robot.SendCartesianMotionForce(nrt_cmds);
bool is_contacted = false;
while (!is_contacted) {
for (const auto& [group, states] : robot.states()) {
Eigen::Vector3d ext_force
= {states.tcp_wrench[0], states.tcp_wrench[1], states.tcp_wrench[2]};
if (ext_force.norm() > kPressingForce) {
is_contacted = true;
spdlog::info(
"[{}] Contact detected at robot TCP", rdk::kJointGroupNames.at(group));
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
robot.SwitchMode(
rdk::Mode::RT_CARTESIAN_MOTION_FORCE);
for (const auto& [group, _] : single_arm_groups) {
robot.SetForceControlFrame(group, force_ctrl_frame);
}
for (const auto& [group, _] : single_arm_groups) {
robot.SetForceControlAxis(
group, std::array<bool, rdk::kCartDoF> {false, false, true, false, false, false});
}
std::array<double, rdk::kCartDoF> inf;
inf.fill(std::numeric_limits<double>::infinity());
for (const auto& [group, _] : single_arm_groups) {
robot.SetMaxContactWrench(group, inf);
}
for (auto& [group, pose] : all_init_pose) {
pose = robot.states().at(group).tcp_pose;
}
rdk::Scheduler scheduler;
scheduler.AddTask(std::bind(PeriodicTask, std::ref(robot), std::cref(all_init_pose),
std::ref(force_ctrl_frame), enable_polish),
"HP periodic", 1, scheduler.max_priority());
scheduler.Start();
while (!g_stop_sched) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
scheduler.Stop();
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}