Bird
Raised Fist0
ROSframework~10 mins

Velocity smoothing in ROS - Step-by-Step Execution

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
Concept Flow - Velocity smoothing
Receive target velocity
Check velocity difference
Apply smoothing filter
Publish smoothed velocity
Repeat on new input
Velocity smoothing in ROS takes a target velocity, checks how much it changes from current velocity, applies a filter to smooth sudden changes, then publishes the smoothed velocity repeatedly.
Execution Sample
ROS
current_vel = 0.0
input_vel = 1.0
alpha = 0.1
smoothed_vel = alpha * input_vel + (1 - alpha) * current_vel
publish(smoothed_vel)
This code smooths velocity by blending current and input velocities using a smoothing factor alpha.
Execution Table
Stepcurrent_velinput_velalphasmoothed_velAction
10.01.00.10.1Calculate smoothed velocity: 0.1*1.0 + 0.9*0.0 = 0.1
20.11.00.10.19Calculate smoothed velocity: 0.1*1.0 + 0.9*0.1 = 0.19
30.191.00.10.271Calculate smoothed velocity: 0.1*1.0 + 0.9*0.19 = 0.271
40.2711.00.10.3439Calculate smoothed velocity: 0.1*1.0 + 0.9*0.271 = 0.3439
50.34391.00.10.40951Calculate smoothed velocity: 0.1*1.0 + 0.9*0.3439 = 0.40951
60.409511.00.10.468559Calculate smoothed velocity: 0.1*1.0 + 0.9*0.40951 = 0.468559
70.4685591.00.10.521703Calculate smoothed velocity: 0.1*1.0 + 0.9*0.468559 = 0.521703
80.5217031.00.10.5695327Calculate smoothed velocity: 0.1*1.0 + 0.9*0.521703 = 0.5695327
90.56953271.00.10.61257943Calculate smoothed velocity: 0.1*1.0 + 0.9*0.5695327 = 0.61257943
100.612579431.00.10.65132149Calculate smoothed velocity: 0.1*1.0 + 0.9*0.61257943 = 0.65132149
Exit0.651321491.00.10.65132149Velocity smoothing converges, changes become small
💡 Velocity smoothing converges as smoothed velocity approaches input velocity gradually.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
current_vel0.00.10.190.2710.34390.409510.4685590.5217030.56953270.612579430.651321490.65132149
input_vel1.01.01.01.01.01.01.01.01.01.01.01.0
alpha0.10.10.10.10.10.10.10.10.10.10.10.1
smoothed_velN/A0.10.190.2710.34390.409510.4685590.5217030.56953270.612579430.651321490.65132149
Key Moments - 3 Insights
Why does the smoothed velocity not jump immediately to the input velocity?
Because the smoothing uses a weighted average with alpha, it blends the current velocity and input velocity gradually, as shown in execution_table rows 1 to 10.
What happens if alpha is very small?
A small alpha means the smoothed velocity changes very slowly, relying mostly on the current velocity. This slows down response, visible by smaller increments in smoothed_vel in execution_table.
Why do we keep updating current_vel to smoothed_vel each step?
Updating current_vel to the new smoothed_vel ensures the smoothing filter uses the latest velocity for the next calculation, creating a smooth transition as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the smoothed_vel value?
A0.19
B0.271
C0.3439
D0.1
💡 Hint
Check the smoothed_vel column in execution_table row with Step 3.
At which step does the smoothed velocity first exceed 0.5?
AStep 7
BStep 5
CStep 9
DStep 10
💡 Hint
Look at smoothed_vel values in execution_table rows and find first value > 0.5.
If alpha was increased to 0.5, how would the smoothed_vel at step 1 change?
AIt would stay the same at 0.1
BIt would be smaller than 0.1
CIt would be closer to input_vel, around 0.5
DIt would be zero
💡 Hint
Recall smoothed_vel = alpha * input_vel + (1 - alpha) * current_vel; higher alpha weights input_vel more.
Concept Snapshot
Velocity smoothing blends current and target velocities to avoid sudden jumps.
Use formula: smoothed_vel = alpha * input_vel + (1 - alpha) * current_vel.
Alpha controls smoothness: small alpha = slow change, large alpha = fast change.
Update current_vel each step with smoothed_vel for continuous smoothing.
Common in ROS to publish smooth velocity commands for robots.
Full Transcript
Velocity smoothing in ROS helps robots move smoothly by avoiding sudden velocity changes. It takes the current velocity and the new target velocity, then calculates a weighted average using a smoothing factor called alpha. This weighted average is the smoothed velocity, which changes gradually towards the target. Each step updates the current velocity to the new smoothed velocity, repeating this process to create a smooth transition. The smaller the alpha, the slower the velocity changes; the larger the alpha, the faster it adapts. This method is important to keep robot movements safe and stable.

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