ROSAgent.cpp
Go to the documentation of this file.
1 
7 #include "ROSAgent.h"
8 
10 {
11  agentId = id;
12  std::stringstream initServerName;
13  initServerName << "initAgentServer_" << id;
14  ros::ServiceClient client = n.serviceClient<ORCAStar::Init>(initServerName.str());
15  ros::service::waitForService(initServerName.str());
16  ORCAStar::Init srv;
17 
18  if (client.call(srv))
19  {
20  client.shutdown();
21  ROS_DEBUG("Agent %lu Parameters received!", id);
22  }
23  else
24  {
25  ROS_ERROR("Agent %lu Parameters not received!", id);
26  exit(-1);
27  }
28 
31  CN_DEFAULT_HWEIGHT, CN_DEFAULT_TIME_STEP, srv.response.delta);
32 
33  param = new AgentParam(srv.response.sightRadius,srv.response.timeBoundary,srv.response.timeBoundaryObst,
34  srv.response.radius, CN_DEFAULT_REPS, srv.response.speed, srv.response.agentMaxNum);
35 
36  Point start = {srv.response.start.x, srv.response.start.y};
37  Point goal = {srv.response.goal.x, srv.response.goal.y};
38 
39 
40  std::vector<std::vector<Point>> obstacles;
41 
42  for(auto &obst : srv.response.Obstacles)
43  {
44  std::vector<Point> tmp;
45  for(auto &vert : obst.points)
46  {
47  tmp.push_back({vert.x, vert.y});
48  }
49  obstacles.push_back(tmp);
50  }
51 
52 
53  size_t w, h;
54  float cs;
55  std::vector<std::vector<int>> grid;
56 
57  ros::ServiceClient mapClint = n.serviceClient<nav_msgs::GetMap>("static_map");
58  nav_msgs::GetMap mapSrv;
59 
60  if(mapClint.call(mapSrv))
61  {
62  mapClint.shutdown();
63  ROS_DEBUG("Agent %lu Map received!", id);
64  }
65  else
66  {
67  ROS_ERROR("Agent %lu Map not received!", id);
68  exit(-1);
69  }
70 
71  w = mapSrv.response.map.info.width;
72  h = mapSrv.response.map.info.height;
73  cs = mapSrv.response.map.info.resolution;
74 
75  for(size_t i = 0; i < h; i++)
76  {
77  grid.push_back(std::vector<int>(w, 0));
78  for(size_t j = 0; j < w; j++)
79  {
80  if(mapSrv.response.map.data[j + w * (h - i - 1)] == 100)
81  {
82  grid[i][j] = 1;
83  }
84  else
85  {
86  grid[i][j] = 0;
87  }
88  }
89  }
90  map = new Map(cs, grid, obstacles);
91 
92 
93  ROS_DEBUG("Map %lu Created!", id);
94 
95  agent = new ORCAAgent(0, start, goal, *map, *options, *param);
96  agent->SetPlanner(ThetaStar(*map, *options, agent->GetStart(), agent->GetGoal(), param->radius + param->rEps));
97  agent->InitPath();
98 
99  ROS_DEBUG("Agent %lu Created!", id);
100 
101  std::stringstream inpTopicName;
102  inpTopicName << "AgentInput_" << id;
103  std::stringstream outTopicName;
104  outTopicName << "AgentOutput_" << id;
105 
106  ROSAgentSub = n.subscribe(inpTopicName.str(), 1000, &ROSAgent::DoStep, this);;
107  ROSAgentPub = n.advertise<geometry_msgs::Point32>(outTopicName.str(), 1000);;
108 
109 
110  ROS_DEBUG("Agent %lu Topics Created!", id);
111 
112  ros::spin();
113 
114 }
115 
116 
117 
118 
119 void ROSAgent::DoStep(const ORCAStar::ORCAInput &msg)
120 {
121 
122 
123  agent->SetPosition({msg.pos.x, msg.pos.y});
124  agent->SetVelocity({msg.vel.x, msg.vel.y});
125  Map emptymap = Map();;
126 
127  for(size_t i = 0; i < msg.neighbours.pos.size(); i++)
128  {
129  AgentParam nParam = *param;
130  nParam.radius = msg.neighbours.rad[i];
131  Agent *tmpAgent = new ORCAAgent(static_cast<int>(i + 1),Point(), Point(), emptymap, *options, nParam );
132  Point nPos = {msg.neighbours.pos[i].x, msg.neighbours.pos[i].y};
133  Point nVel = {msg.neighbours.vel[i].x, msg.neighbours.vel[i].y};
134 
135  tmpAgent->SetPosition(nPos);
136  tmpAgent->SetVelocity(nVel);
137 
138  float distSq = (agent->GetPosition()-nPos).SquaredEuclideanNorm();
139  agent->AddNeighbour(*tmpAgent, distSq);
140  }
141 
142  agent->UpdatePrefVelocity();
143  agent->UpdateNeighbourObst();
144 
145 
146  agent->ComputeNewVelocity();
147  agent->ApplyNewVelocity();
148 
149 
150  geometry_msgs::Point32 vel;
151  vel.x = agent->GetVelocity().X();
152  vel.y = agent->GetVelocity().Y();
153 
154  for(auto &n : neighbous)
155  {
156  delete n;
157  }
158 
159  neighbous.clear();
160 
161  ROS_DEBUG("Agent %lu Do Step!", agentId);
162  ROSAgentPub.publish(vel);
163 }
#define CN_DEFAULT_HWEIGHT
Default H-weight for Theta* value.
Definition: Const.h:124
float radius
Size of the agent (radius of the agent).
Definition: Agent.h:75
ROSAgent()=default
The Point class defines a position (or euclidean vector from (0,0)) in 2D space.
Definition: Geom.h:61
#define CN_DEFAULT_CUTCORNERS
Default Theta* CUTCORNERS value.
Definition: Const.h:123
#define CN_DEFAULT_REPS
Default agent buffer size value.
Definition: Const.h:127
Class AgentParam contains agent and algoritms parameters.
Definition: Agent.h:38
float x
X-coordinate of the point.
Definition: Geom.h:192
#define CN_DEFAULT_METRIC_TYPE
Default Theta* heuristic-function value.
Definition: Const.h:120
Agent class implements base agent interface and behavior.
Definition: Agent.h:95
Class EnvironmentOptions contains environment and algoritms parameters.
virtual void SetVelocity(const Point &vel)
Sets current velocity. Should be used if the new velocity may not coincide with the computed...
Definition: Agent.cpp:247
Map class describes static environment.
Definition: Map.h:36
File contains ROSAgent class.
ThetaStar class implements Theta* algorithm, which creates path from start to goal position...
Definition: ThetaStar.h:22
ORCAAgent class implements agent with ORCA algoritm behavior.
Definition: ORCAAgent.h:26
void DoStep(const ORCAStar::ORCAInput &msg)
Definition: ROSAgent.cpp:119
virtual void SetPosition(const Point &pos)
SetsS current position of agent. Can be ovveriden.
Definition: Agent.cpp:143
#define CN_DEFAULT_BREAKINGTIES
Default Theta* BREAKINGTIES value.
Definition: Const.h:121
#define CN_DEFAULT_TIME_STEP
Default simulation timestep value.
Definition: Const.h:125
#define CN_DEFAULT_ALLOWSQUEEZE
Default Theta* ALLOWSQUEEZE value.
Definition: Const.h:122


ORCAStar
Author(s): Stepan Drgachev
autogenerated on Wed Jul 15 2020 16:13:14