This tutorial runs real-time Cartesian-space pure motion control to hold or sine-sweep the robot TCP. A simple collision detection is also included.
#include <iostream>
#include <cmath>
#include <thread>
#include <atomic>
namespace {
constexpr size_t k_loopFreq = 1000;
constexpr double k_loopPeriod = 0.001;
constexpr double k_swingAmp = 0.1;
constexpr double k_swingFreq = 0.3;
constexpr double k_extForceThreshold = 10.0;
constexpr double k_extTorqueThreshold = 5.0;
std::atomic<bool> g_schedStop = {false};
}
void printDescription()
{
std::cout << "This tutorial runs real-time Cartesian-space pure motion control to hold or "
"sine-sweep the robot TCP. A simple collision detection is also included."
<< 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: [--hold] [--collision]" << std::endl;
std::cout << " --hold: robot holds current TCP pose, otherwise do a sine-sweep" << std::endl;
std::cout << " --collision: enable collision detection, robot will stop upon collision" << std::endl;
std::cout << std::endl;
}
const std::vector<double>& initPose, bool enableHold, bool enableCollision)
{
static uint64_t loopCounter = 0;
try {
"periodicTask: Fault occurred on robot server, exiting ...");
}
auto targetTcpPose = initPose;
if (!enableHold) {
targetTcpPose[1]
= initPose[1]
+ k_swingAmp * sin(2 * M_PI * k_swingFreq * loopCounter * k_loopPeriod);
}
switch (loopCounter % (20 * k_loopFreq)) {
case (3 * k_loopFreq): {
std::vector<double> preferredJntPos
= {0.938, -1.108, -1.254, 1.464, 1.073, 0.278, -0.658};
log.
info(
"Preferred joint positions set to: "
+
flexiv::utility::vec2Str(preferredJntPos));
} break;
case (6 * k_loopFreq): {
for (auto& v : newK) {
v *= 0.5;
}
log.
info(
"Cartesian stiffness set to: " + flexiv::utility::vec2Str(newK));
} break;
case (9 * k_loopFreq): {
std::vector<double> preferredJntPos
= {-0.938, -1.108, 1.254, 1.464, -1.073, 0.278, 0.658};
log.
info(
"Preferred joint positions set to: "
+ flexiv::utility::vec2Str(preferredJntPos));
} break;
case (12 * k_loopFreq): {
log.
info(
"Cartesian stiffness is reset");
} break;
case (14 * k_loopFreq): {
log.
info(
"Preferred joint positions are reset");
} break;
case (16 * k_loopFreq): {
std::vector<double> maxWrench = {10.0, 10.0, 10.0, 2.0, 2.0, 2.0};
log.
info(
"Max contact wrench set to: " + flexiv::utility::vec2Str(maxWrench));
} break;
case (19 * k_loopFreq): {
log.
info(
"Max contact wrench is reset");
} break;
default:
break;
}
if (enableCollision) {
bool collisionDetected = false;
if (extForce.norm() > k_extForceThreshold) {
collisionDetected = true;
}
for (
const auto& v : robotStates.
tauExt) {
if (fabs(v) > k_extTorqueThreshold) {
collisionDetected = true;
}
}
if (collisionDetected) {
log.
warn(
"Collision detected, stopping robot and exit program ...");
g_schedStop = true;
}
}
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();
bool enableHold = false;
if (
flexiv::utility::programArgsExist(argc, argv,
"--hold")) {
log.
info(
"Robot holding current TCP pose");
enableHold = true;
} else {
log.
info(
"Robot running TCP sine-sweep");
}
bool enableCollision = false;
if (flexiv::utility::programArgsExist(argc, argv, "--collision")) {
log.
info(
"Collision detection enabled");
enableCollision = true;
} else {
log.
info(
"Collision detection disabled");
}
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");
robot.
setMode(
flexiv::Mode::RT_CARTESIAN_MOTION_FORCE);
auto initPose = robotStates.
tcpPose;
log.
info(
"Initial TCP pose set to [position 3x1, rotation (quaternion) 4x1]: "
+ flexiv::utility::vec2Str(initPose));
std::bind(periodicTask, std::ref(robot), std::ref(log), std::ref(robotStates),
std::ref(initPose), enableHold, enableCollision),
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 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.
void resetCartesianStiffness(void)
[Non-blocking] Reset motion stiffness for the Cartesian motion-force control modes to nominal value.
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.
void setCartesianStiffness(const std::vector< double > &stiffness)
[Non-blocking] Set motion stiffness for the Cartesian motion-force control modes.
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...
const RobotInfo info(void)
[Non-blocking] Access general information of the robot.
void resetNullSpacePosture(void)
[Non-blocking] Reset preferred joint positions to the ones automatically recorded when entering the a...
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...
void setNullSpacePosture(const std::vector< double > &preferredPositions)
[Non-blocking] Set preferred joint positions for the null-space posture control module used in the Ca...
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.
std::vector< double > nominalK
Data struct containing the joint- and Cartesian-space robot states.
std::vector< double > tauExt
std::vector< double > tcpPose
std::vector< double > extWrenchInBase