This tutorial zeros the robot's force and torque sensors, which is a recommended (but not mandatory) step before any operations that require accurate force/torque measurement.
#include <spdlog/spdlog.h>
#include <iostream>
#include <thread>
#include <algorithm>
using namespace flexiv;
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: None" << std::endl;
std::cout << std::endl;
}
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 zeros the robot's force and torque sensors, "
"which is a recommended (but not mandatory) step before any operations that require "
"accurate force/torque measurement.\n");
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");
for (const auto& [group, states] : robot.states()) {
spdlog::info(
"[{}] TCP force and moment reading in world frame BEFORE sensor zeroing: {} N-Nm",
rdk::kJointGroupNames.at(group),
rdk::utility::Arr2Str(states.tcp_wrench));
}
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");
for (const auto& [group, _] : single_arm_groups) {
spdlog::info(
"[{}] TCP force and moment reading in world frame AFTER sensor zeroing: {} N-Nm",
rdk::kJointGroupNames.at(group),
rdk::utility::Arr2Str(robot.states().at(group).tcp_wrench));
}
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}