c210 - Code Diagram
c210 - Code Diagram
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.
Here, you can see that we have created one function called number_of_vehicle() in
which we are performing the below task:
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,
• receives 0.1
millisecond time and runs the number_of_vehicle function.
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
1
• Now inside this ‘if-condition’, we have a function with
•
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