-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pather_force_simulator_main.cpp
More file actions
252 lines (222 loc) · 11.3 KB
/
er_force_simulator_main.cpp
File metadata and controls
252 lines (222 loc) · 11.3 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
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
#include <boost/program_options.hpp>
#include "extlibs/er_force_sim/src/protobuf/world.pb.h"
#include "proto/message_translation/tbots_protobuf.h"
#include "proto/tbots_software_msgs.pb.h"
#include "proto/vision.pb.h"
#include "proto/world.pb.h"
#include "software/constants.h"
#include "software/logger/logger.h"
#include "software/networking/unix/threaded_proto_unix_listener.hpp"
#include "software/networking/unix/threaded_proto_unix_sender.hpp"
#include "software/simulation/er_force_simulator.h"
int main(int argc, char** argv)
{
struct CommandLineArgs
{
bool help = false;
std::string runtime_dir = "/tmp/tbots";
std::string division = "div_b";
bool enable_realism = false; // realism flag
};
CommandLineArgs args;
boost::program_options::options_description desc{"Options"};
desc.add_options()("help,h", boost::program_options::bool_switch(&args.help),
"Help screen");
desc.add_options()("runtime_dir",
boost::program_options::value<std::string>(&args.runtime_dir),
"The directory to output logs and setup unix sockets.");
desc.add_options()("division",
boost::program_options::value<std::string>(&args.division),
"div_a or div_b");
desc.add_options()("enable_realism",
boost::program_options::bool_switch(&args.enable_realism),
"realism simulator"); // install terminal flag
boost::program_options::variables_map vm;
boost::program_options::store(parse_command_line(argc, argv, desc), vm);
boost::program_options::notify(vm);
if (args.help)
{
std::cout << desc << std::endl;
}
else
{
std::string runtime_dir = args.runtime_dir;
LoggerSingleton::initializeLogger(runtime_dir, nullptr);
LOG(CSV, "realism_kalman_filter_v12.csv") << "timestamp_s,fused_x,fused_y,fused_vel_x, fused_vel_y, truth_x,truth_y, true_vel_x, true_vel_y,is_occluded\n";
/**
* Creates a ER force simulator and sets up the appropriate
* communication channels (unix senders/listeners). All inputs (left) and
* outputs (right) shown below are over unix sockets.
*
*
* ┌────────────────────────────┐
* SimulatorTick │ │
* ─────────────────────► │
* │ ER Force Simulator │
* │ Main │
* WorldState │ │
* ─────────────────────► │ SSL_WrapperPacket
* │ ├───────────────────►
* Blue Primitive Set │ │
* ─────────────────────► ┌──────────────────────┐ │ Blue Robot Status
* Yellow Primitive Set │ │ │ ├───────────────────►
* │ │ │ │ Yellow Robot Status
* │ │ ER Force Simulator │ │
* Blue World │ │ │ │
* ─────────────────────► │ │ │
* Yellow World │ └──────────────────────┘ │
* └────────────────────────────┘
*/
std::shared_ptr<ErForceSimulator> er_force_sim;
std::unique_ptr<RealismConfigErForce> realism_config;
if (args.enable_realism)
{
realism_config = ErForceSimulator::createRealisticRealismConfig();
}
else
{
realism_config = ErForceSimulator::createDefaultRealismConfig();
}
if (args.division == "div_a")
{
er_force_sim = std::make_shared<ErForceSimulator>(
TbotsProto::FieldType::DIV_A, robot_constants::createRobotConstants(),
realism_config);
}
else
{
er_force_sim = std::make_shared<ErForceSimulator>(
TbotsProto::FieldType::DIV_B, robot_constants::createRobotConstants(),
realism_config);
}
std::mutex simulator_mutex;
// World Buffer
TbotsProto::World blue_vision;
TbotsProto::World yellow_vision;
// Outputs
// SSL Wrapper Output
auto blue_ssl_wrapper_output =
ThreadedProtoUnixSender<SSLProto::SSL_WrapperPacket>(runtime_dir +
BLUE_SSL_WRAPPER_PATH);
auto yellow_ssl_wrapper_output =
ThreadedProtoUnixSender<SSLProto::SSL_WrapperPacket>(runtime_dir +
YELLOW_SSL_WRAPPER_PATH);
auto common_ssl_wrapper_output =
ThreadedProtoUnixSender<SSLProto::SSL_WrapperPacket>(runtime_dir +
SSL_WRAPPER_PATH);
// Robot Status Outputs
auto blue_robot_status_output = ThreadedProtoUnixSender<TbotsProto::RobotStatus>(
runtime_dir + BLUE_ROBOT_STATUS_PATH);
auto yellow_robot_status_output =
ThreadedProtoUnixSender<TbotsProto::RobotStatus>(runtime_dir +
YELLOW_ROBOT_STATUS_PATH);
// Simulator State as World State Output
auto simulator_state_output = ThreadedProtoUnixSender<world::SimulatorState>(
runtime_dir + SIMULATOR_STATE_PATH);
// World State Received Trigger as Simulator Output
auto world_state_received_trigger =
ThreadedProtoUnixSender<TbotsProto::WorldStateReceivedTrigger>(
runtime_dir + WORLD_STATE_RECEIVED_TRIGGER_PATH);
bool has_sent_world_state_trigger = false;
double start_timestamp_s = 0.0;
// Inputs
// World State Input: Configures the ERForceSimulator
auto world_state_input = ThreadedProtoUnixListener<TbotsProto::WorldState>(
runtime_dir + WORLD_STATE_PATH,
[&](TbotsProto::WorldState input)
{
std::scoped_lock lock(simulator_mutex);
er_force_sim->setWorldState(input);
if (!has_sent_world_state_trigger)
{
auto world_state_received_trigger_msg =
*createWorldStateReceivedTrigger();
world_state_received_trigger.sendProto(
world_state_received_trigger_msg);
has_sent_world_state_trigger = true;
}
});
// World Input: Buffer vision until we have primitives to tick
// the simulator with
auto blue_world_input = ThreadedProtoUnixListener<TbotsProto::World>(
runtime_dir + BLUE_WORLD_PATH,
[&](TbotsProto::World input)
{
std::scoped_lock lock(simulator_mutex);
blue_vision = input;
});
auto yellow_world_input = ThreadedProtoUnixListener<TbotsProto::World>(
runtime_dir + YELLOW_WORLD_PATH,
[&](TbotsProto::World input)
{
std::scoped_lock lock(simulator_mutex);
yellow_vision = input;
});
// PrimitiveSet Input: set the primitive set with cached vision
auto yellow_primitive_set_input =
ThreadedProtoUnixListener<TbotsProto::PrimitiveSet>(
runtime_dir + YELLOW_PRIMITIVE_SET,
[&](TbotsProto::PrimitiveSet input)
{
std::scoped_lock lock(simulator_mutex);
er_force_sim->setYellowRobotPrimitiveSet(
input, std::make_unique<TbotsProto::World>(yellow_vision));
});
auto blue_primitive_set_input =
ThreadedProtoUnixListener<TbotsProto::PrimitiveSet>(
runtime_dir + BLUE_PRIMITIVE_SET,
[&](TbotsProto::PrimitiveSet input)
{
std::scoped_lock lock(simulator_mutex);
er_force_sim->setBlueRobotPrimitiveSet(
input, std::make_unique<TbotsProto::World>(blue_vision));
});
// Simulator Tick Input
auto simulator_tick = ThreadedProtoUnixListener<TbotsProto::SimulatorTick>(
runtime_dir + SIMULATION_TICK_PATH,
[&](TbotsProto::SimulatorTick input)
{
std::scoped_lock lock(simulator_mutex);
// Step the simulation and send back the wrapper packets and
// the robot status msgs
er_force_sim->stepSimulation(
Duration::fromMilliseconds(input.milliseconds()));
for (const auto packet : er_force_sim->getSSLWrapperPackets())
{
blue_ssl_wrapper_output.sendProto(packet);
yellow_ssl_wrapper_output.sendProto(packet);
common_ssl_wrapper_output.sendProto(packet);
}
for (const auto packet : er_force_sim->getBlueRobotStatuses())
{
blue_robot_status_output.sendProto(packet);
}
for (const auto packet : er_force_sim->getYellowRobotStatuses())
{
yellow_robot_status_output.sendProto(packet);
}
auto sim_state = er_force_sim->getSimulatorState();
double current_ts = yellow_vision.time_sent().epoch_timestamp_seconds();
if (start_timestamp_s == 0.0)
{
start_timestamp_s = current_ts;
}
LOG(CSV, "realism_kalman_filter_v12.csv")
<< (current_ts - start_timestamp_s) << ","
<< yellow_vision.ball().current_state().global_position().x_meters()
<< ","
<< yellow_vision.ball().current_state().global_position().y_meters()
<< ","
<< yellow_vision.ball().current_state().global_velocity().x_component_meters()
<< ","
<< yellow_vision.ball().current_state().global_velocity().y_component_meters()
<< "," << sim_state.ball().p_x() << "," << sim_state.ball().p_y()
<< "," << sim_state.ball().v_x() << "," << sim_state.ball().v_y()
<< "," << !er_force_sim->isBallVisible()
<< "\n";
simulator_state_output.sendProto(sim_state);
});
// This blocks forever without using the CPU
std::promise<void>().get_future().wait();
}
}