World.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014 Jose Luis Blanco Claraco (University of Almeria) |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under GNU General Public License version 3 |
7  | See <http://www.gnu.org/licenses/> |
8  +-------------------------------------------------------------------------+ */
9 #include <mvsim/World.h>
10 
11 #include <mrpt/system/filesystem.h> // filePathSeparatorsToNative()
12 
13 #include <iostream> // for debugging
14 #include <algorithm> // count()
15 #include <stdexcept>
16 #include <map>
17 
18 using namespace mvsim;
19 using namespace std;
20 
21 // Default ctor: inits empty world.
23  : m_gravity(9.81),
24  m_simul_time(0.0),
25  m_simul_timestep(0.010),
26  m_b2d_vel_iters(6),
27  m_b2d_pos_iters(3),
28  m_base_path("."),
29  m_box2d_world(NULL)
30 {
31  this->clear_all();
32 }
33 
34 // Dtor.
36 {
37  this->clear_all();
38  delete m_box2d_world;
39  m_box2d_world = NULL;
40 }
41 
42 // Resets the entire simulation environment to an empty world.
43 void World::clear_all(bool acquire_mt_lock)
44 {
45  try
46  {
47  if (acquire_mt_lock) m_world_cs.lock();
48 
49  // Reset params:
50  m_simul_time = 0.0;
51 
52  // (B2D) World contents:
53  // ---------------------------------------------
54  delete m_box2d_world;
56 
57  // Define the ground body.
58  b2BodyDef groundBodyDef;
59  m_b2_ground_body = m_box2d_world->CreateBody(&groundBodyDef);
60 
61  // Clear lists of objs:
62  // ---------------------------------------------
63  for (TListVehicles::iterator it = m_vehicles.begin();
64  it != m_vehicles.end(); ++it)
65  delete it->second;
66  m_vehicles.clear();
67 
68  for (std::list<WorldElementBase*>::iterator it =
69  m_world_elements.begin();
70  it != m_world_elements.end(); ++it)
71  delete *it;
72  m_world_elements.clear();
73 
74  for (TListBlocks::iterator it = m_blocks.begin(); it != m_blocks.end();
75  ++it)
76  delete it->second;
77  m_blocks.clear();
78 
79  if (acquire_mt_lock) m_world_cs.unlock();
80  }
81  catch (std::exception&)
82  {
83  if (acquire_mt_lock) m_world_cs.unlock();
84  throw; // re-throw
85  }
86 }
87 
89 void World::run_simulation(double dt)
90 {
91  m_timlogger.registerUserMeasure("run_simulation.dt", dt);
92 
93  // sanity checks:
94  ASSERT_(dt > 0);
95  ASSERT_(m_simul_timestep > 0);
96 
97  // Run in time steps:
98  const double end_time = m_simul_time + dt;
99  const double timetol =
100  1e-6; // tolerance for rounding errors summing time steps
101  while (m_simul_time < (end_time - timetol))
102  {
103  // Timestep: always "simul_step" for the sake of repeatibility
105  }
106 }
107 
110 {
111  m_timer_iteration.Tic();
112 
113  TSimulContext context;
114  context.b2_world = m_box2d_world;
115  context.simul_time = m_simul_time;
116  context.dt = dt;
117 
118  // 1) Pre-step
119  {
120  CTimeLoggerEntry tle(m_timlogger, "timestep.0.prestep");
121  for (TListVehicles::iterator it = m_vehicles.begin();
122  it != m_vehicles.end(); ++it)
123  it->second->simul_pre_timestep(context);
124 
125  for (TListVehicles::iterator it = m_vehicles.begin();
126  it != m_vehicles.end(); ++it)
127  {
128  VehicleBase::TListSensors& sensors = it->second->getSensors();
129  for (VehicleBase::TListSensors::iterator itSen = sensors.begin();
130  itSen != sensors.end(); ++itSen)
131  {
132  (*itSen)->simul_pre_timestep(context);
133  }
134  }
135 
136  for (TListBlocks::iterator it = m_blocks.begin(); it != m_blocks.end();
137  ++it)
138  it->second->simul_pre_timestep(context);
139 
140  for (std::list<WorldElementBase*>::iterator it =
141  m_world_elements.begin();
142  it != m_world_elements.end(); ++it)
143  (*it)->simul_pre_timestep(context);
144  }
145 
146  // 2) Run dynamics
147  {
148  CTimeLoggerEntry tle(
149  m_timlogger, "timestep.1.dynamics_integrator");
150 
152  m_simul_time += dt; // Avance time
153  }
154 
155  // 3) Save dynamical state into vehicles classes
156  {
157  CTimeLoggerEntry tle(
158  m_timlogger, "timestep.3.save_dynstate");
159 
160  context.simul_time = m_simul_time;
161  for (TListVehicles::iterator it = m_vehicles.begin();
162  it != m_vehicles.end(); ++it)
163  {
164  it->second->simul_post_timestep_common(context);
165  it->second->simul_post_timestep(context);
166  }
167  for (TListBlocks::iterator it = m_blocks.begin(); it != m_blocks.end();
168  ++it)
169  {
170  it->second->simul_post_timestep_common(context);
171  it->second->simul_post_timestep(context);
172  }
173  }
174 
175  // 4) Post-step:
176  {
177  CTimeLoggerEntry tle(m_timlogger, "timestep.4.poststep");
178  for (TListVehicles::iterator it = m_vehicles.begin();
179  it != m_vehicles.end(); ++it)
180  it->second->simul_post_timestep(context);
181 
182  for (TListVehicles::iterator it = m_vehicles.begin();
183  it != m_vehicles.end(); ++it)
184  {
185  VehicleBase::TListSensors& sensors = it->second->getSensors();
186  for (VehicleBase::TListSensors::iterator itSen = sensors.begin();
187  itSen != sensors.end(); ++itSen)
188  {
189  (*itSen)->simul_post_timestep(context);
190  }
191  }
192 
193  for (TListBlocks::iterator it = m_blocks.begin(); it != m_blocks.end();
194  ++it)
195  it->second->simul_post_timestep(context);
196 
197  for (std::list<WorldElementBase*>::iterator it =
198  m_world_elements.begin();
199  it != m_world_elements.end(); ++it)
200  (*it)->simul_post_timestep(context);
201  }
202 
203  const double ts = m_timer_iteration.Tac();
204  m_timlogger.registerUserMeasure(
205  (ts > dt ? "timestep_too_slow_alert" : "timestep"), ts);
206 }
207 
210 std::string World::resolvePath(const std::string& s_in) const
211 {
212  std::string ret;
213  const std::string s = mrpt::system::trim(s_in);
214 
215  // Relative path? It's not if:
216  // "X:\*", "/*"
217  // -------------------
218  bool is_relative = true;
219  if (s.size() > 2 && s[1] == ':' && (s[2] == '/' || s[2] == '\\'))
220  is_relative = false;
221  if (s.size() > 0 && (s[0] == '/' || s[0] == '\\')) is_relative = false;
222  if (is_relative)
223  {
224  ret = m_base_path;
225  if (!ret.empty() && ret[ret.size() - 1] != '/' &&
226  ret[ret.size() - 1] != '\\')
227  ret += string("/");
228  ret += s;
229  }
230  else
231  ret = s;
232 
233  // Expand macros: (TODO)
234  // -------------------
235 
236  return mrpt::system::filePathSeparatorsToNative(ret);
237 }
238 
241 {
242  for (TListVehicles::iterator it = m_vehicles.begin();
243  it != m_vehicles.end(); ++it)
244  v.visit(it->second);
245 }
246 
249 {
250  for (std::list<WorldElementBase*>::iterator it = m_world_elements.begin();
251  it != m_world_elements.end(); ++it)
252  v.visit(*it);
253 }
const b2Vec2 b2Vec2_zero(0.0f, 0.0f)
Useful constant.
void Step(float32 timeStep, int32 velocityIterations, int32 positionIterations)
Definition: b2World.cpp:897
int m_b2d_pos_iters
Velocity and position iteration count (Box2D)
Definition: World.h:245
TListWorldElements m_world_elements
Definition: World.h:277
~World()
Dtor.
Definition: World.cpp:35
CTicTac m_timer_iteration
Definition: World.h:287
double m_simul_time
evaluate weights for friction, etc.
Definition: World.h:240
TListVehicles m_vehicles
Definition: World.h:276
void runVisitorOnWorldElements(WorldElementVisitorBase &v)
Definition: World.cpp:248
XmlRpcServer s
double simul_time
Current time in the simulated world.
Definition: basic_types.h:56
void clear_all(bool acquire_mt_lock=true)
Definition: World.cpp:43
virtual void visit(VehicleBase *obj)=0
void internal_one_timestep(double dt)
Definition: World.cpp:109
double dt
timestep
Definition: basic_types.h:57
TListBlocks m_blocks
Definition: World.h:278
CTimeLogger m_timlogger
Definition: World.h:286
std::string resolvePath(const std::string &in_path) const
Definition: World.cpp:210
std::mutex m_world_cs
first time the GUI window is created.
Definition: World.h:269
b2Body * m_b2_ground_body
Used to declare friction between vehicles-ground.
Definition: World.h:274
virtual void visit(WorldElementBase *obj)=0
int m_b2d_vel_iters
integration.
Definition: World.h:245
std::vector< SensorBase::Ptr > TListSensors
Definition: VehicleBase.h:129
double m_simul_timestep
Definition: World.h:243
void runVisitorOnVehicles(VehicleVisitorBase &v)
Definition: World.cpp:240
World()
Default ctor: inits an empty world.
Definition: World.cpp:22
void run_simulation(double dt)
Definition: World.cpp:89
std::string m_base_path
Path from which to take relative directories.
Definition: World.h:247
b2World * m_box2d_world
objects from multithreading access.
Definition: World.h:272
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2World.cpp:107


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:40