Bird
Raised Fist0
ROSframework~20 mins

Velocity smoothing in ROS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
šŸŽ–ļø
Velocity Smoothing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ component_behavior
intermediate
2:00remaining
What is the output velocity after smoothing?
Given a ROS node that applies a simple velocity smoothing algorithm by averaging the current and previous velocity commands, what will be the output velocity if the input velocities are [1.0, 3.0, 5.0] m/s in sequence?
ROS
prev_velocity = 0.0
input_velocities = [1.0, 3.0, 5.0]
output_velocities = []
for v in input_velocities:
    smoothed = (prev_velocity + v) / 2
    output_velocities.append(smoothed)
    prev_velocity = smoothed
print(output_velocities)
A[1.0, 3.0, 5.0]
B[0.5, 1.75, 3.375]
C[0.0, 2.0, 4.0]
D[0.5, 2.5, 4.0]
Attempts:
2 left
šŸ’” Hint
Think about how averaging the previous output with the current input smooths sudden changes.
šŸ“ Syntax
intermediate
2:00remaining
Identify the syntax error in this ROS velocity smoothing callback
Which option contains the syntax error in this ROS Python callback function that smooths velocity commands?
ROS
def velocity_callback(msg):
    global prev_vel
    smoothed_vel = (prev_vel + msg.data) / 2
    prev_vel = smoothed_vel
    pub.publish(smoothed_vel)

prev_vel = 0.0
APublishing a float instead of a ROS message
BUsing global without declaration
CMissing colon after function definition
DNo syntax error
Attempts:
2 left
šŸ’” Hint
Check the type of data being published in ROS topics.
ā“ state_output
advanced
2:00remaining
What is the final velocity state after applying exponential smoothing?
A ROS node applies exponential smoothing to velocity commands with alpha=0.3. Given input velocities [10, 20, 30] m/s and initial smoothed velocity 0, what is the final smoothed velocity?
ROS
alpha = 0.3
smoothed = 0
inputs = [10, 20, 30]
for v in inputs:
    smoothed = alpha * v + (1 - alpha) * smoothed
print(round(smoothed, 2))
A12.3
B30.0
C21.0
D14.67
Attempts:
2 left
šŸ’” Hint
Calculate step by step: new smoothed = 0.3 * current input + 0.7 * previous smoothed.
šŸ”§ Debug
advanced
2:00remaining
Why does this velocity smoothing node publish zero velocity always?
This ROS node is supposed to smooth velocity commands but always publishes zero. What is the cause?
ROS
prev_vel = 0.0

def callback(msg):
    smoothed = (prev_vel + msg.data) / 2
    prev_vel = smoothed
    pub.publish(smoothed)

sub = rospy.Subscriber('input_vel', Float32, callback)
pub = rospy.Publisher('smoothed_vel', Float32, queue_size=10)
rospy.spin()
ACallback does not convert msg.data to float before calculation
BPublisher is created after subscriber, causing race condition
Cprev_vel is not declared global inside callback, so assignment creates local variable
Drospy.spin() is missing
Attempts:
2 left
šŸ’” Hint
Check variable scope inside the callback function.
🧠 Conceptual
expert
2:00remaining
Which smoothing method best handles sudden velocity spikes in ROS?
You want to smooth velocity commands in ROS to avoid sudden spikes but still respond quickly to changes. Which method is best?
AMedian filter over last N commands
BExponential smoothing with a low alpha value
CSimple moving average over last N commands
DNo smoothing, use raw commands
Attempts:
2 left
šŸ’” Hint
Consider which method reduces spikes without delaying response too much.

Practice

(1/5)
1. What is the main purpose of velocity smoothing in ROS robot control?
easy
A. To gradually change speed and avoid sudden jumps
B. To increase the maximum speed instantly
C. To stop the robot immediately
D. To ignore acceleration limits

Solution

  1. Step 1: Understand velocity smoothing concept

    Velocity smoothing means changing speed gradually to avoid sudden jumps.
  2. Step 2: Identify the purpose in robot control

    This gradual change improves safety and comfort by preventing abrupt movements.
  3. Final Answer:

    To gradually change speed and avoid sudden jumps -> Option A
  4. Quick Check:

    Velocity smoothing = gradual speed change [OK]
Hint: Velocity smoothing means smooth speed changes, not instant jumps [OK]
Common Mistakes:
  • Thinking velocity smoothing increases speed instantly
  • Confusing smoothing with emergency stop
  • Ignoring acceleration limits in smoothing
2. Which of the following is the correct Python function signature for a velocity smoothing function in ROS?
easy
A. def smooth_velocity(current_vel: float, target_vel: float, max_accel: float):
B. def smooth_velocity(current_vel, target_vel, max_accel):
C. def smooth_velocity(current_vel: int, target_vel: int, max_accel: int, dt: int):
D. def smooth_velocity(current_vel: float, target_vel: float, max_accel: float, dt: float) -> float:

