如何使用 parser.AddModelFromFile(full_name) 定位 urdf 文件;在德雷克

How to locate a urdf file using parser.AddModelFromFile(full_name); in Drake

#include "drake/geometry/scene_graph.h"
#include "drake/multibody/parsing/parser.h"
#include "drake/common/find_resource.h"

int main(void) {
// Building a floating-base plant
    drake::multibody::MultibodyPlant<double> plant_{0.0};
    drake::geometry::SceneGraph<double> scene_graph;
    std::string full_name = drake::FindResourceOrThrow(
        "model/model.urdf");
    drake::multibody::Parser parser(&plant_, &scene_graph);
    parser.AddModelFromFile(full_name);
    plant_.Finalize();
    return 1;
}

上面的代码给我以下错误:

terminate called after throwing an instance of 'std::runtime_error'
  what():  Drake resource_path 'model/model.urdf' does not start with drake/.
Aborted (core dumped)

我的目录布局是:

我的 src CmakeLists.txt 是:

cmake_minimum_required(VERSION 3.16.3)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
message("CMAKE Directory : " ${PROJECT_SOURCE_DIR})

project(IK VERSION 1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package (Eigen3 3.3 REQUIRED NO_MODULE)
find_package(drake CONFIG REQUIRED)

# specify the main source file
set(SOURCE main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE})

target_link_libraries("${PROJECT_NAME}" 
    Eigen3::Eigen
    drake::drake}

本质上,我的 objective 是使用自定义 urdf 并构建模型并将其用于反向运动学 class。

我认为您的问题很容易解决。您目前正在使用 drake::FindResourceOrThrow()。如果您查看 its documentation,您会注意到:

The resource_path refers to the relative path within the Drake source repository, prepended with drake/.

您在任意位置有一个 urdf。在这种情况下,只需传递文件系统路径而不调用 FindResource() (或其任何变体)。它是绝对路径还是相对路径将取决于您的可执行文件的最终位置 w.r.t。 built/installed 系统中的 urdf。