carla 0.14.1

Rust client library for Carla simulator
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Multiple Sensor Grid View - Phase 14.2
//!
//! This example demonstrates a 2x3 grid layout showing multiple camera and LiDAR sensors
//! in real-time. It matches the Python PythonAPI/examples/visualize_multiple_sensors.py
//! reference implementation.
//!
//! Grid Layout:
//! ```
//! [Left Camera]   [Front Camera]   [Right Camera]
//! [LiDAR]        [Rear Camera]    [Semantic LiDAR]
//! ```
//!
//! Features:
//! - 4 RGB cameras at different angles (left, front, right, rear)
//! - Regular LiDAR with bird's-eye view (white points on black)
//! - Semantic LiDAR with bird's-eye view (white points on black)
//! - 2x3 grid layout with synchronized rendering
//! - Real-time sensor data display
//!
//! Run with: `cargo run --example sensor_grid_view --profile dev-release`

use anyhow::{Context, Result};
use carla::{
    client::{ActorBase, Client, Sensor, Vehicle, World as CarlaWorld},
    geom::Transform,
    rpc::{ActorId, AttachmentType},
    sensor::data::{
        Image, LidarDetection, LidarMeasurement, SemanticLidarDetection, SemanticLidarMeasurement,
    },
};
use macroquad::prelude::*;
use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
    time::Duration,
};

const WINDOW_WIDTH: i32 = 1280;
const WINDOW_HEIGHT: i32 = 720;
const GRID_ROWS: usize = 2;
const GRID_COLS: usize = 3;
const LIDAR_RANGE: f32 = 100.0;

// ViewportInfo tracks which grid cell renders which sensor
#[derive(Debug, Clone)]
struct ViewportInfo {
    row: usize,
    col: usize,
    sensor_type: String,
    actor_id: ActorId,
}

impl ViewportInfo {
    fn new(row: usize, col: usize, sensor_type: String, actor_id: ActorId) -> Self {
        Self {
            row,
            col,
            sensor_type,
            actor_id,
        }
    }

    fn get_bounds(&self, window_width: i32, window_height: i32) -> (i32, i32, i32, i32) {
        let cell_width = window_width / GRID_COLS as i32;
        let cell_height = window_height / GRID_ROWS as i32;
        let x = self.col as i32 * cell_width;
        let y = self.row as i32 * cell_height;
        (x, y, cell_width, cell_height)
    }
}

// DisplayManager manages the 2x3 grid layout
struct DisplayManager {
    viewports: Vec<ViewportInfo>,
    camera_textures: HashMap<ActorId, Texture2D>,
    lidar_textures: HashMap<ActorId, Texture2D>,
}

impl DisplayManager {
    fn new() -> Self {
        Self {
            viewports: Vec::new(),
            camera_textures: HashMap::new(),
            lidar_textures: HashMap::new(),
        }
    }

    fn add_sensor(&mut self, row: usize, col: usize, sensor_type: String, actor_id: ActorId) {
        self.viewports
            .push(ViewportInfo::new(row, col, sensor_type, actor_id));
    }

    fn update_camera_texture(&mut self, actor_id: ActorId, texture: Texture2D) {
        self.camera_textures.insert(actor_id, texture);
    }

    fn update_lidar_texture(&mut self, actor_id: ActorId, texture: Texture2D) {
        self.lidar_textures.insert(actor_id, texture);
    }

    fn render(&self) {
        let window_width = screen_width() as i32;
        let window_height = screen_height() as i32;

        // Clear background
        clear_background(BLACK);

        // Render each viewport
        for viewport in &self.viewports {
            let (x, y, width, height) = viewport.get_bounds(window_width, window_height);

            // Draw sensor data
            if viewport.sensor_type.contains("Camera") {
                if let Some(texture) = self.camera_textures.get(&viewport.actor_id) {
                    draw_texture_ex(
                        texture,
                        x as f32,
                        y as f32,
                        WHITE,
                        DrawTextureParams {
                            dest_size: Some(vec2(width as f32, height as f32)),
                            ..Default::default()
                        },
                    );
                }
            } else if viewport.sensor_type.contains("LiDAR")
                && let Some(texture) = self.lidar_textures.get(&viewport.actor_id)
            {
                draw_texture_ex(
                    texture,
                    x as f32,
                    y as f32,
                    WHITE,
                    DrawTextureParams {
                        dest_size: Some(vec2(width as f32, height as f32)),
                        ..Default::default()
                    },
                );
            }

            // Draw grid lines
            draw_rectangle_lines(x as f32, y as f32, width as f32, height as f32, 2.0, GRAY);

            // Draw label
            draw_text(
                &viewport.sensor_type,
                x as f32 + 10.0,
                y as f32 + 25.0,
                20.0,
                WHITE,
            );
        }

        // Draw help text
        draw_text(
            "ESC/Q: Quit",
            10.0,
            window_height as f32 - 10.0,
            20.0,
            WHITE,
        );
    }
}

