ROSMission.cpp
Go to the documentation of this file.
1 
7 #include "ROSMission.h"
8 
9 
10 ROSMission::ROSMission(std::string fileName, size_t agNum, int threashold , bool endOnFin)
11 {
12  ROS_DEBUG("Mission Init!");
13  taskReader = new XMLReader(fileName);
14  agNumber = agNum;
15  agCount = 0;
16  agents = std::vector<Agent*>();
17 
18  stepsCount = 0;
19  stepsTreshhold = threashold;
20  endOnFinish = endOnFin;
21 
22  options = nullptr;
23  missionResult = Summary();
24  resultsLog = std::unordered_map<int, std::pair<bool, int>>();
25  resultsLog.reserve(agNumber);
26 
27  stepsCount = 0;
28 
29  agentStateMsg.pos = std::vector<geometry_msgs::Point32>();
30  agentStateMsg.vel = std::vector<geometry_msgs::Point32>();
31  agentStateMsg.rad = std::vector<float> ();
32 
33  ROSMissionPub = n.advertise<ORCAStar::AgentState>("AgentStates", 1000);
34  loopRate = new ros::Rate(10);
35 
36  ROSMissionSub = n.subscribe("AgentVelocities", 1000, &ROSMission::UpdateVelocity, this);
37 }
38 
39 
41 {
42  delete loopRate;
43  delete taskReader;
44 }
45 
46 
47 
49 {
50 
51  ROS_DEBUG("Mission Start!");
52 
53  while (ros::ok() && !IsFinished())
54  {
55  ROS_DEBUG("Mission step!");
56 
57  UpdateState();
59  ROSMissionPub.publish(agentStateMsg);
60 
61  ros::spinOnce();
62  stepsCount++;
63 
64  loopRate->sleep();
65 
66  }
67  ROS_DEBUG("Mission End!");
68 }
69 
70 
71 
73 {
74  geometry_msgs::Point32 tmp;
75  int i = 0;
76 
77  for(auto &a : agents)
78  {
79  tmp.x = a->GetPosition().X();
80  tmp.y = a->GetPosition().Y();
81  agentStateMsg.pos[i] = tmp;
82 
83  tmp.x = a->GetVelocity().X();
84  tmp.y = a->GetVelocity().Y();
85  agentStateMsg.vel[i] = tmp;
86 
87  agentStateMsg.rad[i] = a->GetRadius();
88  i++;
89  }
90 }
91 
92 
93 void ROSMission::UpdateVelocity(const ORCAStar::AgentVelocity &msg)
94 {
95  size_t id = msg.id;
96  ROS_DEBUG("Agent %lu Update Velocity", id);
97  agents[id]->SetVelocity(Point(msg.vel.x, msg.vel.y));
98 }
99 
101 {
102  for(auto &a : agents)
103  {
104  Point vel = a->GetVelocity();
105  Point newPos = a->GetPosition() + vel * options->timestep;
106  a->SetPosition(newPos);
107  }
108 }
109 
110 
112 {
113 
114  if(!ReadTask()) return false;
115 
116  ROS_DEBUG("Task File is OK!");
117 
118  agentStateMsg.pos.resize(agNumber);
119  agentStateMsg.vel.resize(agNumber);
120  agentStateMsg.rad.resize(agNumber);
121 
122  ros::ServiceServer initServer = n.advertiseService("initServer", &ROSMission::InitAgent, this);
123  initFlag = false;
124  while(!initFlag && ros::ok())
125  {
126  ros::spinOnce();
127  }
128 
129  ROS_DEBUG("Agents is OK!");
130  agCount = 0;
131 
132  return true;
133 }
134 
135 
136 
138 {
139  return taskReader->ReadData() && taskReader->GetMap(&map) &&
140  taskReader->GetAgents(agents, agNumber) &&
141  taskReader->GetEnvironmentOptions(&options);
142 }
143 
144 
145 
146 bool ROSMission::InitAgent(ORCAStar::Init::Request &req, ORCAStar::Init::Response &res)
147 {
148  res.id = agCount;
149 
150  res.start.x = agents[agCount]->GetStart().X();
151  res.start.y = agents[agCount]->GetStart().Y();
152  res.goal.x = agents[agCount]->GetGoal().X();
153  res.goal.y = agents[agCount]->GetGoal().Y();
154 
155  res.delta = options->delta;
156  res.speed = agents[agCount]->GetParam().maxSpeed;
157  res.radius = agents[agCount]->GetParam().radius;
158  res.agentMaxNum = agents[agCount]->GetParam().agentsMaxNum;
159  res.sightRadius = agents[agCount]->GetParam().sightRadius;
160  res.timeBoundary = agents[agCount]->GetParam().timeBoundary;
161  res.timeBoundaryObst = agents[agCount]->GetParam().timeBoundaryObst;
162 
163  for(auto &O : map->GetObstacles())
164  {
165  geometry_msgs::Polygon polygon;
166  for(auto &o : O)
167  {
168  geometry_msgs::Point32 vertex;
169  vertex.x = o.left.X();
170  vertex.y = o.left.Y();
171  polygon.points.push_back(vertex);
172  }
173 
174  res.Obstacles.push_back(polygon);
175  }
176 
177  ROS_DEBUG("Sim Actor %lu\n", agCount);
178 
179  agCount++;
180  if(agCount == agNumber)
181  {
182  initFlag = true;
183  }
184 
185  return true;
186 }
187 
188 
189 
191 {
192  if(!endOnFinish)
193  {
194  return false;
195  }
196 
197  if(stepsCount == stepsTreshhold)
198  {
199  return true;
200  }
201 
202  bool result = true;
203  for(auto &agent : agents)
204  {
205  result = result && agent->isFinished();
206  if(!result)
207  break;
208  }
209 
210  return result;
211 }
bool PrepareSimulation()
Prepare simulation of multiagen navigation. Read task from XML file. Create ROS service for agent ini...
Definition: ROSMission.cpp:111
File contains ROSMission class.
Class Summary contains brief information about execution results.
Definition: Summary.h:32
bool IsFinished()
Definition: ROSMission.cpp:190
bool InitAgent(ORCAStar::Init::Request &req, ORCAStar::Init::Response &res)
Definition: ROSMission.cpp:146
bool ReadTask()
Definition: ROSMission.cpp:137
The Point class defines a position (or euclidean vector from (0,0)) in 2D space.
Definition: Geom.h:61
ROSMission(std::string fileName, size_t agNum, int threashold=-1, bool endOnFin=false)
ROSMission constructor with parameters. Initialize multiagent simulation. Creates ROS topics and serv...
Definition: ROSMission.cpp:10
void UpdateState()
Definition: ROSMission.cpp:100
void UpdateVelocity(const ORCAStar::AgentVelocity &msg)
Definition: ROSMission.cpp:93
void GenerateAgentStateMsg()
Definition: ROSMission.cpp:72
The XMLReader class implements interface for input data reading from XML-file with a specific structu...
Definition: XMLReader.h:73
void StartSimulation()
Start multiagent simulation execution.
Definition: ROSMission.cpp:48
~ROSMission()
Default ROSMission destructor.
Definition: ROSMission.cpp:40


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