libfranka 最小示例:如何查询 q(位姿)

此最小示例使用 libfranka 查询 Franka 机器人的当前关节位姿。

franka_q.cpp
#include <franka/robot.h>
#include <franka/robot_state.h>
#include <franka/exception.h>
#include <iostream>
#include <array>
#include <chrono>
#include <iostream>
#include <thread>
#include <iomanip>

using std::cout, std::endl;

int main() {
  try {
    // Connect to the robot.
    franka::Robot robot("192.168.0.1");  // Replace with your robot's IP address.

    // Continuously query and print the robot pose.
    while (true) {
      // Get the current robot state.
      franka::RobotState state = robot.readOnce();

      // Extract and print the pose of the end-effector.
      // Use fixed width to align all columns
      cout << "q =" << std::fixed << std::setprecision(5);
      for (size_t i = 0; i < state.q.size(); i++)
      {
        cout << state.q.at(i) << " ";
      }
      cout << endl;
      // Add a small delay to avoid excessive output.
      std::this_thread::sleep_for(std::chrono::milliseconds(1));
    }
  } catch (const franka::Exception& ex) {
    // Handle exceptions thrown by the robot control.
    std::cerr << "Exception: " << ex.what() << std::endl;
    return -1;
  }

  return 0;
}

如何编译

假设 libfranka 已系统安装,你可以使用以下命令编译此示例:

build_franka_q.sh
g++ -o franka_q franka_q.cpp -lfranka

使用以下命令运行

run_franka_q.sh
./franka_q

示例输出

franka_q_output.txt
q =-0.04501 -0.75216 -0.01165 -2.35292 -0.07323 1.57705 0.79462
[...]

Check out similar posts by category: Robotics