# Bi-directional PID control

I am currently a Mechanical engineering student and I am trying to create a device that will balance two weights by moving the fulcrum between them. The whole thing looks like a see-saw with a motor and gyroscope attached, mostly because that's exactly what it is. The problem I am having is getting the motor to move the board to place its center of mass above the motor in order to balance the load. The motor has a large gear on it that meshes with a linear gear across the bottom of the board. I am using an arduino mega to control a Spark motor controller using PWM. My first idea was to simply measure the angle from the gyro(between -4 and 4 degrees) and activate the motor whenever it did not read 0 degrees(level). This threw the board off of the motor. Later I decided that a PID loop would be a better solution. I wanted to have something like this video shows. https://www.youtube.com/watch?v=fusr9eTceEo The problem is that as far as I know, PID only works in one direction. A gas pedal in a car is a good example because the PID loop can only give more or less gas and cannot hit the brake pedal. In my case I need two PID loops in order to control the position both left and right. Currently I am running the two loops side by side using the Arduino library with the angle as the input and the motor power as the output. I take the difference between the outputs and the resulting positive or negative value goes to the motor. I really don't think this is the right way to do this as I still cannot even balance the board itself. What do I need to do in order get the right motion?

• Actually it is quite routine for a PID loop to output a correction in either a positive or negative direction, which is what you seem to need here - you seem to be getting a bit confused by the car example, but you could consider the gas pedal and brake as the positive and negative outputs with a bit of a "do nothing" threshold in between. To solve your problem, you may need to model the inertia of the system, rather than only trying to correct the angle error. Ultimately this is not an electrical engineering problem but a general engineering one, especially given it is a mechanical system. – Chris Stratton Apr 2 '17 at 1:26
• This electromechanical control system physics problem, is more non-linear and complex than it looks as a 3D dynamic balance and static balance are not the same fulcrum point, so platform response to motor position, velocity and acceleration are not simple derivatives of each transfer function. Also the gyro feedback is 2nd order derivative of angular position error with a pivoting platform, so will have a constant drift error. Gear backlash can add problematic instability. Computing the transfer functions will not be easy. A fuzzy logic algorithm or a nonlinear PID might be needed. – Tony Stewart Sunnyskyguy EE75 Apr 2 '17 at 3:29
• It is an excellent 4th Year Control System problem if the transfer functions can be defined , much more difficult than the inverted pendulum train-track thesis project or car speed control and spacing control with laser linear position feedback in a 1D system. The lateral and rotational Moment of Inertia must be defined not just the gravity forces. Good COMSOL project. – Tony Stewart Sunnyskyguy EE75 Apr 2 '17 at 3:33
• Without knowing initial conditions for angle, hunting for the fulcrum with a level platform may be difficult. I would use differential proximity detectors under both sides of platform to detect balance rather than the gyro. Use two IR reflective signals with balanced diffused reflection... Sharp Vishay IR for balanced position error. – Tony Stewart Sunnyskyguy EE75 Apr 2 '17 at 4:04

Chris is correct and PID commonly operate to give positive and negative outputs - even your car cruise control example is out of data, modern radar controlled cruise controls can indeed operate the brakes as well as the throttle. A more general example is called a servo. Small scale ones are used for models.

Here is another example of an Arduino using a PID loop to balance a beam in a different way:

Beam balancing example

• How do I get a negative value from a PID loop? I thought that if the input passed the target, the output would drop to zero while waiting for the input to come back down below the target. – E Sully Apr 2 '17 at 4:39
• If you are using op amps with a single supply, it's true that the output cannot go negative. So the answer is: use op amps with bipolar power supplies, such as +/- 5, +/- 12, or +/- 15. There is no need to tie the (power) V- input to ground. – WhatRoughBeast Apr 2 '17 at 12:44

There's nothing about a PID loop that restricts it to positive values. The inability to use negative numbers is a limitation of the throttle, not the PID controller. Try the math.

$$u(t) = K_p e(t) + K_i \int_0^t e(\tau)d\tau + K_d {de(t) \over dt}$$

• $e(t)$ is the error at time $t$.
• $u(t)$ is the control value, the input to the motor controller.
• $K_p$, $K_i$, and $K_d$ are the coefficients, which you adjust to get the response you want from the PID controller.

Say we define "0" to be level, positive values tipping to the right, and negative values tipping to the left. The goal is to come up with some input to the motor controller $u(t)$ that makes the tip $e(t)$ zero.

A zero $u(t)$ means the motor isn't driven at all. A positive $u(t)$ drives the motor to shift the fulcrum right, whereas a negative $u(t)$ does the opposite.

To keep things simple let's say $K_p = K_i = 1$, and $K_d = 0$.

Lay it's tipping left. That means $e(t)$ is negative. $K_p e(t)$ is then negative, and $K_i \int e(t)dt$ is decreasing, and will become negative if it isn't already. We'll ignore the derivative term because you might not need it.

Thus, $u(t)$ is negative or is tending towards negative, which will shift the fulcrum to the left.