# Tactiverge Academy — companion code for "Integral action and steady-state error"
# Lesson: https://tactiverge.win/academy/robotics-foundations/introduction-to-feedback-control/integral-action/
# Licence: MIT (see https://tactiverge.win/open-engineering/). Examples appear in lesson order.

# ── Example 1 ────────────────────────────────────────
def run(kp, ki, steps=40, slope=5):
    position = 0.0
    total = 0.0                  # running sum of error
    history = []
    for step in range(steps + 1):
        error = 100 - position
        history.append(position)
        total += error
        position += kp * error + ki * total - slope
    return history

for ki in (0.0, 0.05, 0.1):
    h = run(0.5, ki)
    print(f"Ki={ki:<4}  step 5: {h[5]:6.1f}  step 10: {h[10]:6.1f}"
          f"  step 40: {h[40]:6.1f}  peak: {max(h):6.1f}")
