-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawner.cpp
186 lines (152 loc) · 5.93 KB
/
spawner.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
#include <iostream>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
#include <tuple>
#include <carla/client/ActorBlueprint.h>
#include <carla/client/BlueprintLibrary.h>
#include <carla/client/Client.h>
#include <carla/client/Map.h>
#include <carla/client/Sensor.h>
#include <carla/client/TimeoutException.h>
#include <carla/client/World.h>
#include <carla/geom/Transform.h>
#include <carla/image/ImageIO.h>
#include <carla/image/ImageView.h>
#include <carla/sensor/data/Image.h>
namespace cc = carla::client;
namespace cg = carla::geom;
namespace csd = carla::sensor::data;
using namespace std::chrono_literals;
using namespace std::string_literals;
#define EXPECT_TRUE(pred) if (!(pred)) { throw std::runtime_error(#pred); }
#define NUM_VEHICLES 2
/// Pick a random element from @a range.
template <typename RangeT, typename RNG>
static auto &RandomChoice(const RangeT &range, RNG &&generator) {
EXPECT_TRUE(range.size() > 0u);
std::uniform_int_distribution<size_t> dist{0u, range.size() - 1u};
return range[dist(std::forward<RNG>(generator))];
}
/// Save a semantic segmentation image to disk converting to CityScapes palette.
/*
static void SaveSemSegImageToDisk(const csd::Image &image) {
using namespace carla::image;
char buffer[9u];
std::snprintf(buffer, sizeof(buffer), "%08zu", image.GetFrame());
auto filename = "_images/"s + buffer + ".png";
auto view = ImageView::MakeColorConvertedView(
ImageView::MakeView(image),
ColorConverter::CityScapesPalette());
ImageIO::WriteView(filename, view);
}
*/
static auto ParseArguments(int argc, const char *argv[]) {
EXPECT_TRUE((argc == 1u) || (argc == 3u));
using ResultType = std::tuple<std::string, uint16_t>;
return argc == 3u ?
ResultType{argv[1u], std::stoi(argv[2u])} :
ResultType{"localhost", 2000u};
}
int main(int argc, const char *argv[]) {
try {
std::string host;
uint16_t port;
std::tie(host, port) = ParseArguments(argc, argv);
std::mt19937_64 rng((std::random_device())());
auto client = cc::Client(host, port);
client.SetTimeout(40s);
std::cout << "Client API version : " << client.GetClientVersion() << '\n';
std::cout << "Server API version : " << client.GetServerVersion() << '\n';
/*
TODO Add town select as well as random towns.
[] Town select
[] Vehicle blueprint select
[] Spawn point select
Might be passed as arguments
*/
// Load a random town.
auto town_name = RandomChoice(client.GetAvailableMaps(), rng);
std::cout << "Loading world: " << town_name << std::endl;
auto world = client.LoadWorld(town_name);
// Get a random vehicle blueprint.
auto blueprint_library = world.GetBlueprintLibrary();
auto vehicles = blueprint_library->Filter("vehicle");
std::vector<cc::BlueprintLibrary::value_type> v_list;
for (size_t i = 0; i < NUM_VEHICLES; ++i) {
v_list.push_back(RandomChoice(*vehicles, rng));
}
// Randomize the blueprint.
// if (blueprint.ContainsAttribute("color")) {
// auto &attribute = blueprint.GetAttribute("color");
// blueprint.SetAttribute(
// "color",
// RandomChoice(attribute.GetRecommendedValues(), rng));
// }
// Find a valid spawn point.
auto map = world.GetMap();
std::vector<carla::traffic_manager::ActorPtr> actors;
for (size_t i = 0; i < NUM_VEHICLES; i++){
auto transform = RandomChoice(map->GetRecommendedSpawnPoints(), rng);
actors.push_back(world.SpawnActor(v_list[i], transform));
std::cout << "Spawned " << actors[i]->GetDisplayId() << '\n';
}
/*
TODO Spawn camera fixed to the actor for debugging purposes.
May need to be optional for allowing windowless simulator execution.
*/
carla::traffic_manager::ActorPtr spectator;
carla::traffic_manager::ActorPtr camera;
if (NUM_VEHICLES == 1) {
auto camera_bp = blueprint_library->Find("sensor.other.collision");
auto c = world.SpawnActor(*camera_bp, cg::Transform{cg::Location{0.0f, 5.0f, 1.0f}, cg::Rotation{0.0f, -90.0f, 0.0f}}, actors[0].get());
auto s = world.GetSpectator();
spectator = s;
camera = c;
}
while (true){
if (NUM_VEHICLES == 1) spectator->SetTransform(camera->GetTransform());
std::this_thread::sleep_for(200ms);
}
// auto vehicle = boost::static_pointer_cast<cc::Vehicle>(actor);
// Apply control to vehicle.
// cc::Vehicle::Control control;
// control.throttle = 1.0f;
// vehicle->ApplyControl(control);
// Move spectator so we can see the vehicle from the simulator window.
// auto spectator = world.GetSpectator();
// transform.location += 32.0f * transform.GetForwardVector();
// transform.location.z += 2.0f;
// transform.rotation.yaw += 180.0f;
// transform.rotation.pitch = -15.0f;
// spectator->SetTransform(transform);
/*
// Find a camera blueprint.
auto camera_bp = blueprint_library->Find("sensor.camera.semantic_segmentation");
EXPECT_TRUE(camera_bp != nullptr);
// Spawn a camera attached to the vehicle.
auto camera_transform = cg::Transform{
cg::Location{-5.5f, 0.0f, 2.8f}, // x, y, z.
cg::Rotation{-15.0f, 0.0f, 0.0f}}; // pitch, yaw, roll.
auto cam_actor = world.SpawnActor(*camera_bp, camera_transform, actor.get());
auto camera = boost::static_pointer_cast<cc::Sensor>(cam_actor);
// Register a callback to save images to disk.
camera->Listen([](auto data) {
auto image = boost::static_pointer_cast<csd::Image>(data);
EXPECT_TRUE(image != nullptr);
SaveSemSegImageToDisk(*image);
});
std::this_thread::sleep_for(10s);
// Remove actors from the simulation.
camera->Destroy();
*/
} catch (const cc::TimeoutException &e) {
std::cout << '\n' << e.what() << std::endl;
return 1;
} catch (const std::exception &e) {
std::cout << "\nException: " << e.what() << std::endl;
return 2;
}
}