29 #include <SimoxUtility/color/Color.h>
30 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
31 #include <VirtualRobot/Random.h>
32 #include <VirtualRobot/SceneObjectSet.h>
46 #include <RVOSimulator.h>
66 const std::string Component::defaultName =
"human_simulator";
89 def->optional(properties.taskPeriodMs,
"p.taskPeriodMs",
"");
90 def->optional(properties.nHumans,
"p.nHumans",
"Number of humans to spawn.");
93 properties.humanParams.goalDistanceThreshold,
"p.human.goalDistanceThreshold",
"");
94 def->optional(properties.humanParams.minLinearVelocity,
"p.human.maxLinearVelocity",
"");
95 def->optional(properties.humanParams.maxLinearVelocity,
"p.human.maxLinearVelocity",
"");
97 def->optional(properties.objectPoseProviderName,
"p.objectPoseProviderName",
"");
114 Component::setPreferredVelocities(RVO::RVOSimulator* simulator,
115 const std::vector<RVO::Vector2>& goals)
123 for (
int i = 0; i < static_cast<int>(properties.nHumans); ++i)
132 const auto agentPosition = simulator->getAgentPosition(i);
133 auto& simulatedHuman = simulatedHumans.at(i);
136 pose.translation().x() = agentPosition.x() * 1000;
137 pose.translation().y() = agentPosition.y() * 1000;
139 simulatedHuman.reinitialize(pose);
141 const std::vector<core::Position> trajectoryPoints =
142 simulatedHuman.trajectory().positions();
144 const std::size_t lookAheadGoalIdx =
std::min(trajectoryPoints.size() - 1, 5ul);
145 const auto& intermediateGoal = trajectoryPoints.at(lookAheadGoalIdx);
147 const Eigen::Vector2f rvoGoalHelper = intermediateGoal.head<2>() / 1000;
149 const RVO::Vector2 rvoGoal(rvoGoalHelper.x(), rvoGoalHelper.y());
153 if (RVO::absSq(goalVector) > 1.0
F)
155 goalVector = RVO::normalize(goalVector);
158 simulator->setAgentPrefVelocity(i, goalVector);
179 .
providerName =
"distance_to_obstacle_costmap_provider",
180 .name =
"distance_to_obstacles",
181 .timestamp = timestamp};
184 costmapReaderPlugin->get().query(costmapQuery);
198 return costmapResult.
costmap.value();
206 simulator.setTimeStep(
static_cast<float>(properties.taskPeriodMs) / 1000);
207 simulator.setAgentDefaults(15.0
F, 10U, 1.5
F, .1
F, .4
F, .5
F);
211 properties.objectPoseProviderName);
222 for (
const auto& obst : objects->getCollisionModels())
226 const VirtualRobot::BoundingBox bbox = obst->getGlobalBoundingBox();
228 Eigen::Vector2f bbMin = bbox.getMin().head<2>();
229 Eigen::Vector2f bbMax = bbox.getMax().head<2>();
232 const float margin = 100;
239 ARMARX_VERBOSE <<
"Bounding box: (" << bbMin.transpose() <<
"), ("
240 << bbMax.transpose() <<
")";
243 constexpr
float zHeight = 3;
246 polygon.
addPoint(Eigen::Vector3f{bbMin.x(), bbMin.y(), zHeight});
247 polygon.
addPoint(Eigen::Vector3f{bbMax.x(), bbMin.y(), zHeight});
248 polygon.
addPoint(Eigen::Vector3f{bbMax.x(), bbMax.y(), zHeight});
249 polygon.
addPoint(Eigen::Vector3f{bbMin.x(), bbMax.y(), zHeight});
251 polygon.
color(simox::Color::black());
252 polygon.
color(simox::Color::black());
258 const Eigen::Vector2d
min = bbMin.cast<
double>() / 1000;
259 const Eigen::Vector2d
max = bbMax.cast<
double>() / 1000;
261 std::vector<RVO::Vector2> obstacle;
262 obstacle.emplace_back(
min.x(),
min.y());
263 obstacle.emplace_back(
max.x(),
min.y());
264 obstacle.emplace_back(
max.x(),
max.y());
265 obstacle.emplace_back(
min.x(),
max.y());
267 simulator.addObstacle(obstacle);
273 simulator.processObstacles();
278 simulatedHumans.clear();
280 for (std::size_t i = 0U; i < properties.nHumans; ++i)
283 simulatedHumans.emplace_back(costmap);
287 human.
human().
pose.translation().y() / 1000);
295 goals.emplace_back(goal.x(), goal.y());
301 robot = virtualRobotReaderPlugin->get().getRobotWaiting(properties.robotName);
305 const auto robotPosH = robot->getGlobalPosition();
306 const RVO::Vector2 robotPos(robotPosH.x() / 1000, robotPosH.y() / 1000);
308 robotAgentId = simulator.addAgent(robotPos);
309 simulator.setAgentMaxSpeed(robotAgentId,
311 simulator.setAgentPrefVelocity(robotAgentId,
RVO::Vector2(0, 0));
312 simulator.setAgentRadius(robotAgentId, 0.6);
317 this, &Component::runPeriodically, properties.taskPeriodMs,
false,
"runningTask");
338 return Component::defaultName;
345 return Component::defaultName;
349 Component::runPeriodically()
353 setPreferredVelocities(&simulator, goals);
362 const auto robotPosH = robot->getGlobalPosition();
363 const RVO::Vector2 robotPos(robotPosH.x() / 1000, robotPosH.y() / 1000);
365 simulator.setAgentPosition(robotAgentId, robotPos);
373 for (
const auto& simulatedHuman : simulatedHumans)
377 for (
const auto& pt : simulatedHuman.trajectory().points())
379 path.addPoint(pt.waypoint.pose.translation());
383 path.colorGlasbeyLUT(layer.size());
393 for (std::size_t i = 0; i < properties.nHumans; i++)
395 const auto pos = simulator.getAgentPosition(i);
398 human.pose.setIdentity();
399 human.pose.translation().x() = pos.x() * 1000;
400 human.pose.translation().y() = pos.y() * 1000;
402 humans.push_back(human);
406 humanWriterPlugin->get().store(humans,
getName(), timestamp);