RobotPool.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2018
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "RobotPool.h"
25 
26 #include <VirtualRobot/CollisionDetection/CollisionChecker.h>
27 #include <VirtualRobot/Robot.h>
28 
31 
32 namespace armarx
33 {
34 
35  RobotPool::RobotPool(VirtualRobot::RobotPtr robot, size_t defaultSize) : baseRobot(robot)
36  {
37  std::vector<VirtualRobot::RobotPtr> tempVec;
38  for (size_t i = 0; i < defaultSize; ++i)
39  {
40  tempVec.push_back(getRobot());
41  }
42  }
43 
45  RobotPool::getRobot(int inflation)
46  {
47  std::scoped_lock lock(mutex);
48  for (auto& r : robots[inflation])
49  {
50  if (r.use_count() == 1)
51  {
52  return r;
53  }
54  }
55 
56  ARMARX_INFO << "Cloning robot";
57  auto newRobot = baseRobot->clone(
58  baseRobot->getName(),
59  VirtualRobot::CollisionCheckerPtr(new VirtualRobot::CollisionChecker()));
60  newRobot->inflateCollisionModel(inflation);
61  ARMARX_CHECK_EQUAL(newRobot.use_count(), 1);
62  ARMARX_INFO << "created new robot clone n with inflation " << inflation;
63  robots[inflation].push_back(newRobot);
64  return newRobot;
65  }
66 
67  size_t
69  {
70  std::scoped_lock lock(mutex);
71  size_t size = 0;
72  for (auto& pair : robots)
73  {
74  size += pair.second.size();
75  }
76  return size;
77  }
78 
79  size_t
81  {
82  std::scoped_lock lock(mutex);
83  size_t count = 0;
84  for (auto& pair : robots)
85  {
86  for (auto& r : pair.second)
87  {
88  if (r.use_count() > 1)
89  {
90  count++;
91  }
92  }
93  }
94  return count;
95  }
96 
97  size_t
99  {
100  std::scoped_lock lock(mutex);
101  size_t count = 0;
102  for (auto& pair : robots)
103  {
104  std::vector<VirtualRobot::RobotPtr> newList;
105  for (auto& r : pair.second)
106  {
107  if (r.use_count() > 1)
108  {
109  newList.push_back(r);
110  }
111  else
112  {
113  count++;
114  }
115  }
116  pair.second = newList;
117  }
118  return count;
119  }
120 
121 } // namespace armarx
armarx::RobotPool::RobotPool
RobotPool(VirtualRobot::RobotPtr robot, size_t defaultSize=1)
Definition: RobotPool.cpp:35
armarx::RobotPool::getPoolSize
size_t getPoolSize() const
Definition: RobotPool.cpp:68
armarx::RobotPool::clean
size_t clean()
Removes unused robots from the pool.
Definition: RobotPool.cpp:98
ExpressionException.h
armarx::RobotPool::getRobot
VirtualRobot::RobotPtr getRobot(int inflation=0)
getRobot
Definition: RobotPool.cpp:45
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
Logging.h
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::RobotPool::getRobotsInUseCount
size_t getRobotsInUseCount() const
Definition: RobotPool.cpp:80
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
RobotPool.h