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
  • Run motors for 5 seconds

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 hub import port
import runloop
import motor_pair

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

    # Move straight at default velocity for 5 second
    await motor_pair.move_for_time(motor_pair.PAIR_1, 5000, 0)

runloop.run(main())

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 move_for_time section with students. Note to students that they can set variables for time, steering and velocity (speed). All are integer type inputs (whole numbers). Time can be any number greater than 0. Steering has a range from -100 to 100. steering has a range from -100 to 100. Velocity (Speed) has a range of –1000 to 1000.

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 hub import port, light_matrix
import runloop
import motor_pair

async def main():
    # Countdown
    await light_matrix.write("3")   
    await runloop.sleep_ms(1000)
    await light_matrix.write("2")
    await runloop.sleep_ms(1000)
    await light_matrix.write("1")
    await runloop.sleep_ms(1000)

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

    # Move straight at default velocity for 5 second
    await motor_pair.move_for_time(motor_pair.PAIR_1, 7000, 0, velocity=500)

runloop.run(main())

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:

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)

    # Turn for different amounts
    await motor_pair.move_for_time(motor_pair.PAIR_1, 3000, -20, velocity=280)
    await motor_pair.move_for_time(motor_pair.PAIR_1, 3000, 90, velocity=280)
    await motor_pair.move_tank_for_time(motor_pair.PAIR_1, 500, 1000, 3000)

runloop.run(main())

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 motor_pair.move_for_time are for time, steering and velocity respectively. All numbers are integer types meaning that only whole numbers can be used.
Review this line of code specifically with students to discuss how to move the motors differently. 500 is the velocity of the left motor and 1000 is the velocity of the right motor. 3000 is the amount of time the hopper will run.

await motor_pair.move_tank_for_time(motor_pair.PAIR_1, 500, 1000, 3000)

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