Skip to content

Commit 43de984

Browse files
Olivier StasseOlivier Stasse
authored andcommitted
Merge pull request #34 from andreadelprete/master
Extend Device to allow for position control
2 parents afa50c1 + e854acb commit 43de984

File tree

2 files changed

+92
-47
lines changed

2 files changed

+92
-47
lines changed

include/sot/core/device.hh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ namespace ml= maal::boost;
3838
namespace dynamicgraph {
3939
namespace sot {
4040

41+
/// Define the type of input expected by the robot
42+
enum ControlInput
43+
{
44+
CONTROL_INPUT_POSITION=0,
45+
CONTROL_INPUT_VELOCITY=1,
46+
CONTROL_INPUT_ACCELERATION=2,
47+
CONTROL_INPUT_SIZE=3
48+
};
49+
50+
const std::string ControlInput_s[] =
51+
{
52+
"position", "velocity", "acceleration"
53+
};
54+
4155
/* --------------------------------------------------------------------- */
4256
/* --- CLASS ----------------------------------------------------------- */
4357
/* --------------------------------------------------------------------- */
@@ -64,7 +78,7 @@ namespace dynamicgraph {
6478
ml::Vector velocity_;
6579
bool vel_controlInit_;
6680
ml::Vector vel_control_;
67-
bool secondOrderIntegration_;
81+
ControlInput controlInputType_;
6882
bool withForceSignals[4];
6983
PeriodicCall periodicCallBefore_;
7084
PeriodicCall periodicCallAfter_;
@@ -80,6 +94,7 @@ namespace dynamicgraph {
8094
void setVelocitySize(const unsigned int& size);
8195
virtual void setVelocity(const ml::Vector & vel);
8296
virtual void setSecondOrderIntegration();
97+
virtual void setControlInputType(const std::string& cit);
8398
virtual void increment(const double & dt = 5e-2);
8499

85100
public: /* --- DISPLAY --- */

src/tools/device.cpp

Lines changed: 76 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ void Device::integrateRollPitchYaw(ml::Vector& state, const ml::Vector& control,
4747
jrlMathTools::Vector3D<double> omega;
4848
// Translation part
4949
for (unsigned int i=0; i<3; i++) {
50-
state(i) += control(i)*dt;
51-
ffPose_(i,3) = state(i);
52-
omega(i) = control(i+3);
53-
}
50+
state(i) += control(i)*dt;
51+
ffPose_(i,3) = state(i);
52+
omega(i) = control(i+3);
53+
}
5454
// Rotation part
5555
double roll = state(3);
5656
double pitch = state(4);
@@ -73,20 +73,20 @@ void Device::integrateRollPitchYaw(ml::Vector& state, const ml::Vector& control,
7373
// Apply Rodrigues (1795–1851) formula for rotation about omega vector
7474
double angle = dt*omega.norm();
7575
if (angle == 0) {
76-
return;
77-
}
76+
return;
77+
}
7878
jrlMathTools::Vector3D<double> k = omega/omega.norm();
7979
// ei <- ei cos(angle) + sin(angle)(k ^ ei) + (k.ei)(1-cos(angle))k
8080
for (unsigned int i=0; i<3; i++) {
81-
jrlMathTools::Vector3D<double> ei = column[i];
82-
column[i] = ei*cos(angle) + (k^ei)*sin(angle) + k*((k*ei)*(1-cos(angle)));
83-
}
81+
jrlMathTools::Vector3D<double> ei = column[i];
82+
column[i] = ei*cos(angle) + (k^ei)*sin(angle) + k*((k*ei)*(1-cos(angle)));
83+
}
8484
// Store new position if ffPose_ member.
8585
for (unsigned int r = 0; r < 3; r++) {
86-
for (unsigned int c = 0; c < 3; c++) {
87-
ffPose_(r,c) = column[c](r);
88-
}
86+
for (unsigned int c = 0; c < 3; c++) {
87+
ffPose_(r,c) = column[c](r);
8988
}
89+
}
9090
const double & nx = column[2](2);
9191
const double & ny = column[1](2);
9292

@@ -106,16 +106,16 @@ Device::
106106
~Device( )
107107
{
108108
for( unsigned int i=0; i<4; ++i ) {
109-
delete forcesSOUT[i];
110-
}
109+
delete forcesSOUT[i];
110+
}
111111
}
112112

113113
Device::
114114
Device( const std::string& n )
115115
:Entity(n)
116116
,state_(6)
117117
,vel_controlInit_(false)
118-
,secondOrderIntegration_(false)
118+
,controlInputType_(CONTROL_INPUT_VELOCITY)
119119
,controlSIN( NULL,"Device("+n+")::input(double)::control" )
120120
//,attitudeSIN(NULL,"Device::input(matrixRot)::attitudeIN")
121121
,attitudeSIN(NULL,"Device::input(vector3)::attitudeIN")
@@ -196,6 +196,17 @@ Device( const std::string& n )
196196
command::makeCommandVoid0(*this,&Device::setSecondOrderIntegration,
197197
docstring));
198198

199+
/* SET of control input type. */
200+
docstring =
201+
"\n"
202+
" Set the type of control input which can be \n"
203+
" acceleration, velocity, or position\n"
204+
"\n";
205+
206+
addCommand("setControlInputType",
207+
new command::Setter<Device,string>
208+
(*this, &Device::setControlInputType, docstring));
209+
199210
// Handle commands and signals called in a synchronous way.
200211
periodicCallBefore_.addSpecificCommands(*this, commandMap, "before.");
201212
periodicCallAfter_.addSpecificCommands(*this, commandMap, "after.");
@@ -260,12 +271,25 @@ setRoot( const MatrixHomogeneous & worldMwaist )
260271
void Device::
261272
setSecondOrderIntegration()
262273
{
263-
secondOrderIntegration_ = true;
274+
controlInputType_ = CONTROL_INPUT_ACCELERATION;
264275
velocity_.resize(state_.size());
265276
velocity_.setZero();
266277
velocitySOUT.setConstant( velocity_ );
267278
}
268279

280+
void Device::
281+
setControlInputType(const std::string& cit)
282+
{
283+
for(int i=0; i<CONTROL_INPUT_SIZE; i++)
284+
if(cit==ControlInput_s[i])
285+
{
286+
controlInputType_ = (ControlInput)i;
287+
sotDEBUG(25)<<"Control input type: "<<ControlInput_s[i]<<endl;
288+
return;
289+
}
290+
sotDEBUG(25)<<"Unrecognized control input type: "<<cit<<endl;
291+
}
292+
269293
void Device::
270294
increment( const double & dt )
271295
{
@@ -309,19 +333,19 @@ increment( const double & dt )
309333
/* Position the signals corresponding to sensors. */
310334
stateSOUT .setConstant( state_ ); stateSOUT.setTime( time+1 );
311335
//computation of the velocity signal
312-
if( secondOrderIntegration_ )
313-
{
314-
velocitySOUT.setConstant( velocity_ );
315-
velocitySOUT.setTime( time+1 );
316-
}
336+
if( controlInputType_==CONTROL_INPUT_ACCELERATION )
337+
{
338+
velocitySOUT.setConstant( velocity_ );
339+
velocitySOUT.setTime( time+1 );
340+
}
317341
else
318-
{
319-
velocitySOUT.setConstant( controlSIN.accessCopy() );
320-
velocitySOUT.setTime( time+1 );
321-
}
342+
{
343+
velocitySOUT.setConstant( controlSIN.accessCopy() );
344+
velocitySOUT.setTime( time+1 );
345+
}
322346
for( int i=0;i<4;++i ){
323-
if( !withForceSignals[i] ) forcesSOUT[i]->setConstant(forceZero6);
324-
}
347+
if( !withForceSignals[i] ) forcesSOUT[i]->setConstant(forceZero6);
348+
}
325349
ml::Vector zmp(3); zmp.fill( .0 );
326350
ZMPPreviousControllerSOUT .setConstant( zmp );
327351

@@ -359,12 +383,18 @@ void Device::integrate( const double & dt )
359383
{
360384
const ml::Vector & control = controlSIN.accessCopy();
361385

386+
if (controlInputType_==CONTROL_INPUT_POSITION)
387+
{
388+
state_ = control;
389+
return;
390+
}
391+
362392
if( !vel_controlInit_ )
363-
{
364-
vel_control_ = ml::Vector(control.size());
365-
vel_control_.setZero();
366-
vel_controlInit_ = true;
367-
}
393+
{
394+
vel_control_ = ml::Vector(control.size());
395+
vel_control_.setZero();
396+
vel_controlInit_ = true;
397+
}
368398

369399
// If control size is state size - 6, integrate joint angles,
370400
// if control and state are of same size, integrate 6 first degrees of
@@ -373,27 +403,27 @@ void Device::integrate( const double & dt )
373403

374404

375405

376-
if (secondOrderIntegration_)
406+
if (controlInputType_==CONTROL_INPUT_ACCELERATION)
407+
{
408+
for( unsigned int i=0;i<control.size();++i )
377409
{
378-
for( unsigned int i=0;i<control.size();++i )
379-
{
380-
if(control.size() == velocity_.size()) offset = 0;
381-
vel_control_(i) = velocity_(i+offset) + control(i)*dt*0.5;
382-
velocity_(i+offset) = velocity_(i+offset) + control(i)*dt;
383-
}
410+
if(control.size() == velocity_.size()) offset = 0;
411+
vel_control_(i) = velocity_(i+offset) + control(i)*dt*0.5;
412+
velocity_(i+offset) = velocity_(i+offset) + control(i)*dt;
384413
}
414+
}
385415
else
386-
{
387-
vel_control_ = control;
388-
}
416+
{
417+
vel_control_ = control;
418+
}
389419

390420
if (vel_control_.size() == state_.size()) {
391-
offset = 0;
392-
integrateRollPitchYaw(state_, vel_control_, dt);
393-
}
421+
offset = 0;
422+
integrateRollPitchYaw(state_, vel_control_, dt);
423+
}
394424

395425
for( unsigned int i=6;i<state_.size();++i )
396-
{ state_(i) += (vel_control_(i-offset)*dt); }
426+
{ state_(i) += (vel_control_(i-offset)*dt); }
397427
}
398428

399429
/* --- DISPLAY ------------------------------------------------------------ */

0 commit comments

Comments
 (0)