Thursday, March 14, 2013

Friendship With State Space

Last post we saw how to represent any system in a block diagram and barely touched upon mathematical representation of it called state space. Today I will try to make you believe how simple it is to convert any system into state space representation and give a few “Engineering Secrets” to you.

Best way is to go through an example and then generalize it. Let’s recall Newton (He has tortured us many times before! :-/). So (simplified) Newton’s second law is can be represented by 

Where “m” is the mass of the body, “a” is its acceleration and “F” is the force applied on it. We all have used this equation a million times before. We use this equation to answer the question “What force “F” is required to maintain/attain acceleration “a” of a mass “m”??) Now let’s try to convert this to the form I mentioned in the last post, which is

To do this we need values for “A”, “B” and “C”. Looking at this we can easily answer the question that we need an input “u” to maintain/ attain a state ([X]) ̇ where X is a vector of all the states of the system. So relating this to the answer mentioned above: We have Force (F) as an Input (u) and acceleration as an output (y)!! We can also represent the above equation as 


Engineering Secret 1!: “Generally the number of states in a state space representation is equal to the highest derivative in the differential equation of the system”

Hence in above equation (d^2 x)/(dt^2 ) suggests the highest derivative is second order. Hence the number of states (length of X vector) is 2!!

Engineering Secret 2!: List all the states X1…Xn. To guess the states, let the first state X1 be the original variable itself, in this case “x”. Let the second state, X2, be the derivative of first state, the third be the derivative of second state and so on!
So now we get 

Now computing LHS of the state space equation which is given by the matrix below is super easy!


We have all the ingredients! 
So,

Engineering Secret 3! How do you know the dimensions of A and B???
Well.. we know LHS is 2*1 and is 2*1 hence if A is m*n then 2*1 = (m*n)X(2*1). So n should be equal to 2 (for a valid matrix multiplication) and m should be 2 for valid dimensionality.  Thus A is 2*2. Similarly if LHS is 2*1 and u is 1*1 then 2*1 = (m*p)X(1*1). Hence p = 1 and m =2. So B is 2*1 matrix.

Dimensions of C depend on dimensions of “y”. That means if you  are going to measure both the states the Y is 2X1 and hence C will be 2X2. If you are measuring only X1 (displacement) then C will be 1X2.


Engineering Secret 4!: To rearrange this into state space format  write the skeleton first (according to the dimensions you derived from the above trick!) and then fill in the values for A and B using matrix multiplication rules

And then filling in the values we get


Hence to Summarize:
  1. Determine highest order in the system equation (given to you by physics!). Your number of  states is equal to the highest order of derivative. 
  2. Let first state be the variable itself, second be its derivative, third be second’s derivative and so on.. Calculate values for all states. 
  3. Calculate LHS by taking first derivative of each state. And write all equations. 
  4. Derive the dimensions of A, B  and C matrices
  5. Write the SS skeleton and then fill in the numbers according to the equation coefficients you got from step3!



 So.. how does that feel? I will try to go over a more real life example in next post so that you may get more familiar to this. 



Saturday, March 9, 2013

The Basics of Controls and State Space Behavior



Hey Guys.. I will try to share some tools that I learnt during the course of my Masters and in the industry. In my last post I mentioned a few things related to controls that you will learn during your 3rd or 4th year. Today I will discuss a few things that are important and always needed in industry. These may or may not be covered in the class and hence I felt I could share these.

In this field you will be given a task of modeling a particular system and then designing a controller for it which will help you attain desired behavior. If you consider a black box model your entire work could be represented by the block diagram shown below. Let’s discuss the parts of this which will result as a perfect segway into state space (the most important tool to design systems).

Block Diagram

  1. The block “A” is called the plant. This is defined by the laws of physics and is the guy that we  have to control. E.g. This could be a car going uphill at constant speed, a tap filling water in a tank, a line follower robot, a hydro electric power plant etc. All you need to remember is.. the behavior of this plant is defined by physical laws and is constant. You don’t have any control over its definition.
  2. The block “B” is your controller. You design this so that you can control “A” as you want. E.g You will have a flexibility to design a controller “B” that will maintain the speed of your car going uphill at 50Km/Hr or maintain the robot on the black line all the time. 
  3. The block “C” is your output that you can measure or observe and then use it in your controller. E.g It can be the speed of the car or velocity of the line follower. 
  4. The signal “u” is the control. This is the signal that is generated from your controller which is fed to the plant to control it as you want. 
  5. And finally, this controller will be responsible to control a particular “state” of your system. Your system can have many states. One of these many states is your “output” that you observe/ measure with the help of block “C”.

