32 #include <SimoxUtility/color/Color.h>
33 #include <VirtualRobot/BoundingBox.h>
34 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
35 #include <VirtualRobot/SceneObjectSet.h>
60 #include <RVOSimulator.h>
79 const std::string Component::defaultName =
"human_simulator";
101 def->optional(properties.taskPeriodMs,
"p.taskPeriodMs",
"");
102 def->optional(properties.nHumans,
"p.nHumans",
"Number of humans to spawn.");
105 properties.humanParams.goalDistanceThreshold,
"p.human.goalDistanceThreshold",
"");
106 def->optional(properties.humanParams.minLinearVelocity,
"p.human.maxLinearVelocity",
"");
107 def->optional(properties.humanParams.maxLinearVelocity,
"p.human.maxLinearVelocity",
"");
109 def->optional(properties.objectPoseProviderName,
"p.objectPoseProviderName",
"");
111 def->required(properties.robotName,
"p.robotName",
"");
127 Component::setPreferredVelocities(RVO::RVOSimulator* simulator,
128 const std::vector<RVO::Vector2>& goals)
136 for (
int i = 0; i < static_cast<int>(properties.nHumans); ++i)
145 const auto agentPosition = simulator->getAgentPosition(i);
146 auto& simulatedHuman = simulatedHumans.at(i);
149 pose.translation().x() = agentPosition.x() * 1000;
150 pose.translation().y() = agentPosition.y() * 1000;
152 simulatedHuman.reinitialize(pose);
154 const std::vector<core::Position> trajectoryPoints =
155 simulatedHuman.trajectory().positions();
157 const std::size_t lookAheadGoalIdx =
std::min(trajectoryPoints.size() - 1, 5ul);
158 const auto& intermediateGoal = trajectoryPoints.at(lookAheadGoalIdx);
160 const Eigen::Vector2f rvoGoalHelper = intermediateGoal.head<2>() / 1000;
162 const RVO::Vector2 rvoGoal(rvoGoalHelper.x(), rvoGoalHelper.y());
166 if (RVO::absSq(goalVector) > 1.0
F)
168 goalVector = RVO::normalize(goalVector);
171 simulator->setAgentPrefVelocity(i, goalVector);
191 .
providerName =
"distance_to_obstacle_costmap_provider",
192 .name =
"distance_to_obstacles",
193 .timestamp = timestamp};
196 costmapReaderPlugin->get().query(costmapQuery);
210 return costmapResult.
costmap.value();
218 simulator.setTimeStep(
static_cast<float>(properties.taskPeriodMs) / 1000);
219 simulator.setAgentDefaults(15.0
F, 10U, 1.5
F, .1
F, .4
F, .5
F);
223 properties.objectPoseProviderName);
234 for (
const auto& obst : objects->getCollisionModels())
238 const VirtualRobot::BoundingBox bbox = obst->getGlobalBoundingBox();
240 Eigen::Vector2f bbMin = bbox.getMin().head<2>();
241 Eigen::Vector2f bbMax = bbox.getMax().head<2>();
244 const float margin = 100;
251 ARMARX_VERBOSE <<
"Bounding box: (" << bbMin.transpose() <<
"), ("
252 << bbMax.transpose() <<
")";
255 constexpr
float zHeight = 3;
258 polygon.
addPoint(Eigen::Vector3f{bbMin.x(), bbMin.y(), zHeight});
259 polygon.
addPoint(Eigen::Vector3f{bbMax.x(), bbMin.y(), zHeight});
260 polygon.
addPoint(Eigen::Vector3f{bbMax.x(), bbMax.y(), zHeight});
261 polygon.
addPoint(Eigen::Vector3f{bbMin.x(), bbMax.y(), zHeight});
263 polygon.
color(simox::Color::black());
264 polygon.
color(simox::Color::black());
270 const Eigen::Vector2d
min = bbMin.cast<
double>() / 1000;
271 const Eigen::Vector2d
max = bbMax.cast<
double>() / 1000;
273 std::vector<RVO::Vector2> obstacle;
274 obstacle.emplace_back(
min.x(),
min.y());
275 obstacle.emplace_back(
max.x(),
min.y());
276 obstacle.emplace_back(
max.x(),
max.y());
277 obstacle.emplace_back(
min.x(),
max.y());
279 simulator.addObstacle(obstacle);
285 simulator.processObstacles();
290 simulatedHumans.clear();
292 for (std::size_t i = 0U; i < properties.nHumans; ++i)
295 simulatedHumans.emplace_back(costmap);
299 human.
human().
pose.translation().y() / 1000);
307 goals.emplace_back(goal.x(), goal.y());
313 robot = virtualRobotReaderPlugin->get().getRobotWaiting(properties.robotName);
317 const auto robotPosH = robot->getGlobalPosition();
318 const RVO::Vector2 robotPos(robotPosH.x() / 1000, robotPosH.y() / 1000);
320 robotAgentId = simulator.addAgent(robotPos);
321 simulator.setAgentMaxSpeed(robotAgentId,
323 simulator.setAgentPrefVelocity(robotAgentId,
RVO::Vector2(0, 0));
324 simulator.setAgentRadius(robotAgentId, 0.6);
329 this, &Component::runPeriodically, properties.taskPeriodMs,
false,
"runningTask");
347 return Component::defaultName;
353 return Component::defaultName;
357 Component::runPeriodically()
361 setPreferredVelocities(&simulator, goals);
370 const auto robotPosH = robot->getGlobalPosition();
371 const RVO::Vector2 robotPos(robotPosH.x() / 1000, robotPosH.y() / 1000);
373 simulator.setAgentPosition(robotAgentId, robotPos);
381 for (
const auto& simulatedHuman : simulatedHumans)
385 for (
const auto& pt : simulatedHuman.trajectory().points())
387 path.addPoint(pt.waypoint.pose.translation());
391 path.colorGlasbeyLUT(layer.size());
401 for (std::size_t i = 0; i < properties.nHumans; i++)
403 const auto pos = simulator.getAgentPosition(i);
406 human.pose.setIdentity();
407 human.pose.translation().x() = pos.x() * 1000;
408 human.pose.translation().y() = pos.y() * 1000;
410 humans.push_back(human);
414 humanWriterPlugin->get().store(humans,
getName(), timestamp);