Flexiv RDK APIs  2.1
basics1_display_robot_states.cpp

This tutorial does the very first thing: check connection with the robot server and print received robot states.

Author
Flexiv
#include <spdlog/spdlog.h>
#include <iostream>
#include <thread>
using namespace flexiv;
void PrintHelp()
{
// clang-format off
std::cout << "Required arguments: [robot_sn]" << std::endl;
std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Enlight-L-123456" << std::endl;
std::cout << "Optional arguments: None" << std::endl;
std::cout << std::endl;
// clang-format on
}
void PrintRobotStates(rdk::Robot& robot)
{
while (true) {
// Print Available joint groups
std::string joint_groups_str;
for (const auto& [_, name] : robot.info().all_groups) {
joint_groups_str += "[" + name + "] ";
}
spdlog::info("Available joint groups: {}", joint_groups_str);
// Print all robot states in JSON format using the built-in ostream operator overloading
for (const auto& [group, states] : robot.states()) {
spdlog::info("[{}] robot states:", rdk::kJointGroupNames.at(group));
std::cout << states << std::endl;
}
// Print all robot actions in JSON format using the built-in ostream operator overloading
for (const auto& [group, actions] : robot.actions()) {
spdlog::info("[{}] robot actions:", rdk::kJointGroupNames.at(group));
std::cout << actions << std::endl;
}
// Print digital inputs and outputs
spdlog::info("Digital inputs:");
std::cout << rdk::utility::Arr2Str(robot.digital_inputs()) << std::endl;
spdlog::info("Digital outputs:");
std::cout << rdk::utility::Arr2Str(robot.digital_outputs()) << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
int main(int argc, char* argv[])
{
// Program Setup
// =============================================================================================
// Parse parameters
if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
PrintHelp();
return 1;
}
// Serial number of the robot to connect to
std::string robot_sn = argv[1];
// Print description
spdlog::info(
">>> Tutorial description <<<\nThis tutorial does the very first thing: check connection "
"with the robot server and print received robot states.\n");
try {
// RDK Initialization
// =========================================================================================
// Instantiate robot interface
rdk::Robot robot(robot_sn);
// Clear fault on the connected robot if any
if (robot.fault()) {
spdlog::warn("Fault occurred on the connected robot, trying to clear ...");
// Try to clear the fault
if (!robot.ClearFault()) {
spdlog::error("Fault cannot be cleared, exiting ...");
return 1;
}
spdlog::info("Fault on the connected robot is cleared");
}
// Servo on the robot, make sure the E-stop is released
spdlog::info("Servo on the robot ...");
robot.ServoOn();
// Wait for the robot to become operational
while (!robot.operational()) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
spdlog::info("Robot is now operational");
// Print States
// =========================================================================================
// Use std::thread to do scheduling so that this example can run on all OS, since not all OS
// support rdk::Scheduler
std::thread low_priority_thread(std::bind(PrintRobotStates, std::ref(robot)));
// Properly exit thread
low_priority_thread.join();
} catch (const std::exception& e) {
spdlog::error(e.what());
return 1;
}
return 0;
}