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>
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;
}
{
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 ||
flexiv::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.");
try {
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(
flexiv::rdk::Mode::NRT_PLAN_EXECUTION);
robot.ExecutePlan("PLAN-Home");
std::this_thread::sleep_for(std::chrono::seconds(1));
spdlog::info("Initializing gripper, this process takes about 10 seconds ...");
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");
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");
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");
std::this_thread::sleep_for(std::chrono::seconds(10));
}
g_finished = true;
spdlog::info("Program finished");
print_thread.join();
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}
Interface with the robot gripper.
const GripperStates states() const
[Non-blocking] Access the current gripper states.
void Stop()
[Blocking] Stop the gripper.
bool moving() const
[Non-blocking] Whether the gripper fingers are moving.
void Move(double width, double velocity, double force_limit=0)
[Non-blocking] Move the gripper fingers with position control.
void Init()
[Blocking] Initialize the gripper.
void Grasp(double force)
[Non-blocking] Grasp with direct force control. Requires the mounted gripper to support direct force ...
Main interface with the robot, containing several function categories and background services.