Newer
Older
/*
* Copyright 2011, Nicolas Mansard, LAAS-CNRS
*
* This file is part of sot-dyninv.
* sot-dyninv 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-dyninv 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 Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with sot-dyninv. If not, see <http://www.gnu.org/licenses/>.
*/
#define VP_DEBUG_MODE 50
#include <sot/core/debug.hh>
#ifdef VP_DEBUG
class solver_op_space__INIT
{
//public:solver_op_space__INIT( void ) { dynamicgraph::sot::DebugTrace::openFile(); }
};
solver_op_space__INIT solver_op_space_initiator;
#endif //#ifdef VP_DEBUG
#include <sot-dyninv/solver-kine.h>
#include <sot-dyninv/commands-helper.h>
#include <dynamic-graph/factory.h>
#include <boost/foreach.hpp>
#include <sot-dyninv/commands-helper.h>
#include <dynamic-graph/pool.h>
#include <soth/HCOD.hpp>
#include <sot-dyninv/task-dyn-pd.h>
#include <sot/core/feature-point6d.hh>
#include <sstream>
#include <soth/Algebra.hpp>
#include <Eigen/QR>
#include <sot-dyninv/mal-to-eigen.h>
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace soth
{
Bound& operator -= (Bound& xb, const double & x )
{
return xb;
}
const Bound operator - (const Bound& a, const Bound & b )
{
assert( b.getType()==Bound::BOUND_TWIN || a.getType()==b.getType() );
if( b.getType() ==Bound::BOUND_TWIN )
{
switch( a.getType() )
{
case Bound::BOUND_TWIN:
case Bound::BOUND_INF:
case Bound::BOUND_SUP:
return Bound(a.getBound(a.getType())-b.getBound(Bound::BOUND_TWIN),
a.getType());
break;
case Bound::BOUND_DOUBLE:
return Bound(a.getBound(Bound::BOUND_INF)-b.getBound(Bound::BOUND_TWIN),
a.getBound(Bound::BOUND_SUP)-b.getBound(Bound::BOUND_TWIN));
break;
}
}
else
{
// TODO
throw "TODO";
}
return a;
}
}
namespace dynamicgraph
{
namespace sot
{
namespace dyninv
{
namespace dg = ::dynamicgraph;
using namespace dg;
using dg::SignalBase;
static bool isLH(boost::shared_ptr<soth::Stage> s)
{
return s->name == "tasklh";
}
static bool isRH(boost::shared_ptr<soth::Stage> s)
{
return s->name == "taskrhorient";
}
/* --- DG FACTORY ------------------------------------------------------- */
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(SolverKine,"SolverKine");
/* --- CONSTRUCTION ----------------------------------------------------- */
/* --- CONSTRUCTION ----------------------------------------------------- */
/* --- CONSTRUCTION ----------------------------------------------------- */
SolverKine::
SolverKine( const std::string & name )
: Entity(name)
,stack_t()
,CONSTRUCT_SIGNAL_IN(damping,double)
,CONSTRUCT_SIGNAL_OUT(control,ml::Vector,
dampingSIN )
,CONSTRUCT_SIGNAL(velocity,OUT,ml::Vector)
,controlFreeFloating(true)
,hsolver()
,Ctasks(),btasks()
,solution()
,activeSet(),relevantActiveSet(false)
{
signalRegistration( controlSOUT << velocitySOUT
<< dampingSIN );
/* Command registration. */
addCommand("debugOnce",
makeCommandVoid0(*this,&SolverKine::debugOnce,
docCommandVoid0("open trace-file for next iteration of the solver.")));
addCommand("resetAset",
makeCommandVoid0(*this,&SolverKine::resetAset,
docCommandVoid0("Reset the active set.")));
addCommand("decompo",
makeCommandVoid1(*this,&SolverKine::getDecomposition,
docCommandVoid1("Return the decomposition of the given level.","Stage level")));
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
addCommand("setControlFreeFloating",
makeDirectSetter(*this,&controlFreeFloating,
docDirectSetter("If true, the ouput control includes the ff (ie, size nbDof). Oterwise, size is nbDof-6. FF is supposed to be at the head.","bool")));
ADD_COMMANDS_FOR_THE_STACK;
}
/* --- STACK ----------------------------------------------------------- */
/* --- STACK ----------------------------------------------------------- */
/* --- STACK ----------------------------------------------------------- */
SolverKine::TaskDependancyList_t SolverKine::
getTaskDependancyList( const TaskAbstract& task )
{
TaskDependancyList_t res;
res.push_back( &task.taskSOUT );
res.push_back( &task.jacobianSOUT );
return res;
}
void SolverKine::
addDependancy( const TaskDependancyList_t& depList )
{
BOOST_FOREACH( const SignalBase<int>* sig, depList )
{ controlSOUT.addDependency( *sig ); }
}
void SolverKine::
removeDependancy( const TaskDependancyList_t& depList )
{
BOOST_FOREACH( const SignalBase<int>* sig, depList )
{ controlSOUT.removeDependency( *sig ); }
}
void SolverKine::
resetReady( void )
{
controlSOUT.setReady();
}
/* --- NOTIFICATION HCOD ------------------------------------------------ */
struct HCODUpdateCounter
{
void operator() (std::string stage,soth::ConstraintRef cst, std::string event)
{
if(!onoff) return;
//sotDEBUG(0)
//std::cout<< "At t="<<iter<<" [" << stage << "," << cst << "]: " << event << std::endl;
count ++;
}
static int iter;
static int count;
static bool onoff;
HCODUpdateCounter() {count = 0;}
};
int HCODUpdateCounter::count = 0;
int HCODUpdateCounter::iter = 0;
bool HCODUpdateCounter::onoff = true;
/* --- INIT SOLVER ------------------------------------------------------ */
/* --- INIT SOLVER ------------------------------------------------------ */
/* --- INIT SOLVER ------------------------------------------------------ */
/** Force the update of all the task in-signals, in order to fix their
* size for resizing the solver.
*/
void SolverKine::
refreshTaskTime( int time )
{
BOOST_FOREACH( TaskAbstract* task, stack )
{
task->taskSOUT( time );
}
}
/** Knowing the sizes of all the stages (except the task ones),
* the function resizes the matrix and vector of all stages (except...).
*/
void SolverKine::
resizeSolver( void )
{
hsolver = hcod_ptr_t(new soth::HCOD( nbDofs,stack.size() ));
Ctasks.resize(stack.size());
btasks.resize(stack.size());
relevantActiveSet = false;
// std::cout << controlSOUT.getTime() << std::endl;
// if( controlSOUT.getTime() == 10)
// {
// std::cout <<"Exceptionaly keep the aset." << std::endl;
// relevantActiveSet = true;
// activeSet[5] = soth::cstref_vector_t();
// activeSet[6] = soth::cstref_vector_t();
// activeSet.push_back(soth::cstref_vector_t());
// activeSet.push_back(soth::cstref_vector_t());
// }
int i=0;
BOOST_FOREACH( TaskAbstract* task, stack )
{
const int nx = task->taskSOUT.accessCopy().size();
Ctasks[i].resize(nx,nbDofs);
btasks[i].resize(nx);
hsolver->pushBackStage( Ctasks[i],btasks[i] );
hsolver->stages.back()->name = task->getName();
i++;
}
solution.resize( nbDofs );
hsolver->notifiorRegistration(HCODUpdateCounter());
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
}
/* Return true iff the solver sizes fit to the task set. */
bool SolverKine::
checkSolverSize( void )
{
sotDEBUGIN(15);
assert( nbDofs>0 );
if(! hsolver ) return false;
if( stack.size() != hsolver->nbStages() ) return false;
bool toBeResized=false;
for( int i=0;i<(int)stack.size();++i )
{
assert( Ctasks[i].cols() == nbDofs && Ctasks[i].rows() == btasks[i].size() );
TaskAbstract & task = *stack[i];
if( btasks[i].size() != (int)task.taskSOUT.accessCopy().size() )
{
toBeResized = true;
break;
}
}
return !toBeResized;
}
/* --- SIGNALS ---------------------------------------------------------- */
/* --- SIGNALS ---------------------------------------------------------- */
/* --- SIGNALS ---------------------------------------------------------- */
ml::Vector& SolverKine::
controlSOUT_function( ml::Vector &mlcontrol, int t )
{
sotDEBUG(15) << " # In time = " << t << std::endl;
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
refreshTaskTime( t );
if(! checkSolverSize() ) resizeSolver();
using namespace soth;
if( dampingSIN ) //damp?
{
sotDEBUG(5) << "Using damping. " << std::endl;
/* Only damp the final stage of the stack, 'cose of the solver known limitation. */
hsolver->setDamping( 0 );
hsolver->useDamp( true );
hsolver->stages.back()->damping( dampingSIN(t) );
}
else
{
sotDEBUG(5) << "Without damping. " << std::endl;
hsolver->useDamp( false );
}
/* -Tasks 1:n- */
/* Ctaski = [ Ji 0 0 0 0 0 ] */
for( int i=0;i<(int)stack.size();++i )
{
TaskAbstract & task = * stack[i];
MatrixXd & Ctask = Ctasks[i];
VectorBound & btask = btasks[i];
EIGEN_CONST_MATRIX_FROM_SIGNAL(J,task.jacobianSOUT(t));
const dg::sot::VectorMultiBound & ddx = task.taskSOUT(t);
const int nx = ddx.size();
assert( Ctask.rows() == nx && btask.size() == nx );
assert( J.rows()==nx && J.cols()==nbDofs && (int)ddx.size()==nx );
Ctask = J; COPY_MB_VECTOR_TO_EIGEN(ddx,btask);
sotDEBUG(15) << "Ctask"<<i<<" = " << (MATLAB)Ctask << std::endl;
sotDEBUG(1) << "btask"<<i<<" = " << btask << std::endl;
}
/* --- */
sotDEBUG(1) << "Initial config." << std::endl;
if(relevantActiveSet)
hsolver->setInitialActiveSet(activeSet);
else hsolver->setInitialActiveSet();
struct timeval t0,t1;
gettimeofday(&t0,NULL);
sotDEBUG(1) << "Run for a solution." << std::endl;
hsolver->activeSearch(solution);
sotDEBUG(1) << "solution = " << (MATLAB)solution << std::endl;
gettimeofday(&t1,NULL);
time = ((t1.tv_sec-t0.tv_sec)+(t1.tv_usec-t0.tv_usec)/1.0e6);
activeSet = hsolver->getOptimalActiveSet(); relevantActiveSet = true;
if( controlFreeFloating )
{
EIGEN_VECTOR_FROM_VECTOR( control,mlcontrol,nbDofs );
control=solution;
}
else
{
EIGEN_VECTOR_FROM_VECTOR( control,mlcontrol,nbDofs-6 );
control=solution.tail( nbDofs-6 );
}
fup << t << "\t" << HCODUpdateCounter::count
<< " " << time << " " << hsolver->sizeA();
fup << std::endl;
HCODUpdateCounter::count = 0;
sotDEBUG(1) << "control = " << mlcontrol << std::endl;
return mlcontrol;
}
/* --- COMMANDS ---------------------------------------------------------- */
/* --- COMMANDS ---------------------------------------------------------- */
/* --- COMMANDS ---------------------------------------------------------- */
void SolverKine::
debugOnce( void )
{
std::cout << "Open the trace"<<std::endl;
dg::sot::DebugTrace::openFile("/tmp/sot.txt");
hsolver->debugOnce("/tmp/soth.txt",true);
}
void SolverKine::
resetAset( void )
{
relevantActiveSet = false;
}
void SolverKine::
getDecomposition(const int & i)
{
using namespace soth;
std::cout << "M"<<i<<" = " << (MATLAB) hsolver -> stage(i).getM() << std::endl;
std::cout << "L"<<i<<" = " << (MATLAB)(MatrixXd) (hsolver -> stage(i).getLtri()) << std::endl;
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
}
/* --- ENTITY ----------------------------------------------------------- */
/* --- ENTITY ----------------------------------------------------------- */
/* --- ENTITY ----------------------------------------------------------- */
void SolverKine::
display( std::ostream& os ) const
{
os << "SolverKine "<<getName() << ": " << nbDofs <<" joints." << std::endl;
try{
os <<"control = "<<controlSOUT.accessCopy() <<std::endl << std::endl;
} catch (dynamicgraph::ExceptionSignal e) {}
stack_t::display(os);
}
void SolverKine::
commandLine( const std::string& cmdLine,
std::istringstream& cmdArgs,
std::ostream& os )
{
if( cmdLine == "help" )
{
os << "SolverKine:\n"
<< "\t- debugOnce: open trace-file for next iteration of the solver." << std::endl;
stackCommandLine( cmdLine,cmdArgs,os );
Entity::commandLine( cmdLine,cmdArgs,os );
}
else if( cmdLine == "debugOnce" )
{
debugOnce();
}
/* TODO: add controlFreeFloating g/setter. */
else if( stackCommandLine( cmdLine,cmdArgs,os ) );
else
{
Entity::commandLine( cmdLine,cmdArgs,os );
}
}
} // namespace dyninv
} // namespace sot
} // namespace dynamicgraph