Skip to content

Commit

Permalink
Merge pull request #51 from jmirabel/master
Browse files Browse the repository at this point in the history
Fix Timer class
  • Loading branch information
Olivier Stasse authored Apr 20, 2018
2 parents 136552f + 1c46ea8 commit 835c9b7
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 143 deletions.
3 changes: 2 additions & 1 deletion include/sot/core/feature-posture.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ namespace dynamicgraph {
explicit FeaturePosture (const std::string& name);
virtual ~FeaturePosture ();
virtual unsigned int& getDimension( unsigned int& res,int );
void setPosture (const dg::Vector& posture);
void selectDof (unsigned dofId, bool control);

protected:

virtual dg::Vector& computeError( dg::Vector& res, int );
virtual dg::Matrix& computeJacobian( dg::Matrix& res, int );
virtual dg::Vector& computeActivation( dg::Vector& res, int );
virtual dg::Vector& computeErrorDot (dg::Vector& res,int time);

signalIn_t state_;
signalIn_t posture_;
signalIn_t postureDot_;
signalOut_t error_;
dg::Matrix jacobian_;
private:
Expand Down
50 changes: 4 additions & 46 deletions include/sot/core/matrix-svd.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,64 +35,22 @@ namespace Eigen {

void pseudoInverse( dg::Matrix& _inputMatrix,
dg::Matrix& _inverseMatrix,
const double threshold = 1e-6) {
JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV);
JacobiSVD<dg::Matrix>::SingularValuesType m_singularValues=svd.singularValues();
JacobiSVD<dg::Matrix>::SingularValuesType singularValues_inv;
singularValues_inv.resizeLike(m_singularValues);
for ( long i=0; i<m_singularValues.size(); ++i) {
if ( m_singularValues(i) > threshold )
singularValues_inv(i)=1.0/m_singularValues(i);
else singularValues_inv(i)=0;
}
_inverseMatrix = (svd.matrixV()*singularValues_inv.asDiagonal()*svd.matrixU().transpose());
}
const double threshold = 1e-6);

void dampedInverse( const JacobiSVD <dg::Matrix>& svd,
dg::Matrix& _inverseMatrix,
const double threshold = 1e-6) {
typedef JacobiSVD<dg::Matrix>::SingularValuesType SV_t;
ArrayWrapper<const SV_t> sigmas (svd.singularValues());

SV_t sv_inv (sigmas / (sigmas.cwiseAbs2() + threshold * threshold));

_inverseMatrix.noalias() =
( svd.matrixV() * sv_inv.asDiagonal() * svd.matrixU().transpose());
}
const double threshold = 1e-6);

void dampedInverse( const dg::Matrix& _inputMatrix,
dg::Matrix& _inverseMatrix,
dg::Matrix& Uref,
dg::Vector& Sref,
dg::Matrix& Vref,
const double threshold = 1e-6) {
sotDEBUGIN(15);
sotDEBUG(5) << "Input Matrix: "<<_inputMatrix<<std::endl;
JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV);

dampedInverse (svd, _inverseMatrix, threshold);

Uref = svd.matrixU();
Vref = svd.matrixV();
Sref = svd.singularValues();

sotDEBUGOUT(15);
}
const double threshold = 1e-6);

void dampedInverse( const dg::Matrix& _inputMatrix,
dg::Matrix& _inverseMatrix,
const double threshold = 1e-6) {
sotDEBUGIN(15);
sotDEBUG(5) << "Input Matrix: "<<_inputMatrix<<std::endl;

JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV);
dampedInverse (svd, _inverseMatrix, threshold);

sotDEBUGOUT(15);
}



const double threshold = 1e-6);

}

