有多少人工,就有多少智能

Franka Robot demo 真空夹抓控制示例(vacuum_object.cpp)

// Copyright (c) 2019 Franka Robotics GmbH
// Use of this source code is governed by the Apache-2.0 license, see LICENSE
#include <iostream>
#include <thread>

#include <franka/exception.h>
#include <franka/vacuum_gripper.h>

/**
 * @example vacuum_object.cpp
 * An example showing how to control FRANKA's vacuum gripper.
 */
/**
 * @example vacuum_object.cpp
 * 控制FRANKA真空夹爪的示例。
 */

int main(int argc, char** argv) {
  if (argc != 2) {
    std::cerr << "Usage: ./vacuum_object <vacuum-gripper-hostname>" << std::endl;
    return -1;
  }

  franka::VacuumGripper vacuum_gripper(argv[1]);// 连接真空夹爪
  try {
    // Print a vacuum gripper state.
    franka::VacuumGripperState vacuum_gripper_state = vacuum_gripper.readOnce();// 读取真空夹爪状态
    std::cout << "Initial vacuum gripper state: " << vacuum_gripper_state << std::endl; 

    // Vacuum the object.
    if (!vacuum_gripper.vacuum(100, std::chrono::milliseconds(1000))) {// 真空夹爪 超时时间为1s
      std::cout << "Failed to vacuum the object." << std::endl;
      return -1;
    }

    vacuum_gripper_state = vacuum_gripper.readOnce();// 读取真空夹爪状态
    std::cout << "Vacuum gripper state after applying vacuum: " << vacuum_gripper_state
              << std::endl;

    // Wait 3s and check afterwards, if the object is still grasped.
    std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(3000));// 延迟3s

    vacuum_gripper_state = vacuum_gripper.readOnce(); // 读取夹爪状态
    if (!vacuum_gripper_state.in_control_range) { // 判断是否在控制范围
      std::cout << "Object lost." << std::endl;
      return -1;
    }

    std::cout << "Vacuumed object, will release it now." << std::endl;
    vacuum_gripper.dropOff(std::chrono::milliseconds(1000));// 夹爪释放
  } catch (franka::Exception const& e) {
    vacuum_gripper.stop(); // 夹爪停止动作
    std::cout << e.what() << std::endl;
    return -1;
  }

  return 0;
}

功能解释:

  • 命令行参数检查:确保命令行参数个数为 2,若不是则输出正确的使用方式并返回 -1
  • 连接真空夹爪:通过指定的主机名连接 FRANKA 的真空夹爪。
  • 真空夹爪操作
    • 读取初始状态:使用 vacuum_gripper.readOnce() 读取并打印真空夹爪的初始状态。
    • 启动真空吸附:使用 vacuum_gripper.vacuum(100, std::chrono::milliseconds(1000)) 启动真空吸附操作,参数设置了真空值和持续时间。
    • 状态检查:在真空吸附一定时间后,通过 vacuum_gripper.readOnce() 读取夹爪状态,检查物体是否仍在夹持。
    • 释放操作:如果物体仍在夹持状态,则使用 vacuum_gripper.dropOff(std::chrono::milliseconds(1000)) 进行释放操作。
  • 异常处理:使用 try-catch 块捕获可能抛出的 franka::Exception 异常,停止夹爪动作并输出异常信息。

这段示例代码演示了如何使用 FRANKA 真空夹爪库控制真空吸附和释放操作,通过读取夹爪状态来确认物体是否在正确位置并进行相应处理。

posted @ 2024-07-10 19:16  lvdongjie-avatarx  阅读(2)  评论(0编辑  收藏  举报