18 #include <grpc++/grpc++.h>
24 #include "hybridsim.grpc.pb.h"
29 using grpc::ClientContext;
34 const std::string address = oc.
getString(
"pedestrian.remote.address");
35 myHybridsimStub = hybridsim::HybridSimulation::NewStub(grpc::CreateChannel(
36 address, grpc::InsecureChannelCredentials()));
58 double offset = departureOffsetAlongLane == 0 ? 0.4 : -0.4;
59 departureOffsetAlongLane += offset;
62 hybridsim::Coordinate* departureCoordinate = req.mutable_enterlocation();
63 departureCoordinate->set_x(departurePos.
x());
64 departureCoordinate->set_y(departurePos.
y());
69 hybridsim::Coordinate* arrivalCoordinate = req.mutable_leavelocation();
70 arrivalCoordinate->set_x(arrivalPos.
x());
71 arrivalCoordinate->set_y(arrivalPos.
y());
75 for (ConstMSEdgeVector::const_iterator it = stage->
getRoute().begin(); it != stage->
getRoute().end(); it++) {
80 const MSEdge* nxt = *(it + 1);
91 throw ProcessError(
"Cannot map edge : " + edge->
getID() +
" to remote simulation");
95 int frId = dir ==
FORWARD ? std::get<0>(transitions) : std::get<1>(transitions);
96 int toId = dir ==
FORWARD ? std::get<1>(transitions) : std::get<0>(transitions);
97 hybridsim::Destination* destFr = req.add_dests();
99 hybridsim::Destination* destTo = req.add_dests();
100 destTo->set_id(toId);
104 hybridsim::Boolean rpl;
105 ClientContext context;
108 throw ProcessError(
"Person: " + person->
getID() +
" could not be transferred to remote simulation");
112 throw ProcessError(
"Remote simulation declined to accept person: " + person->
getID() +
".");
120 hybridsim::Empty req;
121 hybridsim::Empty rpl;
122 ClientContext context1;
133 hybridsim::LeftClosedRightOpenTimeInterval interval;
134 interval.set_fromtimeincluding(time /
DELTA_T);
139 hybridsim::Empty rpl;
140 ClientContext context1;
141 Status st =
myHybridsimStub->simulatedTimeInerval(&context1, interval, &rpl);
147 hybridsim::Empty req2;
148 hybridsim::Trajectories trajectories;
149 ClientContext context2;
150 Status st2 =
myHybridsimStub->receiveTrajectories(&context2, req2, &trajectories);
152 throw ProcessError(
"Could not receive trajectories from remote simulation");
154 for (hybridsim::Trajectory trajectory : trajectories.trajectories()) {
157 pState->
setPosition(trajectory.x(), trajectory.y());
158 pState->
setPhi(trajectory.phi());
163 if (nextTargetEdge == nextStageEdge) {
165 std::cout <<
"next edge" << std::endl;
175 hybridsim::Empty req3;
176 hybridsim::Agents agents;
177 ClientContext context3;
178 Status st3 =
myHybridsimStub->queryRetrievableAgents(&context3, req3, &agents);
180 throw ProcessError(
"Could not query retrievable agents");
183 for (hybridsim::Agent agent : agents.agents()) {
193 hybridsim::Empty rpl2;
194 ClientContext context4;
195 Status st4 =
myHybridsimStub->confirmRetrievedAgents(&context4, agents, &rpl2);
197 throw ProcessError(
"Could not confirm retrieved agents");
220 hybridsim::Scenario req;
224 if (e->isInternal()) {
227 if (e->isWalkingArea()) {
231 for (
MSLane* l : e->getLanes()) {
239 hybridsim::Edge* edge = req.add_edges();
244 edge->set_type(hybridsim::Edge_Type_OBSTACLE);
246 edge = req.add_edges();
251 edge->set_type(hybridsim::Edge_Type_OBSTACLE);
253 edge = req.add_edges();
258 edge->set_type(hybridsim::Edge_Type_OBSTACLE);
260 edge = req.add_edges();
265 edge->set_type(hybridsim::Edge_Type_OBSTACLE);
268 hybridsim::Empty rpl;
269 ClientContext context;
272 throw ProcessError(
"Remote side could not initialize scenario!");
280 if (shape.size() < 2) {
290 hybridsim::Edge* edge = scenario.add_edges();
291 edge->mutable_c0()->set_x(frst.
x());
292 edge->mutable_c0()->set_y(frst.
y());
293 edge->mutable_c1()->set_x(scnd.
x());
294 edge->mutable_c1()->set_y(scnd.
y());
295 edge->set_type(hybridsim::Edge_Type_OBSTACLE);
302 if (centerLine.size() < 2) {
313 hybridsim::Edge_Type edgeType;
315 edgeType = hybridsim::Edge_Type_TRANSITION_HOLDOVER;
317 edgeType = hybridsim::Edge_Type_TRANSITION_INTERNAL;
319 edgeType = hybridsim::Edge_Type_TRANSITION;
323 Position frst = *centerLine.begin();
324 Position scnd = *(centerLine.begin() + 1);
326 Position thrd = *(centerLine.end() - 1);
327 Position frth = *(centerLine.end() - 2);
338 hybridsim::Edge_Type type,
int id) {
340 double dx = scnd.
x() - frst.
x();
341 double dy = scnd.
y() - frst.
y();
342 double length = sqrt(dx * dx + dy * dy);
345 double x0 = frst.
x() - dy * width / 2.;
346 double y0 = frst.
y() + dx * width / 2.;
347 double x1 = frst.
x() + dy * width / 2.;
348 double y1 = frst.
y() - dx * width / 2.;
349 hybridsim::Edge* edge = scenario.add_edges();
350 edge->mutable_c0()->set_x(x0);
351 edge->mutable_c0()->set_y(y0);
352 edge->mutable_c1()->set_x(x1);
353 edge->mutable_c1()->set_y(y1);
354 edge->set_type(type);
359 PositionVector::const_iterator it = shape.begin();
364 for (; it != shape.end(); it++) {
365 hybridsim::Edge* edge = scenario.add_edges();
366 edge->mutable_c0()->set_x(frst.
x());
367 edge->mutable_c0()->set_y(frst.
y());
368 edge->mutable_c1()->set_x((*it).x());
369 edge->mutable_c1()->set_y((*it).y());
370 edge->set_type(hybridsim::Edge_Type_OBSTACLE);
382 : myPerson(person), myPhi(0), myPosition(0, 0), myStage(stage) {
408 myPosition.set(x, y);