Moveit 移动机械臂API 调用

本文介绍了使用Moveit进行机械臂路径规划的两种方法:一是通过预设的规划组目标位姿;二是手动设定目标位姿。同时详细阐述了如何避免规划失败及调整目标角度的重要性。
部署运行你感兴趣的模型镜像

在moveit 移动机械臂时,有很方便的调用API方式

1.直接设定规划组提前设置的目标位姿

if(arm_.setNamedTarget("down")==false)

  {
    ROS_ERROR("Set forward failed");
    return false;
  }
  moveit::planning_interface::MoveItErrorCode result = arm_.asyncMove();
    if (bool(result) == true)
    {
      return true;
    }
    else
    {
      ROS_ERROR("Move to target failed (error %d)", result.val);
      

      return false;


2.手动输入设定目标位姿

geometry_msgs::Pose target_pose1;
    target_pose1.position.x = 0.1;
        target_pose1.position.y = 0.2;
        target_pose1.position.z = -0.05;
 
  geometry_msgs::PoseStamped target_pose;
  target_pose.header.frame_id = arm_link;
  target_pose.pose = target_pose1;
 
  int attempts = 0;
 
 
 
  while (attempts < 5)
  {
    geometry_msgs::PoseStamped modiff_target;
    modiff_target.header.frame_id = arm_link;
    modiff_target.pose = target_pose1;
     double x = modiff_target.pose.position.x;
     double y = modiff_target.pose.position.y;
     double z = modiff_target.pose.position.z;
     double d = sqrt(x*x + y*y);
     if (d>0.3)
     {
       ROS_ERROR("Target pose out of reach [%f > %f]", d, 0.3);
        
        
      }
      // Pitch is 90 (vertical) at 10 cm from the arm base; the farther the target is, the closer to horizontal
      // we point the gripper (0.205 = arm's max reach - vertical pitch distance + ε). Yaw is the direction to
      // the target. We also try some random variations of both to increase the chances of successful planning.
      // Roll is simply ignored, as our arm lacks the proper dof.
      double rp = M_PI_2 - std::asin((d - 0.1)/0.205) + attempts*fRand(-0.05, +0.05);
      double ry = std::atan2(y, x) + attempts*fRand(-0.05, +0.05);
      double rr = 0.0;

      tf::Quaternion q = tf::createQuaternionFromRPY(rr, rp, ry);
      tf::quaternionTFToMsg(q, modiff_target.pose.orientation);

      // Slightly increase z proportionally to pitch to avoid hitting the table with the lower gripper corner
      ROS_DEBUG("z increase:  %f  +  %f", modiff_target.pose.position.z, std::abs(std::cos(rp))/50.0);
      modiff_target.pose.position.z += std::abs(std::cos(rp))/50.0;

//       ROS_DEBUG("Set pose target [%.2f, %.2f, %.2f] [d: %.2f, r: %.2f, p: %.2f, y: %.2f]", x, y, z, d, rr, rp, ry);
     
        ROS_INFO("Set pose target [%.2f, %.2f, %.2f] [d: %.2f, r: %.2f, p: %.2f, y: %.2f]", x, y, z, d, rr, rp, ry);
      if (arm_.setPoseTarget(modiff_target) == false)
      {
        ROS_ERROR("Set pose target [%.2f, %.2f, %.2f, %.2f] failed",
                  modiff_target.pose.position.x, modiff_target.pose.position.y, modiff_target.pose.position.z,
                  tf::getYaw(modiff_target.pose.orientation));
 
      
      }

      moveit::planning_interface::MoveItErrorCode result = arm_.move();
      if(bool(result) == true){
    return true;
      }
      else
    return false;
    
    attempts++;
  }

这里需要注意,必须正确设定目标的角度(orientation),否则会报 ABORTED: No motion plan found. No execution attempted.错误

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### 如何使用MoveIt框架控制机械 #### 控制流程概述 为了使机械按照预期动作,需先理解MoveIt的工作原理及其与机械交互的方式。MoveIt通过`move_group`节点提供的操作和服务接口与其他组件协作完成运动规划任务[^1]。 #### 发送指令形式 对于大多数情况下,直接向机械发送由MoveIt生成的命令并非总是可行;相反,应依据具体型号利用制造商所提供的SDK转换这些高级别的路径计划成低级别的关节角度序列[joint1, joint2...jointN],以此定义机械的空间姿态,并依序传递给末端执行器实现预定的动作模式[^2]。 #### 编程接口介绍 针对不同的应用场景和技术偏好,提供了多种途径来调用Move_Group的服务: - **C++ API**: 利用`move_group_interface`库编写原生代码; - **Python API**: 借助`moveit_commander`包快速搭建原型系统; - **图形界面工具RVIZ**: 不需要额外编码,在可视化环境中直观设定目标位置并触发相应的移动行为[^3]。 下面给出一段简单的Python脚本作为实例展示如何基于上述提到的方法之一——即Python客户端API—来进行基本的操作: ```python import sys from moveit_commander import RobotCommander, MoveGroupCommander from geometry_msgs.msg import PoseStamped def main(): robot = RobotCommander() group_name = "arm" arm = MoveGroupCommander(group_name) pose_goal = PoseStamped() # 设定期望达到的位置坐标 (x,y,z) 及方向四元数(w,x,y,z) pose_goal.pose.position.x = 0.4 pose_goal.pose.position.y = 0.1 pose_goal.pose.position.z = 0.4 quaternion = [w=0.707, x=0.0, y=0.0, z=0.707] arm.set_pose_target(pose_goal) plan_success, traj, planning_time, error_code = arm.plan() if not plan_success: print("Planning failed.") return False result = arm.execute(traj) return True if __name__ == '__main__': try: success = main() print("Success:",success) except Exception as e: print(e) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值