Stage 02 · Lesson 14/15

Programming & Python · Lesson 14

Python + Robot Control

Est. 35–45 min

Try it · Python in your browser

Run this lesson's example (slew_cmd.py), change a number, run it again.

Why

Why commands need rate limits

By the end you will implement slew(), watch a ramp to 0.8, and tune max_delta.

Concept

The big idea: slew / saturate motion requests

  • target may jump; actual moves by at most max_delta per tick.
  • Also clamp absolute limits (min/max speed).
  • Combine with sensor policy: decide → target → slew → act.
  • Log both target and actual for debugging.

How it works

How it actually works, step by step

  1. Read target from decide()

    0..1 speed or joint setpoint.

  2. Compute delta

    target − current.

  3. Clamp delta

    ±max_delta.

  4. Apply

    current += clamped_delta; send actual.

actual ← actual + clip(target − actual, −Δmax, +Δmax)

Visual

Visualise it: command slew lab

Set target and max_delta, press Run ramp. Watch actual climb in steps — cause and effect.

Interactive · Command slew / rate limit

Like easing an auto-rickshaw accelerator — the target can jump, but actual speed only moves by max_delta each tick.

actual0.00
target marker0.80
tick 0

Code lab

Code lab: slew ramp

slew_cmd.py · Python
#!/usr/bin/env python3
"""Rate-limit (slew) a speed command so the arm does not jerk."""

def slew(current: float, target: float, max_delta: float) -> float:
    delta = target - current
    if delta > max_delta:
        delta = max_delta
    elif delta < -max_delta:
        delta = -max_delta
    return current + delta

speed = 0.0
target = 0.8
max_step = 0.15
print(f"{'tick':>4}  {'speed':>6}  → target {target}")
for t in range(1, 9):
    speed = slew(speed, target, max_step)
    print(f"{t:4d}  {speed:6.2f}")
print("done — notice the ramp, not a jump")

Expected output

tick   speed  → target 0.8
   1    0.15
   2    0.30
   3    0.45
   4    0.60
   5    0.75
   6    0.80
   7    0.80
   8    0.80
done — notice the ramp, not a jump

Real world

Where slew limits protect hardware

  • Joint jog UIs.
  • Mobile base speed commands.
  • Gripper close speed ramps.

Mistakes

Common mistakes and confusions

Hardware & safety

Hardware lab and safety notes

Slew limiting protects gears and stops wheelies. Try it on a real servo: send the ramped angles from Python (or an Arduino loop) and watch the smooth move, then send one big jump and compare.

  • Servo signal → Uno pin 9; servo power from a separate 5–6 V supply (4×AA or a 5 V 2 A adapter); join GND with the Uno.
  • Start with small max_delta and raise it slowly.
  • Keep fingers out of the arm's reach while testing.
Two small hobby servo motors
Hobby servos — ramp the command instead of jumping.Photo: Kborer · Public domain (released by the author) · Source: Wikimedia Commons · resized, converted to WebP

Debugging

Debugging: when it doesn’t work

Symptom → likely cause → fix
SymptomLikely causeFix
Speed overshoots the targetClamp applied after addingClamp the delta, then add
Robot brakes hardSlew only on the way upLimit both positive and negative delta
Ramp never reaches the targetNot enough ticksRun until speed == target
Servo jitters at the endNoisy target from sensorSmooth the target (Lesson 13)
NoteRead the last line of a Python error first — it names the problem — then the line number above it. Change one thing at a time and re-run.

Think

Think about it

Think

If max_delta is 0.15 and target jumps 0→0.8, how many ticks until arrival (ideal)?

Experiment

Try it in the lab: slew on paper

Experiment

Slew on paper

  1. Run slew_cmd.py; confirm tick 6 reaches 0.80.
  2. In the interactive, set max_delta=0.05 and run — count ticks.
  3. Set target to 0 while mid-ramp; confirm it slews down too.

Interactive · Command slew / rate limit

Like easing an auto-rickshaw accelerator — the target can jump, but actual speed only moves by max_delta each tick.

actual0.00
target marker0.80
tick 0

Knowledge check

Check your understanding

Knowledge check

1. Slew limiting means…

2. In the lab with Δ=0.15 to target 0.8, tick 1 speed is…

3. clip(delta, −Δ, +Δ) handles…

4. Logging target and actual helps…

5. Decide should output…

Practice quiz

Loading practice quiz…

Coding practice

Loading coding exercises…

Mini assignment

Hands-on assignment

Mini assignment

Control notes

Implement slew for speed in 0..1 with configurable max_delta; plot or print a ramp table.

Deliverables

  • slew_custom.py
  • Ramp table for target 0.9 with two different Δ values

Do this, step by step

  1. Run slew_cmd.py: 0.15, 0.30, 0.45, 0.60, 0.75, 0.80, 0.80, 0.80.
  2. Set target to 0.9 and max_step to 0.1. How many ticks to reach it?
  3. Now max_step = 0.3. Compare.
  4. Write slew_custom.py and print both ramp tables (target 0.9, two Δ values).

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: With Δ = 0.1 it takes 9 ticks (0.1, 0.2 … 0.9).
  2. Hint 2: With Δ = 0.3 it takes 3 ticks (0.3, 0.6, 0.9).
  3. Hint 3: min(max_d, max(-max_d, target - current)) clamps the delta in one line.

Challenges (pick by level)

  • 🟢 Easy: Ramp down from 0.8 to 0 with the same function.
  • 🟡 Medium: Use different limits for speeding up and slowing down.
  • 🟠 Hard: Ramp a servo angle on hardware and log every command to JSONL.

Next step

Next, Lesson 15: Capstone — Build a Robot Controller. Related: PID and rate limits in Stage 7 — Control Systems & PID and smooth paths in Stage 9 — Trajectory Planning.

Sources

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in