File size: 5,760 Bytes
9897e20 | 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 | #include <madrona/viz/viewer.hpp>
#include "sim.hpp"
#include "mgr.hpp"
#include "types.hpp"
#include <filesystem>
#include <fstream>
#include <optional>
#include <iostream>
using namespace madrona;
using namespace madrona::viz;
static HeapArray<float> readReplayLog(const char *path) {
std::ifstream replay_log(path, std::ios::binary);
replay_log.seekg(0, std::ios::end);
int64_t size = replay_log.tellg();
replay_log.seekg(0, std::ios::beg);
HeapArray<float> log(size / sizeof(float));
replay_log.read((char *)log.data(), (size / sizeof(float)) * sizeof(float));
return log;
}
int main(int argc, char *argv[])
{
using namespace madrona_gpudrive;
constexpr int64_t num_views = 2;
uint32_t num_worlds = 1;
if (argc >= 2) {
num_worlds = (uint32_t)atoi(argv[1]);
}
ExecMode exec_mode = ExecMode::CPU;
if (argc >= 3) {
if (!strcmp("--cpu", argv[2])) {
exec_mode = ExecMode::CPU;
} else if (!strcmp("--cuda", argv[2])) {
exec_mode = ExecMode::CUDA;
}
}
const char *replay_log_path = nullptr;
if (argc >= 4) {
replay_log_path = argv[3];
}
std::optional<HeapArray<float>> replay_log;
uint32_t cur_replay_step = 0;
uint32_t num_replay_steps = 0;
if (replay_log_path != nullptr) {
replay_log = readReplayLog(replay_log_path);
num_replay_steps = replay_log->size() / (num_worlds * num_views * 4);
}
bool enable_batch_renderer =
#ifdef MADRONA_MACOS
false;
#else
true;
#endif
WindowManager wm {};
WindowHandle window = wm.makeWindow("GPUDrive", 1920, 1080);
render::GPUHandle render_gpu = wm.initGPU(0, { window.get() });
Manager mgr({
.execMode = exec_mode,
.gpuID = 0,
.scenes = {"../data/processed/examples/tfrecord-00001-of-01000_307.json"},
.params = {
.polylineReductionThreshold = 1.0,
.observationRadius = 100.0,
.maxNumControlledAgents = 0
},
.enableBatchRenderer = enable_batch_renderer,
.extRenderAPI = wm.gpuAPIManager().backend(),
.extRenderDev = render_gpu.device(),
});
madrona::CountT stepCtr = 0;
// math::Quat initial_camera_rotation = math::Quat::angleAxis(0, math::up).normalize();
math::Quat initial_camera_rotation =
(math::Quat::angleAxis(0, math::up) *
math::Quat::angleAxis(-math::pi / 2.f, math::right)).normalize();
Viewer viewer(mgr.getRenderManager(), window.get(), {
.numWorlds = num_worlds,
.simTickRate = 20,
.cameraMoveSpeed = 20.f,
.cameraPosition = 100.f * math::up,
.cameraRotation = initial_camera_rotation,
});
auto replayStep = [&]() {
if (cur_replay_step == num_replay_steps - 1) {
return true;
}
printf("Step: %u\n", cur_replay_step);
for (uint32_t i = 0; i < num_worlds; i++) {
for (uint32_t j = 0; j < num_views; j++) {
uint32_t base_idx = 0;
base_idx = 4 * (cur_replay_step * num_views * num_worlds +
i * num_views + j);
auto acceleration = (*replay_log)[base_idx];
auto steering = (*replay_log)[base_idx + 1];
auto headAngle = (*replay_log)[base_idx + 2];
printf("%d, %d: %f %f %f\n", i, j, acceleration, steering,
headAngle);
mgr.setAction(i, j, acceleration, steering, headAngle);
}
}
cur_replay_step++;
return false;
};
auto self_printer = mgr.selfObservationTensor().makePrinter();
auto partner_printer = mgr.partnerObservationsTensor().makePrinter();
auto lidar_printer = mgr.lidarTensor().makePrinter();
auto steps_remaining_printer = mgr.stepsRemainingTensor().makePrinter();
auto reward_printer = mgr.rewardTensor().makePrinter();
auto printObs = [&]() {
printf("Self\n");
self_printer.print();
printf("Partner\n");
partner_printer.print();
printf("Lidar\n");
lidar_printer.print();
printf("Steps Remaining\n");
steps_remaining_printer.print();
printf("Reward\n");
reward_printer.print();
printf("\n");
};
viewer.loop(
[&mgr](CountT world_idx, const Viewer::UserInput &input) {
using Key = Viewer::KeyboardKey;
if (input.keyHit(Key::R)) {
mgr.reset({(int)world_idx});
}
(void)world_idx;
},
[&mgr](CountT world_idx, CountT agent_idx,
const Viewer::UserInput &input) {
using Key = Viewer::KeyboardKey;
float steering{0};
const float steeringDelta{math::pi / 8};
float acceleration{0};
const float accelerationDelta{1};
bool shift_pressed = input.keyPressed(Key::Shift);
if (input.keyPressed(Key::W)) {
acceleration += accelerationDelta;
}
if (input.keyPressed(Key::S)) {
acceleration -= accelerationDelta;
}
if (input.keyPressed(Key::D)) {
steering += steeringDelta;
}
if (input.keyPressed(Key::A)) {
steering -= steeringDelta;
}
mgr.setAction(world_idx, agent_idx, acceleration, steering, 0);
}, [&]() {
if (replay_log.has_value()) {
bool replay_finished = replayStep();
if (replay_finished) {
viewer.stopLoop();
}
}
mgr.step();
stepCtr++;
if(stepCtr % consts::episodeLen == 0) {
mgr.reset({0});
}
// printObs();
}, []() {});
}
|