-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Momentum regressor #2411
Open
lvjonok
wants to merge
9
commits into
stack-of-tasks:devel
Choose a base branch
from
lvjonok:master
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Momentum regressor #2411
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fecf8f8
feat: init implementation
lvjonok d5dcdbd
docs: add proper description
lvjonok df5edf7
Merge branch 'devel' into master
lvjonok 41a05d9
Merge branch 'devel' into master
lvjonok fa9f74c
Merge branch 'devel' into master
lvjonok 24a888e
feat: implement indirect regressors
lvjonok ba972a6
feat: remove previous implementation
lvjonok 569347b
feat: add sysid example
lvjonok 2306714
fix: update data attributes
lvjonok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
#include "pinocchio/parsers/urdf.hpp" | ||
|
||
#include "pinocchio/algorithm/joint-configuration.hpp" | ||
#include "pinocchio/algorithm/kinematics-derivatives.hpp" | ||
#include "pinocchio/algorithm/regressor.hpp" | ||
#include "pinocchio/algorithm/rnea.hpp" | ||
#include "pinocchio/algorithm/aba.hpp" | ||
#include "pinocchio/algorithm/energy.hpp" | ||
|
||
#include <iostream> | ||
|
||
// PINOCCHIO_MODEL_DIR is defined by the CMake but you can define your own directory here. | ||
#ifndef PINOCCHIO_MODEL_DIR | ||
#define PINOCCHIO_MODEL_DIR "path_to_the_model_dir" | ||
#endif | ||
int main(int argc, char ** argv) | ||
{ | ||
using namespace pinocchio; | ||
|
||
// You should change here to set up your own URDF file or just pass it as an argument of this | ||
// example. | ||
const std::string urdf_filename = | ||
(argc <= 1) ? PINOCCHIO_MODEL_DIR | ||
+ std::string("/example-robot-data/robots/ur_description/urdf/ur5_robot.urdf") | ||
: argv[1]; | ||
|
||
// Load the URDF model | ||
Model model; | ||
pinocchio::urdf::buildModel(urdf_filename, model); | ||
|
||
// Build a data related to model | ||
Data data(model); | ||
|
||
// In this example we explore some of the system identification tools provided by Pinocchio. | ||
// We start by defining a vector of dynamical parameters of our dynamic model. | ||
Eigen::VectorXd dyn_parameters = Eigen::VectorXd::Zero(model.nv * 10); | ||
for (JointIndex jnt_idx = 1; jnt_idx < model.njoints; ++jnt_idx) | ||
{ | ||
// We can set the inertial parameters of the joints | ||
dyn_parameters.segment<10>((jnt_idx - 1) * 10) = model.inertias[jnt_idx].toDynamicParameters(); | ||
} | ||
|
||
{ | ||
// Sample a random joint configuration as well as random joint velocity and acceleration | ||
Eigen::VectorXd q = randomConfiguration(model); | ||
Eigen::VectorXd v = Eigen::VectorXd::Random(model.nv); | ||
Eigen::VectorXd a = Eigen::VectorXd::Random(model.nv); | ||
|
||
// Some of the dynamics quantities can be parametrized linearly with respect to the dynamical | ||
// parameters. For instance, in RNEA algorithm, the resulting joint torques can be expressed as | ||
// a $Y(q, v, a) \cdot \theta = \tau$ where $Y(q, v, a)$ is a so-called joint-torque regressor. | ||
auto jointTorqueRegressor = computeJointTorqueRegressor(model, data, q, v, a); | ||
auto regressorTau = jointTorqueRegressor * dyn_parameters; | ||
auto rneaTau = rnea(model, data, q, v, a); | ||
// The two torques should be equal | ||
assert((regressorTau - rneaTau).isZero(1e-12)); | ||
|
||
// However, in the real-world scenario, measuring acceleration accurately is almost impossible. | ||
// Instead, we can use other quantities such as energy or momentum to compute how well the | ||
// parameters fit the data. | ||
// Let's start with energy parametrization | ||
auto kineticEnergyRegressor = computeKineticEnergyRegressor(model, data, q, v); | ||
auto potentialEnergyRegressor = computePotentialEnergyRegressor(model, data, q); | ||
auto regressorEnergy = kineticEnergyRegressor + potentialEnergyRegressor; | ||
auto energy = computeKineticEnergy(model, data, q, v) + computePotentialEnergy(model, data, q); | ||
|
||
// The energy should be equal | ||
assert(std::abs((regressorEnergy * dyn_parameters - energy)) < 1e-12); | ||
} | ||
|
||
// However, the logical question is how we can compute the energy (which uses default parameters). | ||
// We recall that the power is time derivative of energy. Therefore, we can use | ||
// the torque and velocity to compute the mechanical power and integrate on horizon to get the | ||
// energy. Let's reset the configuration and simulate the system with the sine wave of joint | ||
// torques; | ||
{ | ||
auto torque_fn = [&model](const double & t) -> Eigen::VectorXd { | ||
return Eigen::VectorXd::Ones(model.nv) * std::sin(t); | ||
}; | ||
|
||
// Reset the configuration | ||
Eigen::VectorXd q = randomConfiguration(model); | ||
Eigen::VectorXd v = Eigen::VectorXd::Zero(model.nv); | ||
// Perform simulation for 1000 steps with dt=1e-3 | ||
double dt = 2e-4; | ||
const int N = 1000; | ||
Eigen::MatrixXd history_q = Eigen::MatrixXd::Zero(model.nq, N); | ||
Eigen::MatrixXd history_v = Eigen::MatrixXd::Zero(model.nv, N); | ||
|
||
for (int i = 0; i < N; ++i) | ||
{ | ||
auto tau = torque_fn(i * dt); | ||
auto a = aba(model, data, q, v, tau); | ||
// simple integration | ||
v += a * dt; | ||
q = integrate(model, q, v * dt); | ||
|
||
history_q.col(i) = q; | ||
history_v.col(i) = v; | ||
} | ||
|
||
// Now we can compute the difference in energy between the initial and final states | ||
// using the regressor and integrate the power. | ||
auto regEnergyFn = [&model, &data, &dyn_parameters]( | ||
const Eigen::VectorXd & q, const Eigen::VectorXd & v) -> double { | ||
auto kineticEnergyRegressor = computeKineticEnergyRegressor(model, data, q, v); | ||
auto potentialEnergyRegressor = computePotentialEnergyRegressor(model, data, q); | ||
auto regressorEnergy = kineticEnergyRegressor + potentialEnergyRegressor; | ||
return regressorEnergy * dyn_parameters; | ||
}; | ||
|
||
// Compute the energy difference | ||
auto energy_diff = regEnergyFn(history_q.col(N - 1), history_v.col(N - 1)) | ||
- regEnergyFn(history_q.col(0), history_v.col(0)); | ||
|
||
// Compute the power integral | ||
double power_integral = 0; | ||
for (int i = 0; i < N; ++i) | ||
{ | ||
power_integral += torque_fn(i * dt).dot(history_v.col(i)) * dt; | ||
} | ||
|
||
// The energy difference should be close to the power integral | ||
assert( | ||
std::abs(energy_diff - power_integral) | ||
< 1e-2); // the tolerance is high due numerical integration | ||
} | ||
|
||
// Another concept we can approach is the momentum. | ||
// Momentum can be defined as $H = M(q) \cdot v$ where $M(q)$ is the mass inertia matrix or $H = | ||
// Y_H(q, v) \pi$ in regressor form. On the other hand, one can show that $\dot_H = \tau + C(q, | ||
// v)^T v - g(q)$ where $C(q, v)$ is the Coriolis matrix and $g(q)$ is the gravity vector. | ||
// Fortunately, C(q, v)^T v can be also expressed in regressor form. | ||
{ | ||
Eigen::VectorXd q = randomConfiguration(model); | ||
Eigen::VectorXd v = Eigen::VectorXd::Random(model.nv); | ||
auto tau = Eigen::VectorXd::Random(model.nv); | ||
const double dt = 1e-3; | ||
|
||
auto regressors1 = computeIndirectRegressors(model, data, q, v); | ||
// compute the momentum using regressor form | ||
Eigen::VectorXd H1 = regressors1.first * dyn_parameters; | ||
|
||
// integrate forward | ||
auto v_next = v + aba(model, data, q, v, tau) * dt; | ||
auto q_next = integrate(model, q, v_next * dt); | ||
auto regressors2 = computeIndirectRegressors(model, data, q_next, v_next); | ||
// compute the momentum using regressor form | ||
Eigen::VectorXd H2 = regressors2.first * dyn_parameters; | ||
|
||
// compute the numerical momentum difference | ||
Eigen::VectorXd numericalMomentumDiff = (H2 - H1); | ||
|
||
// Compare the C^T v term | ||
Eigen::VectorXd CTv_regressor = regressors1.second * dyn_parameters; | ||
Eigen::VectorXd CTv = computeCoriolisMatrix(model, data, q, v).transpose() * v; | ||
assert((CTv_regressor - CTv).isZero(1e-12)); | ||
|
||
// Compare the gravity term | ||
Eigen::VectorXd g_regressor = computePotentialEnergyRegressor(model, data, q); | ||
Eigen::VectorXd g = computeGeneralizedGravity(model, data, q); | ||
assert((g_regressor - g).isZero(1e-12)); | ||
|
||
// find analytical momentum derivative | ||
Eigen::VectorXd analyticalMomentumDot = CTv + tau - g; | ||
|
||
// Verify that the numerical momentum difference is close to the analytical momentum derivative | ||
assert((numericalMomentumDiff - analyticalMomentumDot * dt).isZero(1e-5)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a very small nitpicking: does this include the gravity vector?
Like, should this be
\f$ C^T v - g = Y_CTqd(q, v) \pi \f$
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it does not include gravity. In sysid example I provide a complete comparison and matching of these terms: https://github.com/stack-of-tasks/pinocchio/pull/2411/files#diff-de29e11f15faef229b4e025cc6c9f09af5d32c5268973e47484fc0b08afa6d77R154-R165