This tutorial runs the integrated dynamics engine to obtain robot Jacobian, mass matrix, and gravity torques. Also checks reachability of a Cartesian pose.
#include <spdlog/spdlog.h>
#include <iostream>
#include <iomanip>
#include <thread>
#include <chrono>
#include <mutex>
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 runs the integrated dynamics engine to obtain "
"robot Jacobian, mass matrix, and gravity torques. Also checks reachability of a Cartesian "
"pose.\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");
spdlog::info("Moving to home pose");
robot.Home();
rdk::Model model(robot);
for (size_t i = 0; i < 5; i++) {
auto tic = std::chrono::high_resolution_clock::now();
const auto robot_states = robot.states();
model.Update(robot_states.at(
rdk::JointGroup::ALL).q,
robot_states.at(rdk::JointGroup::ALL).dq);
auto g = model.g();
auto M = model.M();
auto J = model.J("flange");
auto toc = std::chrono::high_resolution_clock::now();
auto computation_time
= std::chrono::duration_cast<std::chrono::microseconds>(toc - tic).count();
spdlog::info("Computation time = {} us", computation_time);
std::cout << "g = \n"
<< std::fixed << std::setprecision(5) << g.transpose() << std::endl;
std::cout << "M = \n" << std::fixed << std::setprecision(5) << M << std::endl;
std::cout << "J = \n" << std::fixed << std::setprecision(5) << J << std::endl;
std::cout << std::endl;
}
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");
}
const auto robot_states = robot.states();
std::map<rdk::JointGroup, rdk::IKParams> ik_params_by_group;
for (const auto& [group, _] : single_arm_groups) {
auto pose_to_check = robot_states.at(group).tcp_pose;
pose_to_check[0] += 0.1;
spdlog::info("[{}] Checking IK feasibility of Cartesian pose [{}]",
rdk::kJointGroupNames.at(group),
rdk::utility::Arr2Str(pose_to_check));
rdk::IKParams ik_params;
ik_params.cartesian_pose = pose_to_check;
ik_params.seed_q = robot_states.at(group).q;
ik_params.free_orientation = false;
ik_params_by_group[group] = ik_params;
}
auto result = model.SolveConstrainedIK(ik_params_by_group);
spdlog::info("IK result success = {}", result.success);
for (const auto& [group, q] : result.solved_q) {
spdlog::info(
"[{}] solved_q = [{}]", rdk::kJointGroupNames.at(group),
rdk::utility::Vec2Str(q));
}
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}