c208 - Code Diagram
c208 - Code Diagram
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.
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.
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:
● 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.
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:
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.