SPIKE™ Prime with Python

Clean Up with Multiple Functions

Investigate programming a grabber to pick up trash using multiple functions

45 min
Beginner
Years 7-9 OR Key Stage 3

Questions to investigate

• How can grabber be programmed to pick up trash using multiple functions?

Prepare

• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.

Engage

(Group Discussion, 5 minutes)

Engage students in a discussion about trash and how trash affects our environment.

Consider showing images and videos of trash in different types of environments like the ocean, rivers, parks, etc. Have students think about how this trash affects the things that live in these environments.

Ask students to consider ways to clean up trash.

Explore

(Small Groups, 20 minutes)

Students will investigate how to use more than one function in a program.
Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for the Super Cleanup model. Ask students to build the model. The building instructions are also available at https://education.lego.com/en-us/support/spike-prime/building-instructions.   

Direct students to open a new project in the Python programming canvas. Ask students to erase any code that is already in the programming area. Students should connect their hub.  

Prompt students to think about how they program a tool to help them pick up trash.

Sample Program:

from hub import port
import runloop
import motor
import force_sensor

def is_pressed():
    return force_sensor.pressed(port.E)

def is_released():
    return not force_sensor.pressed(port.E)

async def main():
    while True:
        await runloop.until(is_pressed)
        #motor will close grabber
        await motor.run_to_absolute_position(port.A, 345, 500, direction=motor.COUNTERCLOCKWISE )
        await runloop.until(is_released)
        #motor will release grabber
        await motor.run_to_absolute_position(port.A, 25, 500, direction=motor.SHORTEST_PATH )

runloop.run(main())

Allow students to test the program by experimenting picking up a variety of materials with both grabbers. Discuss as a group which materials each grabber is best suited for picking up.

Using Multiple Functions
Prompt students to modify the program to include a function for a stronger grip and a second function for the lighter grips.

Discuss with students how to include functions that can be called based on which grabber attachment is being used. Students will want to make sure that the grabber opens and closes as needed depending on which attachment is used. Therefore, students can create a separate function for each.

Sample Program:

from hub import port
import runloop
import motor
import force_sensor

def is_pressed():
    return force_sensor.pressed(port.E)

def is_released():
    return not force_sensor.pressed(port.E)

def grab_and_release(grip):
    await runloop.until(is_pressed)
    # motor will close grabber
    await motor.run_to_absolute_position(port.A, 345, grip, direction=motor.COUNTERCLOCKWISE )
    await runloop.until(is_released)
    # motor will release grabber
    await motor.run_to_absolute_position(port.A, 25, grip)

async def main():
    while True:
        await grab_and_release(1000)

runloop.run(main())

Note: Students will have to change grabber arms depending on which is being used at the time.

Allow students time to test their program using both grabbers to see if the functions are working properly. Remind students of the importance of how they call their functions to be used in their program to ensure they use the right function at the right time.

Explain

(Whole Group, 5 minutes)

Discuss with students how each of the programs worked.
Ask students questions like:
• How do the two functions compare?
• Why might someone want to add a function to a program?
• What do you need to consider when using multiple functions in your program?

Elaborate

(Small Groups, 10 minutes)

Challenge students to modify their programs to allow for picking up objects differently with each grabber.

Students should modify their program to include a function specific to each grabber. This will allow students to call the function depending on which grabber arm is being used rather than using the same program for each grabber arm.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• Why would you need a separate function for each grabber arm?
• What did you change about the function for each grabber arm to make it more specific to the way that grabber arm worked?
• Why is it helpful to define multiple functions in a program that can be called later as needed?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using multiple functions in your program?
• What characteristics of a good teammate did I display today?
• Ask students to rate themselves on a scale of 1-3, on their time management today.
• Ask students to rate themselves on a scale of 1-3, on their materials (parts) management today.

Teacher Support

Students will:
• Build and program a grabber to pick up items
• Modify the program to include multiple functions

• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Student journals
• Various items to pick up with the grabber (items that can represent trash are ideal)

CSTA
2-CS-02 Design projects that combine hardware and software components to collect and exchange data.
2-AP-13 Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
2-AP-17 Systematically test and refine programs using a range of test cases.