Expand Down
13 changes: 7 additions & 6 deletions include/sot/core/memory-task-sot.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace dynamicgraph {
{
public:// protected:
/* Internal memory to reduce the dynamic allocation at task resolution. */
dg::Vector err;
dg::Matrix Jt; //( nJ,mJ );
dg::Matrix Jp;
dg::Matrix PJp;
Expand All @@ -46,20 +47,20 @@ namespace dynamicgraph {
dg::Matrix Jact; //( nJ,mJ ); // Activated part
dg::Matrix JK; //(nJ,mJ);

dg::Matrix V;
dg::Matrix Proj;

typedef Eigen::JacobiSVD<dg::Matrix> SVD_t;
SVD_t svd;

public:
/* mJ is the number of actuated joints, nJ the number of feature in the task,
* and ffsize the number of unactuated DOF. */
MemoryTaskSOT( const std::string & name,const unsigned int nJ=0,
const unsigned int mJ=0,const unsigned int ffsize =0 );
MemoryTaskSOT( const std::string & name,const Matrix::Index nJ=0,
const Matrix::Index mJ=0,const Matrix::Index ffsize =0 );

virtual void initMemory( const unsigned int nJ,
const unsigned int mJ,
const unsigned int ffsize,
virtual void initMemory( const Matrix::Index nJ,
const Matrix::Index mJ,
const Matrix::Index ffsize,
bool atConstruction = false);

public: /* --- ENTITY INHERITANCE --- */
Expand Down
10 changes: 3 additions & 7 deletions include/sot/core/sot.hh
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ namespace dynamicgraph {
/*! \brief Store a pointer to compute the gradient */
TaskAbstract* taskGradient;

/*! Projection used to compute the control law. */
dg::Matrix Proj;
//Eigen::MatrixXd<double,Eigen::Dynamic,Eigen::Dynamic, Eigen::RowMajor> Proj;
/*! Force the recomputation at each step. */
bool recomputeEachTime;
Expand All @@ -131,13 +129,11 @@ namespace dynamicgraph {

static dg::Matrix & computeJacobianConstrained( const dg::Matrix& Jac,
const dg::Matrix& K,
dg::Matrix& JK,
dg::Matrix& Jff,
dg::Matrix& Jact );
dg::Matrix& JK);
static dg::Matrix & computeJacobianConstrained( const TaskAbstract& task,
const dg::Matrix& K );
static dg::Vector
taskVectorToMlVector(const VectorMultiBound& taskVector);
static void
taskVectorToMlVector(const VectorMultiBound& taskVector, Vector& err);

public:

Expand Down
37 changes: 27 additions & 10 deletions include/sot/core/timer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Timer_EXPORT Timer
protected:

struct timeval t0,t1;
clock_t c0, c1;
double dt;

public:
Expand All @@ -87,6 +88,7 @@ class Timer_EXPORT Timer

dg::SignalPtr<T,int> sigSIN;
dg::SignalTimeDependent<T,int> sigSOUT;
dg::SignalTimeDependent<T,int> sigClockSOUT;
dg::Signal<double,int> timerSOUT;


Expand All @@ -96,20 +98,33 @@ class Timer_EXPORT Timer
sigSIN = &sig; dt=0.;
}

template <bool UseClock>
T& compute( T& t,const int& time )
{
sotDEBUGIN(15);
gettimeofday(&t0,NULL);
sotDEBUG(15) << "t0: "<< t0.tv_sec << " - " << t0.tv_usec << std::endl;
if (UseClock) {
c0 = clock();
sotDEBUG(15) << "t0: "<< c0 << std::endl;
} else {
gettimeofday(&t0,NULL);
sotDEBUG(15) << "t0: "<< t0.tv_sec << " - " << t0.tv_usec << std::endl;
}

t = sigSIN( time );

gettimeofday(&t1,NULL);
dt = ( (t1.tv_sec-t0.tv_sec) * 1000.
+ (t1.tv_usec-t0.tv_usec+0.) / 1000. );
sotDEBUG(15) << "t1: "<< t1.tv_sec << " - " << t1.tv_usec << std::endl;
if (UseClock) {
c1 = clock();
sotDEBUG(15) << "t1: "<< c0 << std::endl;
dt = ((double)(c1 - c0) * 1000 ) / CLOCKS_PER_SEC;
} else {
gettimeofday(&t1,NULL);
dt = ( (t1.tv_sec-t0.tv_sec) * 1000.
+ (t1.tv_usec-t0.tv_usec+0.) / 1000. );
sotDEBUG(15) << "t1: "<< t1.tv_sec << " - " << t1.tv_usec << std::endl;
}

timerSOUT = dt;
timerSOUT.setTime (time);

sotDEBUGOUT(15);
return t;
Expand Down Expand Up @@ -145,18 +160,20 @@ template< class T >
Timer<T>::
Timer( const std::string& name )
:Entity(name)
,t0(),t1()
,dt(0.)
,sigSIN( NULL,"Timer("+name+")::output(T)::sin" )
,sigSOUT( boost::bind(&Timer::compute,this,_1,_2),
,sigSIN( NULL,"Timer("+name+")::input(T)::sin" )
,sigSOUT( boost::bind(&Timer::compute<false>,this,_1,_2),
sigSIN,
"Timer("+name+")::output(T)::sout" )
,sigClockSOUT( boost::bind(&Timer::compute<true>,this,_1,_2),
sigSIN,
"Timer("+name+")::output(T)::clockSout" )
,timerSOUT( "Timer("+name+")::output(double)::timer" )
{
sotDEBUGIN(15);
timerSOUT.setFunction( boost::bind(&Timer::getDt,this,_1,_2) );

signalRegistration( sigSIN<<sigSOUT<<timerSOUT );
signalRegistration( sigSIN<<sigSOUT<<sigClockSOUT<<timerSOUT );
sotDEBUGOUT(15);
}

Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ SET(${LIBRARY_NAME}_SOURCES
tools/periodic-call
tools/device
tools/trajectory

matrix/matrix-svd
)

ADD_LIBRARY(${LIBRARY_NAME}
Expand Down
30 changes: 23 additions & 7 deletions src/feature/feature-posture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ namespace dynamicgraph {
: FeatureAbstract(name),
state_(NULL, "FeaturePosture("+name+")::input(Vector)::state"),
posture_(0, "FeaturePosture("+name+")::input(Vector)::posture"),
postureDot_(0, "FeaturePosture("+name+")::input(Vector)::postureDot"),
jacobian_(),
activeDofs_ (),
nbActiveDofs_ (0)
{
signalRegistration (state_ << posture_);
signalRegistration (state_ << posture_ << postureDot_);

errorSOUT.addDependency (state_);

Expand Down Expand Up @@ -91,8 +92,8 @@ namespace dynamicgraph {

dg::Vector& FeaturePosture::computeError( dg::Vector& res, int t)
{
dg::Vector state = state_.access (t);
dg::Vector posture = posture_.access (t);
const dg::Vector& state = state_.access (t);
const dg::Vector& posture = posture_.access (t);

res.resize (nbActiveDofs_);
std::size_t index=0;
Expand All @@ -116,14 +117,29 @@ namespace dynamicgraph {
return res;
}

dg::Vector& FeaturePosture::computeErrorDot( dg::Vector& res, int t)
{
const Vector& postureDot = postureDot_.access (t);

res.resize (nbActiveDofs_);
std::size_t index=0;
for (std::size_t i=0; i<activeDofs_.size (); ++i) {
if (activeDofs_ [i]) {
res (index) = postureDot (i);
index ++;
}
}
return res;
}

void
FeaturePosture::selectDof (unsigned dofId, bool control)
{
dg::Vector state = state_.accessCopy();
dg::Vector posture = posture_.accessCopy ();
unsigned int dim = state.size();
const Vector& state = state_.accessCopy();
const Vector& posture = posture_.accessCopy ();
std::size_t dim (state.size());

if (dim != posture.size ()) {
if (dim != (std::size_t)posture.size ()) {
throw std::runtime_error
("Posture and State should have same dimension.");
}
Expand Down
82 changes: 82 additions & 0 deletions src/matrix/matrix-svd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2018, Joseph Mirabel
// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
//
// This file is part of sot-core.
// sot-core is free software: you can redistribute it
// and/or modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// sot-core is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Lesser Public License for more details. You should have
// received a copy of the GNU Lesser General Public License along with
// sot-core. If not, see <http://www.gnu.org/licenses/>.


#include <sot/core/debug.hh>
#include <sot/core/matrix-svd.hh>

namespace Eigen {

void pseudoInverse( dg::Matrix& _inputMatrix,
dg::Matrix& _inverseMatrix,
const double threshold) {
JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV);
JacobiSVD<dg::Matrix>::SingularValuesType m_singularValues=svd.singularValues();
JacobiSVD<dg::Matrix>::SingularValuesType singularValues_inv;
singularValues_inv.resizeLike(m_singularValues);
for ( long i=0; i<m_singularValues.size(); ++i) {
if ( m_singularValues(i) > threshold )
singularValues_inv(i)=1.0/m_singularValues(i);
else singularValues_inv(i)=0;
}
_inverseMatrix = (svd.matrixV()*singularValues_inv.asDiagonal()*svd.matrixU().transpose());
}

void dampedInverse( const JacobiSVD <dg::Matrix>& svd,
dg::Matrix& _inverseMatrix,
const double threshold) {
typedef JacobiSVD<dg::Matrix>::SingularValuesType SV_t;
ArrayWrapper<const SV_t> sigmas (svd.singularValues());

SV_t sv_inv (sigmas / (sigmas.cwiseAbs2() + threshold * threshold));
const dg::Matrix::Index m = sv_inv.size();

_inverseMatrix.noalias() =
( svd.matrixV().leftCols(m) * sv_inv.asDiagonal() * svd.matrixU().leftCols(m).transpose());
}

void dampedInverse( const dg::Matrix& _inputMatrix,
dg::Matrix& _inverseMatrix,
dg::Matrix& Uref,
dg::Vector& Sref,
dg::Matrix& Vref,
const double threshold) {
sotDEBUGIN(15);
sotDEBUG(5) << "Input Matrix: "<<_inputMatrix<<std::endl;
JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeThinV);

dampedInverse (svd, _inverseMatrix, threshold);

Uref = svd.matrixU();
Vref = svd.matrixV();
Sref = svd.singularValues();

sotDEBUGOUT(15);
}

void dampedInverse( const dg::Matrix& _inputMatrix,
dg::Matrix& _inverseMatrix,
const double threshold) {
sotDEBUGIN(15);
sotDEBUG(5) << "Input Matrix: "<<_inputMatrix<<std::endl;

JacobiSVD<dg::Matrix> svd(_inputMatrix, ComputeThinU | ComputeFullV);
dampedInverse (svd, _inverseMatrix, threshold);

sotDEBUGOUT(15);
}

}
Loading

0 comments on commit 835c9b7

Please sign in to comment.