I have a 76 cm, aluminum rod, attached to the shaft of a 12v DC banebot motor. The rod is hanging vertically initially, and rotates in the vertical plane. The task is to control the rod to make it stop at a particular angle. I am using a quadrature encoder alongside the motors to measure the angle. Apart from this, I am also using a cytron 10A motor driver along with an Arduino UNO.

What makes this problem tricky IMO, is the fact that we have to offset the effect of gravity. Had the rod been just rotating in the horizontal plane, we simply could've controlled the angular speed to be = $kp(\theta-\theta_{ref})$, so that as $\theta$ approaches $\theta_{ref}$, the motor keeps slowing down. (Ofcourse, we could've added I and D terms too for better performance.)

But the same code will not work in the vertical plane, as we have a continuous torque due to gravity as well. The equation of the system looks something like

$$\ddot{\theta}= \dfrac{6g}{l}\sin \theta + \dfrac{\tau_m}{I}$$ $$\dot{\theta}= \int \dfrac{6g}{l}\sin \theta dt + \Omega$$

Where I think $\Omega$ is the motor's contribution of the motor output to the angular speed, which is something we can control.So if we set $\Omega$ to be something like $kp(\theta-\theta_{ref})-\int \dfrac{6g}{l}\sin \theta dt$, Then I think as $\theta$ approaches $\theta_{ref}$, $\dot{\theta}$ approaches zero. But I am not sure if that will drive $\ddot{\theta}$ to zero too...

Also, How exactly will we implement the integration step? Something simply like I+=6*g*sin(\theta)*dt/l in a loop?


2 Answers 2


The control of a pendulum subject to gravity is a quite standard control problem to which you may apply different techniques such as PID or LQR.

Unless you perform current control, you usually don't have direct access to the torque $\tau_m$ generated by the motor. A more common setting foresees voltage control instead. In this context, the dynamic equation you have to address is slightly more complex as it needs to comprise the electromechanical behavior of the motor per se.

That said, from a control standpoint, it's essential to understand what is your control variable (CV), which is the output of your controller, and what is the process variable (PV), which is the variable you want to control such that it will eventually assume/track the desired setpoint value (SP).

Here, it's clearly:

  • CV = motor input voltage $u$
  • PV = angular position of the rod $\theta$
  • SP = the desired position $\theta_d$

Your PID is, therefore: $$ \begin{equation} e = \theta_d - \theta \\ u = K_p \cdot e + K_d \cdot \dot{e} + K_i \cdot \int{e} \end{equation}. $$

The integral part is fundamental in this scenario. In fact, assume that $K_i = 0$ and that you're already on the setpoint in a stationary condition. It follows that $e = 0$ and $\dot{e} = 0$; thus, $u = 0$, meaning you're not delivering voltage to the motor at all and the rod will start falling because of gravity. To sum up, the integral part is the only contribution capable of compensating for the effect of gravity as it can deliver non-null output when the input $\left(e,\dot{e}\right)$ is zero thanks to its internal reservoir.

In theory, you could also provide a feed-forward term $u_{FF}$ to better help the controller: $$ u = K_p \cdot e + K_d \cdot \dot{e} + K_i \cdot \int{e} + u_{FF}. $$

The term $u_{FF}$ can be calculated starting from the gravity and then reflecting it to the side of motor voltage. The calculation is often imprecise; that's why an integral part is always required.


Usually, we make use of the Tustin method for implementing the integral part in a discrete system:

$$ y_k = y_{k-1} + T_s \cdot \frac{e_k + e_{k-1}}{2}. $$

The output $y$ of the integral at the instant $k$ is a function of two subsequent samples of the input signal $e$, whereas $T_s$ is the sample time.

The Tustin (a.k.a. trapezoidal) method provides smoother outcomes with respect to the more common forward or backward Euler formulas.

A somewhat related Q&A: https://robotics.stackexchange.com/a/19658/6941.


Wouldn't we expect vanilla PID control, once it's properly tuned, to "just work"? When the arm is at rest, constant torque due to gravity will be opposed by constant torque from the motor (or friction in the gear train). Dynamically, it's nonlinear and all, maybe difficult to tune, "tricky" as you say. But PID can always —eventually— "achieve zero steady-state error in the presence of constant disturbances", right?. When you go to maximize performance, you'll probably want to add a compensating nonlinearity that effectively tells the motor to try harder when $g \sin \theta$ is larger.

For your second question, integration: Numeric integration has a long and rich history, since the 18th century at least. On wikipedia, you could read articles on "Numerical integration", "Runge–Kutta methods", "Numerical methods for ordinary differential equations", and "Predictor–corrector method". If you go down that rabbit hole, beware: much of the historical work has been to get the most out of analytic formulations, integrating some expression that can be evaluated anywhere in the region of integration. A lot of that turns out to be irrelevant when all we have is a time series of measured error numbers with no formula in sight.

But you can probably skip all that: do a web search for "time series integral for PID". Common practice seems to be to just maintain a running total of the errors, trusting that the controller will correct for integration error just like it corrects position errors. You do have to worry about "integral windup", though (check wikipedia).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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