Solution

  1. Step 1: Check function parameters for velocity smoothing

    The function needs current velocity, target velocity, max acceleration, and time delta (dt) to calculate smoothing.
  2. Step 2: Verify correct typing and return type

    Using floats for velocities and acceleration is correct, and the function returns a float for new velocity.
  3. Final Answer:

    def smooth_velocity(current_vel: float, target_vel: float, max_accel: float, dt: float) -> float: -> Option D
  4. Quick Check:

    Correct parameters and types = def smooth_velocity(current_vel: float, target_vel: float, max_accel: float, dt: float) -> float: [OK]
Hint: Include all needed parameters with correct types and return value [OK]
Common Mistakes:
  • Missing dt parameter for time step
  • Using int instead of float for velocities
  • No return type annotation
3. Given the following code snippet for velocity smoothing, what will be the output if current_vel = 1.0, target_vel = 3.0, max_accel = 1.0, and dt = 1.0?
def smooth_velocity(current_vel, target_vel, max_accel, dt):
    max_change = max_accel * dt
    delta = target_vel - current_vel
    if abs(delta) > max_change:
        delta = max_change if delta > 0 else -max_change
    return current_vel + delta

print(smooth_velocity(1.0, 3.0, 1.0, 1.0))
medium
A. 2.0
B. 3.0
C. 1.0
D. 4.0

Solution

  1. Step 1: Calculate maximum allowed velocity change

    max_change = max_accel * dt = 1.0 * 1.0 = 1.0
  2. Step 2: Calculate delta and limit it

    delta = target_vel - current_vel = 3.0 - 1.0 = 2.0, which is greater than max_change, so delta is limited to 1.0
  3. Step 3: Calculate new velocity

    new velocity = current_vel + delta = 1.0 + 1.0 = 2.0
  4. Final Answer:

    2.0 -> Option A
  5. Quick Check:

    Velocity change limited by max_accel * dt = 2.0 [OK]
Hint: Limit velocity change by max_accel * dt before adding [OK]
Common Mistakes:
  • Adding full delta without limiting by max_change
  • Returning target_vel directly
  • Ignoring sign of delta
4. Identify the bug in this velocity smoothing function and choose the correct fix:
def smooth_velocity(current_vel, target_vel, max_accel, dt):
    max_change = max_accel * dt
    delta = target_vel - current_vel
    if delta > max_change:
        delta = max_change
    elif delta < max_change:
        delta = -max_change
    return current_vel + delta
medium
A. Change max_change to max_accel / dt
B. Remove the if-else and always set delta = max_change
C. Change 'elif delta < max_change' to 'elif delta < -max_change'
D. Add abs() around delta in the if condition

Solution

  1. Step 1: Analyze the conditions for limiting delta

    The function limits delta if it exceeds max_change positively or negatively.
  2. Step 2: Identify incorrect condition

    The condition 'elif delta < max_change' is wrong because it triggers for any delta less than max_change, including values that don't need limiting. It should check if delta is less than negative max_change.
  3. Step 3: Correct the condition

    Change 'elif delta < max_change' to 'elif delta < -max_change' to correctly limit negative large changes.
  4. Final Answer:

    Change 'elif delta < max_change' to 'elif delta < -max_change' -> Option C
  5. Quick Check:

    Negative delta limit needs correct comparison [OK]
Hint: Check negative limit uses -max_change, not max_change [OK]
Common Mistakes:
  • Using max_change instead of -max_change for negative check
  • Removing conditions and causing wrong velocity jumps
  • Incorrect calculation of max_change
5. You want to implement velocity smoothing for a robot that receives a list of target velocities every second: [0, 2, 5, 3, 0]. The robot's max acceleration is 1.5 m/s² and the time step is 1 second. Which sequence of smoothed velocities will correctly apply velocity smoothing starting from 0 m/s?
hard
A. [0, 1.5, 3.0, 4.5, 3.0]
B. [0, 1.5, 3.0, 3.0, 1.5]
C. [0, 1.5, 2.5, 3.0, 1.5]
D. [0, 2, 5, 3, 0]

Solution

  1. Step 1: Calculate smoothed velocities step-by-step

    Start at 0 m/s. Max change per step = 1.5 m/s. - Step 1: target 0 -> 0 (start) - Step 2: target 2, delta=2-0=2 >1.5, so velocity=0+1.5=1.5 - Step 3: target 5, delta=5-1.5=3.5 >1.5, velocity=1.5+1.5=3.0 - Step 4: target 3, delta=3-3=0 ≤1.5, velocity=3.0 - Step 5: target 0, delta=0-3=-3 < -1.5, velocity=3.0-1.5=1.5
  2. Step 2: Compare with options

    The sequence [0, 1.5, 3.0, 3.0, 1.5] matches the calculated sequence.
  3. Final Answer:

    [0, 1.5, 3.0, 3.0, 1.5] -> Option B
  4. Quick Check:

    Apply max_accel limit each step = [0, 1.5, 3.0, 3.0, 1.5] [OK]
Hint: Apply max acceleration limit stepwise to each velocity [OK]
Common Mistakes:
  • Using target velocities directly without smoothing
  • Adding max_accel multiple times incorrectly
  • Ignoring negative acceleration limits