鱼香ROS社区
    • 版块
    • 最新
    • 未解决
    • 已解决
    • 群组
    • 注册
    • 登录
    紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
    提问前必看的发帖注意事项: 社区问答规则(小鱼个人)更新 | 高质量帖子发布指南

    moveit2中MoveGroupInterface如何在类里面使用

    已定时 已固定 已锁定 已移动
    机械臂运动规划
    moveit2使用 moveit在类里使用
    4
    5
    920
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 小小明小
      小小明
      最后由 编辑

      平时的官方教程的MoveGroupInterface大都是这样写的,直接在main函数里面

      auto const node = std::make_shared<rclcpp::Node>(
            "moveit_hello",
            rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true));
      ......
      using moveit::planning_interface::MoveGroupInterface;
      auto move_group_interface = MoveGroupInterface(node, "panda_arm");
      
      

      现在我想把他写在类里面使用,假如类是这样的

      class TestMoveGroupInterface : public rclcpp::Node 
      {
      
      public:
        PointMap() : Node(
                         "hello_moveit",
                      rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true))
        {
      
       
          // Create the MoveIt MoveGroup Interface
          // 这行代码报错
          moeve_interface_= MoveGroupInterface(this, "panda_arm");
        }
      
      
      
      private:
      
        moveit::planning_interface::MoveGroupInterface  moeve_interface_;
      
      };
      
      

      但是这行代码不能这样写,不能用this关键字,这行代码报错

      moeve_interface_= MoveGroupInterface(this, "panda_arm");
      

      进到这个MoveGroupInterface函数具体查看是这样传参的

      MoveGroupInterface(const rclcpp::Node::SharedPtr& node, const std::string& group,
                           const std::shared_ptr<tf2_ros::Buffer>& tf_buffer = std::shared_ptr<tf2_ros::Buffer>(),
                           const rclcpp::Duration& wait_for_servers = rclcpp::Duration::from_seconds(-1));
      

      想问一下各位大佬怎么在类里面使用MoveGroupInterface

      小鱼小 1 条回复 最后回复 回复 引用 0
      • 小鱼小
        小鱼 技术大佬 @小小明
        最后由 编辑

        @小小明 chatgpt 答案
        在类中使用MoveGroupInterface需要对参数进行适当的传递。可以使用类成员变量 shared_from_this() 函数获取当前对象的 std::shared_ptr,然后将其作为参数传递给 MoveGroupInterface 构造函数。以下是修改后的代码示例:

        class TestMoveGroupInterface : public rclcpp::Node {
        public:
          TestMoveGroupInterface() : Node(
              "hello_moveit",
              rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true)) {
            // Create the MoveIt MoveGroup Interface
            move_interface_ = std::make_shared<moveit::planning_interface::MoveGroupInterface>(
                shared_from_this(), "panda_arm");
          }
        
        private:
          std::shared_ptr<moveit::planning_interface::MoveGroupInterface> move_interface_;
        };
        

        在构造函数中,使用 shared_from_this() 函数获取当前对象的 std::shared_ptr,然后通过 std::make_shared 创建 move_interface_ 对象。这样就能正确地传递当前节点对象的指针给 MoveGroupInterface 构造函数,以完成初始化。

        新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

        A 1 条回复 最后回复 回复 引用 0
        • A
          alg037 @小鱼
          最后由 编辑

          @小鱼
          我也碰到这个问题,但是按照小鱼的建议修改后,还是会出现错误。
          代码如下:

          #include "arm_moveit_interface.h"
          ArmMoveitInterface::ArmMoveitInterface(const std::string &node_name, const std::string &group_name)
              :rclcpp::Node(node_name,rclcpp::NodeOptions().automatically_declare_parameters_from_overrides(true)),
              group_name_(group_name)
          {
              initialize();
          }
          
          void ArmMoveitInterface::initialize()
          {
              move_group_interface_ = std::make_shared<moveit::planning_interface::MoveGroupInterface>(shared_from_this(), group_name_);
              RCLCPP_INFO(LOGGER, "Arm Moveit Interface Node Initialized");
          }
          

          错误信息如下:

          terminate called after throwing an instance of 'std::bad_weak_ptr'
          [arm_moveit_interface-5]   what():  bad_weak_ptr
          

          请问一下这个是什么原因呢?

          1 条回复 最后回复 回复 引用 0
          • 3
            395510695
            最后由 编辑

            不要在构造函数中初始化,初始化完成后,shared_from_this才能找到自己

            A 1 条回复 最后回复 回复 引用 1
            • A
              alg037 @395510695
              最后由 编辑

              @395510695 在 moveit2中MoveGroupInterface如何在类里面使用 中说:

              不要在构造函数中初始化,初始化完成后,shared_from_this才能找到自己

              多谢

              1 条回复 最后回复 回复 引用 0
              • 第一个帖子
                最后一个帖子
              皖ICP备16016415号-7
              Powered by NodeBB | 鱼香ROS