I'm constructing a 2 wheels balancing robot which uses a PID controller. I've tuned my parameters on numerical simulations based on a continuous inverted pendulum system so that the simulated inverted pendulum balances by controlling the horizontal (linear) cart acceleration $\ddot{x}$.

Now that I've done this, I want to take the next step and turn my PID control commands into electrical commands onto a DC motor to give the desired linear acceleration $\ddot{x}$. However I'm not sure how exactly to do this for my specific robot's motors. Are there experimental tests should I run to determine how to convert PID commands into DC motor acceleration commands? Or is there a formula to do this based on the motor's specifications?


The non-linear dynamic equation I'm using is


where $\ddot{x}(t)$ is the linear acceleration, $g$ is the acceleration due to gravity, and $\ddot{\theta}$ is the angular acceleration, and $d(t)$ is an external disturbance to the system. To simplify things, I've linearized the equations around $\theta\approx0$, yielding


I've assumed that the only control input is the cart's linear acceleration $\ddot{x}(t)$, and chose this control command as $\ddot{x}(t)=K_1\theta(t) + K_2\int_0^t\theta(t) dt + K_3\dot{\theta}$, where $K_i$ are the PID gains.

  • $\begingroup$ Acceleration is proportional to force and force is proportional to the motor's torque. Which is in turn proportional to the motor's current. So the acceleration is linearly proportional to the motor current. $\endgroup$
    – Eugene Sh.
    Mar 4, 2015 at 13:34
  • 1
    $\begingroup$ The acceleration is proportional to the overall sum of external forces acting on a system, whose motor's torque is just one force. Therefore, I wouldn't say that acceleration is proportional to the torque. Nonlinearities do emerge also in the remaining part of the chain. $\endgroup$ Mar 4, 2015 at 15:45
  • $\begingroup$ For curiosity, you said you did rely on simulation to tune your parameters, hence what kind of dynamic model did you employ to simulate the system whose input is the original PID output and whose output is the linear acceleration? $\endgroup$ Mar 4, 2015 at 15:52
  • $\begingroup$ @ugo: what exactly are you referring to by "the remaining part of the chain"? $\endgroup$
    – Paul
    Mar 4, 2015 at 15:53
  • $\begingroup$ I was referring to the parts going from the torque to the current. $\endgroup$ Mar 4, 2015 at 17:04

1 Answer 1


First comment, the equation you gave for your PID seems a bit weird. The PID normally acts to steer an error, let's say $e_{\theta}=\theta_d-\theta$, to zero, where $\theta_d\left(t\right)$ is your set-point, possibly varying, and $\theta\left(t\right)$ is the current feedback.

If we always assume $\theta_d=0$ - is this your task? -, then you have to consider the right sign for the feedback, i.e. $-\theta$, since usually the PID gains are deemed to be positive. This way though, you'll prevent yourself from having fun while trying to stabilize the pole in non-upright positions (i.e. $\theta_d \neq 0$, wherein the linearization does not hold).

What is missing is to make the dynamic equation of linear motion explicit, that is something like $\ddot{x}=f\left(x,u,d_x,t\right)$, where $u$ is the motor torque and $d_x$ are other type of disturbances (e.g. friction forces) acting on the system. You could plug this second equation in the first one coming up with a new PID whose output is directly $u$ and no longer $\ddot{x}$, changing thus the input to the system.

You might describe the function $f\left(\cdot\right)$ in terms of what you know about the system (e.g. wheels geometry to transform torque into force, motors' parameters to deal with transformation of voltage/current into mechanical torque), providing therefore a feed-forward term to help the PID do its job or even let the PID work alone in the closed-loop.

Another possibility is to replace the single PID scheme with a cascade of a inner loop where you control $\ddot{x}$ through the first PID (you have somehow to measure/estimate the linear acceleration), and an outer loop that finally controls the angular position of the pole by means of a second PID. Anyway, two PIDs means more gains to fine tune. You may also want to identify $f$ via proper identification techniques.

As a side note, one effective way to deal with all the disturbances and uncertainties you have in the real system that are not taken into account in the model is to resort to sliding mode control.

Finally, I'd really suggest you to search in literature: there are plenty of methods and models out of the box for you to read and use for such a classical control problem.

  • $\begingroup$ Indeed, my goal was to obtain balance by restricting my set-point to the zero angle. Clearly, the case with $\theta_{d}\ne0$ is more interesting, but I wanted to work with the simplest case first before branching outward. I'm really interested in determining $f(x,u,d_x,t)$. Do you have any references with examples on how to accomplish this specifically for cart-pole systems? If I can see one worked out example, I'm sure I can extrapolate it to my specific case. $\endgroup$
    – Paul
    Mar 4, 2015 at 18:04
  • $\begingroup$ Do you know of any useful tutorials on how to work with the $\theta_d\ne0$ case? $\endgroup$
    – Paul
    Mar 4, 2015 at 18:44
  • $\begingroup$ A quick search gives the following reference: look at page 25-26. I'm pretty sure there exist simpler models, though. However, as I said, you could even avoid relying on such detailed descriptions of $f$, since PID controllers work quite well in closed loop. $\endgroup$ Mar 4, 2015 at 18:54
  • $\begingroup$ Here's a better reference. $\endgroup$ Mar 4, 2015 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.