Lab 4: Understanding Your Code
Purpose
I noticed that some people are struggling with understanding what the code does. So we will spend some time to go through this.
Learning Goals
After completing this exercise, you should be able to
- understand the order in which lines of the code are executed when running the program
- differentiate between initial declarations, measurement function, and measurement loop.
- describe what is happening inside the measurement loop
- be able to place the additional lines of code in the correct location of the program
Initial Program
You were provided with this initial program:
We can split this program into four parts:
- Initialization: The first part of the program, loads the require Python modules and sets up the
GPIO
to communicate with the sensor. - Measurement Function:The second part of the code defines a function that is later executed to conduct the measurements. This function contains all the steps necessary to conduct a single distance measurement.
- Measurement Loop: After the program is initialized and the measurement function is defined, we can conduct measurements. This is done in a loop to perform multiple consecutive measurements.
- Shut down: The final part of the code, de-initializes the
GPIO
so that this resource is being released rather than abruptly terminated when the program ends.
Task:
Go through the program below and identify each of the four program sections:
import RPi.GPIO as GPIO
import time
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
#set GPIO Pins
= 21
GPIO_TRIGGER = 20
GPIO_ECHO
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def distance():
# set Trigger to HIGH
True)
GPIO.output(GPIO_TRIGGER,
# set Trigger after 0.01ms to LOW
0.00001)
time.sleep(False)
GPIO.output(GPIO_TRIGGER,
= time.time()
StartTime = time.time()
StopTime
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
= time.time()
StartTime
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
= time.time()
StopTime
# time difference between start and arrival
= StopTime - StartTime
TimeElapsed # multiply with the sonic speed (34326 cm/s)
# and divide by 2, because there and back
= (TimeElapsed * 34326) / 2
distance
return distance
for x in range(100):
= distance()
dist print ("Measured Distance = %.1f cm" % dist)
.1)
time.sleep(
GPIO.cleanup()
Make sure that you understand what each line in the measurement loop does.
Answer the following questions:
- What line executes the measurement?
- How often are measurements conducted?
- What is likely a good approximation of the time step (\(\Delta t\)) between measurements?
Code Additions
You were asked to add two functionalities to your code:
- Code to save each measurement to a
csv-file
:
for x in range(...):
...print("Measured Distance = %1.f cm" % dist)
= open("ultrasounddata.csv", "a")
f '\n' + "Actual Distance = , 10 cm, Measured Distance , %.2f" % dist)
f.write(
f.close() ...
- Code to switch on an LED as soon as a measurement hits a certain threshold
= 17 #
GPIO_LED
GPIO.setup(GPIO_LED, GPIO.OUT)if (dist < 5):
GPIO.output(GPIO_LED, GPIO.HIGH)print (“WARNING!!!!”)
else:
GPIO.output(GPIO_LED, GPIO.LOW)
Task:
DO this in your group:
- Go through these line by line to make sure that you understand what each line does.
- Decide where in the code each line has to go to accomplish.
- How would you implement a speed calculation in your code?