Home > Back-end >  How to locate a urdf file using parser.AddModelFromFile(full_name); in Drake
How to locate a urdf file using parser.AddModelFromFile(full_name); in Drake

Time:10-09

#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;
}

The above code give me the following error:

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)

My Directory layout is:

  • Project Folder
    • model
      • urdf
    • src
      • main.cpp
      • CmakeLists.txt

my src CmakeLists.txt is:

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}

Essentially, my objective is to use a custom urdf and build a model and use it for the inverse kinematics class.

CodePudding user response:

I think you have a very easy problem to resolve. You're currently using drake::FindResourceOrThrow(). If you look at its documentation, you'll note:

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

You have a urdf in an arbitrary location. In that case, just pass a filesystem path without calling FindResource() (or any of its variants). Whether it is an absolute or relative path will depend on where your executable ends up w.r.t. the urdf in your built/installed system.

  • Related