00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00021
00022
00023 #include "geometry/VRML_Reader.h"
00024
00025 #include "geometry/Frame.h"
00026
00027 #include "geometry/ObjectBase.h"
00028
00029 #include "geometry/VRML_20_Reader.h"
00030
00031 #include "Kinematics/DH_Link.h"
00032
00033 #include "additional/streams/IfstreamWithComments.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 DH_Link::DH_Link (FrameManager* frameManager)
00048
00049
00050
00051 :LinkBase( frameManager ),
00052 a( 0 ),
00053 alpha( 0 ),
00054 d( 0 ),
00055 theta( 0 ),
00056 q( NULL ),
00057 type( DH_NONE )
00058
00059 {
00060
00061 frame = frameManager->AddFrame() ;
00062 controlledFrames.push_back( FrameNum() ) ;
00063
00064 }
00065
00066 DH_Link::DH_Link (const DH_Link& right)
00067
00068
00069
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
00078 {
00079
00080 frame = right.frame ;
00081
00082
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 ) ;
00106 }
00107
00108 }
00109
00110
00111 DH_Link::~DH_Link()
00112 {
00113
00114 q = NULL ;
00115
00116
00117 }
00118
00119
00120
00121
00122 void DH_Link::UpdateFrames () const
00123 {
00124
00125
00126
00127 Frame* theFrame = new Frame;
00128 assert( theFrame != NULL ) ;
00129
00130
00131
00132 (*theFrame)( 0, 0 ) = CosDeg( theta );
00133 (*theFrame)( 0, 1 ) = -SinDeg( theta );
00134 (*theFrame)( 0, 2 ) = 0;
00135 (*theFrame)( 0, 3 ) = a;
00136
00137
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
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
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
00159 }
00160
00161 unsigned int DH_Link::FrameNum () const
00162 {
00163
00164 return frame ;
00165
00166 }
00167
00168 LinkBase* DH_Link::Clone () const
00169 {
00170
00171 return new DH_Link( *this ) ;
00172
00173 }
00174
00175 void DH_Link::SetAlpha (const double value)
00176 {
00177
00178 alpha = value ;
00179
00180 }
00181
00182 void DH_Link::SetA (const double value)
00183 {
00184
00185 a = value ;
00186
00187 }
00188
00189 void DH_Link::SetD (const double value)
00190 {
00191
00192 d = value ;
00193
00194 }
00195
00196 void DH_Link::SetTheta (const double value)
00197 {
00198
00199 theta = value ;
00200
00201 }
00202
00203 void DH_Link::SetControllingParameter (DH_Parameter parameter)
00204 {
00205
00206
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 ) ;
00233 }
00234 }
00235
00236 }
00237
00238 void DH_Link::SetJointVariable (const double newValue)
00239 {
00240
00241 assert( q != NULL ) ;
00242 *q = newValue ;
00243
00244
00245 }
00246
00247 bool DH_Link::DoesLinkControlFrame (const unsigned int frameNum) const
00248 {
00249
00250 if( frameNum == frame )
00251 {
00252 return true ;
00253 }
00254 return false ;
00255
00256 }
00257
00258 void DH_Link::Serialize (ostream& os) const
00259 {
00260
00261 os << *this ;
00262
00263 }
00264
00265 void DH_Link::AddObject (const ObjectBase* object)
00266 {
00267
00268
00269 assert( object != NULL ) ;
00270 ObjectBase* newObject = dynamic_cast< ObjectBase* >( object->Clone() ) ;
00271 newObject->SetBaseFrame( FrameNum() ) ;
00272 objects.push_back( newObject ) ;
00273
00274 }
00275
00276 bool DH_Link::DeSerialize (IfstreamWithComments& is)
00277 {
00278
00279
00280
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 ) ;
00294 SetControllingParameter( type ) ;
00295
00296
00297 char marker[ 300 ] = "" ;
00298 is.eatwhite() ;
00299 is >> marker ;
00300 while( stricmp( marker, "#shape" ) == 0 )
00301 {
00302
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
00311 VRML_20_Reader reader ;
00312
00313 reader.SetFilename( fullname ) ;
00314
00315 bool readerSuccess = reader.Read() ;
00316 assert( readerSuccess ) ;
00317
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
00330 is.eatwhite() ;
00331 is >> marker ;
00332 }
00333 assert( stricmp( marker, "#end_dh_link" ) == 0 ) ;
00334 return true;
00335
00336 }
00337
00338
00339 DH_Parameter DH_Link::GetJointType() const
00340 {
00341 return this->type;
00342 }
00343
00344 double DH_Link::GetJointVariable () const
00345 {
00346
00347 return *q ;
00348
00349 }
00350
00351 Frame DH_Link::GetFrame () const
00352 {
00353
00354
00355
00356
00357
00358 Frame returnMe ;
00359 Frame* theFrame = &returnMe ;
00360
00361
00362
00363 (*theFrame)( 0, 0 ) = CosDeg( theta ) ;
00364 (*theFrame)( 0, 1 ) = -SinDeg( theta ) ;
00365 (*theFrame)( 0, 2 ) = 0 ;
00366 (*theFrame)( 0, 3 ) = a ;
00367
00368
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
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
00381 (*theFrame)( 3, 0 ) = 0 ;
00382 (*theFrame)( 3, 1 ) = 0 ;
00383 (*theFrame)( 3, 2 ) = 0 ;
00384 (*theFrame)( 3, 3 ) = 1 ;
00385 return returnMe ;
00386
00387 }
00388
00389
00390 Frame& DH_Link::GetFrame (Frame &frame)
00391 {
00392 Frame* theFrame = &frame ;
00393
00394
00395
00396 (*theFrame)( 0, 0 ) = CosDeg( theta ) ;
00397 (*theFrame)( 0, 1 ) = -SinDeg( theta ) ;
00398 (*theFrame)( 0, 2 ) = 0 ;
00399 (*theFrame)( 0, 3 ) = a ;
00400
00401
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
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
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
00421 }
00422
00423
00424
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
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 ) ;
00457
00458 eatwhite( is ) ;
00459 is.getline( marker, 300 ) ;
00460 assert( strcmp( marker, "#end_dh_link" ) == 0 ) ;
00461
00462 return is ;
00463 }
00464
00465
00466
00467