Computer Science
Grade 9
20 min
Controlling a Robot: Giving Instructions
Students learn that robots need instructions to perform tasks.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define and differentiate between a command, a sequence, and a program.
Write a linear sequence of instructions to make a robot perform a specific task.
Use loops to repeat a set of instructions efficiently.
Apply conditional logic (if-else statements) to make a robot react to its environment.
Translate a simple real-world task, like navigating a maze, into a set of pseudocode instructions for a robot.
Debug a simple robot control program by tracing the execution of commands step-by-step.
How does a Mars rover know how to avoid a giant rock, or a robot in a factory know when to pick up a part? 🤖 It's all about the instructions we give them!
In this lesson, you'll learn the fundamental building blocks of controlling a robot. We'll explore h...
2
Key Concepts & Vocabulary
TermDefinitionExample
CommandA single, specific instruction given to a robot that it can understand and execute.`robot.move_forward(10)` tells the robot to move forward 10 centimeters.
SequenceA series of commands executed one after another, in a specific order.To make a robot draw a square, you would use a sequence of 'move forward' and 'turn right' commands.
ProgramA complete set of instructions, including sequences, loops, and conditionals, that tells a robot how to accomplish a complex task.A program to navigate a maze would include instructions for moving, checking for walls, and deciding which way to turn.
LoopA programming structure that repeats a block of code a certain number of times or until a condition is met.`for i in range(4): robot.move_forward(10); robo...
3
Core Syntax & Patterns
Sequential Execution
Commands in a program are executed from top to bottom, one at a time, unless directed otherwise by a loop or conditional.
This is the default behavior of a program. Always write your instructions in the exact order you want the robot to perform them.
For Loop for Repetition
for i in range(count):
# Code to repeat
Use a 'for' loop when you know exactly how many times you need to repeat an action. The 'count' determines the number of repetitions.
If-Else for Decisions
if condition:
# Code to run if condition is true
else:
# Code to run if condition is false
Use an 'if-else' statement to make the robot choose between two different actions based on sensor input or an internal state. The 'else' block is op...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A robot is placed in a narrow hallway and needs to shuttle back and forth 3 times (a total of 6 segments). The hallway is 100cm long. Which is the most efficient and correct program for this task?
A.robot.move_forward(100)
robot.turn_around(180)
robot.move_forward(100)
robot.turn_around(180)
...
B.for i in range(6):
robot.move_forward(100)
robot.turn_around(180)
C.for i in range(3):
robot.move_forward(100)
robot.turn_around(180)
D.robot.move_forward(100)
for i in range(5):
robot.turn_around(180)
robot.move_forward(100)
Challenging
A robot is designed to follow a wall on its left side. It has distance sensors on its front and left. Which pseudocode best describes a simple wall-following behavior?
A.if front_sensor < 10: turn_right(90)
else: move_forward(1)
B.if left_sensor > 20: turn_left(10)
else if left_sensor < 10: turn_right(10)
else: move_forward(1)
C.while True:
move_forward(1)
turn_left(1)
D.if left_sensor < 10: turn_left(90)
else: move_forward(1)
Challenging
A robot's `robot.turn_right(90)` command is faulty and sometimes turns 85 degrees. After running the square-drawing program (`for i in range(4): ...`), the robot does not end up at its starting point. How could you use a sensor to make the program more robust and correct this error?
A.Add a touch sensor to feel for the corner.
B.Use a `while` loop for turning until a compass sensor shows the correct 90-degree heading change.
C.Increase the `move_forward` distance to compensate.
D.Replace `turn_right` with two `turn_left` commands.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free