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

Franka Robot demo 外部控制循环生成关节速度运动 (generate_joint_velocity_motion_external_control_loop.cpp)

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

#include <franka/active_control.h>
#include <franka/active_motion_generator.h>
#include <franka/exception.h>
#include <franka/robot.h>

#include "examples_common.h"

/**
 * @example generate_joint_velocity_motion_external_control_loop.cpp
 * An example showing how to generate a joint velocity motion with an external control loop.
 *
 * @warning Before executing this example, make sure there is enough space in front of the robot.
 */

/**
 * @example generate_joint_velocity_motion_external_control_loop.cpp
 * 一个演示如何通过外部控制循环生成关节速度运动的示例。
 *
 * @warning 在执行此示例之前,请确保机器人前方有足够的空间。
 */
int main(int argc, char** argv) {
  // Check whether the required arguments were passed
  if (argc != 2) {
    std::cerr << "Usage: " << argv[0] << " <robot-hostname>" << std::endl;
    return -1;
  }

  try {
    franka::Robot robot(argv[1]); // 连接机器人
    setDefaultBehavior(robot); // 设置默认行为

    // First move the robot to a suitable joint configuration
    std::array<double, 7> q_goal = {{0, -M_PI_4, 0, -3 * M_PI_4, 0, M_PI_2, M_PI_4}};
    MotionGenerator motion_generator(0.5, q_goal);
    std::cout << "WARNING: This example will move the robot! "
              << "Please make sure to have the user stop button at hand!" << std::endl
              << "Press Enter to continue..." << std::endl;
    std::cin.ignore();
    robot.control(motion_generator); // 控制机器人运动到合适的位姿
    std::cout << "Finished moving to initial joint configuration." << std::endl;

    // Set additional parameters always before the control loop, NEVER in the control loop!
    // Set collision behavior.
    robot.setCollisionBehavior(// 重设碰撞行为
        {{20.0, 20.0, 18.0, 18.0, 16.0, 14.0, 12.0}}, {{20.0, 20.0, 18.0, 18.0, 16.0, 14.0, 12.0}},
        {{20.0, 20.0, 18.0, 18.0, 16.0, 14.0, 12.0}}, {{20.0, 20.0, 18.0, 18.0, 16.0, 14.0, 12.0}},
        {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}}, {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}},
        {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}}, {{20.0, 20.0, 20.0, 25.0, 25.0, 25.0}});

    double time_max = 1.0; // 最大运行时间
    double omega_max = 1.0; // 最大运行角速度
    double time = 0.0; // 累计运行时间

    auto callback_control = [=, &time](const franka::RobotState&, // 控制循环回调
                                       franka::Duration period) -> franka::JointVelocities {
      time += period.toSec();

      double cycle = std::floor(std::pow(-1.0, (time - std::fmod(time, time_max)) / time_max));
      double omega = cycle * omega_max / 2.0 * (1.0 - std::cos(2.0 * M_PI / time_max * time));

      franka::JointVelocities velocities = {{0.0, 0.0, 0.0, omega, omega, omega, omega}};

      if (time >= 2 * time_max) {
        std::cout << std::endl << "Finished motion, shutting down example" << std::endl;
        return franka::MotionFinished(velocities);
      }
      return velocities;
    };
    
    ////// 外部控制循环
    bool motion_finished = false;
    auto active_control = robot.startJointVelocityControl(// 开启关节速度运动
        research_interface::robot::Move::ControllerMode::kJointImpedance);// 关节阻抗
    while (!motion_finished) {
      auto read_once_return = active_control->readOnce();//读取机器人状态
      auto robot_state = read_once_return.first;
      auto duration = read_once_return.second;
      auto joint_velocities = callback_control(robot_state, duration);// 生成速度
      motion_finished = joint_velocities.motion_finished;//获取完成标志
      active_control->writeOnce(joint_velocities);//写入关节速度
    }

  } catch (const franka::Exception& e) {
    std::cout << e.what() << std::endl;
    return -1;
  }

  return 0;
}

 

  • 初始化和连接:通过命令行参数连接到机器人,并设置默认行为。
  • 运动到初始位置:将机器人移动到一个预定义的关节配置,以确保其处于合适的初始位置。
  • 设置碰撞行为:在控制循环之前设置碰撞行为参数,以确保机器人在运动过程中能够正确处理碰撞。
  • 关节速度控制循环:通过正弦波函数生成关节速度,并控制关节运动。在控制循环中,机器人关节会按照一个正弦波的模式进行运动,直到运动完成。
  • 异常处理:捕获并处理可能出现的异常,确保程序能够正确终止。

这个示例展示了如何通过外部控制循环生成机器人关节速度运动,并确保安全和稳定的操作。

posted @ 2024-07-11 17:05  lvdongjie-avatarx  阅读(1)  评论(0编辑  收藏  举报