SPIKE™ Prime with Python

Hopper Run

Students build and program a robot without wheels to go forward.

45 min
Beginner
Years 7-9 OR Key Stage 3

Questions to investigate

How does the design of a robot determine how it can move?

Prepare

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

1. Engage

Engage students to think about how to move when using more than one motor. Put students in groups of 4. Have them link together either by holding arms or a ruler/stick between them. Ask groups to move together in unison in a line. Discuss what happens.

Challenge students to become synced in the way they move (stepping forward together at the same time, for the same distance). See how students communicate together to make these moves happen. Discuss as a group.

2. Explore

Students will build a hopper model to investigate different ways to move using the motors.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for Hopper. 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.

Get Moving
Challenge students to have their hopper run forward for 5 seconds.

Discuss with students how the hopper model will move using the two motors attached to move simultaneously. Ask students to write a pseudocode program to explain how they need to program hopper to move forward. Discuss the pseudocode examples students create.

Example pseudocode:

  • Import motors
  • Initialize motors
  • Start motors
  • Move for 5 seconds
  • Stop motors

Note: This is a sample of what students might be thinking and does not represent an actual code.

Share this sample program with students. Students will need to type this program into the programming canvas.

from spike import PrimeHub, MotorPair
from spike.control import wait_for_seconds

# Initialize the hub and motor
hub = PrimeHub()
hop_motors = MotorPair('E', 'F')

# Run motors for 5 seconds then stop
hop_motors.start()
wait_for_seconds(5)
hop_motors.stop()

Note: Remind students to watch for errors in the console. Students can reference the line from the error message to pinpoint where a typing error might have occurred.

Review how the hopper model moved with this code. Then, take students to the Knowledge Base and open the Motor Pairs section. This section is an in-depth review of how to program motors together.

Review the start() section with students. Note to students that they can set variables for steering and speed. Both are integer type inputs (whole numbers) with a range from -100 to 100.

Challenge students to create a 3-2-1 countdown on their hubs and the hopper to travel a distance of 50 cm. Remind students to consider the needed libraries and the time needed to move 50 cm when writing their program.

Sample program:

from spike import PrimeHub, MotorPair
from spike.control import wait_for_seconds

# Initialize the hub and motor
hub = PrimeHub()
hop_motors = MotorPair('E', 'F')

# Countdown
hub.light_matrix.write('3')
wait_for_seconds(1)

hub.light_matrix.write('2')
wait_for_seconds(1)

hub.light_matrix.write('1')
wait_for_seconds(1)

# Run motors for 50cm then stop
hop_motors.start()
wait_for_seconds(7)
hop_motors.stop()

3. Explain

Discuss with students how they were able to move their models and review the different code combinations together.

Ask students questions like:

  • How did your program work?
  • How did programming two motors differ from programming one motor?
  • How does the design of the hopper model determine how it is able to move?
  • What was difficult about getting hopper to move?
  • What was difficult about moving hopper to 50 cm when programming with seconds?

4. Elaborate

Investigate moving two motors using distance and turning.

Introduce three new lines of code to students:

hop_motors.start(-20,50)
hop_motors.start(90,30)
hop_motors.move_tank(10, 'cm', left_speed=25, right_speed=75)

Allow students time to explore these new lines of code and investigate how they can move their hopper model in various ways.
Discuss with students how they were able to move their models and review the different code combinations together.

Ask students questions like:

  • How did each line of code work?
  • How did your program allow the motor to move in a different direction?
  • What do the two numbers after start represent?
  • What are different ways you can set the speed of the motors?
  • What happened when you set the motors to move for cm?
  • When might you use these types of codes?

Point out to students that the numbers used in the line hop_motors.start are for steering and speed respectively. Both numbers are integer types meaning that only whole numbers ranging from -100 to 100 can be used.
Review this line of code specifically with students to discuss how to move the motors differently. Point out that 10 represents a float type because a decimal can be used. However, the units, in this case centimeters, is a string type since it is text and needs to be put in quotation marks as a result.

hop_motors.move_tank(10, 'cm', left_speed=25, right_speed=75)

5. Evaluate

Teacher Observation:
Discuss the program with students.

Ask students questions like:

  • How were you able to program multiple motors to move the hopper model in a variety of ways?
  • How does the design of the hopper model determine how it can move?
  • What was difficult about programming hopper?

Self-Assessment:
Have students answer the following in their journals:

  • What did you learn today about programming multiple motors?
  • How does the design of a robot determine how it moves?
  • 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.

Teacher Support

Students will:

  • Program two motors to move simultaneously.
  • Build and program a robot without wheels to move forward.
  • SPIKE Prime sets
  • 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-10
Use flowcharts and/or pseudocode to address complex problems as algorithms

2-AP-13
Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.

2-AP-16
Incorporate existing code, media, and libraries into original programs, and give attribution.

2-AP-17
Systematically test and refine programs using a range of test cases.

2-AP-19Document programs in order to make them easier to follow, test, and debug.

CCSS ELA
SL.8.1
Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 8 topics, texts, and issues, building on others' ideas and expressing their own clearly

SL.8.4
Present claims and findings, emphasizing salient points in a focused, coherent manner with relevant evidence, sound valid reasoning, and well-chosen details; use appropriate eye contact, adequate volume, and clear pronunciation

RST.6-8.3
Follow precisely a multistep procedure when carrying out experiments, taking measurements, or performing technical tasks

L.8.6
Acquire and use accurately grade-appropriate general academic and domain-specific words and phrases; gather vocabulary knowledge when considering a word or phrase important to comprehension or expression

Vocabulary
Motor Pair