This tutorial executes a plan selected by the user from a list of available plans. A plan is a pre-written script to execute a series of robot primitives with pre-defined transition conditions between 2 adjacent primitives. Users can use Flexiv Elements to compose their own plan and assign to the robot, which will appear in the plan list.
#include <spdlog/spdlog.h>
#include <iostream>
#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 a plan selected by the user from a "
"list of available plans. A plan is a pre-written script to execute a series of robot "
"primitives with pre-defined transition conditions between 2 adjacent primitives. Users "
"can use Flexiv Elements to compose their own plan and assign to the robot, which will "
"appear in the plan list.\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_PLAN_EXECUTION);
while (true) {
if (robot.fault()) {
throw std::runtime_error("Fault occurred on the connected robot, exiting ...");
}
spdlog::info("Choose an action:");
std::cout << "[1] Show available plans" << std::endl;
std::cout << "[2] Execute a plan by index" << std::endl;
std::cout << "[3] Execute a plan by name" << std::endl;
std::string inputBuffer;
std::cin >> inputBuffer;
int userInput = std::stoi(inputBuffer);
switch (userInput) {
case 1: {
auto plan_list = robot.plan_list();
for (size_t i = 0; i < plan_list.size(); i++) {
std::cout << "[" << i << "] " << plan_list[i] << std::endl;
}
std::cout << std::endl;
} break;
case 2: {
spdlog::info("Enter plan index to execute:");
int index;
std::cin >> index;
robot.ExecutePlan(index, true);
while (robot.busy()) {
spdlog::info("Current plan info:");
std::cout << robot.plan_info() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
} break;
case 3: {
spdlog::info("Enter plan name to execute:");
std::string name;
std::cin >> name;
robot.ExecutePlan(name, true);
while (robot.busy()) {
spdlog::info("Current plan info:");
std::cout << robot.plan_info() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
} break;
default:
spdlog::warn("Invalid input");
break;
}
}
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}