basic/kinematics/DH_Link.cpp

Go to the documentation of this file.
00001 //## begin module%3707C4FC037A.cm preserve=no
00002 //        %X% %Q% %Z% %W%
00003 //## end module%3707C4FC037A.cm
00004 
00005 //## begin module%3707C4FC037A.cp preserve=no
00006 //## end module%3707C4FC037A.cp
00007 
00008 //## Module: DH_Link%3707C4FC037A; Pseudo Package body
00009 //## Source file: E:\mpk\code\Kinematics\DH_Link.cpp
00010 
00011 //## begin module%3707C4FC037A.additionalIncludes preserve=no
00012 //## end module%3707C4FC037A.additionalIncludes
00013 
00014 //## begin module%3707C4FC037A.includes preserve=yes
00015 #include <assert.h>
00016 #include <math/math2.h>
00017 #include <string/string2.h>
00018 #include <streams/mystreams.h>
00019 #include <serializable/MPK_Dir.h>
00020 //## end module%3707C4FC037A.includes
00021 
00022 // VRML_Reader
00023 #include "geometry/VRML_Reader.h"
00024 // Frame
00025 #include "geometry/Frame.h"
00026 // ObjectBase
00027 #include "geometry/ObjectBase.h"
00028 // VRML_20_Reader
00029 #include "geometry/VRML_20_Reader.h"
00030 // DH_Link
00031 #include "Kinematics/DH_Link.h"
00032 // IfstreamWithComments
00033 #include "additional/streams/IfstreamWithComments.h"
00034 //## begin module%3707C4FC037A.additionalDeclarations preserve=yes
00035 //## end module%3707C4FC037A.additionalDeclarations
00036 
00037 
00038 // Class DH_Link 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 DH_Link::DH_Link (FrameManager*  frameManager)
00048   //## begin DH_Link::DH_Link%924391851.hasinit preserve=no
00049   //## end DH_Link::DH_Link%924391851.hasinit
00050   //## begin DH_Link::DH_Link%924391851.initialization preserve=yes
00051   :LinkBase( frameManager ),
00052   a( 0 ),
00053   alpha( 0 ),
00054   d( 0 ),
00055   theta( 0 ),
00056   q( NULL ),
00057   type( DH_NONE )
00058   //## end DH_Link::DH_Link%924391851.initialization
00059 {
00060   //## begin DH_Link::DH_Link%924391851.body preserve=yes
00061         frame = frameManager->AddFrame() ;
00062         controlledFrames.push_back( FrameNum() ) ;
00063   //## end DH_Link::DH_Link%924391851.body
00064 }
00065 
00066 DH_Link::DH_Link (const DH_Link& right)
00067   //## begin DH_Link::DH_Link%925178921.hasinit preserve=no
00068   //## end DH_Link::DH_Link%925178921.hasinit
00069   //## begin DH_Link::DH_Link%925178921.initialization preserve=yes
00070   :
00071         LinkBase( right ),
00072         a( right.a ),
00073         alpha( right.alpha ),
00074         d( right.d ),
00075         theta( right.theta ),
00076         type( right.type )
00077   //## end DH_Link::DH_Link%925178921.initialization
00078 {
00079   //## begin DH_Link::DH_Link%925178921.body preserve=yes
00080         frame = right.frame ;           //IMPROVE: does this leave us with two pointers to the same frame?
00081 
00082         //make sure that our q is mapped to the same joint variable
00083         if( right.q == &right.a )
00084         {
00085                 q = &a ;
00086         }
00087         else if( right.q == &right.alpha )
00088         {
00089                 q = &alpha ;
00090         }
00091         else if( right.q == &right.d )
00092         {
00093                 q = &d ;
00094         }
00095         else if( right.q == &right.theta )
00096         {
00097                 q = &theta ;
00098         }
00099         else if( right.q == NULL )
00100         {
00101                 q = NULL ;
00102         }
00103         else
00104         {
00105                 assert( false ) ; //should never get to here 
00106         }
00107   //## end DH_Link::DH_Link%925178921.body
00108 }
00109 
00110 
00111 DH_Link::~DH_Link()
00112 {
00113   //## begin DH_Link::~DH_Link%.body preserve=yes
00114         q = NULL ;
00115         
00116   //## end DH_Link::~DH_Link%.body
00117 }
00118 
00119 
00120 
00121 //## Other Operations (implementation)
00122 void DH_Link::UpdateFrames () const
00123 {
00124   //## begin DH_Link::UpdateFrames%924391847.body preserve=yes
00125         //get a reference to the frame
00126         //Frame* theFrame = frameManager->GetFrameRef( frame ) ;
00127         Frame* theFrame = new Frame;
00128         assert( theFrame != NULL ) ;
00129 
00130         //start updating the indicies of the frame
00131         //row 0
00132         (*theFrame)( 0, 0 ) = CosDeg( theta );
00133         (*theFrame)( 0, 1 ) = -SinDeg( theta );
00134         (*theFrame)( 0, 2 ) = 0;
00135         (*theFrame)( 0, 3 ) = a;
00136 
00137         //row 1
00138         (*theFrame)( 1, 0 ) = SinDeg( theta ) * CosDeg( alpha );
00139         (*theFrame)( 1, 1 ) = CosDeg( theta ) * CosDeg( alpha );
00140         (*theFrame)( 1, 2 ) = -SinDeg( alpha );
00141         (*theFrame)( 1, 3 ) = -SinDeg( alpha ) * ( d );
00142 
00143         //row 2
00144         (*theFrame)( 2, 0 ) = SinDeg( theta ) * SinDeg( alpha );
00145         (*theFrame)( 2, 1 ) = CosDeg( theta ) * SinDeg( alpha );
00146         (*theFrame)( 2, 2 ) = CosDeg( alpha );
00147         (*theFrame)( 2, 3 ) = CosDeg( alpha ) * ( d );
00148 
00149         //row 3
00150         (*theFrame)( 3, 0 ) = 0;
00151         (*theFrame)( 3, 1 ) = 0;
00152         (*theFrame)( 3, 2 ) = 0;
00153         (*theFrame)( 3, 3 ) = 1;
00154 
00155         frameManager->GetFrameRef( frame )->SetValues( this->positioningFrame * *theFrame );
00156         frameManager->MarkAllFramesChanged() ;
00157         delete theFrame;
00158   //## end DH_Link::UpdateFrames%924391847.body
00159 }
00160 
00161 unsigned int DH_Link::FrameNum () const
00162 {
00163   //## begin DH_Link::FrameNum%924391852.body preserve=yes
00164         return frame ;
00165   //## end DH_Link::FrameNum%924391852.body
00166 }
00167 
00168 LinkBase* DH_Link::Clone () const
00169 {
00170   //## begin DH_Link::Clone%924739818.body preserve=yes
00171         return new DH_Link( *this ) ;
00172   //## end DH_Link::Clone%924739818.body
00173 }
00174 
00175 void DH_Link::SetAlpha (const double value)
00176 {
00177   //## begin DH_Link::SetAlpha%925324839.body preserve=yes
00178         alpha = value ;
00179   //## end DH_Link::SetAlpha%925324839.body
00180 }
00181 
00182 void DH_Link::SetA (const double value)
00183 {
00184   //## begin DH_Link::SetA%925324840.body preserve=yes
00185         a = value ;
00186   //## end DH_Link::SetA%925324840.body
00187 }
00188 
00189 void DH_Link::SetD (const double value)
00190 {
00191   //## begin DH_Link::SetD%925324841.body preserve=yes
00192         d = value ;
00193   //## end DH_Link::SetD%925324841.body
00194 }
00195 
00196 void DH_Link::SetTheta (const double value)
00197 {
00198   //## begin DH_Link::SetTheta%925324842.body preserve=yes
00199         theta = value ;
00200   //## end DH_Link::SetTheta%925324842.body
00201 }
00202 
00203 void DH_Link::SetControllingParameter (DH_Parameter parameter)
00204 {
00205   //## begin DH_Link::SetControllingParameter%925420850.body preserve=yes
00206         //IMPROVE: is this function properly named?
00207         type = parameter ;
00208         switch( parameter )
00209         {
00210         case DH_ALPHA:
00211                 {
00212                         q = &alpha ;
00213                 }
00214                 break ;
00215         case DH_A :
00216                 {
00217                         q = &a ;
00218                 }
00219                 break ;
00220         case DH_D :
00221                 {
00222                         q = &d ;
00223                 }
00224                 break ;
00225         case DH_THETA :
00226                 {
00227                         q = &theta ;
00228                 }
00229                 break ;
00230         default :
00231                 {
00232                         assert( false ) ;       //should never get to here
00233                 }
00234         }
00235   //## end DH_Link::SetControllingParameter%925420850.body
00236 }
00237 
00238 void DH_Link::SetJointVariable (const double newValue)
00239 {
00240   //## begin DH_Link::SetJointVariable%925420852.body preserve=yes
00241         assert( q != NULL ) ;
00242         *q = newValue ;
00243         //IMPROVE: should we check here to see if this value is a valid one or not?
00244   //## end DH_Link::SetJointVariable%925420852.body
00245 }
00246 
00247 bool DH_Link::DoesLinkControlFrame (const unsigned int frameNum) const
00248 {
00249   //## begin DH_Link::DoesLinkControlFrame%928263349.body preserve=yes
00250         if( frameNum == frame )
00251         {
00252                 return true ;
00253         }
00254         return false ;
00255   //## end DH_Link::DoesLinkControlFrame%928263349.body
00256 }
00257 
00258 void DH_Link::Serialize (ostream& os) const
00259 {
00260   //## begin DH_Link::Serialize%933801284.body preserve=yes
00261         os << *this ;
00262   //## end DH_Link::Serialize%933801284.body
00263 }
00264 
00265 void DH_Link::AddObject (const ObjectBase* object)
00266 {
00267   //## begin DH_Link::AddObject%932766018.body preserve=yes
00268         //IMPROVE: this should call a member function in the base class not use object directly
00269         assert( object != NULL ) ;
00270         ObjectBase* newObject = dynamic_cast< ObjectBase* >( object->Clone() ) ;
00271         newObject->SetBaseFrame( FrameNum() ) ;
00272         objects.push_back( newObject ) ;
00273   //## end DH_Link::AddObject%932766018.body
00274 }
00275 
00276 bool DH_Link::DeSerialize (IfstreamWithComments& is)
00277 {
00278   //## begin DH_Link::DeSerialize%933801287.body preserve=yes
00279 
00280         //do the deserialization of the base class
00281         LinkBase::DeSerialize( is ) ;
00282 
00283         is.eatwhite() ;
00284 
00285         is >> a ;
00286         is >> alpha ;
00287         is >> d ;
00288         is >> theta ;
00289         is >> frame ;
00290         
00291         int typeInt ;
00292         is >> typeInt ;
00293         type = DH_Parameter( typeInt ) ;                //IMPROVE: enough with the old style casts
00294         SetControllingParameter( type ) ;
00295 
00296         //read in shapes
00297         char marker[ 300 ] = "" ;
00298         is.eatwhite() ;
00299         is >> marker ;
00300         while( stricmp( marker, "#shape" ) == 0 )
00301         {
00302                 //read in the filename
00303                 char filename[ 300 ] ;
00304                 is.getline( filename, 300 ) ;
00305                 MPKstring::TrimLeadingWhitespace( filename ) ;
00306                 MPKstring::TrimTrailingWhitespace( filename ) ;
00307                 
00308                 char fullname[_MAX_PATH];
00309                 GetAbsoluteFileName(fullname, _MAX_PATH, is.path, filename);
00310                 //VRML_Reader reader ;
00311                 VRML_20_Reader reader ;
00312                 //reader.SetRootPath( currentPath );
00313                 reader.SetFilename( fullname ) ;
00314 
00315                 bool readerSuccess = reader.Read() ;
00316                 assert( readerSuccess ) ;
00317                 //AddObject( reader.GetObjectA() ) ;
00318                 assert( reader.GetTheObject() != NULL ) ;
00319                 if ((readerSuccess == false) || (reader.GetTheObject()==NULL))
00320                         return false;
00321 
00322                 Entity* theEntity = reader.GetTheObject()->Clone()  ;
00323                 ObjectBase* theObject = dynamic_cast< ObjectBase* >( theEntity );
00324                 assert( theObject != NULL );
00325                 theObject->SetName( filename ) ;
00326                 AddObject( theObject ) ;
00327                 delete theObject ;
00328 
00329                 //open the file and add it to the link
00330                 is.eatwhite() ;
00331                 is >> marker ;
00332         }
00333         assert( stricmp( marker, "#end_dh_link" ) == 0 ) ;
00334         return true;
00335   //## end DH_Link::DeSerialize%933801287.body
00336 }
00337 
00338 //exposes the type of this joint
00339 DH_Parameter DH_Link::GetJointType() const
00340 {
00341         return this->type;
00342 }
00343 
00344 double DH_Link::GetJointVariable () const
00345 {
00346   //## begin DH_Link::GetJointVariable%940357561.body preserve=yes
00347         return *q ;
00348   //## end DH_Link::GetJointVariable%940357561.body
00349 }
00350 
00351 Frame DH_Link::GetFrame () const
00352 {
00353   //## begin DH_Link::GetFrame%943395407.body preserve=yes
00354 
00355         //IMPROVE: this is a HACK method of building a frame out of DH parameters.
00356         //IMPROVE: this code should not be in two places
00357         //get a reference to the frame
00358         Frame returnMe ;
00359         Frame* theFrame = &returnMe ;
00360 
00361         //start updating the indicies of the frame
00362         //row 0
00363         (*theFrame)( 0, 0 ) = CosDeg( theta ) ;
00364         (*theFrame)( 0, 1 ) = -SinDeg( theta ) ;
00365         (*theFrame)( 0, 2 ) = 0 ;
00366         (*theFrame)( 0, 3 ) = a ;
00367 
00368         //row 1
00369         (*theFrame)( 1, 0 ) = SinDeg( theta ) * CosDeg( alpha ) ;
00370         (*theFrame)( 1, 1 ) = CosDeg( theta ) * CosDeg( alpha ) ;
00371         (*theFrame)( 1, 2 ) = -SinDeg( alpha ) ;
00372         (*theFrame)( 1, 3 ) = -SinDeg( alpha ) * ( d ) ;
00373 
00374         //row 2
00375         (*theFrame)( 2, 0 ) = SinDeg( theta ) * SinDeg( alpha ) ;
00376         (*theFrame)( 2, 1 ) = CosDeg( theta ) * SinDeg( alpha ) ;
00377         (*theFrame)( 2, 2 ) = CosDeg( alpha ) ;
00378         (*theFrame)( 2, 3 ) = CosDeg( alpha ) * ( d ) ;
00379 
00380         //row 3
00381         (*theFrame)( 3, 0 ) = 0 ;
00382         (*theFrame)( 3, 1 ) = 0 ;
00383         (*theFrame)( 3, 2 ) = 0 ;
00384         (*theFrame)( 3, 3 ) = 1 ;
00385         return returnMe ;
00386   //## end DH_Link::GetFrame%943395407.body
00387 }
00388 
00389 //This function is added by Zhenwang.Yao to replace the previous function
00390 Frame& DH_Link::GetFrame (Frame &frame)
00391 {
00392         Frame* theFrame = &frame ;
00393 
00394         //start updating the indicies of the frame
00395         //row 0
00396         (*theFrame)( 0, 0 ) = CosDeg( theta ) ;
00397         (*theFrame)( 0, 1 ) = -SinDeg( theta ) ;
00398         (*theFrame)( 0, 2 ) = 0 ;
00399         (*theFrame)( 0, 3 ) = a ;
00400 
00401         //row 1
00402         (*theFrame)( 1, 0 ) = SinDeg( theta ) * CosDeg( alpha ) ;
00403         (*theFrame)( 1, 1 ) = CosDeg( theta ) * CosDeg( alpha ) ;
00404         (*theFrame)( 1, 2 ) = -SinDeg( alpha ) ;
00405         (*theFrame)( 1, 3 ) = -SinDeg( alpha ) * ( d ) ;
00406 
00407         //row 2
00408         (*theFrame)( 2, 0 ) = SinDeg( theta ) * SinDeg( alpha ) ;
00409         (*theFrame)( 2, 1 ) = CosDeg( theta ) * SinDeg( alpha ) ;
00410         (*theFrame)( 2, 2 ) = CosDeg( alpha ) ;
00411         (*theFrame)( 2, 3 ) = CosDeg( alpha ) * ( d ) ;
00412 
00413         //row 3
00414         (*theFrame)( 3, 0 ) = 0 ;
00415         (*theFrame)( 3, 1 ) = 0 ;
00416         (*theFrame)( 3, 2 ) = 0 ;
00417         (*theFrame)( 3, 3 ) = 1 ;
00418         
00419   return frame ;
00420   //## end DH_Link::GetFrame%943395407.body
00421 }
00422 
00423 // Additional Declarations
00424   //## begin DH_Link%3707C4FC037A.declarations preserve=yes
00425 //-----------------------------------------------------------------------------
00426 std::ostream & operator<<(std::ostream &os, const DH_Link& v)
00427 {
00428         using std::endl ;
00429         os << "#link" << endl ;
00430         os << "#dh_link" << endl ;
00431         v.LinkBase::Serialize( os ) ;
00432         os << v.a << '\t' ;
00433         os << v.alpha << '\t' ;
00434         os << v.d << '\t' ;
00435         os << v.theta << '\t' ;
00436         os << v.frame << '\t' ;
00437         os << v.type << '\t' ;
00438         os << endl ;
00439         os << "#end_dh_link" << endl ;
00440         return os ;
00441 }
00442 //-----------------------------------------------------------------------------
00443 IfstreamWithComments & operator>>(IfstreamWithComments &is, DH_Link& v)
00444 {
00445         char marker[ 300 ] = "" ;
00446 
00447         //verify the first few lines - header lines
00448         eatwhite( is ) ;
00449         is.getline( marker, 300 ) ;
00450         assert( strcmp( marker, "#link" ) == 0 ) ;
00451 
00452         eatwhite( is ) ;
00453         is.getline( marker, 300 ) ;
00454         assert( strcmp( marker, "#dh_link" ) == 0 ) ;
00455 
00456         v.DeSerialize( is ) ;           //IMPROVE: need to override this function for DH_Links
00457 
00458         eatwhite( is ) ;
00459         is.getline( marker, 300 ) ;
00460         assert( strcmp( marker, "#end_dh_link" ) == 0 ) ;
00461 
00462         return is ;
00463 }
00464 //-----------------------------------------------------------------------------
00465   //## end DH_Link%3707C4FC037A.declarations
00466 //## begin module%3707C4FC037A.epilog preserve=yes
00467 //## end module%3707C4FC037A.epilog

Generated on Sat Apr 1 21:30:34 2006 for Motion Planning Kernel by  doxygen 1.4.6-NO