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

c210 - 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)
31 views

c210 - 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/ 8

Let’s have a look at the code.

Let’s understand the code:


1. First, we have imported our libraries and set the path for importing CARLA files.

2. Then we have defined the get_actor_display_name() function which returns the


name of the collider.

3. Then we have the car_control() function which will drive the car straight.
4. Then we have a block of code for initialising the car in the try block where we can
call and define all the libraries we are going to need for spawning and selecting a
car.

5. Then in line 66-68, we have defined a collision sensor. In case of a collision, this
sensor will take action.

6. Then we have called the sensor.listen() method to pass the environment data to
the _on_collision() function, get the collision data, and append this sensor
to actor_list.

sensor is the variable that holds the collision sensor and collision point of the vehicle.
In the previous class, we had the _on_collision() function for displaying collision.
We are going to gather data from the surrounding and sense a collision before it
happens. This is going to help us make our car intelligent.
For this, we are going to work on a powerful technique called Data gathering.
Data gathering :
Data gathering basically means collecting, measuring, and analyzing accurate data for
getting the useful insights of a research.

The following code we will be doing in today’s class:

Here, you can see that we have created one function called number_of_vehicle() in
which we are performing the below task:

1. First, get the actors from and apply


the filter method to select all

vehicles and store it in

the variable.
2. Get the data continuously from all the moving cars: Since the cars keep
moving and we want to get the data continuously, we will be using one library
called threading.
Threading()
Threading is a process where we perform certain operations continuously after a set
interval of time, and each of the operations performed can be called as a thread.
There are 3 things we can do:
• Stop a thread.
• Start a thread.
• Sleep a thread.
Whatever we want to continuously perform certain operations, we can do it using
the threading() library.
For example: Remember in p5.js in class C113, we had used the draw() function which
was called continuously. Similarly, you can consider this as an example of threading to
perform an operation or process continuously.
Example:
import threading #import threading library
def newFunction(): #Define function
print("Hello, Whitehat coders!",) #Print a line
threading.Timer(0.1, newFunction).start()
• threading is the library which we have imported.
• Timer() is a function that receives two parameters. One is Time
in millisecond and the other one is the name of the
function newFunction() which you want to run in threads.
• Thread means calling the function continuously which is mentioned with an
interval of 0.1 millisecond.
• start() function starts the threading.
So in our case,

• calls a threading library.

• receives 0.1
millisecond time and runs the number_of_vehicle function.

• This function starts the threading process by running


the number_of_vehicle function multiple times with an interval of 0.1
millisecond, which will result in getting the car's location after every 0.1
millisecond.
3. Now, we need the location of our car. We will get the location of our car by

- and store this

location in the variable .


Here dropped_vehicle holds information about the spawn car. So using
the get_transform() function, we can get the location of our spawn_car.
4. Now print the transform_location variable where we can see the updated
location of all the cars in the environment.
5. At the end of the code, we are

calling and
functions before the finally code block.

Now we have the number of bot cars running in the city and also the updated location of
our car.
Using this information, now we can get the distance of each bot car from our car. In
other words, we are going to get the distance of each car and check it with our car
location.
Note: Below code is only the explanation part, you dont have to implement it.
For this we are going to follow below steps,
• Check condition where number of bot cars should be more than 1
• Subtract the location of our car with every other bot car
• Apply square root on result
• Display the distance of each car from our car

Here,
1. First, we are checking the length

of bot_vehicle must be greater than >

1
• Now inside this ‘if-condition’, we have a function with

lambda and perform math square root


operation on two entities. Which
are location of bot cars and our car.
This lambda function will be used to calculate the distance between bot cars and our
car.
Let’s understand with an example -
We have two cars one is a BMW and the second is an Audi. We have location of BMW in
X, Y and Z format like this,

BMW = (X=0.0000, Y=77.120049, Z=0.0000)


Audi = (X=21.1400, Y=47.120049, Z=0.0000)
Now we want to get the distance between these two cars, Which we can get by
subtracting BMW and AUDI X,Y and Z axis respectively and add this result together like
this,
(X=0.0000 - X=21.1400) + (Y=77.120049 - Y=47.120049) + (Z=0.0000 - Z=0.0000)
And we can get some value right.
But this value won’t be accurate. So to get the accurate value we will multiply each axis
with 2.
((X=0.0000 - X=21.1400) ** 2 + (Y=77.120049 - Y=47.120049) **2 + (Z=0.0000 -
Z=0.0000) ** 2)
And then apply math.sqrt function. So we can have an exact number in meters.
Now lets see this in code -


subtraction of axis X of car A with car B.


subtraction of axis Y of car A with car B.


subtraction of axis Z of car A with car B.
• Now add this result of all the three axises

• And apply math.sqrt function for square root.

And store in the distance variable.


2. Now lets loop bot_vehicles variable and use the lambda function created above
to get the distance between our car and bot car.

Now, we have stored all the distance values in get_disatnce_of_bot_vehicle variable.


To see the result, on line 42 we have provided a print statement to print the result.
Now uncomment this statement and run the program again.

You might also like