PA Designer Learn 4: More Python Commands

Site: GoLabs
Course: Robotic Challenges with Python and GoPiGo
Book: PA Designer Learn 4: More Python Commands
Printed by: Guest user
Date: Wednesday, 4 December 2024, 11:49 AM

Description

This is the second challenge within the Robotics with GoPiGo and Python Curriculum.

Your task is to program your robot to alert a person with a hearing impairment to visiting house guests.

Learn how to use the Loudness Sensor alongside Jupyter Notebooks.

1. Making Decisions

We all have to make decisions in life. For instance, imagine you were about to go outside. If it were raining you would take an umbrella with you, wouldn’t you? You’ve just made a decision!

Your robot is no different. It will have to decide when to stop listening for the bell and turn the light on. The way it makes decisions is with conditional statements. In Python code , these take the following form:

if condition:
    action

Don’t forget the colon after the condition and indentation of the action code. Both are important!

2. Let’s try a decision example

Let’s try an example by telling our robot to open its eyes if more than 5 seconds has elapsed. In our Main Code cell, enter the highlighted text just above the sleep instruction and click on Run Selected Cell. What behavior do you observe?

Main Code cell

# perform a time check and add elapsed time to end of the times list
    times.append(elapsed_time())

    # conditional statement
    if int(elapsed_time()) == 5:
        assistant.open_eyes()

    time.sleep(0.02)

Next, let’s look at what this code does.

3. The int() function

The int(  ) function returns the integer part of a number. This is the part of the number that comes before the decimal point.

In the above code int(elapsed_time()) returns the elapsed time in whole seconds only.


4. Equality tests

int(elapsed_time()) == 5 is an equality test.

If the values on both sides of the equality operator (==) are the same then the test result is True. Otherwise the test result is False.

Note the use of a double equal sign, not just a single equal sign!

In our example, the equality test will become True after 5 seconds of elapsed time and turn False again after 6 seconds of elapsed time. This will result in our robot opening its eyes for one second only.

5. Reading List Values

What if we want to examine or use some of the values stored in our lists?

We might want to do this if we need to know about the most recent value that we have added to it. We can access values in a list by referring to their position (or index) in the list. 

          • In the Data Analysis cell, enter the following code underneath your graph plotting instructions and click on Run Selected Cell:

Data Analysis cell

# examine some of the values that we have read
print ( f“First loudness value = {values[0]}” )
print ( f“Last loudness value = {values[-1]}” )

          • Compare the numbers displayed under your graphs with the height of the first (leftmost) and last (rightmost) sample points in the graph of raw loudness values.

List indices start counting from 0 so values[0] is the first sensor value that we read, values[1] is the second etc…

Starting from -1, negative numbered indices count back from the end of the list so that values[-1] is the last value that we read, values[-2] is the penultimate value etc…

Why not change the index numbers above to display some other values from the list or change the list name (values) to read some values from the smoothed_values or times lists?



6. Mark Learn as Done

Congratulations! You are now ready for the big project!!