// CameraSensor manages a single camera sensor
struct CameraSensor {
    _sensor: Sensor,
    actor_id: ActorId,
    latest_image: Arc<Mutex<Option<Image>>>,
}

impl CameraSensor {
    fn spawn(world: &mut CarlaWorld, transform: &Transform, parent: &Vehicle) -> Result<Self> {
        let blueprint_library = world.blueprint_library()?;
        let camera_bp = blueprint_library
            .iter()
            .find(|bp| bp.id() == "sensor.camera.rgb")
            .context("Failed to find RGB camera blueprint")?;

        let actor = world
            .spawn_actor_opt(&camera_bp, transform, Some(parent), AttachmentType::Rigid)
            .context("Failed to spawn camera sensor")?;

        let sensor = Sensor::try_from(actor)
            .map_err(|_| anyhow::anyhow!("Failed to convert actor to sensor"))?;

        let actor_id = sensor.id();
        let latest_image = Arc::new(Mutex::new(None));
        let image_clone = Arc::clone(&latest_image);

        sensor.listen(move |data| {
            if let Ok(image) = Image::try_from(data) {
                *image_clone.lock().unwrap() = Some(image);
            }
        })?;

        Ok(Self {
            _sensor: sensor,
            actor_id,
            latest_image,
        })
    }

    fn get_latest_image(&self) -> Option<Image> {
        self.latest_image.lock().unwrap().clone()
    }

    fn actor_id(&self) -> ActorId {
        self.actor_id
    }
}

// Type alias for LiDAR points to reduce complexity
type LidarPoints = Arc<Mutex<Option<Vec<(f32, f32)>>>>;

// LidarSensor manages a single LiDAR sensor
struct LidarSensor {
    _sensor: Sensor,
    actor_id: ActorId,
    latest_points: LidarPoints,
}

impl LidarSensor {
    fn spawn(world: &mut CarlaWorld, parent: &Vehicle, is_semantic: bool) -> Result<Self> {
        let blueprint_library = world.blueprint_library()?;
        let blueprint_id = if is_semantic {
            "sensor.lidar.ray_cast_semantic"
        } else {
            "sensor.lidar.ray_cast"
        };

        let mut lidar_bp = blueprint_library
            .iter()
            .find(|bp| bp.id() == blueprint_id)
            .context("Failed to find LiDAR blueprint")?
            .clone();

        // Set LiDAR parameters matching Python example
        let _ = lidar_bp.set_attribute("range", "100.0");
        let _ = lidar_bp.set_attribute("rotation_frequency", "10.0");
        let _ = lidar_bp.set_attribute("channels", "64");
        let _ = lidar_bp.set_attribute("points_per_second", "100000");

        let transform = Transform {
            location: carla::geom::Location::new(0.0, 0.0, 0.0),
            rotation: carla::geom::Rotation::new(0.0, 0.0, 0.0),
        };

        let actor = world
            .spawn_actor_opt(&lidar_bp, &transform, Some(parent), AttachmentType::Rigid)
            .context("Failed to spawn LiDAR sensor")?;

        let sensor = Sensor::try_from(actor)
            .map_err(|_| anyhow::anyhow!("Failed to convert actor to sensor"))?;

        let actor_id = sensor.id();
        let latest_points = Arc::new(Mutex::new(None));
        let points_clone = Arc::clone(&latest_points);

        if is_semantic {
            sensor.listen(move |data| {
                if let Ok(measurement) = SemanticLidarMeasurement::try_from(data) {
                    let points: Vec<(f32, f32)> = measurement
                        .as_slice()
                        .iter()
                        .map(|detection: &SemanticLidarDetection| {
                            (detection.point.x, detection.point.y)
                        })
                        .collect();
                    *points_clone.lock().unwrap() = Some(points);
                }
            })?;
        } else {
            sensor.listen(move |data| {
                if let Ok(measurement) = LidarMeasurement::try_from(data) {
                    let points: Vec<(f32, f32)> = measurement
                        .as_slice()
                        .iter()
                        .map(|detection: &LidarDetection| (detection.point.x, detection.point.y))
                        .collect();
                    *points_clone.lock().unwrap() = Some(points);
                }
            })?;
        }

        Ok(Self {
            _sensor: sensor,
            actor_id,
            latest_points,
        })
    }

