SPIKE™ Prime with Python

Make It Move

Investigate how an automobile-like model can move forward and change directions using mathematical functions

45 min
Poziom podstawowy
Klasy 4–8

Questions to investigate

• How can use mathematical functions to change directions?

Prepare

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

Engage

(Group Discussion, 5 minutes)

Engage students in a conversation about how the driver causes an automobile to move.

Guide students in a discussion on the different ways a driver controls the movement of an automobile. For example, the driver can make the automobile move forward and in reverse. The driver can stop the vehicle. The driver can also turn left and right at a variety of degrees.

Show students a video of the driving base model they will work with in the explore moving to see how it can move similar to an automobile. The engage video from the Training Camp 1 lesson can be used. The video is available at
https://education.lego.com/en-us/lessons/prime-competition-ready/training-camp-1-driving-around#ignite-a-discussion.

Discuss the video with the students.

Explore

(Small Groups, 20 minutes)

Students will investigate an automobile-like model as a type of transportation.

Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for the Driving Base model. Ask students to build the driving base 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 their driving base to move. Students should write a program that allows the vehicle to move forward, backward and turn by programming the motors. If students need inspiration for their program, suggest they look at the sample program from the Bike Riding for Data lesson. Similar to this program, students should include a method for adding (speeding up) and subtracting (slowing down) speed.

Remind students that when driving a vehicle like a car there is a speed limit restriction that must be followed. In addition to driving forward, challenge students to display the speed of their driving base on the hub of the model to act as a speedometer.

Sample Program:

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

hub = PrimeHub()
drive = MotorPair('C', 'D')

\# set the driving speed as a function
driving_speed = 25

def ride():
    hub.light_matrix.write(driving_speed)
    drive.move(25, 'cm', 0, driving_speed)

\# set conditions for moving forward and setting speed
while True:
    if hub.left_button.was_pressed():
        driving_speed = driving_speed - 5
        ride()
        wait_for_seconds(3)

    if hub.right_button.was_pressed():
        driving_speed = driving_speed + 5
        ride()
        wait_for_seconds(3)

Allow students time to explore the program. Prompt them to continue pushing the right and/or left buttons while running the program to see how the driving base will continue to move adding or subtracting 5. Students will be able to see the new speed each time on the hub.

Driving Around
Since automobiles do not often drive in a straight line only, challenge students to modify their program to include moving in different directions. Encourage students to use their math functions to help set how they make turns.

Sample Program:

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

hub = PrimeHub()
drive = MotorPair('C', 'D')
turn_rt = Motor('C')
turn_lt = Motor('D')

driving_speed = 25
turning = 360

\# set the function for driving forward. The speed must be an integer, so when dividing you must round to keep the variable a whole number
def ride():
hub.light_matrix.write(round(driving_speed))
drive.move(5, 'cm', 0, round(driving_speed))
wait_for_seconds(1)

\# set the function for turning right. 
def turning_rt():
hub.light_matrix.write(turning/4)
turn_rt.run_for_degrees(-turning, driving_speed)

\# set the function for turning left. 
def turning_lt():
hub.light_matrix.write(turning/4)
turn_lt.run_for_degrees(turning, driving_speed)

while True:
ride()
if hub.right_button.was_pressed():
driving_speed = driving_speed + 5
turning = turning + 360
turning_rt()
wait_for_seconds(3)

if hub.left_button.was_pressed():
driving_speed = driving_speed - 5 
turning = turning - 360
turning_lt()
wait_for_seconds(3)

Explain

(Whole Group, 5 minutes)

Discuss with students how the program worked. Ask students questions like:
• Why did the program allow you to continue to add or subtract speeds?
• How were you able to modify your program to include turning and moving in different directions?
• Why did the drive base continue to drive when the buttons were not pressed?

When displaying the turns a math function was used to convert what the actual degrees of wheel rotation to how much the drive base turned. In this case, when the single motor rotates 360 degrees it makes a full wheel rotation which turns the drive base 90 degrees. Using math you can determine the answer to 360/n=90. In this case n=4. Thus, you see this math function used in the line hub.light_matrix.write(turning/4).

Elaborate

(Small Groups, 10 minutes)

Challenge students to recognize bugs in their programs.

Show students each program below and its error message. Have students discuss what needs to change in each code to fix the bug or add the missing code. Students can type in the program or review together as a whole class. Consider making the changes as a class and ensuring the program runs correctly after each change.

Debugging Activity 1:
Find the error in the program. Remind students sometimes that error is not found until the program is running and an action is taken to read that part of the program.

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

hub = PrimeHub()
drive = MotorPair('C', 'D')

\# set the driving speed as a function
driving_speed = 25

def ride():
    hub.light_matrix.write(driving_speed)
    drive.move(25, 0, driving_speed)

\# set conditions for movement and speed
while True:
    if hub.left_button.was_pressed():
        driving_speed = driving_speed - 5
        ride()
        wait_for_seconds(3)

    if hub.right_button.was_pressed():
        driving_speed = driving_speed + 5
        ride()
        wait_for_seconds(3)

Error Message: 
File "./projects/22622.py", line 23
File "./projects/22622.py", line 13, in ride
File "_api/motorpair.py", line 1, in move
TypeError: unit is not a string.

The error message references several lines that are incorrect. However, it does not actually point of the one line that has the error. In line 12, the units were removed that tell the driving base how far to move forward. This line of code should read drive.move(25, 'cm', 0, driving_speed). When the button is pressed for the driving base to move, it references the function and does not have the units needed to know how to move forward.

Debugging Activity 2:
Find the error in the program. Remind students sometimes that error is not found until the program is running and an action is taken to read that part of the program.

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

hub = PrimeHub()
drive = MotorPair('C', 'D')

\# set the driving speed as a function
driving_speed = 25

def ride():
    hub.light_matrix.write(driving_speed)
    drive.move(-25, 'cm', 0, driving_speed)

\# set conditions for moving forward and setting speed
while True:
    if hub.left_button.was_pressed():
        driving_speed = driving1_speed - 5
        ride()
        wait_for_seconds(3)

    if hub.right_button.was_pressed():
        driving_speed = driving2_speed + 5
        ride()
        wait_for_seconds(3)

Error Message: 
File "programrunner/__init__.py", line 1, in start_program
File "./projects/31408.py", line 17
NameError: name 'driving1_speed' isn't defined

The error message will not appear until students press one of the buttons. Students should recognize that the error is occurring in line 17. The error message indicates that driving1_speed is not defined. This is a typo in the program where the program is trying to reference a variable that is not set. This could indicate that the program was written incorrectly in trying to include a new variable where one is not needed or in forgetting to define a needed variable. In this case, a new variable is not needed.

Challenge students to find the second error in this program. No error message is provided for this error, but it is similar to the first error identified. Discuss ways that students can find this second error and have them identify how to fix it. Note: the second error is the same as the first and found in line 22.

Evaluate

(Group Exercise, 5 minutes)

Teacher Observation:
Discuss the program with students.
Ask students questions like:
• How were you able to use math functions to change how the driving base moved both in straight lines and when turning?
• What errors can occur when creating complex programs? What are strategies to debug these errors?
• How could you use the drive base to turn instead of a single motor?

Self-Assessment:
Have students answer the following in their journals:
• What did you learn today about using multiple math functions in one 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.

Wsparcie dla nauczyciela

Students will:
• Program a driving base to move forward and change directions
• Create a program with mathematical functions

• 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-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-19 Document programs in order to make them easier to follow, test, and debug.

Legal NoticePrivacy PolicyCookies

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.