Skip to content

New Models

Oliver Fischer edited this page Mar 18, 2022 · 1 revision

Convention

Some commonalities are required for every model. Refer to implemented models (such as SoftTrunkModel) if things are unclear.

srl::State

Your model must contain a public dyn_ object which can be read by the Model. This object can be loaded from SoftTrunk_common.h

Update on demand

Your model have an update method which takes an srl::State object as an argument and then updates it's dynamic parameters dyn_ accordingly.

Constructor

Your model's constructor must take a SoftTrunkParameters object to be able to verify states to be correct. Upon initialization, ensure it is finalized:

assert(st_params_.is_finalized());

Destructor

Upon destruction, your model must stop all loops and join all threads so it can terminate correctly.

I want to add a new model, how do I do that?

First, we'll need to do some management stuff to ensure you can write it correctly.

Locations

Put your header file in include/3d-soft-trunk/Models/ Put your implementation file in src/Models

CMake

So that the project will take your new model into account, you'll have to edit the main CMake file. It is called CMakeLists.txt and is in the main directory.

Add a new library for your model and link dependencies (around line 70): Recommended link libraries are Eigen, Boost, fmt, and yaml-cpp.

add_library(YourModel SHARED src/models/Yourmodel.cpp)
target_link_libraries(YourModel ${Boost_LIBRARIES} ${EIGEN3_LIBRARIES} fmt yaml-cpp YourModelDependency1 YourModelDependency2)

Then, add your model to the Model as a link library by editing the respective line:

target_link_libraries(Model SoftTrunkModel Lagrange)

SoftTrunk_common.h

This header file contains the SoftTrunkParameters definition, which you'll want to edit to account for your new model. Add your model to the enum class ModelType and edit the SoftTrunkParameters::write_yaml and SoftTrunkParameters::load_yaml functions to read/write your model.

Model.h

Now you can update the Model header: include your model's header:

#include "3d-soft-trunk/models/YourModel.h"

and add an object for your model in the class:

std::unique_ptr<YourModel> yourmodel_;

Model.cpp

Now, add an initialization of your model's pointer to the Model constructor by adding it to the switch on line 7 of Model.cpp. Simply add a new case to the switch, and initialize your model:

yourmodel_ = std::make_unique<Yourmodel>(st_params_);

You'll also need to update the Model::update method. Similarily, add a new case to the switch and read in your model's state:

yourmodel_->update(state);
this->dyn_ = yourmodel_.dyn_;