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 <iostream>
#include <cmath>
#include <thread>
#include <atomic>
namespace {
constexpr double k_loopPeriod = 0.001;
constexpr double k_swingAmp = 0.1;
constexpr double k_swingFreq = 0.3;
constexpr double k_pressingForce = 5.0;
constexpr double k_searchVelocity = 0.02;
constexpr double k_searchDistance = 1.0;
const std::vector<double> k_maxWrenchForContactSearch = {10.0, 10.0, 10.0, 3.0, 3.0, 3.0};
std::atomic<bool> g_schedStop = {false};
}
void printDescription()
{
std::cout << "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."
<< std::endl
<< std::endl;
}
void printHelp()
{
std::cout << "Required arguments: [robot IP] [local IP]" << std::endl;
std::cout << " robot IP: address of the robot server" << std::endl;
std::cout << " local IP: address of this PC" << std::endl;
std::cout << "Optional arguments: [--TCP] [--polish]" << std::endl;
std::cout << " --TCP: use TCP frame as reference frame for force control, otherwise use base frame" << std::endl;
std::cout << " --polish: run a simple polish motion along XY plane in base frame, otherwise hold robot motion in non-force-control axes"
<< std::endl
<< std::endl;
}
const std::string frameStr, bool enablePolish)
{
static uint64_t loopCounter = 0;
try {
"periodicTask: Fault occurred on robot server, exiting ...");
}
std::vector<double> targetPose = initPose;
double Fz = 0.0;
if (frameStr == "BASE") {
Fz = -k_pressingForce;
} else if (frameStr == "TCP") {
Fz = k_pressingForce;
}
std::vector<double> targetWrench = {0.0, 0.0, Fz, 0.0, 0.0, 0.0};
if (enablePolish) {
targetPose[1] = initPose[1]
+ k_swingAmp * sin(2 * M_PI * k_swingFreq * loopCounter * k_loopPeriod);
}
else {
}
loopCounter++;
g_schedStop = true;
}
}
int main(int argc, char* argv[])
{
if (argc < 3 ||
flexiv::utility::programArgsExistAny(argc, argv, {
"-h",
"--help"})) {
printHelp();
return 1;
}
std::string robotIP = argv[1];
std::string localIP = argv[2];
log.
info(
"Tutorial description:");
printDescription();
std::string frameStr = "BASE";
if (
flexiv::utility::programArgsExist(argc, argv,
"--TCP")) {
log.
info(
"Reference frame used for force control: robot TCP frame");
frameStr = "TCP";
} else {
log.
info(
"Reference frame used for force control: robot base frame");
}
bool enablePolish = false;
if (flexiv::utility::programArgsExist(argc, argv, "--polish")) {
log.
info(
"Robot will run a polish motion along XY plane in robot base frame");
enablePolish = true;
} else {
log.
info(
"Robot will hold its motion in all non-force-controlled axes");
}
try {
log.
warn(
"Fault occurred on robot server, trying to clear ...");
std::this_thread::sleep_for(std::chrono::seconds(2));
log.
error(
"Fault cannot be cleared, exiting ...");
return 1;
}
log.
info(
"Fault on robot server is cleared");
}
log.
info(
"Enabling robot ...");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
log.
info(
"Robot is now operational");
log.
info(
"Moving to home pose");
robot.
setMode(
flexiv::Mode::NRT_PRIMITIVE_EXECUTION);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
log.
warn(
"Zeroing force/torque sensors, make sure nothing is in contact with the robot");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
log.
info(
"Sensor zeroing complete");
log.
info(
"Searching for contact ...");
std::vector<double> initPose = robotStates.
tcpPose;
log.
info(
"Initial TCP pose set to [position 3x1, rotation (quaternion) 4x1]: "
+
flexiv::utility::vec2Str(initPose));
robot.
setMode(
flexiv::Mode::NRT_CARTESIAN_MOTION_FORCE);
auto targetPose = initPose;
targetPose[2] -= k_searchDistance;
bool isContacted = false;
while (!isContacted) {
if (extForce.norm() > k_pressingForce) {
isContacted = true;
log.
info(
"Contact detected at robot TCP");
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
robot.
setMode(
flexiv::Mode::RT_CARTESIAN_MOTION_FORCE);
scheduler.
addTask(std::bind(periodicTask, std::ref(robot), std::ref(log),
std::ref(initPose), std::ref(frameStr), enablePolish),
while (!g_schedStop) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
std::this_thread::sleep_for(std::chrono::seconds(2));
return 1;
}
return 0;
}
Base class for all custom runtime exception classes.
Helper functions to print messages with timestamp and coloring. Logging raw data to csv file coming s...
void warn(const std::string &message) const
[Non-blocking] Print warning message with timestamp and coloring.
void info(const std::string &message) const
[Non-blocking] Print info message with timestamp and coloring.
void error(const std::string &message) const
[Non-blocking] Print error message with timestamp and coloring.
Main interface with the robot, containing several function categories and background services.
void setForceControlFrame(const std::string &referenceFrame)
[Blocking] Set force control reference frame for the Cartesian motion-force control modes....
void stop(void)
[Blocking] Stop the robot and transit robot mode to Idle.
void resetMaxContactWrench(void)
[Non-blocking] Reset max contact wrench regulation to nominal state, i.e. disabled.
void clearFault(void)
[Blocking] Clear minor fault of the robot.
void executePrimitive(const std::string &ptCmd)
[Blocking] Execute a primitive by specifying its name and parameters, which can be found in the Flexi...
void getRobotStates(RobotStates &output)
[Non-blocking] Get the latest robot states.
bool isFault(void) const
[Non-blocking] Check if the robot is in fault state.
void setMode(Mode mode)
[Blocking] Set a new control mode to the robot and wait until the mode transition is finished.
bool isBusy(void) const
[Non-blocking] Check if the robot is currently executing a task. This includes any user commanded ope...
void streamCartesianMotionForce(const std::vector< double > &pose, const std::vector< double > &wrench=std::vector< double >(6))
[Non-blocking] Continuously stream Cartesian motion and/or force command for the robot to track using...
void setMaxContactWrench(const std::vector< double > &maxWrench)
[Non-blocking] Set maximum contact wrench for the motion control part of the Cartesian motion-force c...
void sendCartesianMotionForce(const std::vector< double > &pose, const std::vector< double > &wrench=std::vector< double >(6), double maxLinearVel=0.5, double maxAngularVel=1.0)
[Non-blocking] Discretely send Cartesian motion and/or force command for the robot to track using its...
void setForceControlAxis(const std::vector< bool > &enabledAxis)
[Blocking] Set force-controlled Cartesian axis(s) for the Cartesian motion-force control modes....
void enable(void)
[Blocking] Enable the robot, if E-stop is released and there's no fault, the robot will release brake...
bool isOperational(bool verbose=true) const
[Non-blocking] Check if the robot is normally operational, which requires the following conditions to...
Real-time scheduler that can simultaneously run multiple periodic tasks. Parameters for each task are...
void stop()
[Blocking] Stop all added tasks. The periodic execution will stop and all task threads will be closed...
void addTask(std::function< void(void)> &&callback, const std::string &taskName, int interval, int priority, int cpuAffinity=-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...
int maxPriority() const
[Non-blocking] Get maximum available priority for user tasks.
Thrown if the robot server is not operational or has fault.
Data struct containing the joint- and Cartesian-space robot states.
std::vector< double > tcpPose
std::vector< double > extWrenchInBase