Monday, April 27, 2015

Flywheel PID

Interesting title for blog post

Background
Control theory is a branch of mathematics that deals with how a moving system reacts to feedback. This is a very broad definition, which boils down to: control is changing your input based on how the system reacts until you can make a system do what you want. For this project, Jiaying and I wanted a suspended flywheel assembly to follow a light.

  

In order to do this, we needed to create some type of controller where the voltage we gave the motor was influenced by the voltage we read after a photoresistor.


In this case, our controller - K - is proportional to the value of photoresistor, the derivative of that value, and the integral of that value. This voltage is fed to the motor which changes its position and by extension the voltage that we read from the photoresistor. These transfer functions are represented by the ‘motor’ and ‘sensor’ boxes in the diagram above.


Why Proportional, Integral, and Derivative?

Proportional, Integral, Derivative (PID) control is one of the most common types of controllers because it is easily implemented in code. These types of controllers have also historically been considered on of the best options when knowledge of the how exactly a system functions is unknown. In our case, we care more about the PID controller’s ability to compensate for the unknown system because in order to have a complete transfer function we would have to model how the input voltage to the motor affects the dynamic movement of the motor and the translation of that to value our photoresistor reads.

How does PID control work?


Proportional:

For a system based purely on proportional control, the process (in this case our motor + photoresistor) has some form of output, the difference - or error - between this value and a set point is found. This value is then passed into a function that multiplies it by some constant and that new value is passed into the system. In theory, doing this means that when the system is far from the set point, it will change a lot, but as it approaches the setpoint, the change in it’s position will go to zero.

Integral:
In practice, a controller that has only proportional control may have some form of residual error and never actually reach equilibrium so many systems will add a term proportional to the integral of the error. This term is proportional to both the magnitude of the error and the duration that the error has existed. In some cases, however, because it is proportional to the duration of the error, this term may cause the system to overshoot its set point

Derivative:
Derivative control is proportional to the slope of the error, and can help reduce settling time because it, in a sense, is a prediction of the future. In practice, however, derivative control can become very noisy/unstable because it does not really use past values so it is very sensitive to changes. For this reason, if it is implemented, there is also generally a low pass filter to get rid of the worst of the high frequency noise.

Circuit Design

In order to create the PID controller we needed to determine how to measure error, and then what values made sense for each of the components in the controller itself.

Choosing the setpoint

To simplify our circuit design, we wanted the find a way to avoid creating a subtractor circuit, and thus wanted to make sure that our setpoint was zero. In order to accomplish this, we connected one leg of our photoresistor (the pale blue wires) to the negative rail and the other to the output of a pot which was connected to the positive rail. With this in place, we simply turn the pot to change the location of our zero, or setpoint.

Creating the PID circuit

When we originally looked into creating a PID circuit, we planned on having three separate op amp circuits, one for the proportional, integral, and derivative components of the controller. After talking with our professor, however, we realized that with the use of Kirchoff’s Voltage law we could actually combine all three of these aspects into a single op amp circuit.

This greatly simplified the construction and reduced the number of variables we needed to tune. However, it also meant that all three systems became tied to the same four components.

Choosing resistor and capacitor values

Ideally, to choose the gains we would apply to our system, we would be able to model the transfer function between the voltage applied to the motor and the voltage from the photoresistor. If we had this function, we could use a process called the Laplace transform to investigate the system behavior to different PID gains. Doing this would have given us a good idea of what types of gains were stable and from there we could have chosen values of our resistors and capacitors.

In this case, however, the transfer function is rather complicated. It involves the translation of voltage to torque, and the interaction between the angular momentum of a spoked wheel and the motor itself, as well as the resistance of the wires tethering the motor to its control circuit. This lead us to choose gains in a more experimental method. We created a Matlab script in order to tell us the values of each of our coefficients and started building a proportional controller. Once we had values for the proportional constant, we used the script to get a rough estimate of what capacitor value we would need to keep the correct order of magnitude for the integral component. After getting a rough estimate, we experimentally tweaked the system and then repeated the process for the derivative component.

Filtering

After adding the derivative component, we realized that the values we were getting out were too noisy to be very useful in controlling the motor. With these oscillations, the motor became very jerky and prone to oscillating out of control. To reduce this, we created a low pass filter to kill some of the very high frequency oscillations before they were passed to the motor.

Results

So with proportional control our system behaves as we would expect it to, with the motor input following the photoresistor output by a factor of 2 as determined by our Kp (proportional constant). We found that our motor followed our light source when the motor input smoothly followed the photo resistor output as seen from T= -6 to T = -1. However when the motor input railed we would see that the motor no longer acknowledge the light source and returned to its natural bias (T = -1 to T = 0). The times characterized by extreme oscillations (T = 1 to T = 4), the motor attempts to follow the lightsource but mirrors the input behavior in that it follows the light source in a jittery fashion.


After adding derivative control, we found that the motor more often followed the light source. As seen above we still had problems with the motor input railing against the 12V limit when the light moved too fast or got too close/far from the sensor.
Using PD control we were able to control the motor in a sinusoid like manner, making the motor turn one way and then the next. However, this control was only possible when the light moved very slowly and remained a very constant distance from the motor.


After adding the integral control, we can see that almost any movement results in either the motor input railing or in severe oscillations. This resulted in the motor being more unstable, and extremely jittery.


Analysis
Although normally derivative control tends to add noise to a system, we found that a combination of PD or PID control provided the most stability. That said, we were not able to find gains (all abt dem gainz) that created a very stable system. If the radius the light is held from the sensor changes too much or the system starts to overshoot by too much, it rapidly loses stability. Part of this may be due to to the type of controller we are using, however most of this is probably a result of us not being to able to find ideal constants of proportionality.