SPIKE™ Prime with Python

Automate the Clean Up

Create a sorting robot and program it to use functions for an automated clean-up process

90-120 min.
Intermed.
Years 7-9 OR Key Stage 3

Questions to investigate

• How can functions be used in programming for efficiency?

Prepare

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

Engage

(Group Discussion, 10 minutes)

Engage students in a discussion about how to automate the clean-up effort.

Discuss with students the ways that they need to work to keep the environment clean. Think of ways that this can be automated using robotics to help keep things clean. Discuss that some materials are trash, but many can be recycled. Have students identify various materials that would be considered trash and items that could be recycled.

Prompt students to think about ways to sort the materials into trash and recyclable materials.

Explore

(Small Groups, 45 minutes)

Students will investigate how to automate clean up and sorting materials into trash and recycling.

Direct students to build the Quality Check Robot model. The building instructions are 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 to program their sorting robot to identify if a material is trash or recyclable. Students should create a function for checking the color of materials then sorting the recyclable materials into the blue area in front of the robotic sorter while throwing the trash away.
Note: Students will need to assign a color to trash and a color to recycle. The colors used in the building instructions are yellow and violet. Consider letting students change these to other colors if they want.

Sample Program:

from hub import port
import runloop
import color_sensor
import motor
import color
from app import sound

async def main():
    await motor.run_to_absolute_position(port.A, 0, 500)
    await motor.run_to_absolute_position(port.F, 240, 500)
    await motor.run_to_absolute_position(port.A, 90, 500)
    await motor.run_to_absolute_position(port.F, 25, 500)

    await check_color()

    await motor.run_to_absolute_position(port.A, 0, 500)
    await motor.run_to_absolute_position(port.F, 240, 500)
    await motor.run_to_absolute_position(port.A, 270, 500)
    await motor.run_to_absolute_position(port.F, 25, 500)

    await check_color()

    await motor.run_to_absolute_position(port.A, 0, 500)
    await motor.run_to_absolute_position(port.F, 240, 500)

async def check_color():
    await motor.run_to_absolute_position(port.F, 235, 500)

    if color_sensor.color(port.D) is color.MAGENTA:
        await motor.run_to_absolute_position(port.A, 0, 500)
        await motor.run_to_absolute_position(port.F, 25, 500)
        await sound.play('Triumph')
        await motor.run_to_absolute_position(port.F, 240, 500)
    else:
        await sound.play('Oops')
        await motor.run_to_absolute_position(port.F, 25, 500)
        for x in range(3):
            await motor.run_for_degrees(port.F, -100, 1000)
            await motor.run_for_degrees(port.F, 100, 1000)

runloop.run(main())

Allow students time to create and run their program. Remind students to watch the console for error messages during their programming. Students may need to debug their program as they test it.

Explain

(Whole Group, 15 minutes)

Discuss with students how the program worked.
Ask students questions like:
• Why did we use a function in this program? How did it make the program more efficient?
• What are other ways that functions could be used in your program?
• If students included parameters, what parameters were used and why?
• Show students the sample program used here and ask them to provide code comments (using the #) that should be included to make the program easier to ready.

Elaborate

(Small Groups, 25 minutes)

Challenge students to modify their program to include a second function in order to make their program more efficient.

Discuss the idea of making programs more efficient with students. Taking time to define variables, functions, etc. at the beginning of the program can allow students to make the body of the program shorter and easier to execute.

Discuss ways to make the program more efficient. One way could be to include a second function that will help the robotic checker know what to do with the materials after they are checked. Students should try to reduce the lines of code being used after their function is defined. If there are currently 12 lines of code after the function, could they cut that in half with a new program. Challenge students to see how efficient they can make their program. Remind students to include their code comments

Evaluate

(Group Exercise, 15 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• What happened when you added an additional function to your program?
• Why is efficiency important to creating large programs?
• What are the different ways to use functions in your programs? What did you learn from how other students approached this challenge?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about making programs efficient?
• 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:
• Program a sorting robot to identify if a material is recyclable or non-recyclable
• Incorporate a second function into a program to make the program more efficient

• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Student journals

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-14 Create procedures with parameters to organize code and make it easier to reuse.
2-AP-17 Systematically test and refine programs using a range of test cases.
2-AP-19 Document programs in order to make them easier to follow, test, and debug.