Programming Robots
Download the Lessonotes Mobile Ghana app for faster lesson access on Android and iPhone.
Subject: Robotics
Class: SHS 2
Term: 2nd Term
Week: 16
Grade code: 2.3.3.LI.2
Strand code: 3
Sub-strand code: 3
Content standard code: 2.3.3.CS.2
Indicator code: 2.3.3.LI.2
Theme: Robot Construction and Programming
Subtheme: Programming Robots
This page supports the lesson note with a companion video and a short classroom-ready summary.
For class groups and homework, share this lesson page so learners also get the summary, objectives, and full lesson context.
This lesson focuses on a critical skill for any programmer or engineer: debugging and iterative improvement. In robotics, it is rare for a program to work perfectly on the first try. Just like a mechanic troubleshoots a faulty car engine or a cook adjusts a soup recipe that doesn't taste right, we must learn to find and fix errors ("bugs") in our robot's code. This process of testing, finding a flaw, fixing it, and testing again is called iteration. Mastering this skill is essential for building reliable and efficient robots that can solve real-world problems in Ghana, from automating farming tasks to managing urban traffic.
This section breaks down the essential ideas you need to understand for debugging and improving robot programs. A. What is a "Bug" and "Debugging"? Bug: A bug is simply an error or a flaw in a computer program that causes it to produce an incorrect or unexpected result, or to behave in unintended ways. The term comes from a time when an actual moth got stuck in a computer relay, causing a malfunction! Debugging: This is the systematic process of finding and removing bugs from a program. It is a core part of programming and is like being a detective for your code. B. Main Types of Programming Errors
It's important to know what kind of error you are looking for. They fall into three main categories: Syntax Errors Explanation: These are like grammatical mistakes in the English language. They happen when you break the rules of the programming language. The code does not make sense to the computer, so it usually won't even run. Characteristics: The programming software (IDE) often highlights these errors with a red underline or gives a specific error message *before* you even run the code. They are usually the easiest to fix. Ghanaian Context Example: Imagine you are writing Python code for a robot motor. Incorrect Code (Syntax Error): `print("Moving Forward)` *The Bug:* The closing quotation mark is missing. The computer doesn't know where the text ends. Correct Code: `print("Moving Forward")` Another Example: `MotorA.run(100)` might be correct, but `motorA.run(100)` could be a syntax error if the language is case-sensitive and requires a capital 'M'. Logic Errors Explanation: These are the trickiest errors. The program runs without crashing, but it does not do what you *intended* it to do. The syntax is correct, but the thinking (the logic) behind it is flawed. Characteristics: The computer will not tell you there is an error. You must observe the robot's behaviour and compare it to what you *expected* it to do. Ghanaian Context Example: You program a robotic arm to pick up a mango. Intention: The arm should move down 10cm, close the gripper, and then move up 10cm. Incorrect Code (Logic Error): ```python move_arm_down(10) move_arm_up(10) # This line is in the wrong place close_gripper() ``` Actual Behaviour: The arm moves down, then moves back up *before* closing the gripper. It never picks up the mango. The code ran successfully, but the sequence of instructions was wrong. Correct Logic: ```python move_arm_down(10) close_gripper() move_arm_up(10) ``` Runtime Errors Explanation: These errors happen *while* the program is running. The program starts correctly, but something unexpected occurs that causes it to crash or halt. Characteristics: The program stops unexpectedly, often with an error message that appears in a console or on the screen. Ghanaian Context Example: You have a robot that uses a distance sensor to avoid walls. Code: `distance = get_distance_cm()` Scenario: The wire to the distance sensor becomes unplugged. Runtime Error: When the program tries to get a reading from the sensor (`get_distance_cm()`), it can't find the device. The program might crash and show an error like "Device not found" or "Sensor returned no value." Another common runtime error is trying to divide by a variable that has become zero, which is mathematically impossible. C. The Iterative Improvement Cycle
Fixing complex problems is not a one-time event. It is a cycle. This process is called iteration.
The 5 Steps of Iteration: Test: Run your program on the robot and carefully observe its behaviour. Don't just watch, *document* what happens. *Example: "I ran the line-follower code. The robot moved forward but did not stop at the black line."* Identify the Flaw: Compare the robot's actual behaviour with the expected behaviour. What is the specific problem? *Example: "The flaw is that the robot completely ignores the line."* Analyse the Code: Look at your code and flowchart. Think like the robot. Go through the code line by line. Where could the logic be wrong? Is the sensor pin number correct? Is the threshold value for "black" set correctly? *Example: "Looking at the code, the `if` statement says `if color_sensor_value > 50: stop()`. Maybe the black line gives a reading of 20, which is not greater than 50. The condition is never met."* Modify the Code: Make one small, specific change to the code that you believe will fix the flaw. *Example: "I will change the condition to `if color_sensor_value Identify Flaw -> Analyse Code -> Modify Code -> Re-test, with an arrow leading back from Re-test to Test)*