New Moves with Motors
Students program their motors to move to exact positions and for exact degrees.
Questions to investigate
How do robots move precisely?
Prepare
- Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.
- Ensure students have built the Getting Started 2: Motors and Sensors model through step 19 utilized in the Making Moves with Motors Lesson.
1. Engage
Using a tire from the SPIKE Prime set, ask students to think of different ways to measure how far the tire would drive if it went around one rotation. Students should come up with several methods to determine this distance. Prompt student to think about the wheel as a circle. Discuss with them how to calculate the circumference to determine the distance. Ask students how they could measure the circumference of the wheel using a flat surface if they are struggling.
Ask students to imagine they have a robot with wheels. They need to code the motors to move a certain distance. Ask students how could they program the motor to move other than for time? How would this help move the robot more accurately? Discuss ideas with the students about how to move only ½ a turn (0.5 rotation), a full turn (1 rotation), 2 turns (2 rotations).
Note: The wheel has a diameter of 5.6 cm (2.2 in.) and travels a distance of 17.6 cm (6.9 in) per rotation or a complete 360o degree circle.
2. Explore
Students will explore how to program motors to move using degrees.
Run Single Motor for Degrees
Running a motor for degrees allows you to determine how far the motor will turn in degree increments – 1/360th of a rotation. Brainstorm with students how they could program the motor to move.
Students should think about what information the software needs to run the hardware correctly. Write a pseudocode program with students for making the motor run for one complete circle.
An example could be:
- Import motor
- Initialize motor
- Turn motor on
- Move motor 360 degrees
Note: The code written step-by-step does not need to match exactly what will be in the program. Pseudocode is used to help students think through what steps are needed so programming can be coded linearly.
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 and plug a motor into the C port of their hub.
Provide students with the sample code to move the motor. Ask students to run the program. Students can also copy and paste this code from the Knowledge Base Getting Started Part 2: Controlling the Motors section into the programming canvas.
from spike import Motor
\# Initialize the motor
motor = Motor('C')
\# Rotate the motor 360 degrees clockwise
motor.run_for_degrees(360)
Discuss with students how the motor moved. Help students identify the marker on the motor that allows you to set the position to 0 degrees. This will allow students to measure how far the motor moves.
Ask students to set the mark to the 0 position. Run the program again. Ask students if the marker return to the same position.
Prompt students to put different numbers into the degree values to see how the motor moves. Have them try large and small numbers.
Ensure students specifically try to run the motor using a negative value. Ask them what changed. (The direction reversed.)
Ask students to enter a number larger than 360. What happens?
# Run the motor 360 degrees clockwise, at 30% speed
motor.run_for_degrees(-360, 30)
Discuss what students discover after their investigations.
Run Single Motor to Position
Students will explore how to run a single motor to a position.
Have students locate the position marker again and ensure it is lined up. When aligned, the motor is in the 0-degree position. Discuss with students how to program the motor to move to a specific position (the motor will stop at an exact position). Prompt students to change their pseudocode to use position instead of degrees.
Provide students with the sample code to move the motor. Ask students to type this program into the programming canvas. Students can also copy and paste this code from the Knowledge Base Getting Started Part 2: Controlling the Motors section. Ask students to run this program.
from spike import Motor
# Initialize the motor
motor = Motor('C')
# Place the motor in position “0,” aligning the markers
motor.run_to_position(0, 'shortest path', 75)
Discuss the program with students to identify what happens when they run the code. If students start at the 0 degrees position, the motor will not move.
Ask students to move the motor so it is no longer in the 0 degrees position. Run the program. The motor should move to the 0 degrees position. Which direction did it move to get there?
Ask students to run the program several more times with the motor starting in different positions each time. Discuss what happens. Students should identify that the motor is not moving the same each time. The motor moves in the direction that makes the shortest path back to the 0 degrees position.
Review the lines of code together to highlight why the motor is moving in this way.
What is the longest move a motor can make to return to 0 position?
Ask students to change the code to make the motor stop in different positions. You can provide this sample code to them and then let them explore ideas of how they might change the code. Remind students that when exploring there is a high likelihood of a receiving an error message in the bottom console and the program not executing.
from spike import PrimeHub, Motor
from spike.control import wait_for_seconds
# Initialize the motor
motor = Motor('C')
# Run the motor to different positions, at different speeds
wait_for_seconds(1)
motor.run_to_position(0, 'shortest path', 30)
wait_for_seconds(1)
motor.run_to_position(90, 'clockwise', 100)
3. Explain
Allow students to share the new example programs they created for degrees and position and discuss ways to program the motor. Discuss how the motor can be set to move for degrees and when this might be useful in a program.
- How does this program work?
- When would it be useful to program a motor to move using degrees or to position?
- Why does using degrees or to position allow for more precision in movement than programming for seconds?
- How did the motor move when you put a number in larger than 360?
- How can you calculate how many degrees you will need to program?
- When using the shortest path in your program for position, why does the motor not move if you start at the 0 degree position?
- How can you program the motor to move in the opposite direction?
4. Elaborate
Work together to work on debugging. Provide students with each of the following codes. Discuss what is wrong with each code. Make sure students run each code and review the error message in the console.
1) What is missing?
from spike import Motor
# Rotate the motor 360 degrees clockwise
motor.run_for_degrees(360)
Students should identify that the motor is not initialized and therefore cannot run for degrees. The error message in the console is
File "lib/hub/flash/programrunner/__init__.py", line 1, in start_program
File "./projects/31408.py", line 4
NameError: name 'motor' isn't defined
Students need to add motor=Motor(‘C’) after the import line. Have students add this in and run the program again to check their program.
2) What number is wrong and why?
from spike import Motor
# Initialize the motor
motor = Motor('C')
# Run motor for 2 seconds at 50% speed
motor.run_for_seconds(2.0, 150)
Students should identify that the motor power is set to 150 which is outside the allowable 100% range. The program, however, will still run with no error message in the console. The power will revert back to 100% and run the program.
3) What is wrong?
from spike import PrimeHub, Motor
from spike.control import wait_for_seconds
# Initialize the motor
motor = Motor('C')
# Run the motor to different positions, at different speeds
wait_for_seconds(1)
motor.run_to_position(0, 'shorest path', 30)
wait_for_seconds(1)
motor.run_to_position(90, 'clockwise', 100)
Students should recognize that the word shortest is misspelled in line 9. The error message indicates that there is an issue in line 9 and that the error is a value that is not allowed.
File "lib/hub/flash/programrunner/__init__.py", line 1, in start_program
File "./projects/23262.py", line 9
File "lib/hub/flash/spike/motor.py", line 1, in run_to_position
ValueError: direction is not one of the allowed values: ('shortest path', 'clockwise', 'counterclockwise').
5. Evaluate
Teacher Observation:
Discuss the program with students.
Ask students questions like:
- What are different ways you can program your motors to move?
- Which methods allow for more precise movements?
- What are ways you can program to move using position?
Self-Assessment:
Have students answer the following in their journals:
- What did you learn today that might be helpful in programming robots to move with precision?
- 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 a motor to move to position using the shortest path.
- Program a motor to move to a specific position.
- Program a motor to move a defined number of degrees.
- 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-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.
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
Degrees
Position