RobotUnitModuleRobotData.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotAPI::ArmarXObjects::RobotUnit
17 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18 * @date 2018
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
25#include <SimoxUtility/algorithm/string/string_tools.h>
26#include <VirtualRobot/Robot.h>
27#include <VirtualRobot/RobotNodeSet.h>
28#include <VirtualRobot/VirtualRobotException.h>
29#include <VirtualRobot/XML/RobotIO.h>
30
33
35
37{
38
41 {
43 "Robot file name, e.g. robot_model.xml");
44 defineOptionalProperty<std::string>("RobotFileNameProject",
45 "",
46 "Project in which the robot filename is located "
47 "(if robot is loaded from an external project)");
48
50 "RobotName",
51 "",
52 "Override robot name if you want to load multiple robots of the same type");
54 "RobotNodeSetName",
55 "Robot",
56 "Robot node set name as defined in robot xml file, e.g. 'LeftArm'");
58 "PlatformName",
59 "Platform",
60 "Name of the platform needs to correspond to a node in the virtual robot.");
61 defineOptionalProperty<bool>("PlatformAndLocalizationUnitsEnabled",
62 true,
63 "Enable or disable the platform and localization units.");
64 defineOptionalProperty<std::string>("PlatformInstanceName",
65 "Platform",
66 "Name of the platform instance (will publish "
67 "values on PlatformInstanceName + 'State')");
68 }
69
70 bool
72 {
73 return _arePlatformAndLocalizationUnitsEnabled;
74 }
75
76 const std::string&
78 {
79 throwIfInControlThread(BOOST_CURRENT_FUNCTION);
81 << __FUNCTION__ << " should only be called after _initVirtualRobot was called";
82 return robotPlatformName;
83 }
84
85 std::string
87 {
88 return robotPlatformInstanceName;
89 }
90
91 const std::string&
93 {
94 throwIfInControlThread(BOOST_CURRENT_FUNCTION);
96 << __FUNCTION__ << " should only be called after _initVirtualRobot was called";
97 return robotNodeSetName;
98 }
99
100 const std::string&
102 {
103 throwIfInControlThread(BOOST_CURRENT_FUNCTION);
105 << __FUNCTION__ << " should only be called after _initVirtualRobot was called";
106 return robotProjectName;
107 }
108
109 const std::string&
111 {
112 throwIfInControlThread(BOOST_CURRENT_FUNCTION);
114 << __FUNCTION__ << " should only be called after _initVirtualRobot was called";
115 return robotFileName;
116 }
117
118 std::string
120 {
121 throwIfInControlThread(BOOST_CURRENT_FUNCTION);
122 std::lock_guard<std::mutex> guard{robotMutex};
124 << __FUNCTION__ << " should only be called after _initVirtualRobot was called";
125 return robot->getName();
126 }
127
129 RobotData::cloneRobot(bool updateCollisionModel) const
130 {
131 throwIfInControlThread(BOOST_CURRENT_FUNCTION);
132 std::lock_guard<std::mutex> guard{robotMutex};
134 << __FUNCTION__ << " should only be called after _initVirtualRobot was called";
135 ARMARX_CHECK_EXPRESSION(robotPool);
136 const VirtualRobot::RobotPtr clone = robotPool->getRobot();
137 clone->setUpdateVisualization(false);
138 clone->setUpdateCollisionModel(updateCollisionModel);
139 return clone;
140 }
141
142 void
143 RobotData::_initVirtualRobot()
144 {
145 throwIfInControlThread(BOOST_CURRENT_FUNCTION);
146 std::lock_guard<std::mutex> guard{robotMutex};
148
149 std::string robotName = getProperty<std::string>("RobotName").getValue();
150
151 robotNodeSetName = getProperty<std::string>("RobotNodeSetName").getValue();
152 robotProjectName = getProperty<std::string>("RobotFileNameProject").getValue();
153 robotFileName = getProperty<std::string>("RobotFileName").getValue();
154 robotPlatformName = getProperty<std::string>("PlatformName").getValue();
155 _arePlatformAndLocalizationUnitsEnabled =
156 getProperty<bool>("PlatformAndLocalizationUnitsEnabled").getValue();
157 robotPlatformInstanceName = getProperty<std::string>("PlatformInstanceName").getValue();
158
159 //load robot
160 {
161 Ice::StringSeq includePaths;
162 if (!robotProjectName.empty())
163 {
164 CMakePackageFinder finder(robotProjectName);
165 auto pathsString = finder.getDataDir();
166 includePaths = simox::alg::split(pathsString, ";,");
167 ArmarXDataPath::addDataPaths(includePaths);
168 }
169 if (!ArmarXDataPath::getAbsolutePath(robotFileName, robotFileName, includePaths))
170 {
171 std::stringstream str;
172 str << "Could not find robot file " + robotFileName
173 << "\nCollected paths from RobotFileNameProject '" << robotProjectName
174 << "':" << includePaths;
175 throw UserException(str.str());
176 }
177 // read the robot
178 try
179 {
180 // We initialize the robot pool with this robot instance. This means, these robots will be used in different modules, i.e. for self collision checking and in the NJointControllers. Hence, we need to make sure that at least the collision model is available.
181 robot =
182 VirtualRobot::RobotIO::loadRobot(robotFileName, VirtualRobot::RobotIO::eCollisionModel);
183 if (robotName.empty())
184 {
185 robotName = robot->getName();
186 }
187 else
188 {
189 robot->setName(robotName);
190 }
191 }
192 catch (VirtualRobot::VirtualRobotException& e)
193 {
194 throw UserException(e.what());
195 }
196 ARMARX_INFO << "Loaded robot:"
197 << "\n\tProject : " << robotProjectName
198 << "\n\tName : " << robotName
199 << "\n\tRobot file : " << robotFileName
200 << "\n\tRobotNodeSet : " << robotNodeSetName << "\n\tNodeNames : "
201 << robot->getRobotNodeSet(robotNodeSetName)->getNodeNames();
203 robotPool.reset(new RobotPool(robot, 20));
204 }
205 }
206
207} // namespace armarx::RobotUnitModule
std::string str(const T &t)
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
static void addDataPaths(const std::string &dataPathList)
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Property< PropertyType > getProperty(const std::string &name)
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
PropertyDefinition< PropertyType > & defineRequiredProperty(const std::string &name, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
void throwIfInControlThread(const std::string &fnc) const
Throws if the current thread is the ControlThread.
const std::string & getRobotFileName() const
Returns the file name of the robot's model.
const std::string & getRobotNodetSeName() const
Returns the name of the robot's RobotNodeSet.
const std::string & getRobotProjectName() const
Returns the name of the project containing the robot's model.
const std::string & getRobotPlatformName() const
Returns the name of the robot's platform.
std::string getRobotName() const
Returns the robot's name.
VirtualRobot::RobotPtr cloneRobot(bool updateCollisionModel=false) const
Returns a clone of the robot's model.
std::string getRobotPlatformInstanceName() const
Returns the name of the robot platform instance.
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_CHECK_IS_NULL(ptr)
This macro evaluates whether ptr is null and if it turns out to be false it will throw an ExpressionE...
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19