safety_controller.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::JointSafety::Ptr parseSafetyController(const tinyxml2::XMLElement* xml_element)
42 {
43  auto s = std::make_shared<tesseract_scene_graph::JointSafety>();
44  if (xml_element->QueryDoubleAttribute("k_velocity", &(s->k_velocity)) != tinyxml2::XML_SUCCESS)
45  std::throw_with_nested(std::runtime_error("SafetyController: Missing or failed to parse attribute 'k_velocity'!"));
46 
47  if (xml_element->Attribute("soft_upper_limit") == nullptr && xml_element->Attribute("soft_lower_limit") == nullptr &&
48  xml_element->Attribute("k_position") == nullptr)
49  {
50  CONSOLE_BRIDGE_logDebug("SafetyController: Missing attributes 'soft_upper_limit', 'soft_lower_limit', and "
51  "'k_position', using default value 0, 0, and 0!");
52  }
53  else if (xml_element->Attribute("soft_upper_limit") == nullptr ||
54  xml_element->Attribute("soft_lower_limit") == nullptr || xml_element->Attribute("k_position") == nullptr)
55  {
56  if (xml_element->Attribute("soft_upper_limit") == nullptr)
57  CONSOLE_BRIDGE_logDebug("SafetyController: Missing attribute 'soft_upper_limit', using default value 0!");
58 
59  if (xml_element->Attribute("soft_lower_limit") == nullptr)
60  CONSOLE_BRIDGE_logDebug("SafetyController: Missing attribute 'soft_lower_limit', using default value 0!");
61 
62  if (xml_element->Attribute("k_position") == nullptr)
63  CONSOLE_BRIDGE_logDebug("SafetyController: Missing attribute 'k_position', using default value 0!");
64  }
65 
66  s->soft_upper_limit = 0;
67  s->soft_lower_limit = 0;
68  s->k_position = 0;
69  xml_element->QueryDoubleAttribute("soft_upper_limit", &s->soft_upper_limit);
70  xml_element->QueryDoubleAttribute("soft_lower_limit", &s->soft_lower_limit);
71  xml_element->QueryDoubleAttribute("k_position", &s->k_position);
72 
73  return s;
74 }
75 
76 tinyxml2::XMLElement* writeSafetyController(const std::shared_ptr<const tesseract_scene_graph::JointSafety>& safety,
77  tinyxml2::XMLDocument& doc)
78 {
79  if (safety == nullptr)
80  std::throw_with_nested(std::runtime_error("Safety Controller is nullptr and cannot be converted to XML"));
81  tinyxml2::XMLElement* xml_element = doc.NewElement(SAFETY_CONTROLLER_ELEMENT_NAME.data());
82 
83  xml_element->SetAttribute("k_velocity", toString(safety->k_velocity).c_str());
84 
85  // Could potentially check for and not write out zero.
86  xml_element->SetAttribute("soft_upper_limit", toString(safety->soft_upper_limit).c_str());
87  xml_element->SetAttribute("soft_lower_limit", toString(safety->soft_lower_limit).c_str());
88  xml_element->SetAttribute("k_position", toString(safety->k_position).c_str());
89 
90  return xml_element;
91 }
92 
93 } // namespace tesseract_urdf
tesseract_urdf::parseSafetyController
std::shared_ptr< tesseract_scene_graph::JointSafety > parseSafetyController(const tinyxml2::XMLElement *xml_element)
Parse xml element safety_controller.
Definition: safety_controller.cpp:41
safety_controller.h
Parse safety_controller from xml string.
joint.h
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
utils.h
tesseract_urdf::writeSafetyController
tinyxml2::XMLElement * writeSafetyController(const std::shared_ptr< const tesseract_scene_graph::JointSafety > &safety, tinyxml2::XMLDocument &doc)
Definition: safety_controller.cpp:76
tesseract_scene_graph::JointSafety::Ptr
std::shared_ptr< JointSafety > Ptr
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_urdf::toString
std::string toString(const double &float_value, int precision=3)
Definition: utils.cpp:12
tesseract_urdf::SAFETY_CONTROLLER_ELEMENT_NAME
static constexpr std::string_view SAFETY_CONTROLLER_ELEMENT_NAME
Definition: safety_controller.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