Up until this lab, we were calling the desired functions from our ENGI1020 module. In this lab, we will practice designing and using our own functions!
Purpose and Outcomes
In Lab 5 we will write functions with the theme of data collection from Labs 3 and 4, where we will collect data samples from one of the sensors and use that information to build a simple game.
We will:
-
create lists, append to lists;
-
practice defining and calling functions in Python; and
-
implementing flow control through while/for loops and if statements
By the end of this lab, you should be able to:
-
use a function specification to design and implement a function definition; and
-
call functions you have defined;
Preparation
We will use loops, lists, and functions to build a simple reaction game using the following:
-
Accelerometer Access: We will be monitoring the x, y, and z measurements from the accelerometer.
-
LED
The objective of this game is to test a player’s reaction time to specific hand motions. The game will instruct the player to "Flip left" or "Flip right" the Arduino using a 3-axis accelerometer. The player’s task is to execute the correct motion in response to the instruction. The game features include:
-
The game has two possible instructions: "Flip left" or "Flip right."
-
The game will randomly select and announce an instruction.
-
The player should be ready to execute the indicated gesture as soon as it’s announced.
-
If the player performs the correct motion within the specified time window, they win the round. Otherwise, they loose.
-
The player can choose to play again after each round.
Given the above discussion, here are the requirements:
-
Design 1: You will create a function that takes a certain number of samples from the accelerometer sensor and store these in the correct order in a list. What will be the function header if the function is called collect_data. What are the parameters that will need to be passed into the function to make your algorithm work for any number of samples?
-
Design 2: Create a flowchart of the following game design:
-
The instruction is announced to the player, whether to lift right or left.
-
Print to the user 'Get ready to:' and print the selected instruction.
-
Give a few seconds (e.g., 3 seconds) to prepare the player to play.
-
Call the function get_result() and save the output in a variable (i.e., This function returns True if the user implemented the task successfully, and False if the user didn’t).
-
If the player successfully performs the motion, print a "Great job!" message. If not, print "Too slow!" message.
-
At the end of each round: ask the user if they would like to play again. If yes, allow to play again. Otherwise, end the game.
-
Procedure
Lab 5 will be completed in groups of two students. This means that, as a pair, you will submit one logbook entry. Each pair also needs to submit one script.
To start, download this script lab5-w2024.py and open it in Thonny.
2.1 Implement Data Collection: collect_data(…)
Using your understanding of the problem and the answer from the preparation design, add the required parameters to the provided function definition header and implement an algorithm that reads N samples from the x-axis acceleration, y-axis acceleration, and z-axis acceleration and stores them in separate lists. The function will return the average of the three lists separately. You can use the append() method to add new data entries to the defined lists. You will have three readings to take each time.
Call the function to ensure that this function is working correctly.
Q1: Record the output and note any errors you got while implementing this step.
2.2 Implement the decision function: get_result()
This function will take the string instruction as an input, and it will return True if the user implemented the task successfully or False if the user failed to complete it.
The function should start by collecting the data (i.e., call the collect_data function). Then, depending on the instruction, the code checks if the average accelerometer value for the accelerometer crosses a predefined value.
Here are a few points to consider:
-
What would be the condition if the instruction was to flip the Arduino to the left?
-
What would be the condition if the instruction was to flip the Arduino to the right?
Tip
|
To figure out these conditions, observe the output of the accelerometer when you do the action (i.e., flip to left and right), and determine the value that you should compare the averages to. |
Q2: Describe how did you choose your conditions, and your observations.
2.3 Incorporating the game
-
At the start of the round, the program creates a list of two possible instructions: "Flip to the left" and "Flip to the right."
-
Then, it randomly selects one instruction from the list.
-
The selected instruction is announced to the player, who is instructed to get ready to perform the indicated motion.
-
You can print to the user 'Get ready to:' and print the selected instruction
-
-
Give a few seconds (e.g., 3 seconds) to prepare the player.
-
Call the function get_result and save the output in a variable (i.e., which will be boolean, True or False)
-
If the player successfully performs the motion, print a "Great job!" message.
-
If not, print "Too slow!" message and turn on the LED.
Q3: Have you found the conditions you selected in the previous step to be feasible, or have you made adjustments? If adjustments were made, what prompted these changes, and why?
2.4 Allow the game to run continuously
In this step, implement a loop to allow the game to run continuously. At the end of each round: ask the user if they would like to play again. If yes, allow to play again. Otherwise, end the game.
Q4: Which kind of loops did you use, and why?
Warning
|
Logbook submission: Make sure to submit the python file that includes the coding part for all the previous steps, along with the pdf file that includes the rest of the details (preparation, answers to procedure questions, and testing). |
3. Testing
Record how you tested each step of the implementation in a tabular format, including the output of collect_data, the game as a whole, etc.
In addition to that, we are going to test step 2.2 together! Once you are satisfied that your function definition, we are going to trade them.
We will be copying our function definitions and pasting them into Lab 6 Testing postings (Just the function header and body, not the whole script). We will copy another group’s function from a different posting and use it in our own script. Then, we will rerun the script and check if our observations remain the same. In your logbook, note the comparison and the group number.