Sending velocity commands in Drone Programming - Time & Space Complexity
When sending velocity commands to a drone, it's important to know how the time to send these commands changes as we send more commands.
We want to understand how the work grows when we increase the number of velocity commands sent.
Analyze the time complexity of the following code snippet.
for i in range(n):
send_velocity_command(x_speed[i], y_speed[i], z_speed[i])
wait(0.1)
This code sends a velocity command to the drone n times, each with different speeds, waiting a short time between commands.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending a velocity command inside the loop.
- How many times: Exactly n times, once for each command.
As the number of commands n increases, the total time grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 commands sent |
| 100 | 100 commands sent |
| 1000 | 1000 commands sent |
Pattern observation: Doubling the number of commands doubles the total work.
Time Complexity: O(n)
This means the time to send commands grows in a straight line with the number of commands.
[X] Wrong: "Sending multiple commands takes the same time as sending just one."
[OK] Correct: Each command requires its own send operation, so more commands mean more time.
Understanding how sending commands scales helps you design efficient drone control programs and shows you can think about how your code behaves as it grows.
"What if we send commands in batches instead of one by one? How would the time complexity change?"
Practice
send_velocity(x_speed, y_speed, z_speed, duration)Solution
Step 1: Understand velocity command parameters
The parameters x_speed, y_speed, and z_speed represent how fast the drone moves along each axis.Step 2: Connect parameters to drone movement
These speeds control the drone's direction and speed, while duration controls how long it moves.Final Answer:
The speed and direction the drone moves in each axis -> Option BQuick Check:
Velocity commands = speed and direction control [OK]
- Confusing velocity with camera control
- Thinking velocity sets GPS location directly
- Assuming velocity changes battery level
Solution
Step 1: Identify forward direction axis
Forward movement is usually along the X-axis, so x_speed should be 2.Step 2: Check duration and other speeds
Y and Z speeds are zero, duration is 5 seconds, matching the command.Final Answer:
send_velocity(2, 0, 0, 5) -> Option DQuick Check:
X-axis speed = 2, duration = 5 [OK]
- Mixing up X and Y axes
- Putting duration in wrong position
- Setting speed on wrong axis
send_velocity(0, -3, 0, 4)
Solution
Step 1: Identify axis for Y speed
The Y-axis controls left-right movement; negative means left.Step 2: Interpret speed and duration
Speed is 3 m/s left for 4 seconds, no movement on X or Z axes.Final Answer:
The drone moves left at 3 m/s for 4 seconds -> Option AQuick Check:
Negative Y speed = left movement [OK]
- Confusing negative Y with backward
- Mixing Y and Z axes
- Assuming negative means right
send_velocity(1, 0, 0)
Solution
Step 1: Check function parameters
The function requires four parameters: x_speed, y_speed, z_speed, and duration.Step 2: Identify missing parameter
Only three parameters are given; duration is missing, causing an error.Final Answer:
Missing duration parameter -> Option AQuick Check:
Duration required as 4th parameter [OK]
- Forgetting duration parameter
- Swapping speed and duration order
- Using negative duration
Solution
Step 1: Assign speeds to correct axes
X speed = 1 (forward), Y speed = 0 (no sideways), Z speed = 1 (upward).Step 2: Check duration matches requirement
Duration is 3 seconds, matching the problem statement.Final Answer:
send_velocity(1, 0, 1, 3) -> Option CQuick Check:
X=1, Y=0, Z=1, duration=3 [OK]
- Mixing Y and Z speeds
- Wrong duration value
- Setting zero on wrong axis
Ready to prove it?
Pass the test and this pattern moves unknown
