moveit2
The MoveIt Motion Planning Framework for ROS 2.
robot_model.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2022, Peter David Fagan
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of PickNik Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Peter David Fagan */
36 
37 #include "robot_model.h"
38 #include <fstream>
39 #include <urdf_parser/urdf_parser.h>
40 #include <srdfdom/model.h>
41 #include <srdfdom/srdf_writer.h>
43 
44 namespace moveit_py
45 {
46 namespace bind_robot_model
47 {
48 void initRobotModel(py::module& m)
49 {
50  py::module robot_model = m.def_submodule("robot_model");
51 
52  py::class_<moveit::core::RobotModel, std::shared_ptr<moveit::core::RobotModel>>(robot_model, "RobotModel",
53  R"(
54  Representation of a kinematic model.
55  )")
56  // TODO (peterdavidfagan): rewrite with RobotModelLoader.
57  .def(py::init([](std::string& urdf_xml_path, std::string& srdf_xml_path) {
58  // Read in URDF
59  std::string xml_string;
60  std::fstream xml_file(urdf_xml_path.c_str(), std::fstream::in);
61  while (xml_file.good())
62  {
63  std::string line;
64  std::getline(xml_file, line);
65  xml_string += (line + "\n");
66  }
67  xml_file.close();
68 
69  urdf::ModelInterfaceSharedPtr urdf_model = urdf::parseURDF(xml_string);
70 
71  // Read in SRDF
72  srdf::Model srdf_model;
73  srdf_model.initFile(*urdf_model, srdf_xml_path);
74 
75  // Instantiate robot model
76  return std::make_shared<moveit::core::RobotModel>(
77  urdf_model, std::make_shared<const srdf::Model>(std::move(srdf_model)));
78  }),
79  py::arg("urdf_xml_path"), py::arg("srdf_xml_path"),
80  R"(
81  Initializes a kinematic model for a robot.
82 
83  Args:
84  urdf_xml_path (str): The filepath to the urdf file describing the robot.
85  srdf_xml_path (str): The filepath to the srdf file describing the robot semantics.
86  Returns:
87  moveit_py.core.RobotModel: The kinematic model for the robot.
88  )")
89 
90  .def_property("name", &moveit::core::RobotModel::getName, nullptr,
91  R"(
92  str: The name of the robot model.
93  )")
94 
95  .def_property("model_frame", &moveit::core::RobotModel::getModelFrame, nullptr,
96  R"(
97  str: Get the frame in which the transforms for this model are computed (when using a :py:class:`moveit_py.core.RobotState`).
98  This frame depends on the root joint. As such, the frame is either extracted from SRDF, or it is assumed to be the name of the root link.
99  )")
100 
101  // TODO (peterdavidfagan): make available when JointModel is binded
102  //.def_property("root_joint", &moveit::core::RobotModel::getRootJoint, nullptr, "The root joint of the robot model.")
103 
104  .def_property("root_joint_name", &moveit::core::RobotModel::getRootJointName, nullptr,
105  R"(
106  str: The name of the root joint.
107  )")
108 
109  .def(
110  "get_model_info",
111  [](std::shared_ptr<moveit::core::RobotModel>& s) {
112  std::stringstream ss;
113  s->printModelInfo(ss);
114  return ss.str();
115  },
116  py::return_value_policy::move,
117  R"(
118  Gets a formatted string containing a summary of relevant information from the robot model.
119  Returns:
120  str: Formatted string containing generic robot model information.
121  )")
122 
123  // Interacting with joint model groups
124  .def_property("joint_model_group_names", &moveit::core::RobotModel::getJointModelGroupNames, nullptr,
125  R"(
126  list of str: The names of the joint model groups in the robot model.
127  )")
128 
129  .def_property("joint_model_groups", py::overload_cast<>(&moveit::core::RobotModel::getJointModelGroups), nullptr,
130  py::return_value_policy::reference_internal,
131  R"(
132  list of moveit_py.core.JointModelGroup: The joint model groups available in the robot model.
133  )")
134 
135  .def_property("end_effectors", &moveit::core::RobotModel::getEndEffectors, nullptr,
136  py::return_value_policy::reference_internal,
137  R"(
138  TODO
139  )")
140 
141  .def("has_joint_model_group", &moveit::core::RobotModel::hasJointModelGroup, py::arg("joint_model_group_name"),
142  R"(
143  Checks if a joint model group with the given name exists in the robot model.
144  Returns:
145  bool: true if joint model group exists.
146  )")
147 
148  .def("get_joint_model_group",
149  py::overload_cast<const std::string&>(&moveit::core::RobotModel::getJointModelGroup),
150  py::arg("joint_model_group_name"), py::return_value_policy::reference_internal,
151  R"(
152  Gets a joint model group instance by name.
153  Args:
154  joint_model_group_name (str): The name of the joint model group to return.
155  Returns:
156  :py:class:`moveit_py.core.JointModelGroup`: joint model group instance that corresponds with joint_model_group_name parameter.
157  )");
158 }
159 } // namespace bind_robot_model
160 } // namespace moveit_py
const std::string & getModelFrame() const
Get the frame in which the transforms for this model are computed (when using a RobotState)....
Definition: robot_model.h:94
bool hasJointModelGroup(const std::string &group) const
Check if the JointModelGroup group exists.
const std::vector< const JointModelGroup * > & getJointModelGroups() const
Get the available joint groups.
Definition: robot_model.h:383
const std::vector< const JointModelGroup * > & getEndEffectors() const
Get the map between end effector names and the groups they correspond to.
Definition: robot_model.h:410
const std::vector< std::string > & getJointModelGroupNames() const
Get the names of all groups that are defined for this model.
Definition: robot_model.h:395
const std::string & getRootJointName() const
Return the name of the root joint. Throws an exception if there is no root joint.
Definition: robot_model.h:131
const JointModelGroup * getJointModelGroup(const std::string &name) const
Get a joint group from this model (by name)
const std::string & getName() const
Get the model name.
Definition: robot_model.h:85
void initRobotModel(py::module &m)
Definition: robot_model.cpp:48