group_states.cpp
Go to the documentation of this file.
1 
29 #include <tinyxml2.h>
31 
33 #include <tesseract_common/utils.h>
35 
36 namespace tesseract_srdf
37 {
39  const GroupNames& group_names,
40  const tinyxml2::XMLElement* srdf_xml,
41  const std::array<int, 3>& /*version*/)
42 {
43  GroupJointStates group_states;
44 
45  for (const tinyxml2::XMLElement* xml_element = srdf_xml->FirstChildElement("group_state"); xml_element != nullptr;
46  xml_element = xml_element->NextSiblingElement("group_state"))
47  {
48  std::string group_name, state_name;
49  int status = tesseract_common::QueryStringAttributeRequired(xml_element, "group", group_name);
50  if (status != tinyxml2::XML_SUCCESS)
51  std::throw_with_nested(std::runtime_error("GroupStates: Missing or failed to parse attribute 'group'!"));
52 
53  status = tesseract_common::QueryStringAttributeRequired(xml_element, "name", state_name);
54  if (status != tinyxml2::XML_SUCCESS)
55  std::throw_with_nested(
56  std::runtime_error("GroupStates: Failed to parse attribute 'name' for group '" + group_name + "'!"));
57 
58  bool found = std::find(group_names.begin(), group_names.end(), group_name) != group_names.end();
59  if (!found)
60  std::throw_with_nested(std::runtime_error(tesseract_common::strFormat(
61  "GroupStates: State '%s' group '%s' does not exist!", state_name.c_str(), group_name.c_str())));
62 
63  GroupsJointState joint_state;
64 
65  // get the joint values in the group state
66  for (const tinyxml2::XMLElement* joint_xml = xml_element->FirstChildElement("joint"); joint_xml != nullptr;
67  joint_xml = joint_xml->NextSiblingElement("joint"))
68  {
69  std::string joint_name;
70  double joint_value{ 0 };
71  status = tesseract_common::QueryStringAttributeRequired(joint_xml, "name", joint_name);
72  if (status != tinyxml2::XML_SUCCESS)
73  std::throw_with_nested(std::runtime_error(tesseract_common::strFormat("GroupStates: Missing or failed to parse "
74  "attribute 'name' from joint element for "
75  "state '%s' in group '%s'!",
76  state_name.c_str(),
77  group_name.c_str())));
78 
79  if (!scene_graph.getJoint(joint_name))
80  std::throw_with_nested(std::runtime_error(tesseract_common::strFormat("GroupStates: State '%s' for group '%s' "
81  "joint name '%s' is not know to the "
82  "URDF!",
83  state_name.c_str(),
84  group_name.c_str(),
85  joint_name.c_str())));
86 
87  status = tesseract_common::QueryDoubleAttributeRequired(joint_xml, "value", joint_value);
88  if (status != tinyxml2::XML_SUCCESS)
89  std::throw_with_nested(std::runtime_error(tesseract_common::strFormat("GroupStates: State '%s' for group '%s' "
90  "joint element with joint name '%s' is "
91  "missing or failed to parse attribute "
92  "'value'!",
93  state_name.c_str(),
94  group_name.c_str(),
95  joint_name.c_str())));
96 
97  joint_state[joint_name] = joint_value;
98  }
99 
100  if (joint_state.empty())
101  std::throw_with_nested(std::runtime_error(tesseract_common::strFormat("GroupStates: State '%s' for group '%s' is "
102  "missing joint elements!",
103  state_name.c_str(),
104  group_name.c_str())));
105 
106  auto gs = group_states.find(group_name);
107  if (gs == group_states.end())
108  {
109  group_states[group_name] = GroupsJointStates();
110  gs = group_states.find(group_name);
111  }
112 
113  gs->second[state_name] = joint_state;
114  }
115 
116  return group_states;
117 }
118 
119 } // namespace tesseract_srdf
graph.h
tesseract_srdf
Main namespace.
Definition: collision_margins.h:43
tesseract_common::QueryStringAttributeRequired
int QueryStringAttributeRequired(const tinyxml2::XMLElement *xml_element, const char *name, std::string &value)
tesseract_srdf::parseGroupStates
GroupJointStates parseGroupStates(const tesseract_scene_graph::SceneGraph &scene_graph, const GroupNames &group_names, const tinyxml2::XMLElement *srdf_xml, const std::array< int, 3 > &version)
Parse groups states from srdf xml element.
Definition: group_states.cpp:38
utils.h
group_states.h
Parse group states data from srdf file.
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
tesseract_scene_graph::SceneGraph
tesseract_common::strFormat
std::string strFormat(const std::string &format, Args... args)
tesseract_common::QueryDoubleAttributeRequired
int QueryDoubleAttributeRequired(const tinyxml2::XMLElement *xml_element, const char *name, double &value)
tesseract_srdf::GroupNames
std::set< std::string > GroupNames
Definition: kinematics_information.h:60
tesseract_srdf::GroupsJointState
std::unordered_map< std::string, double > GroupsJointState
Definition: kinematics_information.h:49
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_scene_graph::SceneGraph::getJoint
std::shared_ptr< const Joint > getJoint(const std::string &name) const
tesseract_srdf::GroupsJointStates
std::unordered_map< std::string, GroupsJointState > GroupsJointStates
Definition: kinematics_information.h:50
macros.h
tesseract_srdf::GroupJointStates
std::unordered_map< std::string, GroupsJointStates > GroupJointStates
Definition: kinematics_information.h:51


tesseract_srdf
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:02:04