SPIKE™ Prime with Python

Clean Up with Multiple Functions

Investigate programming a grabber to pick up trash using multiple functions

45 min
Başlangıç seviyesi
6.-8. Sınıf

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 spike import PrimeHub, ForceSensor, Motor

hub = PrimeHub()
force = ForceSensor('E')
grabber = Motor('A')

while True:
    force.wait_until_pressed()
    grabber.run_to_position(25, 'clockwise')

    force.wait_until_released()
    grabber.run_to_position(0, 'counterclockwise')

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 the larger grabber and a second function for the smaller.

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 spike import PrimeHub, ForceSensor, Motor

hub = PrimeHub()
force = ForceSensor('E')
grabber = Motor('A')

\# define the function for the grabber arm to grab something
def grab(): 
    grabber.run_to_position(25, 'clockwise')

\# define the function for the grabber arm to release something
def release():
    grabber.run_to_position(0, 'counterclockwise') 

while True:
    force.wait_until_pressed()
    grab()

    force.wait_until_released()
    release()

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 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 student to rate themselves on a scale of 1-3, on their materials (parts) management today.

Öğretmen Desteği

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.

LEGO, the LEGO logo, the Minifigure, DUPLO, the SPIKE logo, MINDSTORMS and the MINDSTORMS logo are trademarks and/or copyrights of the LEGO Group. ©2022 The LEGO Group. All rights reserved. Use of this site signifies your agreement to the terms of use.