Year 1 · Week 10
Chapter 10: Gear Ratios and Function Composition
Last week you wrote functions that made a robot move. This week we stay with functions but switch from wheels to gears. By counting teeth on small, medium, and large gears you can predict exactly how far each axis will turn — and by calling functions inside other functions you can model an entire mechanism in just a few lines of Python.
Part 1: Three Gears
We worked with three different LEGO gear sizes, each with a different number of teeth:
SMALL_GEAR = 12 # 12 teeth MID_GEAR = 20 # 20 teeth BIG_GEAR = 28 # 28 teeth
The tooth count is the key. When two gears mesh, they exchange teeth, not degrees. Every tooth that passes moves both gears forward by one tooth — that's what lets us calculate the output angle.
Part 2: Writing the First Gear-Ratio Function
Start with one pairing: small gear (12 teeth) driving the mid gear (20 teeth).
This function answers the question: if I want the mid gear to turn degree degrees, how many degrees does the motor need to spin the small gear?
Derive it from tooth counts. Two meshing gears pass the same number of teeth to each other:
SMALL_GEAR × small_degrees = MID_GEAR × mid_degrees
Solve for what the motor needs to do:
small_degrees = degree × MID_GEAR / SMALL_GEAR
Turn that into a function:
def small_drive_mid(degree): # degree = how far you want the mid gear to turn # return = how far the motor (small gear) must turn return int(degree * MID_GEAR / SMALL_GEAR)
Test it: you want the mid gear to turn 90 degrees. How much must the motor turn?
print( small_drive_mid(90) ) # prints 150
90 × 20 ÷ 12 = 150 degrees. The motor must turn the small gear 150 degrees to get 90 degrees out of the mid gear. Because the small gear has fewer teeth, it has to spin more to push the mid gear to the target angle.
Now extend the pattern to the other pairings
Each function asks the same question — "to get the driven gear to turn X degrees, how far must the driver turn?" — just with different tooth counts:
def mid_drive_small(degree): # degree = how far you want the small gear to turn # return = how far the motor (mid gear) must turn return int(degree * SMALL_GEAR / MID_GEAR) def small_drive_big(degree): # degree = how far you want the big gear to turn # return = how far the motor (small gear) must turn return int(degree * BIG_GEAR / SMALL_GEAR)
mid_drive_small(90) returns int(90 × 12/20) = 54: to get the small gear (12 teeth) to turn 90 degrees, the mid gear (20 teeth) only needs to turn 54 degrees — it has more teeth, so a smaller rotation delivers the same number of teeth to the output.
Part 3: Function Composition — A Three-Axis Claw
Real robot arms have multiple axes (joints). If a motor drives axis 1, axis 1's gear drives axis 2, and axis 2's gear drives axis 3, how do you calculate the final angle?
Function composition — pass the output of one function directly into the next:
def claw_3(degree): axis_2 = small_drive_mid(degree) # axis 1 → axis 2 axis_3 = small_drive_mid(axis_2) # axis 2 → axis 3 (step-by-step) return small_drive_mid( small_drive_mid(degree) ) # one-liner
Both versions produce the same result. The step-by-step version is easier to read and debug; the one-liner is more compact. Choose based on what makes the code clearest.
Part 4: Good vs. Bad Function Names
We also discussed what makes a good function name. A good name acts like a short description — you understand what the function does without reading its body.
# Bad names — tell you nothing def f(d): ... def calc(x): ... def do_thing(n): ... # Good names — instantly clear def small_drive_mid(degree): ... def mid_drive_small(degree): ... def claw_3(degree): ...
A good name captures two things: what it does (which gear drives which) and what it takes (a degree value). Six months from now, you won't have to re-figure out what each function is for.
Looking Ahead
This week you used tooth counts to calculate gear transmission, wrapped the formula into functions, and used function composition to model a multi-axis mechanism. This is the same math real engineers use to calculate joint angles in robot arms. Next we'll keep building on functions and introduce conditional logic so programs can make decisions based on sensor data.