Courier Extension 1: Event Driven Programming

Site: GoLabs
Course: Robotic Challenges with Python and GoPiGo
Book: Courier Extension 1: Event Driven Programming
Printed by: Guest user
Date: Monday, 20 May 2024, 7:50 PM

Description

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

Your task is to design a robot program to deliver a selection of mail to the residents of GoPiGo Drive, a small cul-de-sac containing 3 houses.

Learn how to how to use and calibrate the color sensor.

1. Behind the Clickable Button Panel

Although we won’t be asking you to write any of this code, it’s useful to see how this is done. Let’s take a brief fly-past tour of the code.

In the Interactive Control Code cell you will see the following line of code:

buttons[0].on_click(test_line)

This tells the robot to run the test_line function when the first button (buttons[0]) is clicked.

2. Adding a Button

In the same code cell you will also see the following code. This tells us that the first button added to the list of buttons is labelled Test Line Follower.

buttons = []
buttons.append(widgets.Button(description="Test Line Follower", layout=items_layout))

3. Defining the Callback

Here’s the definition of the test_line function that is called by the button’s on_click() event:

def test_line(_):
    output.clear_output()         # [1]
    with output:                  # [3]
        test_line_follower()      # [2]

This type of functions, that gets called when an event such as a button click happens, is referred to as a callback function.

This tells the robot to [comment #1] clear the output widget [ comment #2] run the test_line_follower() test function that you wrote and [comment #3] direct any output generated by your test function to the output widget. In this instance, it displays output messages directly under the button panel.

For speed and convenience, we have followed exactly the same process to link all the other clickable buttons to the relevant functions in your template notebook for you.


4. Optional Challenge

By walking yourself through the code, convince yourself that this is indeed the case for another button in that panel, such as the Test Color Sensor button.

5. Second Optional Challenge

Add another button which can do whatever you deem interesting: a special delivery dance, a light show using the eyes, etc...

6. Next Step

You can now attempt the second extension: Look for Open Doors which adds the distance sensor into our sensor mix.