This tutorial does position and force (if available) control of grippers supported by Flexiv.
#include <spdlog/spdlog.h>
#include <iostream>
#include <string>
#include <thread>
#include <atomic>
using namespace flexiv;
namespace {
std::atomic<bool> g_finished = {false};
}
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;
}
void PrintGripperStates(rdk::Gripper& gripper)
{
while (!g_finished) {
spdlog::info("Current gripper states:");
std::cout << gripper.states() << std::endl;
std::cout << "moving: " << gripper.moving() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
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 does position and force (if available) "
"control of grippers supported by Flexiv.\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);
robot.ExecutePlan("PLAN-Home");
std::this_thread::sleep_for(std::chrono::seconds(1));
rdk::Gripper gripper(robot);
spdlog::info("Initializing gripper, this process takes about 10 seconds ...");
gripper.Init();
spdlog::info("Initialization complete");
std::thread print_thread(PrintGripperStates, std::ref(gripper));
spdlog::info("Closing gripper");
gripper.Move(0.01, 0.1, 20);
std::this_thread::sleep_for(std::chrono::seconds(2));
spdlog::info("Opening gripper");
gripper.Move(0.09, 0.1, 20);
std::this_thread::sleep_for(std::chrono::seconds(2));
spdlog::info("Closing gripper");
gripper.Move(0.01, 0.1, 20);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
spdlog::info("Stopping gripper");
gripper.Stop();
std::this_thread::sleep_for(std::chrono::seconds(2));
spdlog::info("Closing gripper");
gripper.Move(0.01, 0.1, 20);
std::this_thread::sleep_for(std::chrono::seconds(2));
spdlog::info("Opening gripper");
gripper.Move(0.09, 0.1, 20);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
spdlog::info("Stopping gripper");
gripper.Stop();
std::this_thread::sleep_for(std::chrono::seconds(2));
if (fabs(gripper.states().force) > std::numeric_limits<double>::epsilon()) {
spdlog::info("Gripper running zero force control");
gripper.Grasp(0);
std::this_thread::sleep_for(std::chrono::seconds(10));
}
gripper.Stop();
g_finished = true;
spdlog::info("Program finished");
print_thread.join();
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}