limits.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <tinyxml2.h>
33 
34 #include <tesseract_common/utils.h>
36 #include <tesseract_urdf/limits.h>
37 #include <tesseract_urdf/utils.h>
38 
39 namespace tesseract_urdf
40 {
41 tesseract_scene_graph::JointLimits::Ptr parseLimits(const tinyxml2::XMLElement* xml_element)
42 {
43  auto limits = std::make_shared<tesseract_scene_graph::JointLimits>();
44 
45  int status = xml_element->QueryDoubleAttribute("lower", &(limits->lower));
46  if (status != tinyxml2::XML_NO_ATTRIBUTE && status != tinyxml2::XML_SUCCESS)
47  std::throw_with_nested(std::runtime_error("Limits: Missing or failed to parse attribute 'lower'!"));
48 
49  status = xml_element->QueryDoubleAttribute("upper", &(limits->upper));
50  if (status != tinyxml2::XML_NO_ATTRIBUTE && status != tinyxml2::XML_SUCCESS)
51  std::throw_with_nested(std::runtime_error("Limits: Missing or failed to parse attribute 'upper'!"));
52 
53  if (xml_element->QueryDoubleAttribute("effort", &(limits->effort)) != tinyxml2::XML_SUCCESS)
54  std::throw_with_nested(std::runtime_error("Limits: Missing or failed to parse attribute 'effort'!"));
55 
56  if (xml_element->QueryDoubleAttribute("velocity", &(limits->velocity)) != tinyxml2::XML_SUCCESS)
57  std::throw_with_nested(std::runtime_error("Limits: Missing or failed to parse attribute 'velocity'!"));
58 
59  status = xml_element->QueryDoubleAttribute("acceleration", &(limits->acceleration));
60  if (status == tinyxml2::XML_NO_ATTRIBUTE)
61  limits->acceleration = 0.5 * limits->velocity;
62  else if (status != tinyxml2::XML_SUCCESS)
63  std::throw_with_nested(std::runtime_error("Limits: Failed to parse attribute 'acceleration'!"));
64 
65  status = xml_element->QueryDoubleAttribute("jerk", &(limits->jerk));
66  if (status == tinyxml2::XML_NO_ATTRIBUTE)
67  limits->jerk = 1000;
68  else if (status != tinyxml2::XML_SUCCESS)
69  std::throw_with_nested(std::runtime_error("Limits: Failed to parse attribute 'jerk'!"));
70 
71  return limits;
72 }
73 
74 tinyxml2::XMLElement* writeLimits(const std::shared_ptr<const tesseract_scene_graph::JointLimits>& limits,
75  tinyxml2::XMLDocument& doc)
76 {
77  if (limits == nullptr)
78  std::throw_with_nested(std::runtime_error("Limits are nullptr and cannot be converted to XML"));
79  tinyxml2::XMLElement* xml_element = doc.NewElement(LIMITS_ELEMENT_NAME.data());
80 
81  // if upper and lower are both zero, don't write it. This should only happen for continuous joints.
82  if (!tesseract_common::almostEqualRelativeAndAbs(limits->lower, 0.0) ||
84  {
85  xml_element->SetAttribute("lower", toString(limits->lower).c_str());
86  xml_element->SetAttribute("upper", toString(limits->upper).c_str());
87  }
88 
89  // Always write effort & velocity
90  xml_element->SetAttribute("effort", toString(limits->effort).c_str());
91  xml_element->SetAttribute("velocity", toString(limits->velocity).c_str());
92 
93  // Write out nonzero acceleration (Tesseract-exclusive)
94  if (!tesseract_common::almostEqualRelativeAndAbs(limits->acceleration, 0.0) &&
95  !tesseract_common::almostEqualRelativeAndAbs(limits->acceleration, limits->velocity * 0.5))
96  xml_element->SetAttribute("acceleration", toString(limits->acceleration).c_str());
97 
98  // Write out nonzero jerk (Tesseract-exclusive)
99  if (!tesseract_common::almostEqualRelativeAndAbs(limits->jerk, 0.0) &&
100  !tesseract_common::almostEqualRelativeAndAbs(limits->jerk, 1000))
101  xml_element->SetAttribute("jerk", toString(limits->jerk).c_str());
102 
103  return xml_element;
104 }
105 
106 } // namespace tesseract_urdf
utils.h
tesseract_urdf::writeLimits
tinyxml2::XMLElement * writeLimits(const std::shared_ptr< const tesseract_scene_graph::JointLimits > &limits, tinyxml2::XMLDocument &doc)
Definition: limits.cpp:74
tesseract_common::almostEqualRelativeAndAbs
bool almostEqualRelativeAndAbs(const Eigen::Ref< const Eigen::VectorXd > &v1, const Eigen::Ref< const Eigen::VectorXd > &v2, const Eigen::Ref< const Eigen::VectorXd > &max_diff, const Eigen::Ref< const Eigen::VectorXd > &max_rel_diff)
tesseract_scene_graph::JointLimits::Ptr
std::shared_ptr< JointLimits > Ptr
joint.h
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
limits.h
Parse limits from xml string.
utils.h
tesseract_urdf::parseLimits
std::shared_ptr< tesseract_scene_graph::JointLimits > parseLimits(const tinyxml2::XMLElement *xml_element)
Parse xml element limits.
Definition: limits.cpp:41
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_urdf::toString
std::string toString(const double &float_value, int precision=3)
Definition: utils.cpp:12
tesseract_urdf::LIMITS_ELEMENT_NAME
static constexpr std::string_view LIMITS_ELEMENT_NAME
Definition: limits.h:45
macros.h
tesseract_urdf
Definition: box.h:43


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