dynamics.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <console_bridge/console.h>
32 #include <tinyxml2.h>
34 
37 #include <tesseract_urdf/utils.h>
38 
39 namespace tesseract_urdf
40 {
41 tesseract_scene_graph::JointDynamics::Ptr parseDynamics(const tinyxml2::XMLElement* xml_element)
42 {
43  if (xml_element->Attribute("damping") == nullptr && xml_element->Attribute("friction") == nullptr)
44  std::throw_with_nested(std::runtime_error("Dynamics: Missing both attributes 'damping' and 'friction', remove tag "
45  "or add attributes and values!"));
46 
47  auto dynamics = std::make_shared<tesseract_scene_graph::JointDynamics>();
48 
49  int status = xml_element->QueryDoubleAttribute("damping", &(dynamics->damping));
50  if (status != tinyxml2::XML_NO_ATTRIBUTE && status != tinyxml2::XML_SUCCESS)
51  std::throw_with_nested(std::runtime_error("Dynamics: Error parsing attribute 'damping'!"));
52 
53  if (status == tinyxml2::XML_NO_ATTRIBUTE)
54  CONSOLE_BRIDGE_logDebug("Dynamics: Missing attribute 'damping', using default value 0!");
55 
56  status = xml_element->QueryDoubleAttribute("friction", &(dynamics->friction));
57  if (status != tinyxml2::XML_NO_ATTRIBUTE && status != tinyxml2::XML_SUCCESS)
58  std::throw_with_nested(std::runtime_error("Dynamics: Error parsing attribute 'friction'!"));
59 
60  if (status == tinyxml2::XML_NO_ATTRIBUTE)
61  CONSOLE_BRIDGE_logDebug("Dynamics: Missing attribute 'friction', using default value 0!");
62 
63  return dynamics;
64 }
65 
66 tinyxml2::XMLElement* writeDynamics(const std::shared_ptr<const tesseract_scene_graph::JointDynamics>& dynamics,
67  tinyxml2::XMLDocument& doc)
68 {
69  if (dynamics == nullptr)
70  std::throw_with_nested(std::runtime_error("Dynamics is nullptr and cannot be converted to XML"));
71  tinyxml2::XMLElement* xml_element = doc.NewElement(DYNAMICS_ELEMENT_NAME.data());
72 
73  xml_element->SetAttribute("damping", toString(dynamics->damping).c_str());
74  xml_element->SetAttribute("friction", toString(dynamics->damping).c_str());
75 
76  return xml_element;
77 }
78 
79 } // namespace tesseract_urdf
tesseract_urdf::DYNAMICS_ELEMENT_NAME
static constexpr std::string_view DYNAMICS_ELEMENT_NAME
Definition: dynamics.h:45
joint.h
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
dynamics.h
Parse dynamics from xml string.
tesseract_urdf::parseDynamics
std::shared_ptr< tesseract_scene_graph::JointDynamics > parseDynamics(const tinyxml2::XMLElement *xml_element)
Parse a xml dynamics element.
Definition: dynamics.cpp:41
tesseract_scene_graph::JointDynamics::Ptr
std::shared_ptr< JointDynamics > Ptr
utils.h
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_urdf::toString
std::string toString(const double &float_value, int precision=3)
Definition: utils.cpp:12
tesseract_urdf::writeDynamics
tinyxml2::XMLElement * writeDynamics(const std::shared_ptr< const tesseract_scene_graph::JointDynamics > &dynamics, tinyxml2::XMLDocument &doc)
Definition: dynamics.cpp:66
macros.h
tesseract_urdf
Definition: box.h:43


tesseract_urdf
Author(s): Levi Armstrong
autogenerated on Thu Apr 24 2025 03:10:44