SPIKE™ Prime with Python

Grasshopper Troubles

Students will investigate how to choose an appropriate sensor for a given task.

45 min
Intermed.
Years 7-9 or Key Stage 3

Questions to investigate

• When are sensors appropriate to use?
• How do you determine which sensor is best to use for a given task? Prepare

Prepare

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

Engage

(Group Discussion, 5 minutes)

Ignite a discussion with students about how to determine the best tool for a job. Provide students will several examples, which could be images, of tasks to complete. Tasks could range from digging a hole, to sharpening a pencil, to coding a robot. For each task, discuss the best tool for the job. For example, you would not code a robot with a shovel to sharpen a pencil. Try to give examples that might have more than one tool that can be used such as writing your name which could be done with a pencil, pen, marker, or crayon, but would be very difficult to do with a shovel, hammer, or watering can.

Discuss with students why it is important to make sure you have the right tool for a task.

Explore

(Small Groups, 20 minutes)

Students will build a Hopper model to investigate different ways to move using a sensor.

Direct students to build the grasshopper 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.
Students should create a program that allows the Grasshopper Model to move forward at 50% power., which is a velocity of 500.

Sample code:

from hub import port
import runloop
import motor_pair

async def main():
    # Pair motors on port A and B
    motor_pair.pair(motor_pair.PAIR_1, port.E, port.F)

    # Move straight at 500 velocity
    motor_pair.move(motor_pair.PAIR_1, 0, velocity=500)

runloop.run(main())

Discuss with students how the Grasshopper model moves.

Ignite a discussion with students about the danger our Grasshopper faces when moving through the park. The Grasshopper does not want to run into a foot that might step on it. Have students think about which sensor, force or distance, would be best to keep the Grasshopper from running into an object. Allow students to discuss different ideas in their group to decide which sensor to use. Encourage students to carefully look at how the model currently functions.

Students need to attach their chosen sensor onto their grasshopper model without changing the basic design of the model. Allow students time to attach the sensor to their Grasshopper model.

Ask students to change their program to include the newly added sensor. Students should test their model to decide if the chosen sensor works. Remind students to test their program several times and change it as needed in order to ensure the model moves as expected. Ask students to add code comments using # to explain the steps of the program.

Explain

(Whole Group, 5 minutes)

Allow students to share their final Grasshopper model designs and programs. Discuss with groups which sensor was chosen and why.

Ask students questions like:
• Which sensor did you choose and why?
• How well did the sensor perform? Did the Grasshopper model run into any objects?
• What issues did you have to overcome when adding the sensor?
• What were some issues you had to debug?
• What was difficult about this challenge?

Consider using a chart to track the sensors chosen. Was there a clear favorite?

Elaborate

(Small Groups, 10 minutes)

Challenge students to change their programs to have the Grasshopper reverse direction or turn around instead of turning the motors off when it reaches the object. Students should consider what part of the code stays the same and what part of the code needs to be changed.

Ask students to plan how they want the Grasshopper to move. Have students write pseudocode before they change the code. Students should include the following in their new programs:

• Add an image on the hub when the grasshopper stops at the object
• Reverse direction or turn around to return to the starting position
• Play a new sound
• Include a second use of the sensor in the program somewhere

Optional: Remind students that they can also print a message in the console.
For this challenge, tell students to include a code comment using the # in their code for each step to explain the movement of the model (i.e. move straight, stop, move backwards).  

Encourage students to complete one step at a time. Testing and iterating on the program will be important during this challenge. Remind students to watch their console for error messages and to reference the Knowledge Base as needed for help.

Allow students to share and discuss their final programs.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation
Discuss the program with students.
• Ask students questions like:
o How did you decide which sensor to use for the challenge?
o How did you create your program to use the sensor in the most effective way?
o How were you able to add to your program to allow the Grasshopper model to move away from the object safely?

Self-Assessment
Have students answer the following in their journals:
• What did you learn today about choosing an appropriate sensor for a task?
• 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:
• Make appropriate hardware decisions
• Re-design a model to add a sensor

• 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-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.