forked from carla-simulator/carla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
147 lines (120 loc) · 4.79 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
#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); }
/// 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(10s);
std::cout << "Client API version : " << client.GetClientVersion() << '\n';
std::cout << "Server API version : " << client.GetServerVersion() << '\n';
// 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");
auto blueprint = 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();
auto transform = RandomChoice(map->GetRecommendedSpawnPoints(), rng);
// Spawn the vehicle.
auto actor = world.SpawnActor(blueprint, transform);
std::cout << "Spawned " << actor->GetDisplayId() << '\n';
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();
vehicle->Destroy();
std::cout << "Actors destroyed." << std::endl;
} 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;
}
}