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  {
42  defineRequiredProperty<std::string>("RobotFileName",
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 
49  defineOptionalProperty<std::string>(
50  "RobotName",
51  "",
52  "Override robot name if you want to load multiple robots of the same type");
53  defineOptionalProperty<std::string>(
54  "RobotNodeSetName",
55  "Robot",
56  "Robot node set name as defined in robot xml file, e.g. 'LeftArm'");
57  defineOptionalProperty<std::string>(
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};
147  ARMARX_CHECK_IS_NULL(robot);
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  // FIXME change the following to VirtualRobot::RobotIO::eStructure and see whether it breaks something
181  robot =
182  VirtualRobot::RobotIO::loadRobot(robotFileName, VirtualRobot::RobotIO::eFull);
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();
202  ARMARX_CHECK_NOT_NULL(robot);
203  robotPool.reset(new RobotPool(robot, 20));
204  }
205  }
206 
207 } // namespace armarx::RobotUnitModule
armarx::RobotUnitModule::RobotData::getRobotNodetSeName
const std::string & getRobotNodetSeName() const
Returns the name of the robot's RobotNodeSet.
Definition: RobotUnitModuleRobotData.cpp:92
armarx::eFull
@ eFull
Definition: MorphingItem.h:35
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
armarx::RobotUnitModule::RobotDataPropertyDefinitions::RobotDataPropertyDefinitions
RobotDataPropertyDefinitions(std::string prefix)
Definition: RobotUnitModuleRobotData.cpp:39
ARMARX_CHECK_NOT_NULL
#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...
Definition: ExpressionException.h:206
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:52
armarx::RobotUnitModule::RobotData::arePlatformAndLocalizationUnitsEnabled
bool arePlatformAndLocalizationUnitsEnabled() const
Definition: RobotUnitModuleRobotData.cpp:71
RobotUnitModuleRobotData.h
ARMARX_CHECK_IS_NULL
#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...
Definition: ExpressionException.h:194
armarx::RobotUnitModule::RobotData::getRobotPlatformName
const std::string & getRobotPlatformName() const
Returns the name of the robot's platform.
Definition: RobotUnitModuleRobotData.cpp:77
armarx::RobotUnitModule::RobotData::getRobotFileName
const std::string & getRobotFileName() const
Returns the file name of the robot's model.
Definition: RobotUnitModuleRobotData.cpp:110
CMakePackageFinder.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ArmarXDataPath::addDataPaths
static void addDataPaths(const std::string &dataPathList)
Definition: ArmarXDataPath.cpp:554
armarx::RobotUnitModule::ModuleBase::throwIfInControlThread
void throwIfInControlThread(const std::string &fnc) const
Throws if the current thread is the ControlThread.
Definition: RobotUnitModuleBase.cpp:418
armarx::RobotUnitModule::RobotData::getRobotProjectName
const std::string & getRobotProjectName() const
Returns the name of the project containing the robot's model.
Definition: RobotUnitModuleRobotData.cpp:101
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:109
armarx::RobotUnitModule
Definition: ControlDevice.h:34
ArmarXDataPath.h
armarx::RobotUnitModule::RobotData::cloneRobot
VirtualRobot::RobotPtr cloneRobot(bool updateCollisionModel=false) const
Returns a clone of the robot's model.
Definition: RobotUnitModuleRobotData.cpp:129
armarx::RobotUnitModule::ModuleBasePropertyDefinitions
Definition: RobotUnitModuleBase.h:107
armarx::RobotUnitModule::RobotData::getRobotName
std::string getRobotName() const
Returns the robot's name.
Definition: RobotUnitModuleRobotData.cpp:119
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
RobotPool.h
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38
armarx::RobotUnitModule::RobotData::getRobotPlatformInstanceName
std::string getRobotPlatformInstanceName() const
Returns the name of the robot platform instance.
Definition: RobotUnitModuleRobotData.cpp:86