    fn get_latest_points(&self) -> Option<Vec<(f32, f32)>> {
        self.latest_points.lock().unwrap().clone()
    }

    fn actor_id(&self) -> ActorId {
        self.actor_id
    }
}

// Render LiDAR points to texture (bird's-eye view)
fn render_lidar_to_texture(points: &[(f32, f32)], width: usize, height: usize) -> Texture2D {
    let mut bytes = vec![0u8; width * height * 4]; // RGBA

    let min_size = width.min(height) as f32;
    let scale = min_size / (LIDAR_RANGE * 2.0);
    let center_x = width as f32 / 2.0;
    let center_y = height as f32 / 2.0;

    // Render points as white dots
    for (x, y) in points {
        let px = (x * scale + center_x) as i32;
        let py = (y * scale + center_y) as i32;

        if px >= 0 && px < width as i32 && py >= 0 && py < height as i32 {
            let idx = ((py as usize * width) + px as usize) * 4;
            if idx + 3 < bytes.len() {
                bytes[idx] = 255; // R
                bytes[idx + 1] = 255; // G
                bytes[idx + 2] = 255; // B
                bytes[idx + 3] = 255; // A
            }
        }
    }

    Texture2D::from_image(&macroquad::texture::Image {
        width: width as u16,
        height: height as u16,
        bytes,
    })
}

// Convert CARLA Image to macroquad Texture2D
fn image_to_texture(image: &Image) -> Texture2D {
    // CARLA images are BGRA, need to convert to RGBA
    let raw_bytes = image.as_raw_bytes();
    let mut rgba_bytes = Vec::with_capacity(raw_bytes.len());
    for chunk in raw_bytes.chunks(4) {
        rgba_bytes.push(chunk[2]); // R
        rgba_bytes.push(chunk[1]); // G
        rgba_bytes.push(chunk[0]); // B
        rgba_bytes.push(chunk[3]); // A
    }

    Texture2D::from_image(&macroquad::texture::Image {
        width: image.width() as u16,
        height: image.height() as u16,
        bytes: rgba_bytes,
    })
}

fn window_conf() -> Conf {
    Conf {
        window_title: "CARLA Multiple Sensor Grid View".to_owned(),
        window_width: WINDOW_WIDTH,
        window_height: WINDOW_HEIGHT,
        window_resizable: false,
        ..Default::default()
    }
}

