Explainer
Stepper motor model for cart pendulum PID control
Short answer
Model the stepper and its current-commanding driver as one gain: torque equals the torque constant times commanded current, and cart force is that torque divided by the pulley radius. Saturate the current at the driver setting, add a speed-dependent torque limit from the datasheet curve, and feed back cart position as well as pendulum angle, because angle-only control lets the cart drift into the end stops.

On this page
- How do you model a stepper motor for a cart-pendulum simulation?
- What numbers fix the force scale of the model?
- Why does angle-only control let the cart drift into the end stops?
- How do you add cart position feedback to a PID loop?
- Where does the constant-torque model stop matching the real motor?
- What does current saturation do to the simulation?
- How do you check the model against the real rig?
- Questions asked
With a current-controlled driver between the controller and the motor, a stepper in a cart-pendulum simulation needs no machine-model block and no electrical detail: motor and driver together are one torque source. Commanded current times the torque constant gives shaft torque, that torque divided by the pulley pitch radius gives cart force, and two limits keep the model honest — the driver's current setting and the falling torque-speed curve. The control question in the same project has one answer too: angle-only feedback can hold the pendulum, but it leaves the cart as an uncontrolled double integrator that drifts into the end stops, so cart position belongs in the loop as a slow outer loop that biases the angle setpoint. Below are the equations, a worked example with the arithmetic shown, the checks that prove the model matches the rig, and the decision for each case.
How do you model a stepper motor for a cart-pendulum simulation? #
Treat the motor and its current-controlled driver as one torque source: commanded current becomes torque through the torque constant, and torque becomes cart force when divided by the pulley pitch radius, so T = Kt × I_cmd and F = T ÷ r is the entire plant.
A driver that knows the rotor position and handles commutation leaves exactly one input, the current command, and the block chain is a gain followed by the cart equation you already have:
T = Kt × I_cmd
F_cart = T / r_pulley
M × x'' + b × x' = F_cart - pendulum reaction
The gain is the whole motor model; everything after it is mechanics you have already built. Add a first-order lag after the gain only when you also want the current loop's finite bandwidth in the picture, and only when the driver's documentation states a bandwidth to set that lag from — the FAQ below gives the conversion. The torque constant comes from the motor datasheet; where the datasheet lists holding torque and rated phase current instead, dividing one by the other is the accepted approximation, and the worked example below takes that route. One condition attaches: the gain holds while the driver keeps winding current at the command, which is the low-to-moderate speed region, and the torque-speed section below covers where that ends.
What numbers fix the force scale of the model? #
Five inputs fix the force scale: the torque constant, the driver current setting, the pulley pitch radius, the moving mass and the travel length, and each one shifts your PID gains by the same ratio when it is wrong.
| Input | What it sets | Where the value comes from |
|---|---|---|
| Torque constant Kt | torque per ampere of command | motor datasheet torque constant, or holding torque ÷ rated phase current as an approximation |
| Driver current setting | the ceiling the command saturates at | the driver's current configuration, set at or below the motor's rated phase current |
| Pulley pitch radius | torque-to-force conversion | mechanical drawing, or tooth count × belt pitch ÷ 2π |
| Moving mass | acceleration per newton of force | measured on the built rig, cart plus pendulum |
| Travel length | the bound the position loop must hold | measured between the end stops |
The current that saturates the model is the driver's setting, not the motor's nameplate, and running the driver below the motor rating is its own sizing question — see what happens when a driver is rated below the motor current.
Worked example — the inputs are example values, so take yours from the datasheet and drawing of your exact parts: a hybrid stepper with 1.9 N·m holding torque at 2.8 A rated phase current, a driver set to 2.5 A, a 36-tooth pulley on a 2 mm pitch belt, and a measured moving mass of 2.5 kg. The five lines then read:
- Kt ≈ 1.9 ÷ 2.8 = 0.68 N·m/A
- torque ceiling = 0.68 × 2.5 = 1.7 N·m
- pitch radius = (36 × 2) ÷ (2π) = 11.5 mm = 0.0115 m
- cart force ceiling = 1.7 ÷ 0.0115 = 148 N
- locked-pendulum acceleration = 148 ÷ 2.5 = 59 m/s², before friction takes its share
These are calculated values for those example inputs, not a rating of any motor and not a measured result. With your own numbers the same five lines give your force scale, and a measured locked-pendulum acceleration far from the last line means the force scale is wrong before any gain is touched.
Why does angle-only control let the cart drift into the end stops? #
Because the motor acts on the cart and not on the pendulum, angle-only feedback leaves cart position as an uncontrolled double integrator: a small sensor bias becomes a constant acceleration demand, cart velocity ramps, and position grows without bound.
The linearized pendulum contributes one unstable pole, and angle feedback can move that pole where you want it, so the theory is satisfied and the simulation will hold the angle indefinitely. What no term in an angle-only loop watches is where the cart ends up. Any constant offset — an encoder zero error, a bent mounting bracket, a bias in the estimated angle — becomes a steady acceleration demand, the velocity ramps at that demand, and position grows with the square of time. A simulation with no travel limits hides this by construction: the position state diverges and the demanded velocity climbs past anything the real motor could follow. The decision: angle-only belongs in analysis, and every real rig carries cart position and velocity as feedback whenever the machine has end stops.
How do you add cart position feedback to a PID loop? #
Run the position loop as a slow outer loop that biases the angle setpoint of a fast inner angle PID, clamp what it may command to a few degrees, and keep its bandwidth well below the angle loop's so the two do not fight.
With PID blocks only, build the cascade in this order:
- Tune the inner angle PID with the position loop disabled and accept the drift; the pendulum should hold while the cart wanders.
- Add the outer position loop with its output wired as an angle-setpoint bias, not as a torque or a current.
- Check the sign before trusting anything: a positive cart displacement must bias the angle setpoint in the direction that accelerates the cart back toward center, and a wrong sign shows up as faster drift, not slower.
- Clamp the angle setpoint the outer loop may command — for example ±3° — so the inner loop stays inside the range where the linearized model matches the rig.
- Close the loop, disturb the pendulum lightly, and confirm the drift stops and the cart settles back inside travel.
On hardware, set the software travel limit inside the end stops before the first angle-only test, because that test is designed to let the cart run.
Where does the constant-torque model stop matching the real motor? #
Above the speed where back-EMF and winding inductance limit current rise, available torque falls with step rate, so cap the model's torque with the value read from the motor's torque-speed curve at the speed your simulation demands.
Convert the cart speed the simulation demands into motor speed and read the curve there:
n [rpm] = 60 × v [m/s] ÷ (2π × r [m])
With the worked example's radius, a cart speed of 0.5 m/s is 60 × 0.5 ÷ (2π × 0.0115) = 415 rpm, and the torque ceiling in the model becomes whatever the datasheet curve of your exact motor reads at 415 rpm — not the low-speed ceiling from the worked example. The curve itself moves with supply voltage and acceleration setting, which is why the same motor shows a different usable speed on a different drive: stepper motor max RPM, torque curve, voltage and acceleration has that breakdown.
The gain model also has a blind spot no curve fixes: missed steps. A torque source assumes the rotor follows the command field, and past pull-out it does not — why a stepper motor stalls above a certain step rate marks where that boundary sits. If the simulation demands speeds the curve cannot support, the model must show the torque saturating there, or a divergence the simulation hides will arrive in hardware.
What does current saturation do to the simulation? #
The controller will command more force than the hardware can deliver, so put a saturation block on the current command at the driver's current setting; without it the simulated controller looks better than the real rig, and the pendulum falls where the model said it would hold.
With the worked example's driver setting, the ceiling is 2.5 A of command, 1.7 N·m of torque and 148 N of cart force; a disturbance asking for more simply pins the command there, and the loop loses authority while the linear model keeps predicting a recovery the hardware cannot produce. Log how often the command spends pinned: sustained saturation means the gains or the disturbance size exceed this motor and driver, and no tuning of the model fixes that — the hardware needs more current, a trade on the pulley radius or a gentler motion profile. Expect less than the nameplate as well, because a driver IC delivers less current than its datasheet peak once it heats, so the true ceiling can sit below the setting you entered.
How do you check the model against the real rig? #
Run five checks: a locked-pendulum current step, a released-pendulum divergence test, an angle-only drift test, a position-closed recovery test and a saturation audit of the logged current command.
- Locked pendulum: step the current command and compare the measured cart acceleration with F ÷ M from the model — 59 m/s² before friction in the worked example. A measured value far below that means the force scale (radius, mass, torque constant or current setting) is wrong, and every gain transferred from it is wrong by the same ratio.
- Released pendulum, loops open: the angle should diverge with a time constant near √(l ÷ g). For a 0.25 m pendulum length that is √(0.25 ÷ 9.81) = 0.16 s; a divergence far slower or faster points at the pendulum model, not at the motor.
- Angle loop closed, position loop open: the pendulum holds and the cart drifts. If the pendulum will not hold here, fix the inner loop before adding anything.
- Position loop closed: drift stops, and the cart stays inside travel after a small disturbance.
- Saturation audit: read the logged command across all the tests above; long pinning at the ceiling means the gains or the disturbance exceed the hardware.
Pass all five and the gain-plus-lag model is good enough to tune PID gains in simulation before the first hardware run. Fail the acceleration check and fix the force scale first, because a wrong force scale carries every gain you tune on top of it.
Questions asked
If the datasheet lists no torque constant, how good is the holding-torque shortcut?
It is an approximation, not a rating. Holding torque is measured with both phases energized at rated current and the rotor stalled, which is not the same condition as rotating torque under commutation. Expect the true force scale to differ, tune the inner angle loop on that estimate, then correct the model with the locked-pendulum acceleration test before you trust any gain you transferred from simulation.
Do I need a licensed machine-model block to simulate this?
No. The whole plant is a gain, an optional first-order lag and a saturation, all standard blocks in any simulation package. If your license happens to include a ready-made stepper machine block, using it is fine, but nothing in this method depends on it, and the gain-plus-lag substitute is a legitimate model built from the blocks you already have.
How far below the angle loop should the cart position loop sit?
A workable rule of thumb is to place the outer position loop at one fifth to one tenth of the inner angle loop's bandwidth, so the two loops do not fight over the same torque. Bring it in after the angle loop is tuned and holding steady; if the cart oscillates instead of drifting, the separation is too small.
What pendulum encoder resolution does the derivative term need?
Enough that quantization stays well below the clamped angle setpoint, then filter the derivative. For example, a 1000-line encoder used in quadrature gives 4000 counts per revolution, or 0.09 degrees per count, which is fine next to a few-degree clamp. The practical limit is usually not resolution but noise on the counted position, and an unfiltered derivative turns that noise into torque chatter the motor then follows.
How do I set the lag that stands in for the current loop?
Take it from the driver's stated current-control bandwidth when the datasheet gives one: a 1 kHz current loop corresponds to a time constant of about 0.16 milliseconds, from tau equals one over two pi times the bandwidth. When no bandwidth is published, leave the lag out; the current loop is far faster than the mechanics, and a pure gain already matches the rig closely enough to tune PID gains.


