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/sim/SimulatedManipulatorDescription>
00026
00027 #include <base/Application>
00028 #include <base/Externalizer>
00029 #include <base/externalization_error>
00030
00031 using robot::sim::SimulatedManipulatorDescription;
00032
00033 using base::Externalizer;
00034 using base::externalization_error;
00035 using base::Application;
00036 using base::PathName;
00037 using base::VFile;
00038 using base::dom::DOMNode;
00039 using base::dom::DOMElement;
00040
00041
00042
00043 void SimulatedManipulatorDescription::externalize(base::Externalizer& e, String format, Real version)
00044 {
00045 if (format == "") format = "xml";
00046
00047 if (!formatSupported(format,version,e.ioType()))
00048 throw std::invalid_argument(Exception(String("format ")+format+" v"+base::realToString(version)+" unsupported"));
00049
00050 if (e.isOutput()) {
00051
00052 DOMElement* manipElem = e.createElement("manipulator");
00053 outputElementXML(e, manipElem, format, version);
00054 e.appendElement(manipElem);
00055
00056 } else {
00057
00058 DOMNode* context = e.context();
00059 DOMElement* manipElem = e.getFirstElement(context, "manipulator");
00060
00061
00062 String link = e.getElementAttribute(manipElem,"link",false);
00063 if (link != "") {
00064
00065 ref<VFile> linkFile = Application::getInstance()->universe()->cache()->findFile(link,e.getArchivePath());
00066 load(linkFile,format,version);
00067 }
00068 else {
00069 inputElementXML(e,manipElem, format, version);
00070 }
00071
00072
00073
00074
00075 DOMElement* configElem = e.getFirstChildElement(manipElem, "initialconfig",false);
00076 bool initialConfigSpecified = (configElem != 0);
00077 if (initialConfigSpecified) {
00078 String configText = e.getContainedText(configElem);
00079 q = e.toVector(configText);
00080 for(Int i=0; i<q.size(); i++)
00081 q[i] = Math::degToRad(q[i]);
00082 }
00083
00084 if (!initialConfigSpecified) {
00085 q = zeroVector(kinematicChain.dof());
00086 }
00087 else
00088 if (q.size() != kinematicChain.dof())
00089 throw new externalization_error(Exception("initialconfig element has vector of wrong dimension ("+base::intToString(q.size())
00090 +"); must be equal to manipulator dof ("+base::intToString(kinematicChain.dof())+")"));
00091
00092
00093 DOMElement* geomElem = e.getFirstChildElement(manipElem, "geometry",false);
00094 linkGeometrySpecified = (geomElem != 0);
00095 if (linkGeometrySpecified) {
00096
00097 String geomText = e.getContainedText(geomElem);
00098 Vector r( e.toVector(geomText) );
00099 linkRadii.resize(r.size());
00100 for(Int i=0; i<r.size(); i++) linkRadii[i] = r[i];
00101 }
00102
00103
00104 if (linkGeometrySpecified)
00105 if (linkRadii.size() != kinematicChain.size()+1)
00106 throw new externalization_error(Exception("geometry element has wrong number of link radii ("+base::intToString(linkRadii.size())
00107 +"); must be equal to number of links + the base ("+base::intToString(kinematicChain.size()+1)+")"));
00108
00109
00110
00111 DOMElement* proxElem = e.getFirstChildElement(manipElem, "proximitysensors",false);
00112 hasProxSensors = (proxElem != 0);
00113 if (hasProxSensors) {
00114 String rangeText = e.getElementAttribute(proxElem, "range", true);
00115 proxSensorRange = base::stringToReal(rangeText);
00116 }
00117
00118
00119 e.removeElement(manipElem);
00120
00121 }
00122 }
00123