-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
313 lines (259 loc) · 6.87 KB
/
main.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "Common.h"
#include "Trajectory.h"
#include "Utility/Timer.h"
#include "Drawing/Visualizer_GLUT.h"
#include "Simulation/QuadDynamics.h"
#include "Simulation/Simulator.h"
#include "Utility/SimpleConfig.h"
#include "Utility/StringUtils.h"
#include "Drawing/GraphManager.h"
#include "MavlinkNode/MavlinkTranslation.h"
using SLR::Quaternion;
using SLR::ToUpper;
void KeyboardInteraction(V3F& force, shared_ptr<Visualizer_GLUT> vis);
bool receivedResetRequest = true;
bool paused = false;
void PrintHelpText();
void ProcessConfigCommands(shared_ptr<Visualizer_GLUT> vis);
void LoadScenario(string scenarioFile);
void ResetSimulation();
vector<QuadcopterHandle> quads;
shared_ptr<Visualizer_GLUT> visualizer;
shared_ptr<GraphManager> grapher;
float dtSim = 0.001f;
const int NUM_SIM_STEPS_PER_TIMER = 5;
Timer lastDraw;
V3F force, moment;
float simulationTime=0;
int randomNumCarry=-1;
void OnTimer(int v);
vector<QuadcopterHandle> CreateVehicles();
string _scenarioFile="../config/1_Intro.txt";
#include "MavlinkNode/MavlinkNode.h"
shared_ptr<MavlinkNode> mlNode;
int main(int argcp, char **argv)
{
PrintHelpText();
// load parameters
ParamsHandle config = SimpleConfig::GetInstance();
// initialize visualizer
visualizer.reset(new Visualizer_GLUT(&argcp, argv));
grapher.reset(new GraphManager(false));
// re-load last opened scenario
FILE *f = fopen("../config/LastScenario.txt", "r");
if (f)
{
char buf[100]; buf[99] = 0;
fgets(buf, 99, f);
_scenarioFile = SLR::Trim(buf);
fclose(f);
}
LoadScenario(_scenarioFile);
glutTimerFunc(1,&OnTimer,0);
glutMainLoop();
return 0;
}
void LoadScenario(string scenarioFile)
{
FILE *f = fopen("../config/LastScenario.txt","w");
if(f)
{
fprintf(f, "%s", scenarioFile.c_str());
fclose(f);
}
ParamsHandle config = SimpleConfig::GetInstance();
_scenarioFile = scenarioFile;
config->Reset(scenarioFile);
grapher->_sources.clear();
grapher->graph1->RemoveAllElements();
grapher->graph2->RemoveAllElements();
grapher->RegisterDataSource(visualizer);
// create a quadcopter to simulate
quads = CreateVehicles();
visualizer->Reset();
visualizer->InitializeMenu(grapher->GetGraphableStrings());
visualizer->quads = quads;
visualizer->graph = grapher;
ProcessConfigCommands(visualizer);
mlNode.reset();
if(config->Get("Mavlink.Enable",0)!=0)
{
mlNode.reset(new MavlinkNode());
}
ResetSimulation();
}
int _simCount = 0;
void ResetSimulation()
{
_simCount++;
ParamsHandle config = SimpleConfig::GetInstance();
printf("Simulation #%d (%s)\n", _simCount, _scenarioFile.c_str());
randomNumCarry = -1;
receivedResetRequest = false;
simulationTime = 0;
config->Reset(_scenarioFile);
dtSim = config->Get("Sim.Timestep", 0.005f);
for (unsigned i = 0; i<quads.size(); i++)
{
quads[i]->Reset();
}
grapher->Clear();
}
void OnTimer(int)
{
ParamsHandle config = SimpleConfig::GetInstance();
// logic to reset the simulation based on key input or reset conditions
float endTime = config->Get("Sim.EndTime",-1.f);
if(receivedResetRequest ==true ||
(ToUpper(config->Get("Sim.RunMode", "Continuous"))=="REPEAT" && endTime>0 && simulationTime >= endTime))
{
ResetSimulation();
}
visualizer->OnMainTimer();
// main loop
if (!paused)
{
for (int i = 0; i < NUM_SIM_STEPS_PER_TIMER; i++)
{
for (unsigned i = 0; i < quads.size(); i++)
{
quads[i]->Run(dtSim, simulationTime, randomNumCarry, force, moment);
}
simulationTime += dtSim;
}
grapher->UpdateData(simulationTime);
}
KeyboardInteraction(force, visualizer);
if (lastDraw.ElapsedSeconds() > 0.030)
{
if (quads.size() > 0)
{
visualizer->SetArrow(quads[0]->Position() - force, quads[0]->Position());
}
visualizer->Update();
grapher->DrawUpdate();
lastDraw.Reset();
// temporarily here
if (mlNode)
{
mlNode->Send(MakeMavlinkPacket_Heartbeat());
mlNode->Send(MakeMavlinkPacket_Status());
mlNode->Send(MakeMavlinkPacket_LocalPose(simulationTime, quads[0]->Position(), quads[0]->Velocity()));
mlNode->Send(MakeMavlinkPacket_Attitude(simulationTime, quads[0]->Attitude(), quads[0]->Omega()));
}
}
glutTimerFunc(5,&OnTimer,0);
}
vector<QuadcopterHandle> CreateVehicles()
{
vector<QuadcopterHandle> ret;
ParamsHandle config = SimpleConfig::GetInstance();
int i = 1;
while (1)
{
char buf[100];
sprintf_s(buf, 100, "Sim.Vehicle%d", i);
if (config->Exists(buf))
{
QuadcopterHandle q = QuadDynamics::Create(config->Get(buf, "Quad"), (int)ret.size());
grapher->RegisterDataSource(q);
ret.push_back(q);
}
else
{
break;
}
i++;
}
return ret;
}
void KeyboardInteraction(V3F& force, shared_ptr<Visualizer_GLUT> visualizer)
{
bool keyPressed = false;
const float forceStep = 0.04f;
if (visualizer->IsSpecialKeyDown(GLUT_KEY_LEFT))
{
force += V3F(0, -forceStep, 0);
keyPressed = true;
}
if (visualizer->IsSpecialKeyDown(GLUT_KEY_UP))
{
force += V3F(0, 0, -forceStep);
keyPressed = true;
}
if (visualizer->IsSpecialKeyDown(GLUT_KEY_RIGHT))
{
force += V3F(0, forceStep, 0);
keyPressed = true;
}
if (visualizer->IsSpecialKeyDown(GLUT_KEY_DOWN))
{
force += V3F(0, 0, forceStep);
keyPressed = true;
}
if (visualizer->IsKeyDown('w') || visualizer->IsKeyDown('W'))
{
force += V3F(forceStep, 0, 0);
keyPressed = true;
}
if (visualizer->IsKeyDown('s') || visualizer->IsKeyDown('S'))
{
force += V3F(-forceStep, 0, 0);
keyPressed = true;
}
if (!keyPressed)
{
force = V3F();
}
if (force.mag() > 2.f)
{
force = force / force.mag() * 2.f;
}
if (visualizer->IsKeyDown('c') || visualizer->IsKeyDown('C'))
{
visualizer->graph->graph1->RemoveAllElements();
visualizer->graph->graph2->RemoveAllElements();
}
if (visualizer->IsKeyDown('r') || visualizer->IsKeyDown('R'))
{
receivedResetRequest = true;
}
static bool key_space_pressed = false;
if (visualizer->IsKeyDown(' '))
{
if (!key_space_pressed)
{
key_space_pressed = true;
paused = !paused;
visualizer->paused = paused;
}
}
else
{
key_space_pressed = false;
}
}
void ProcessConfigCommands(shared_ptr<Visualizer_GLUT> vis)
{
ParamsHandle config = SimpleConfig::GetInstance();
int i = 1;
while (1)
{
char buf[100];
sprintf_s(buf, 100, "Commands.%d", i);
string cmd = config->Get(buf, "");
if (cmd == "") break;
vis->OnMenu(cmd);
i++;
}
}
void PrintHelpText()
{
printf("SIMULATOR!\n");
printf("Select main window to interact with keyboard/mouse:\n");
printf("LEFT DRAG / X+LEFT DRAG / Z+LEFT DRAG = rotate, pan, zoom camera\n");
printf("W/S/UP/LEFT/DOWN/RIGHT - apply force\n");
printf("C - clear all graphs\n");
printf("R - reset simulation\n");
printf("Space - pause simulation\n");
}