#[macroquad::main(window_conf)]
async fn main() -> Result<()> {
    println!("=== CARLA Multiple Sensor Grid View ===");
    println!("Connecting to CARLA...");

    let mut client = Client::connect("127.0.0.1", 2000, Some(2))?;
    client.set_timeout(Duration::from_secs(10))?;

    let mut world = client.world()?;
    println!("Connected to world: {}", world.map()?.name());

    // Spawn vehicle
    println!("Spawning vehicle...");
    let blueprint_library = world.blueprint_library()?;
    let vehicle_bp = blueprint_library
        .iter()
        .find(|bp| bp.id() == "vehicle.tesla.model3")
        .context("Failed to find vehicle blueprint")?;

    let spawn_points = world.map()?.recommended_spawn_points()?;
    let spawn_point = spawn_points.get(0).context("No spawn points available")?;

    let vehicle_actor = world
        .spawn_actor(&vehicle_bp, spawn_point)
        .context("Failed to spawn vehicle")?;

    let vehicle = Vehicle::try_from(vehicle_actor)
        .map_err(|_| anyhow::anyhow!("Failed to convert actor to vehicle"))?;

    vehicle.set_autopilot(true)?;
    println!("Vehicle spawned with autopilot enabled");

    // Setup display manager
    let mut display_manager = DisplayManager::new();

    // Setup cameras at different angles (all at z=2.4m above vehicle)
    println!("Setting up cameras...");

    // Left camera (row 0, col 0)
    let left_camera = CameraSensor::spawn(
        &mut world,
        &Transform {
            location: carla::geom::Location::new(0.0, 0.0, 2.4),
            rotation: carla::geom::Rotation {
                pitch: 0.0,
                yaw: -90.0,
                roll: 0.0,
            },
        },
        &vehicle,
    )?;
    display_manager.add_sensor(0, 0, "Left Camera".to_string(), left_camera.actor_id());

    // Front camera (row 0, col 1)
    let front_camera = CameraSensor::spawn(
        &mut world,
        &Transform {
            location: carla::geom::Location::new(0.0, 0.0, 2.4),
            rotation: carla::geom::Rotation {
                pitch: 0.0,
                yaw: 0.0,
                roll: 0.0,
            },
        },
        &vehicle,
    )?;
    display_manager.add_sensor(0, 1, "Front Camera".to_string(), front_camera.actor_id());

    // Right camera (row 0, col 2)
    let right_camera = CameraSensor::spawn(
        &mut world,
        &Transform {
            location: carla::geom::Location::new(0.0, 0.0, 2.4),
            rotation: carla::geom::Rotation {
                pitch: 0.0,
                yaw: 90.0,
                roll: 0.0,
            },
        },
        &vehicle,
    )?;
    display_manager.add_sensor(0, 2, "Right Camera".to_string(), right_camera.actor_id());

    // Rear camera (row 1, col 1)
    let rear_camera = CameraSensor::spawn(
        &mut world,
        &Transform {
            location: carla::geom::Location::new(0.0, 0.0, 2.4),
            rotation: carla::geom::Rotation {
                pitch: 0.0,
                yaw: 180.0,
                roll: 0.0,
            },
        },
        &vehicle,
    )?;
    display_manager.add_sensor(1, 1, "Rear Camera".to_string(), rear_camera.actor_id());

    // Setup LiDAR sensors
    println!("Setting up LiDAR sensors...");

    // Regular LiDAR (row 1, col 0)
    let lidar = LidarSensor::spawn(&mut world, &vehicle, false)?;
    display_manager.add_sensor(1, 0, "LiDAR".to_string(), lidar.actor_id());

    // Semantic LiDAR (row 1, col 2)
    let semantic_lidar = LidarSensor::spawn(&mut world, &vehicle, true)?;
    display_manager.add_sensor(
        1,
        2,
        "Semantic LiDAR".to_string(),
        semantic_lidar.actor_id(),
    );

    println!("\nAll sensors ready! Grid view active.");
    println!("Press ESC or Q to quit");

    // Main rendering loop
    loop {
        // Handle input
        if is_key_pressed(KeyCode::Escape) || is_key_pressed(KeyCode::Q) {
            println!("\nExiting...");
            break;
        }

        // Update camera textures
        if let Some(image) = left_camera.get_latest_image() {
            let texture = image_to_texture(&image);
            display_manager.update_camera_texture(left_camera.actor_id(), texture);
        }
        if let Some(image) = front_camera.get_latest_image() {
            let texture = image_to_texture(&image);
            display_manager.update_camera_texture(front_camera.actor_id(), texture);
        }
        if let Some(image) = right_camera.get_latest_image() {
            let texture = image_to_texture(&image);
            display_manager.update_camera_texture(right_camera.actor_id(), texture);
        }
        if let Some(image) = rear_camera.get_latest_image() {
            let texture = image_to_texture(&image);
            display_manager.update_camera_texture(rear_camera.actor_id(), texture);
        }

        // Update LiDAR textures
        if let Some(points) = lidar.get_latest_points() {
            let cell_width = WINDOW_WIDTH / GRID_COLS as i32;
            let cell_height = WINDOW_HEIGHT / GRID_ROWS as i32;
            let texture =
                render_lidar_to_texture(&points, cell_width as usize, cell_height as usize);
            display_manager.update_lidar_texture(lidar.actor_id(), texture);
        }
        if let Some(points) = semantic_lidar.get_latest_points() {
            let cell_width = WINDOW_WIDTH / GRID_COLS as i32;
            let cell_height = WINDOW_HEIGHT / GRID_ROWS as i32;
            let texture =
                render_lidar_to_texture(&points, cell_width as usize, cell_height as usize);
            display_manager.update_lidar_texture(semantic_lidar.actor_id(), texture);
        }

        // Render display
        display_manager.render();

        next_frame().await;
    }

    println!("Cleaning up...");
    println!("Note: Actors remain in simulation (manual cleanup required)");

    Ok(())
}