Question: Extend the program in this activity so that it properly stores and sorts the distances measured/calculated. Feel free to implement any of the sorting algorithms

Extend the program in this activity so that it properly stores and sorts the distances measured/calculated. Feel free to implement any of the sorting algorithms that you have learned in this curriculum so far. Your program should first display the distances in the order that they were measured/calculated (i.e., unsorted). It should then sort the distances and subsequently display them again, this time in sorted order. Make sure to represent the distances with a precision of four decimal places (to the right of the decimal point). It is highly recommended that you place the sorting algorithm that you select in its own function that is appropriately called in your program. In fact, not doing so would be considered quite inefficient (and will result in points being deducted). Here's sample output of a properly implemented program. Of course, the known distance and distance values measured/calculated are unique to this particular run of the program. User input is highlighted in bold:

Waiting for sensor to settle (2s)...

Calibrating...

-Place the sensor a measured distance away from an object.

-What is the measured distance (cm)? 20.5

-Getting calibration measurements...

Done.

Press enter to begin...

Getting measurements:

-Measuring...

--Distance measured: 20.5464cm

--Get another measurement (Y/n)? y

-Measuring...

--Distance measured: 98.1243cm

--Get another measurement (Y/n)? y

-Measuring...

--Distance measured: 41.3557cm

--Get another measurement (Y/n)? y

-Measuring...

--Distance measured: 19.9691cm

--Get another measurement (Y/n)? y

-Measuring...

--Distance measured: 5.0077cm

--Get another measurement (Y/n)? n

Done.

*This is the program that I already have completed so this just needs to be added to.

import RPi.GPIO as GPIO from time import sleep, time from random import randint

DEBUG = True FAKE = True SETTLE_TIME = 2 CALIBRATIONS = 5 CALIBRATION_DISPLAY = 1 TRIGGER_TIME = 0.00001 SPEED_OF_SOUND = 343

GPIO.setmode(GPIO.BCM)

TRIG = 18 ECHO = 27

GPIO.setup(TRIG, GPIO.OUT) #TRIG is output pin GPIO.setup(ECHO, GPIO.IN) #ECHO is input pin

def calibrate(): if (FAKE): return 1 # Print a few beginning statements to show what is going on print "Calibrating..." print "-Place the sensor a measured distance away from an object" known_distance = input("-What is the measured distance (cm)?") print "-Getting calibration measurements..." # Start taking measurements to the known object a certain number of # times while sorting the total distance in distance_avg distance_avg = 0 for i in range(CALIBRATIONS): distance = getDistance() if (DEBUG): print "--Got {} cm".format(distance) distance_avg += distance sleep(CALIBRATION_DELAY) # Change total distance to average distance distance_avg /= CALIBRATIONS if (DEBUG): print "--Average is {} cm".format(distance_avg) # Divide the known distance correction_factor = known_distance / distance_avg if (DEBUG): print "--correction factor is {}".format(correction_factor) print "DONE" return correction_factor

def getDistance(): # Trigger the sensor GPIO.output(TRIG, GPIO.HIGH) sleep(TRIGGER_TIME) GPIO.output(TRIG, GPIO.LOW) # keep on resetting start time until the input pin stops being low. if (DEBUG): print "Waiting for echo to start" while (GPIO.input(ECHO) == GPIO.LOW): start = time() # keep on resetting stop time until the input pin stops being high. if (DEBUG): print "Waiting for echo to stop" while (GPIO.input(ECHO) == GPIO.HIGH): end = time() #calculate the distance from the duration duration = end - start distance = duration * SPEED_OF_SOUND distance *= 100 #change M to CM distance /= 2 #half the distance

######################## MAIN ######################### # 1. Allow the sensor to settle for a bit print "Waiting for the sensor to settle ({}s)". format(SETTLE_TIME) sleep(SETTLE_TIME)

# 2. Calibrate the sensor correction_factor = calibrate()

# 3. Measure raw_input("Press enter to begin...") print "Getting measurements:" while (True): print "-Measuring..." #Get distance (using getDistance function) and multiply it #by the correction factor distance = getDistance() * correction_factor #Wait a second (its hammer time) sleep(1) #then round the distance to 4 decimal points distance = round(distance, 4) #Ask if the user wants to make another measurement and break out of #the loop if they do not print "--Distance measured: {}cm".format(distance) i = raw_input("--Get another measurement (Y/n)? ") if (not i in ["y", "Y", "yes", "Yes", "YES", ""]): break # 4. Clean up print "Done" GPIO.cleanup()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!