c209 - Code Diagram
c209 - Code Diagram
2. On line 65, we called the sensor.listen() method to return the data from the sensor.
● We want to display the name of the object to which our car collided.
● Now we want to print the variable actor_type, and as a parameter, we will be passing data.other_actor.
The variable actor_type contains the object name which the car collided with.
● Now we will write code for getting the intensity of the collision.
Now we will use data which is the parameter of the _on_collision() function for getting the values of collision.
Inside data, we have something called normal_impulse which contains the collision result.
The normal_impulse stores the value of the X, Y, and Z.
As the CARLA world is a 3D setup, so the coordinates or the values which we will get will also represent 3D. So it would
have x, y, and z values related to the collision.
Hence, we will write data followed by normal_impulse like this - and store it in the
collision_event_record variable.
Then the final value which we are receiving is going to take the square root of that value and store the value in
intensity_of_collision variable.
In school, you must have learned about square roots. Right?
For example:
Let’s consider, we have 3 variables x, y, and z with the values x = 24 , y=2, and z=63.
Now let’s solve this with math.sqrt.
So, math.sqrt( x*2 + y*2 + z*2 )
math.sqrt(24*2 + 2*2 + 63*2)
Then the answer will be, 13.341664064126334
So collision intensity is 13.341664064126334
● In Python, we want to take the square root of any value. We can just use the math library for it. For this, we need to import
the math library. So on line 5, using the math library, we can perform mathematical operations easily.
● Now we can print the intensity_of_collision variable to display the intensity of collision.
● Now we are done showing the result of the collision. And in the previous class, we added code to add breaks when a
collision is detected.
We are applying brakes for 5 milliseconds after a collision, just to stop the car.
● On line 81, we are calling the car_control function to run the car to reach our destination.
● Now, on line 17, we have defined the function as get_actor_display_name in which we are getting the name of the actors
using the split() and the replace() functions.
Before this, let’s understand how the split and the remove functions work. Let’s take one simple example.
split() -
Using the split function, you can split characters in a sentence in separate words like,
“ Hi i’m whitehat jr student ”
When you apply a split function on it. It will look something like this,
[“Hi”, “i’m”, “whitehat”, “jr”, “student”]
Here the txt variable contains the sentence where we have applied the split() function and have stored the result in x variable.
Then when we print the x variable using the print() function, we will get the output as the list of strings which are in the splitted form
of the original sentence.
print(x)
Output:
[“Hi”, “i’m”, “whitehat”, “jr”, “student”]
It is useful when you are working on text cleaning. Sometimes, we might need to fetch some useful information from raw data or
large sentences. In that case, we need to split those sentences into a list of words and then use only the required data. This is
called text cleaning.
remove() -
As the name suggests, the remove function is used to remove a word or text from a sentence.
Let’s take split for example:
We had a sentence like this:
“ Hi i’m whitehat jr student ”
So if you want to remove “Hi” from the given list of words, you can use the remove function like this:
● Store the list in the txt variable: txt = [“Hi”, “i’m”, “whitehat”, “jr”, “student”]
● Apply the remove function to remove hi from the txt list: txt.remove(‘hi’)
● Print the output: print(txt)
Output:
[“i’m”, “whitehat”, “jr”, “student”]
● So we want the name of the actor/object which collided with the car. If we print actor, we will receive something like this,
Here, the actor stores information about the actor id and type. Here, id is an integer value and type is the name of the actor. It
could be anything. Here, in the above screenshot, we have the actor type as static.pole. It means the car collided with a pole.
If the car collides with any other car, you can see the name of the other car in actor type.
● Now, we need to clean the string which is Actor(id=0, type=static.pole), so we can get the name only.
We will be using a Data technique of cleaning dirty data. In other words, most of the time, we have data which contains a lot of
unwanted information such as a comma(,), a question mark(?), etc. So when we are working on data, we only want important
information that can be useful. For that, the technique we are using is known as data cleaning. I guess that you can relate to this,
as in the data visualization phase, we had done a lot of cleaning data.
● To remove all the unwanted characters and information, we need to use the replace and the join method.
Let understand this code with an example:
❏ When collision happens, each actor is given a type_id, and inside it, we have all the related data such as collider
name; hence we need to clean type_id to get the required data.
❏ For that, first, we will replace the underscore ‘_’ with the dot ‘.’ and get the required data. For this, we will write the
below code -
❏ Then we will apply split(‘.’)[1:] on index 1. We get the collider name by doing this -
Basically, this split(‘.’) function will split the complete text and only get the text which is after ‘.’
❏ After the split() function, we will get data in list format. So, to convert it into string and readable format, we will use the
join() function like this:
Example -
❏ And we want to clean the text and extract only model3 tesla name from the given string.
❏ After we get the string, we will stored it in a variable like this:
text = 'Actor(id=0, type=static.model3 tesla)'
- name.title() - It means that it will look at the type section inside the actor string which is type=static.model3 tesla.
- [1:] is an index number that represents the location of the type parameter present in Actor. So the type parameter is
present at 1. Hence we have written index 1.
- Then when we apply the split('.') function on string which will look into the string ‘static.model3 tesla and split the
text and get the part which is after the ‘.’ and store in the name variable.
print(name)
>Output: [‘model3’, ‘tesla’]
3. Now we will apply the join() function to the name variable which holds the collider name. The reason for applying the join
function is that as the split() function gives us an output in a form of list but we want an output in a form of string, so the join
function helps to show the output in a form of string. Henc, we can apply the join() function.
collider_name = ' '.join(name)
- Now we have [‘model3’, ‘tesla’] in name so the join function will convert the list into a string and we will get output
as model3 tesla.
This is a method which is used to remove unwanted data and retain useful ones and it returns the name of the collider at the end of
the function.