mvsim_main.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 
10 #include <mvsim/mvsim.h>
11 
12 #include <chrono>
13 #include <thread>
14 
15 #include <mrpt/system/os.h> // kbhit()
16 #include <mrpt/utils/CTicTac.h>
17 #include <rapidxml_utils.hpp>
18 #include <iostream>
19 
20 using namespace mvsim;
21 
23 {
25  volatile bool closing;
26  TThreadParams() : world(NULL), closing(false) {}
27 };
28 void usage(const char* argv0);
29 void thread_update_GUI(TThreadParams& thread_params);
31 std::string msg2gui;
32 
33 int main(int argc, char** argv)
34 {
35  try
36  {
37  if (argc != 2)
38  {
39  usage(argv[0]);
40  return -1;
41  }
42 
43  World world;
44 
45  // Load from XML:
46  rapidxml::file<> fil_xml(argv[1]);
47  world.load_from_XML(fil_xml.data(), argv[1]);
48 
49  // Launch GUI thread:
50  TThreadParams thread_params;
51  thread_params.world = &world;
52  std::thread thGUI =
53  std::thread(thread_update_GUI, std::ref(thread_params));
54 
55  // Run simulation:
56  CTicTac tictac;
57  double t_old = tictac.Tac();
58  double REALTIME_FACTOR = 1.0;
59  bool do_exit = false;
60  size_t teleop_idx_veh = 0; // Index of the vehicle to teleop
61 
62  while (!do_exit && !mrpt::system::os::kbhit())
63  {
64  // Simulation
65  // ============================================================
66  // Compute how much time has passed to simulate in real-time:
67  double t_new = tictac.Tac();
68  double incr_time = REALTIME_FACTOR * (t_new - t_old);
69 
70  if (incr_time >= world.get_simul_timestep()) // Just in case the
71  // computer is *really
72  // fast*...
73  {
74  // Simulate:
75  world.run_simulation(incr_time);
76 
77  // t_old_simul = world.get_simul_time();
78  t_old = t_new;
79  }
80 
81  // I could use 10ms here but chono literals are since gcc 4.9.3
82  std::this_thread::sleep_for(std::chrono::milliseconds(10));
83 
84  // GUI msgs, teleop, etc.
85  // ====================================================
86 
87  std::string txt2gui_tmp;
89 
90  // Global keys:
91  switch (keyevent.keycode)
92  {
93  case 27:
94  do_exit = true;
95  break;
96  case '1':
97  case '2':
98  case '3':
99  case '4':
100  case '5':
101  case '6':
102  teleop_idx_veh = keyevent.keycode - '1';
103  break;
104  };
105 
106  { // Test: Differential drive: Control raw forces
107  const World::TListVehicles& vehs = world.getListOfVehicles();
108  txt2gui_tmp += mrpt::format(
109  "Selected vehicle: %u/%u\n",
110  static_cast<unsigned>(teleop_idx_veh + 1),
111  static_cast<unsigned>(vehs.size()));
112  if (vehs.size() > teleop_idx_veh)
113  {
114  // Get iterator to selected vehicle:
115  World::TListVehicles::const_iterator it_veh = vehs.begin();
116  std::advance(it_veh, teleop_idx_veh);
117 
118  // Get speed: ground truth
119  {
120  const vec3& vel = it_veh->second->getVelocityLocal();
121  txt2gui_tmp += mrpt::format(
122  "gt. vel: lx=%7.03f, ly=%7.03f, w= %7.03fdeg/s\n",
123  vel.vals[0], vel.vals[1],
124  RAD2DEG(vel.vals[2]));
125  }
126  // Get speed: ground truth
127  {
128  const vec3& vel =
129  it_veh->second->getVelocityLocalOdoEstimate();
130  txt2gui_tmp += mrpt::format(
131  "odo vel: lx=%7.03f, ly=%7.03f, w= %7.03fdeg/s\n",
132  vel.vals[0], vel.vals[1],
133  RAD2DEG(vel.vals[2]));
134  }
135 
136  // Generic teleoperation interface for any controller that
137  // supports it:
138  {
139  ControllerBaseInterface* controller =
140  it_veh->second->getControllerInterface();
143  teleop_in.keycode = keyevent.keycode;
144  controller->teleop_interface(teleop_in, teleop_out);
145  txt2gui_tmp += teleop_out.append_gui_lines;
146  }
147  }
148  }
149 
150  // Clear the keystroke buffer
151  if (keyevent.keycode != 0) gui_key_events = World::TGUIKeyEvent();
152 
153  msg2gui = txt2gui_tmp; // send txt msgs to show in the GUI
154 
155  } // end while()
156 
157  thread_params.closing = true;
158  thGUI.join(); // TODO: It could break smth
159  }
160  catch (const std::exception& e)
161  {
162  std::cerr << "ERROR: " << e.what() << std::endl;
163  return 1;
164  }
165  return 0;
166 }
167 
168 void thread_update_GUI(TThreadParams& thread_params)
169 {
170  while (!thread_params.closing)
171  {
172  World::TUpdateGUIParams guiparams;
173  guiparams.msg_lines = msg2gui;
174 
175  thread_params.world->update_GUI(&guiparams);
176 
177  // Send key-strokes to the main thread:
178  if (guiparams.keyevent.keycode != 0)
179  gui_key_events = guiparams.keyevent;
180 
181  std::this_thread::sleep_for(std::chrono::milliseconds(25));
182  }
183 }
184 
185 void usage(const char* argv0)
186 {
187  std::cerr << "Usage:\n"
188  " "
189  << argv0 << " [WORLD_MODEL.xml]\n";
190 }
void usage(const char *argv0)
Definition: mvsim_main.cpp:185
Represents data loaded from a file.
void update_GUI(TUpdateGUIParams *params=NULL)
Definition: World_gui.cpp:68
std::string msg2gui
Definition: mvsim_main.cpp:31
World * world
Definition: mvsim_main.cpp:24
virtual void teleop_interface(const TeleopInput &in, TeleopOutput &out)
void thread_update_GUI(TThreadParams &thread_params)
Definition: mvsim_main.cpp:168
std::multimap< std::string, VehicleBase * > TListVehicles
Definition: World.h:167
double vals[3]
Definition: basic_types.h:64
TGUIKeyEvent keyevent
Keystrokes in the window are returned here.
Definition: World.h:142
double get_simul_timestep() const
Simulation fixed-time interval for numerical integration.
Definition: World.h:104
std::string msg_lines
Messages to show.
Definition: World.h:143
volatile bool closing
Definition: mvsim_main.cpp:25
void load_from_XML(const std::string &xml_text, const std::string &fileNameForPath=std::string("."))
const TListVehicles & getListOfVehicles() const
Definition: World.h:181
World::TGUIKeyEvent gui_key_events
Definition: mvsim_main.cpp:30
void run_simulation(double dt)
Definition: World.cpp:89
int main(int argc, char **argv)
Definition: mvsim_main.cpp:33
int keycode
0=no Key. Otherwise, ASCII code.
Definition: World.h:134


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