0% found this document useful (0 votes)
14 views

c208 - Code Diagram

Uploaded by

Divyesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

c208 - Code Diagram

Uploaded by

Divyesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Complete Code -

Code Breakdown -

1. In this code, we are importing libraries such as glob, os, sys, and time.

2. Then comes the try-except block. A try-except block is used to avoid getting errors and crashing our program. This block of
code helps us to call our CARLA module file stored in this location: WindowsNoEditor > PythonAPI > carla > dist.

3. On line 17, we are defining a function car_control() in which we are going to declare car speed, turning action, and also
time to perform the action.

Here, def car_control is a function for providing vehicle control:


- dropped_vehicle is a car which we have spawned.
- apply_control is a method which initiates controlling the dropped vehicle.
- vehicle_control is a CARLA library which controls parameters to drive a car which are throttle for speed, steer for
turning the car, and gear for smooth movement.

4. Then, in the main block, we have defined the CARLA world with get_blueprint_library() with a car model and spawn point
where we want our car to appear.

5. Later we have defined a location for a simulator camera which is very important in this case because we want to see where
our car is going.
6. Now, we need to define a collision sensor.

7. On line 57 and 58, using the carla.transform() method, we have set the location for the collision sensor and stored it in the
sensor_collision_spawn_point variable.

8. Then on line 60, we are using the lambda function to listen to the sensor. It collects information from the environment.

9. Here on line 62, we are appending the collision sensor in actor_list.

10. Now we need to define an action if a collision happens. It's important because the sensor remains active while our car is
being driven on the road and we need to specify which action it must take if a collision happens.

● Here you can see on line 64, we have defined the _on_collision() function for actions. In this function, we are
declaring what action and notification CARLA must return whenever a collision happens.

● On line 65, we are printing (“**Caution Collision**”) to notify about the collision. Whenever the car hits on something,
we will be able to see something like this in the command line:

This indicates that a collision has happened.

● Now let's take some action if a collision occurs:

● Now, after stopping, we can resume the car and start driving again, by calling the car_control() function.

This function will resume driving whenever the car hits something. This function is called only when a collision occurs.

11. But if there is no collision, in that case, the car is supposed to move smoothly, right? Hence for this, we will again call the
car_control() function.
.

12. Then we are proving time.sleep() for 1000 milliseconds. The program will run for this duration.

13. Now, it’s time to destroy the car and other actors.
By destroying actors, we are clearing created sensors and vehicles in CARLA. This eventually clears the cache from the
program and helps to run new programs.

Complete the try and finally block, and destroy the actors created by CARLA.

How to test the code -

NOTE: (Please close all applications for better performance of CARLA simulator)
WINDOWS:
1. Open Task manager.
1. Stop all the processes that are not required and are taking more Memory.
2. Run only 1 CARLA application at a time. Opening multiple CARLA applications will slow down the system.

Run CARLA simulator in command prompt like this from the WindowsNoEditor folder:

Now, run this command in cmd: CarlaUE4 -quality-level=Low


By running the above command, you are running CARLA in low resolution which helps you move in CARLA faster. It won’t
slow down your system.

Open the examples folder from WindowsNoEditor > PythonAPI > examples.
Select the full path from the address bar and type cmd like this:
Now press Enter.

Open two command prompts in the example folder and activate your CARLA environment.
- One command prompt to run the actual python file for collision detection.
- Another command prompt to spawn the cars using the spawn_npc.py file.

In first command prompt, run this file: python spawn_npc.py -n 50

In the second command prompt, run this file: python class_208.py

You might also like