This tutorial executes several basic robot primitives (unit skills). For detailed documentation on all available primitives, please see Flexiv Primitives.
#include <spdlog/spdlog.h>
#include <iostream>
#include <iomanip>
#include <thread>
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 executes several basic robot primitives (unit "
"skills). For detailed documentation on all available primitives, please see [Flexiv "
"Primitives](https://www.flexiv.com/primitives/).\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");
robot.SwitchMode(
rdk::Mode::NRT_PRIMITIVE_EXECUTION);
spdlog::info("Executing primitive: Home");
robot.ExecutePrimitive("Home", std::map<std::string, rdk::FlexivDataTypes> {});
while (!std::get<int>(robot.primitive_states()["reachedTarget"])) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
spdlog::info("Executing primitive: MoveJ");
robot.ExecutePrimitive(
"MoveJ", {{"target", std::vector<double> {30, -45, 0, 90, 0, 40, 30}}});
while (!std::get<int>(robot.primitive_states()["reachedTarget"])) {
spdlog::info("Current primitive states:");
for (const auto& st : robot.primitive_states()) {
std::cout << st.first <<
": " <<
rdk::utility::FlexivTypes2Str(st.second);
std::cout << std::endl;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
spdlog::info("Executing primitive: MoveL");
robot.ExecutePrimitive("MoveL",
{
{"target", rdk::Coord({0.65, -0.3, 0.2}, {180, 0, 180}, {"WORLD", "WORLD_ORIGIN"})},
{"waypoints",
std::vector<rdk::Coord> {
rdk::Coord({0.45, 0.1, 0.2}, {180, 0, 180}, {"WORLD", "WORLD_ORIGIN"}),
rdk::Coord({0.45, -0.3, 0.2}, {180, 0, 180}, {"WORLD", "WORLD_ORIGIN"})}},
{"vel", 0.6},
{"zoneRadius", "Z50"},
});
while (!std::get<int>(robot.primitive_states()["reachedTarget"])) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
spdlog::info("Executing primitive: MoveL");
std::array<double, 4> targetQuat = {0.9185587, 0.1767767, 0.3061862, 0.1767767};
auto targetEulerDeg =
rdk::utility::Rad2Deg(
rdk::utility::Quat2EulerZYX(targetQuat));
robot.ExecutePrimitive(
"MoveL", {
{"target", rdk::Coord({0.0, 0.0, 0.0}, targetEulerDeg, {"TRAJ", "START"})},
{"vel", 0.2},
});
while (!std::get<int>(robot.primitive_states()["reachedTarget"])) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
robot.Stop();
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}