Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

robot/RobotDescription.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: RobotDescription.cpp 1039 2004-02-11 20:50:52Z jungd $
00019   $Revision: 1.4 $
00020   $Date: 2004-02-11 15:50:52 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <robot/RobotDescription>
00026 
00027 #include <base/Externalizer>
00028 #include <base/Application>
00029 #include <base/VFile>
00030 
00031 using robot::RobotDescription;
00032 
00033 using base::Externalizer;
00034 using base::Application;
00035 using base::VFile;
00036 using base::PathName;
00037 using base::dom::DOMNode;
00038 using base::dom::DOMElement;
00039 using robot::KinematicChain;
00040 
00041 
00042 KinematicChain RobotDescription::getKinematicChain(Int platformDOF, const base::Matrix4& platformTransform,
00043                                                    Int manipIndex, Int manipChainIndex) const
00044 {
00045   // construct a chain from platform, manipulator offset, manipulator base transform and manipulator chain
00046   KinematicChain chain( platform()->getKinematicChain(platformDOF, platformTransform) );
00047 
00048   if (manipulators().size() > 0) {
00049     if (manipIndex < manipulators().size()) {
00050       Matrix4 offsetTranslation; offsetTranslation.setToTranslation( manipulatorOffsets()[manipIndex] );
00051       chain += KinematicChain::Link(offsetTranslation); // manip mount offset from platform (fixed - 0-dof)
00052       chain += KinematicChain::Link( manipulators()[manipIndex]->getBaseTransform() ); // mount to base frame (fixed - 0-dof)
00053       chain += manipulators()[manipIndex]->getKinematicChain(manipChainIndex);
00054     }
00055     else
00056       throw std::out_of_range(Exception("manipIndex is out of range"));
00057   }
00058   return chain;
00059 }
00060  
00061  
00062 
00063 
00064 void RobotDescription::externalize(base::Externalizer& e, String format, Real version)
00065 {
00066   if (format == "") format = "xml";
00067 
00068   if (!formatSupported(format,version,e.ioType()))
00069     throw std::invalid_argument(Exception(String("format ")+format+" v"+base::realToString(version)+" unsupported"));
00070 
00071   if (e.isOutput()) {
00072 
00073     DOMElement* robotElem = e.createElement("robot");
00074     e.setElementAttribute(robotElem,"name",getName());
00075 
00076     e.appendComment(robotElem,"description of a robot (with a [mobile] platform and one or more manipulators");
00077     
00078     e.pushContext(robotElem);
00079     platformDescr->externalize(e, format, version);
00080 
00081     for(Int i=0; i<manipulatorDescrs.size(); i++) {
00082       manipulatorDescrs[i]->externalize(e, format, version);
00083 
00084       DOMElement* manipElem = e.lastAppendedElement();
00085       // manipulator offset
00086       DOMElement* offsetElem = e.createElement("offset",false);
00087       e.appendText(offsetElem, e.toString(manipOffsets[i]));
00088       e.appendComment(manipElem,"offset relative to platform");
00089       e.appendNode(manipElem, offsetElem);
00090       e.appendBreak(manipElem);
00091     }
00092 
00093     e.popContext();
00094 
00095     e.appendElement(robotElem);
00096 
00097   } else { // input
00098 
00099     DOMNode* context = e.context();
00100     
00101     DOMElement* robotElem = e.getFirstElement(context, "robot");
00102 
00103     // handle link
00104     String link = e.getElementAttribute(robotElem,"link",false);
00105     if (link != "") {
00106 
00107       ref<VFile> linkFile = Application::getInstance()->universe()->cache()->findFile(link,e.getArchivePath());
00108       load(linkFile,format,version);
00109     }
00110     else {
00111 //!!! does this allow for a link'd robot to have it's name overridden by the linked from element?
00112       setName( e.getDefaultedElementAttribute(robotElem, "name", "robot") );
00113       
00114       e.pushContext(robotElem);
00115       ref<PlatformDescription> pd( newPlatformDescription() );
00116       pd->externalize(e, format, version);
00117       platformDescr = pd;
00118       
00119       // get manipulators
00120       
00121       DOMElement* manipElem = e.getFirstChildElement(robotElem, "manipulator",false);
00122       while (manipElem) {
00123         // get offset
00124         DOMElement* offsetElem = e.getFirstChildElement(manipElem,"offset");
00125         String offsetText = e.getContainedText(offsetElem);
00126         manipOffsets.push_back( Vector3( e.toVector3(offsetText) ) );
00127         
00128         // externalize manipulator
00129         ref<ManipulatorDescription> manipulatorDescr( newManipulatorDescription() );
00130         manipulatorDescr->externalize(e, format, version);
00131         manipulatorDescrs.push_back( manipulatorDescr );
00132         
00133         manipElem = e.getFirstChildElement(robotElem, "manipulator", false);
00134       }
00135       
00136       e.popContext();
00137     }
00138 
00139     e.removeElement(robotElem);
00140 
00141   }
00142 }

Generated on Thu Jul 29 15:56:38 2004 for OpenSim by doxygen 1.3.6