Skip to content

refactor: optimize copy and swap constructors for robot_model::Model#177

Merged
yrh012 merged 15 commits into
developfrom
refactor/optimize-copy
Apr 11, 2024
Merged

refactor: optimize copy and swap constructors for robot_model::Model#177
yrh012 merged 15 commits into
developfrom
refactor/optimize-copy

Conversation

@yrh012

@yrh012 yrh012 commented Apr 9, 2024

Copy link
Copy Markdown

Description

This PR introduces the robot_model::QPSolver class, implementing thequadratic programming tasks that was previously integrated within the robot_model::Model class. The necessity for this refactor arose from the lack of a copy constructor in the OsqpEigen::Solver attribute of robot_model::Model, which impeded the usability of Model's default copy constructors. Additionally, the original implementation burdened the Model class with direct solver initialization and configuration, complicating its source code.

The newly implemented robot_model::QPSolver class encapsulates all solver-related operations, having its own copy constructor and abstracting the solver's complexity away from robot_model::Model. This abstraction not only simplifies the Model class's codebase but also enhances the efficiency of swap and copy operations. Unlike before, Pinocchio models are now directly copied rather than reinitialized, improving the process while preserving operational integrity.

Review guidelines

Estimated Time of Review: 15 minutes

Checklist before merging:

  • Confirm that the relevant changelog(s) are up-to-date in case of any user-facing changes

@yrh012 yrh012 self-assigned this Apr 9, 2024
@yrh012 yrh012 changed the title Refactor/optimize copy refactor: optimize copy and swap constructors for robot_model::Model Apr 9, 2024
Comment thread source/robot_model/include/robot_model/Model.hpp Outdated
Comment thread source/robot_model/src/Model.cpp

@domire8 domire8 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going in a very nice direction

Comment thread source/robot_model/include/robot_model/QPSolver.hpp
Comment thread source/robot_model/src/Model.cpp
Comment thread source/robot_model/include/robot_model/QPSolver.hpp Outdated
Comment thread source/robot_model/src/Model.cpp Outdated
Comment thread source/robot_model/src/Model.cpp Outdated
this->init_solver();
}

bool QPSolver::init_solver() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns a bool but it's not used right? What would a false return value mean?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A false return values mean that the solver is not initialized (failed to initialize). This is from bool initSolver(); from OsqpEigen library and since we are wrapping around it, I decided to keep it the same as the previous interface.

yrh012 and others added 2 commits April 10, 2024 14:46
Co-authored-by: Dominic Reber <71256590+domire8@users.noreply.github.com>
Comment thread source/robot_model/include/robot_model/Model.hpp Outdated
eeberhard
eeberhard previously approved these changes Apr 11, 2024

@eeberhard eeberhard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me, much cleaner than before. Moving out all of those solver properties from the core class was nice even without changing the copy logic, and now copying without needing to re-initialize everything is even nicer. I suppose it depends on #178 to be fully usable in export, but happy to move forward with it!

Comment thread source/robot_model/include/robot_model/Model.hpp
urdf_path_(other.urdf_path_),
robot_model_(other.robot_model_),
robot_data_(other.robot_data_),
geom_model_(other.geom_model_),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to point out, the Pinocchio GeometryModel holds a vector of GeometryObjects which are stored as shared pointers to fcl::CollisionGeometry. This means that the copy refers to the same collision data in memory. In this particular case, a shallow copy is preferable because we only ever mutate the collision geometry when the robot model is constructed, and this way we don't have to duplicate the geometry in memory (which can get quite heavy). However, we must keep this in mind if we ever decide to add functions to modify collision geometry post-construction!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see! Thank you for the note. With that, it is more clear to me when to use deep copy/shallow copy of objects in memory.

eeberhard
eeberhard previously approved these changes Apr 11, 2024

@domire8 domire8 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not fully happy with that bool return value of the init_solver since its not used in the constructors of the QP solvers. To me, we want to improve code and here we create a function that has an unnecessary return value. Either

  1. we make it not return the bool
  2. we let the constructor raise an exception if the init failed
    My vote goes for 2

@yrh012

yrh012 commented Apr 11, 2024

Copy link
Copy Markdown
Author

I'm still not fully happy with that bool return value of the init_solver since its not used in the constructors of the QP solvers. To me, we want to improve code and here we create a function that has an unnecessary return value. Either

  1. we make it not return the bool
  2. we let the constructor raise an exception if the init failed
    My vote goes for 2

I completely agree, thanks for raising this! I implemented option two.

@yrh012 yrh012 merged commit 4b4a7c1 into develop Apr 11, 2024
@yrh012 yrh012 deleted the refactor/optimize-copy branch April 11, 2024 15:50
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants