R2r mpc control

From XuShuwenn/RedAct · View interactive version

R2r mpc control is an AI agent skill from the GitHub repository XuShuwenn/RedAct. Implement MPC controller for roll-to-roll manufacturing to stabilize web tensions during roller changes.

How to use this skill

Skill file

Roll-to-Roll MPC Controller

When to Use

Execution Protocol

Problem

  1. First confirm the environment contract: required action syntax, allowed tools, path conventions, and exact completion string. Treat this as a gate before file inspection or coding.
  2. Derive linearized state-space model at reference operating point

1a. Inspect r2r_simulator.py enough to confirm the actual class/interface before coding: constructor, state representation, initialization/reset behavior, how to advance simulation, what outputs are returned, and how disturbances/reference values are exposed. Read the portions that define the class methods you will call; do not code against assumed methods or return signatures from partial inspection.

1b. Record the exact simulator methods and return signatures you verified from source or safe runtime inspection, and code against those verified names only. If the file view is partial or a call fails, keep inspecting until constructor, step/reset flow, returned values, and time/state access are confirmed.

  1. Design an actual MPC controller with horizon N in [3, 30]: formulate a finite-horizon receding-horizon optimization and solve it at each control step. Do not substitute a fixed LQR/state-feedback law and label it MPC.

2a. Make the runtime implementation visibly MPC: construct and solve the horizon optimization online each step (or via an explicitly equivalent receding-horizon QP formulation), apply only the first control action, and log/export parameters consistent with that implementation.

  1. Run controller for at least 5 seconds

3a. Treat the full 5-second closed-loop simulation as the acceptance test. Use short smoke tests only for debugging; make final tuning and validation decisions from the required full simulation and reported metrics.

3b. For fixed-step simulations, determine whether each log sample is recorded before or after stepping, then choose the sample count so control_log.json includes timestamps spanning at least 5.00 seconds. Read back the first and last timestamps from the saved log to confirm coverage.

  1. Log tensions and compute metrics

4a. Use the required deliverables as a completion checklist: write controller_params.json, control_log.json, and metrics.json from the final controller run, then reopen and validate them before finishing.

  1. After any code or parameter edit, rerun the final selected controller to confirmed successful completion before trusting any existing artifacts.

5a. Inspect the executed controller code/control loop directly before claiming MPC: confirm the runtime controller actually applies a receding-horizon optimization or equivalent MPC law each step, rather than only exporting LQR-related matrices, weights, or a fixed state-feedback controller.

  1. Reopen generated artifacts from the latest successful run and verify full required content, not just file existence or a truncated preview: inspect enough of controller_params.json, control_log.json, and metrics.json to confirm required top-level keys, plausible dimensions, and that the control log spans at least 5.00 seconds.

6a. Cross-check key trajectory-derived metrics—especially settling time after the disturbance at t=0.5—against control_log.json. If the log disagrees, recompute or fix the metric and do not report the stale value.

  1. Compare computed metrics against every stated target before declaring success; if any requirement fails, output is truncated/unverified, required fields/duration are missing, or artifact freshness is unclear, rerun or continue debugging.

7a. Treat any failed threshold in metrics.json as a hard stop: do not end the task, claim success, or emit a completion summary while any requirement is unmet or only "close enough".

  1. If behavior is unstable, biased, produces nan/inf, or suspicious metrics, validate the model/control formulation before further retuning.

8a. Check root-cause assumptions explicitly: verify equilibrium/reference offsets, discretization, feedback sign, state ordering, and that the linearized model qualitatively matches the simulator near the operating point before more weight or gain tuning.

  1. Perform a final end-to-end deliverable and procedural validation from saved files: load each JSON fully, confirm required keys and structure, verify control_log.json reaches at least 5.00 seconds using actual first/last timestamps, confirm the latest artifacts support the claimed metrics, and only then complete the task using the environment's exact required completion signal.## Workflow
  1. Derive linearized state-space model at reference operating point

1a. Inspect r2r_simulator.py enough to confirm the actual class/interface before coding: constructor, state representation, how to advance simulation, what outputs are returned, and how disturbances/reference values are exposed. Do not assume methods or return signatures from naming alone.

  1. Design an actual MPC controller with horizon N in [3, 30]: formulate a finite-horizon receding-horizon optimization and solve it at each control step. Do not substitute a fixed LQR/state-feedback law and label it MPC.
  2. Run controller for at least 5 seconds

3a. Treat the full 5-second closed-loop simulation as the acceptance test. Use short smoke tests only for debugging; make final tuning and validation decisions from the required full simulation and reported metrics.

  1. Log tensions and compute metrics
  2. Reopen generated artifacts and verify that controller_params.json, control_log.json, and metrics.json were created from the latest successful run, are populated, and are structurally correct.
  3. Compare computed metrics against every stated target before declaring success; if any requirement fails or output is truncated/unverified, rerun or continue debugging.
  4. If behavior is unstable, biased, produces nan/inf, or suspicious metrics, validate the model/control formulation before further retuning.

Required Outputs

controller_params.json

{
  "horizon_N": 9,
  "Q_diag": [100.0, ...],
  "R_diag": [0.033, ...],
  "K_lqr": [[...], ...],
  "A_matrix": [[...], ...],
  "B_matrix": [[...], ...]
}

control_log.json

{"phase": "control", "data": [{"time": 0.01, "tensions": [...], ...}]}

metrics.json

{"steady_state_error": 0.5, "settling_time": 1.0, ...}

Performance Targets

Tips

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.