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 to. "
"Remove any space, for example: Rizon4s-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("Enabling robot ...");
robot.Enable();
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.SwitchMode(
rdk::Mode::NRT_PLAN_EXECUTION);
robot.ExecutePlan("PLAN-Home");
while (robot.busy()) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
rdk::Model model(robot);
for (size_t i = 0; i < 5; i++) {
auto tic = std::chrono::high_resolution_clock::now();
model.Update(robot.states().q, robot.states().dtheta);
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;
}
auto pose_to_check = robot.states().tcp_pose;
pose_to_check[0] += 0.1;
spdlog::info(
"Checking reachability of Cartesian pose [{}]",
rdk::utility::Arr2Str(pose_to_check));
auto result = model.reachable(pose_to_check, robot.states().q, true);
spdlog::info("Got a result: reachable = {}, IK solution = [{}]", result.first,
rdk::utility::Vec2Str(result.second));
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}