How motor speed controls movement in Drone Programming - Performance & Efficiency
We want to understand how the time it takes to control a drone's movement changes as we adjust motor speeds.
Specifically, how does the program's work grow when we change motor speed inputs?
Analyze the time complexity of the following code snippet.
for motor in motors:
motor.set_speed(desired_speed)
motor.update_position()
wait(10) # wait 10 milliseconds
This code sets each motor's speed and updates the drone's position accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each motor to set speed and update position.
- How many times: Once for each motor in the motors list.
As the number of motors increases, the program does more work in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 4 motors | 4 sets and updates |
| 8 motors | 8 sets and updates |
| 16 motors | 16 sets and updates |
Pattern observation: The work grows directly with the number of motors.
Time Complexity: O(n)
This means the time to control movement grows in direct proportion to the number of motors.
[X] Wrong: "Changing motor speed happens instantly and does not depend on the number of motors."
[OK] Correct: Each motor must be set and updated separately, so more motors mean more work.
Understanding how motor control scales helps you think about real drone programming challenges and efficient code design.
"What if we added nested loops to control motor groups separately? How would the time complexity change?"
Practice
Solution
Step 1: Understand motor speed effect on lift
When all motors run at the same speed, they produce equal lift, balancing the drone.Step 2: Determine movement from equal lift
Equal lift means the drone either stays in place (hover) or moves straight up/down if speed changes.Final Answer:
The drone hovers or moves straight up or down -> Option BQuick Check:
Equal motor speeds = hover/up/down [OK]
- Thinking equal speeds cause spinning
- Assuming sideways movement with equal speeds
- Confusing motor speed with drone landing
Solution
Step 1: Identify equal motor speeds for hover
Hover requires all motors to run at the same speed to balance lift.Step 2: Check each option for equal speeds
Only setMotorSpeeds(100, 100, 100, 100); has all motors set to 100, equal speeds for hover.Final Answer:
setMotorSpeeds(100, 100, 100, 100); -> Option CQuick Check:
Equal speeds in code = hover [OK]
- Using different speeds thinking it still hovers
- Setting all speeds to zero which stops motors
- Mixing up parameter order
setMotorSpeeds(120, 100, 120, 100);
What movement will the drone most likely perform?
Solution
Step 1: Analyze motor speed differences
Motors 1 and 3 run faster (120) than motors 2 and 4 (100), creating torque imbalance.Step 2: Understand effect of torque imbalance
This imbalance causes the drone to rotate (yaw) clockwise due to uneven motor speeds.Final Answer:
Rotate clockwise -> Option DQuick Check:
Unequal opposite motors speed = rotation [OK]
- Assuming forward movement from speed difference
- Thinking drone hovers with different speeds
- Confusing rotation direction
setMotorSpeeds(110, 110, 90, 90);
But the drone spins instead. What is the likely error?
Solution
Step 1: Identify motor speed pairs
Motors 1 and 2 are 110, motors 3 and 4 are 90; left and right sides differ.Step 2: Understand effect of left-right speed difference
Different speeds on left vs right cause the drone to spin (yaw), not move forward.Final Answer:
Left and right motors have different speeds causing spin -> Option AQuick Check:
Unequal left-right speeds = spin [OK]
- Thinking front-rear speed difference causes spin
- Assuming equal speeds cause spin
- Mixing motor numbering sides
setMotorSpeeds(95, 95, 105, 105);
Why does this work?
Solution
Step 1: Identify motor speed distribution
Motors 1 and 2 (front) run at 95, motors 3 and 4 (rear) at 105; front motors slower than rear.Step 2: Understand tilt direction from speed difference
Slower front motors provide less lift upfront, tilting the nose down for forward movement.Final Answer:
Front motors slower than rear motors tilt drone forward -> Option AQuick Check:
Slower front motors = forward tilt [OK]
- Thinking faster front motors tilt forward
- Confusing left-right with front-rear speeds
- Assuming equal speeds cause tilt
Ready to prove it?
Pass the test and this pattern moves unknown
