experiments/src/musclepair.cpp

00001 /********************************************************************************
00002  *  FARSA Experimentes Library                                                  *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00005  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00006  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00007  *  Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it>                           *
00008  *                                                                              *
00009  *  This program is free software; you can redistribute it and/or modify        *
00010  *  it under the terms of the GNU General Public License as published by        *
00011  *  the Free Software Foundation; either version 2 of the License, or           *
00012  *  (at your option) any later version.                                         *
00013  *                                                                              *
00014  *  This program is distributed in the hope that it will be useful,             *
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00017  *  GNU General Public License for more details.                                *
00018  *                                                                              *
00019  *  You should have received a copy of the GNU General Public License           *
00020  *  along with this program; if not, write to the Free Software                 *
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00022  ********************************************************************************/
00023 
00024 #include "musclepair.h"
00025 #include "phyjoint.h"
00026 
00027 /*
00028 dopo aver inizializzato con i valori di Gianluca
00029 1)setActivation()
00030 2)aplly ritorna la velocità da applicare al giunto
00031 
00032 */
00033 
00034 namespace farsa {
00035 
00036 MusclePair::MusclePair( PhyDOF* dof, float ksh, float tmax, float rl, float lmax ) {
00037     //vedere parametri di Gianluca
00038     this->dof = dof;
00039     this->ksh = ksh;
00040     reduceKsh = 1.0;
00041     this->tmax = tmax;
00042     this->rl = rl;
00043     this->lmax = lmax;
00044     lmin = 2.0f*rl - lmax;
00045     this->ash = (rl*rl) / ( (lmax-rl)*(lmax-rl) );
00046     this->b = 1.0f;
00047     getExtension( last_x1, last_x2 );
00048     this->dx1 = 0.0f;
00049     this->dx2 = 0.0f;
00050 }
00051 
00052 void MusclePair::setRestLength( float rl ) {
00053     this->rl = rl;
00054     lmin = 2.0f*rl - lmax;
00055     ash = (rl*rl) / ( (lmax-rl)*(lmax-rl) );
00056 }
00057 
00058 float MusclePair::getRestLength() {
00059     return rl;
00060 }
00061 
00062 void MusclePair::setMaxTension( float tmax ) {
00063     this->tmax = tmax;
00064 }
00065 
00066 float MusclePair::getMaxTension( ) {
00067     return tmax;
00068 }
00069 
00070 void MusclePair::setMaxLength( float lmax ) {
00071     this->lmax = lmax;
00072     lmin = 2.0f*rl - lmax;
00073     ash = (rl*rl) / ( (lmax-rl)*(lmax-rl) );
00074 }
00075 
00076 float MusclePair::getMaxLength() {
00077     return lmax;
00078 }
00079 
00080 float MusclePair::getMinLength() {
00081     return lmin;
00082 }
00083 
00084 float MusclePair::getAsh( ) {
00085     return ash;
00086 }
00087 
00088 void MusclePair::setAsh( float newash ) {
00089     ash = newash;
00090 }
00091 
00092 void MusclePair::setKsh( float ksh, float reduceFactor ) {
00093     this->ksh = ksh;
00094     reduceKsh = reduceFactor;
00095 }
00096 
00097 float MusclePair::getKsh( ) {
00098     return ksh;
00099 }
00100 
00101 void MusclePair::setViscosity( float b ) {
00102     this->b = b;
00103 }
00104 
00105 float MusclePair::getViscosity( ) {
00106     return b;
00107 }
00108 
00109 void MusclePair::setActivation( float act1, float act2 ) {
00110     //da fare prima di apply
00111     //act1 e act2 devono andare da 0 a 1
00112     this->act1 = act1;
00113     this->act2 = act2;
00114 }
00115 
00116 void MusclePair::getActivation( float& act1, float& act2 ) {
00117     act1 = this->act1;
00118     act2 = this->act2;
00119 }
00120 
00121 void MusclePair::getExtension( float& l1, float& l2 ) {
00122     float angle = dof->position();
00123     real lowLim, higLim;
00124     dof->limits( lowLim, higLim );
00125     l1 = linearMap( angle, lowLim, higLim, lmin, lmax );
00126     l2 = invLinearMap( angle, lowLim, higLim, lmin, lmax );
00127 }
00128 
00129 float MusclePair::apply() {
00130     float x1, x2;
00131     getExtension( x1, x2 );
00132     // Calculate the current velocity of shortening/lengthing of muscles
00133     float timestep = dof->joint()->world()->timeStep();
00134     dx1 = ( x1 - last_x1 ) / timestep;
00135     dx2 = ( x2 - last_x2 ) / timestep;
00136 
00137     float ta1 = act1 * ( -ash*tmax*(x1-rl)*(x1-rl)/(rl*rl) + tmax );
00138     float tp1 = reduceKsh*0.5*tmax * ( exp( ksh*(x1-rl)/(lmax-rl) ) - 1.0f ) / ( exp( ksh ) - 1.0f );
00139     float tv1 = b * dx1;
00140 
00141     float ta2 = act2 * ( -ash*tmax*(x2-rl)*(x2-rl)/(rl*rl) + tmax );
00142     float tp2 = reduceKsh*0.5*tmax * ( exp( ksh*(x2-rl)/(lmax-rl) ) - 1.0f ) / ( exp( ksh ) - 1.0f );
00143     float tv2 = b * dx2;
00144 
00145     tal1 = ta1;
00146     tal2 = ta2;
00147 
00148     last_x1 = x1;
00149     last_x2 = x2;
00150 
00151     return -(ta1+tp1+tv1) + (ta2+tp2+tv2);
00152 }
00153 
00154 } // end namespace farsa