So, the ingredients of the any damn system are: the Plant (A), the Controller (B),the control input (u), the state (x) and the output that you measure (y). And mathematically, any damn system can be represented as


Believe me, with this representation, it becomes super easy to analyze the system compared to writing huge differential equations by drawing free  body diagrams as we all learn in Math 3 and Physics! This representation is called state space and next time we will  see how we generate this state space from any given damn system.

State Space should be your friend and  you should be comfortable in playing with it!

Sunday, March 3, 2013

A Blend of Theory and Practice: A Control Systems Example


My last article states the importance of having a blend of theory and practical exposure in your engineering education. I would like to give you an example of how actually you can go about developing it.

We all have control systems as one of the compulsory courses that we need to pass before we get an engineering degree.  It’s a very important course.  Be it any system in any field, you will, at some point, have to control it to achieve certain objectives. The course that we study during our undergrad spans the following aspects of control systems
  1. What is a system and representing a system in a block diagram.
  2. Mathematical representations of any system by transfer functions and state space (If your professor does not teach state space, go ahead and learn it by yourself. It’s super important in industry). You lean about deriving transfer functions on paper. 
  3. Time and frequency response of that system. Comparison of such responses with higher order systems (You learn big mathematical formulae and derivations to represent different parameters like settling time, overshoot, gain frequency, phase margin etc etc). 
  4. Root Locus, Bode plots, Nyquist plots. You learn all the rules and learn how to sketch these on a graph paper by following the rules.
  5. PID Controller design: You learn about the structure of PID controller and if there is sufficient time in the semester you will learn how to implement it on paper.

So, after giving your final exam, you are proficient in drawing block diagrams, root loci, bode plots on paper, deriving settling time, overshoot numbers given a particular system, changing a system from transfer function form to state space form etc.

This is important to understand the math behind controls. Is it sufficient for the industry?  What else should you do??

  1. Software Implementation: You can do each and every thing mentioned above in MATLAB. When you learn a certain concept, say root locus, in the class, try to plot the same locus using MATLAB. Try to plot a step response and see if it matches to what you sketched in the class. Analyze the system properties (settling time, overshoot etc) by interactively moving the poles and zeros and see the effect. You will have derived relations between the system properties and pole positions in the class. Try to replicate them in MATLAB. 
  2. Take a transfer function change it into state space and vice versa using MATLAB. Design a PID controller and see how the system properties are affected when you change the proportional, integral and derivative gains. 
  3. Get comfortable in plotting and analyzing root loci and bode plots using SISOTOOL in MATLAB.

This skill set with develop an understanding of quickly analyzing the system and coming up with a set of controllers that will satisfy your requirements on settling time, overshoot, steady state errors etc.

Hardware Implementation: If more interested you can also use micro controllers to actually see your controller in action on say a DC motor. Implement DC motor speed control. It’s a classic example to try out and is explained in every single control’s book.

Why I am telling this?
In industry no one will ask you to derive expressions, or ask you a proof of how you did what you did. They will want to see results, see your controllers in action. And 99% of the time it’s about designing a PID controller and tweaking the three gains to achieve the system performance.
All you will do ità Model the system in MATLAB, design a PID controller, tweak the gains and check for a) stability, b) system properties (settling time, overshot etc) and c) tracking (steady state error) and d) robustness. So be sure before you call it a day for control systems, you are comfortable in the above mentioned aspects of the system and controller.

To sum up: Be sure to go one step ahead than a textbook oriented course and get familiar with MATLAB tools (tf(), ss(), pid(), rlocus(), sisotool(), bode() etc commands) to be able to use MATLAB to do what you would otherwise spend time on doing by hand. This one subject will then open arenas like controls, mechatronics, robotics, instrumentation, modeling & development and much more which you can focus if you go for higher studies.

Good Books you can refer for the basics:
Feedback Control Systems by Franklin Powell


Hope these links help you.

(If you are interested in controls/ mechatronics I have designed few projects that I can share with you. Let me know via email at bits2world@gmail.com and I will email them to you)