-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.cc
More file actions
159 lines (142 loc) · 3.72 KB
/
simulation.cc
File metadata and controls
159 lines (142 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* simulation.cc (C) Inaki Rano 2022
*
* Definition of the simulation class for the 2D multi-robot simulator
*
*
*
*/
#include <fstream>
#include <yaml-cpp/yaml.h>
#include "simulation.hh"
namespace mrs {
Simulation::Simulation(const SwarmPtr & swarm): m_dt(0.1), m_tMax(100),
m_swarm_t0(swarm->clone())
{}
SwarmPtr
Simulation::rndSwarm(unsigned int sz, const RobotPtr & rp,
const Environment & env)
{
SwarmPtr swarm = std::make_shared<Swarm>();
int maxTrials(50 * sz);
int idx(0);
do {
int trials(200);
Position2d p(env.random(rp->settings().radius, swarm, trials));
if (trials > 0)
{
RobotPtr nrp = rp->clone();
swarm->add(nrp, idx, p);
idx++;
}
maxTrials--;
}
while ((maxTrials > 0) && (idx < sz));
return swarm;
}
void
Simulation::run()
{
if (m_swarm_t0 == nullptr) {
m_traj.clear();
return;
}
float t(0);
m_traj.push_back(m_swarm_t0);
while (t < m_tMax) {
std::cout << "Time: " << t << std::endl;
SwarmPtr nSwarm(m_traj.back()->clone());
nSwarm->step(m_dt);
m_traj.push_back(nSwarm);
t += m_dt;
}
return ;
}
void
Simulation::save(const char * filename) const
{
YAML::Node file;
file["name"] = "MRS";
file["version"] = "v-0.1-beta";
file["dt"] = m_dt;
file["tMax"] = m_tMax;
YAML::Node swarm;
swarm["size"] = m_swarm_t0->size();
for (unsigned int ii = 0; ii < m_swarm_t0->size(); ii++)
{
YAML::Node robot;
robot["id"] = (*m_swarm_t0)[ii]->id();
robot["name"] = (*m_swarm_t0)[ii]->name();
YAML::Node settings;
settings["radius"] = (*m_swarm_t0)[ii]->settings().radius;
settings["rMax"] = (*m_swarm_t0)[ii]->settings().rMax;
settings["vMin"] = (*m_swarm_t0)[ii]->settings().vMin;
settings["vMax"] = (*m_swarm_t0)[ii]->settings().vMax;
settings["wMin"] = (*m_swarm_t0)[ii]->settings().wMin;
settings["wMax"] = (*m_swarm_t0)[ii]->settings().wMax;
settings["visible"] = (*m_swarm_t0)[ii]->settings().visible;
YAML::Node color;
color.push_back(int((*m_swarm_t0)[ii]->settings().color[0]));
color.push_back(int((*m_swarm_t0)[ii]->settings().color[1]));
color.push_back(int((*m_swarm_t0)[ii]->settings().color[2]));
settings["color"] = color;
robot["settings"] = settings;
YAML::Node params;
for (unsigned int jj = 0; jj < (*m_swarm_t0)[ii]->parameterVector().size(); jj++)
params.push_back((*m_swarm_t0)[ii]->parameterVector()[jj]);
robot["parameters"] = params;
swarm["robot"].push_back(robot);
}
file["swarm"] = swarm;
YAML::Node traj;
for (unsigned int ii = 0; ii < m_traj.size(); ii++)
{
YAML::Node step;
step["t"] = ii * m_dt;
YAML::Node coords;
for (unsigned int jj = 0; jj < m_traj[ii]->size(); jj++)
{
coords.push_back((*m_traj[ii])[jj]->position()[0]);
coords.push_back((*m_traj[ii])[jj]->position()[1]);
}
step["coordinates"] = coords;
traj.push_back(step);
}
file["trajectory"] = traj;
std::ofstream ofd(filename);
ofd << file;
ofd.close();
}
void
Simulation::save(const std::string & filename) const
{
save(filename.c_str());
}
void
Simulation::saveTraj(const char * filename) const
{
std::ofstream ofd(filename);
for (unsigned int ii = 0; ii < m_traj.size(); ii++)
{
ofd << ii * m_dt << " ";
SwarmPtr swarmp = m_traj[ii];
for (unsigned int jj = 0; jj < swarmp->size(); jj++)
{
RobotPtr r = (*swarmp)[jj];
ofd << r->position()[0] << " " << r->position()[1] << " ";
}
ofd << std::endl;
}
ofd.close();
}
void
Simulation::load(const char * filename)
{
YAML::Node file(filename);
}
void
Simulation::load(const std::string & filename)
{
load(filename.c_str());
}
}