00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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);
00052 chain += KinematicChain::Link( manipulators()[manipIndex]->getBaseTransform() );
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
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 {
00098
00099 DOMNode* context = e.context();
00100
00101 DOMElement* robotElem = e.getFirstElement(context, "robot");
00102
00103
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
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
00120
00121 DOMElement* manipElem = e.getFirstChildElement(robotElem, "manipulator",false);
00122 while (manipElem) {
00123
00124 DOMElement* offsetElem = e.getFirstChildElement(manipElem,"offset");
00125 String offsetText = e.getContainedText(offsetElem);
00126 manipOffsets.push_back( Vector3( e.toVector3(offsetText) ) );
00127
00128
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 }