 So, now we will see in the second part of the lecture today, the fundamentals for implementation of closed-loop control, this will need for this practical kind of a part in the assignment and we will talk about more, it is very very important topic for practical implementation with your microcontroller. So, please go through this lecture meticulously to understand like lot of this fundamental conceptual details, this is more of a philosophical level and though that you will be able to kind of use these ideas and things that are talked about for actual making sure your motor runs in the closed-loop the way we decided to happen. So, let us get into all details here. So, this fundamental principle here for any automatic control system which we are talking with respect to the example of motor for our case, there are these following three tasks that happen continuously. One is reading of the sensors, so it is not potentiometer, it is encoder in our case. So, please update that this is encoder in our case not a potentiometer. So, we read a sensor which is encoder in our case, then compute control law which is maybe PEPD kind of a control and that we have seen in the feedback concept of feedback and or maybe you might have seen some of these control algorithms in your automatic control kind of a courses previously and then we need to give this control input to the actuator. So, how do you give this? We give this via PWM interface specifically we keep on changing the duty cycle depending upon what is the computed control input in every step of the sampling. So, this is running continuously every periodic kind of a time. So, say so that is called like sampling time and how do you set up the sampling time which will see that sampling time is set up by using interrupt timer module. So, typically all microcontrollers will have this interrupt timer programmable interrupt timer module which will allow you to set up this interrupt service routine. So, which will run every sampling time that you have set in that routine. Now let me explain you this concept what is this interrupt and timer together would work. So, concept of interrupts I am presuming you know just to recap I will give you that you are reading a book and your mom calls and your phone is ringing what you hear the phone and I hope you want to take up that call and what you do is like no you put a bookmark in the book and go pick up the phone of course I am assuming that you want to take up the call and then you reply to the call you finish the call up and then again come back to the same bookmark and like open the book and start reading it exactly the same kind of a concept is there for the interrupts. So, microcontroller is kind of doing some task and on some pin this some signal comes like you know some pulse comes and that pulse this is like a interrupt pin specifically designed for that purpose. So, on the interrupt pin when this pulse comes microcontroller will halt the current task for a moment and it will serve some other task which is defined for that particular pin. So, that that task will be called as interrupt service routine. So, that task will be executed and then again microcontroller will go back to its original point what your main program and start executing from where it has halted previously. Now, these pulses that are coming on this pin if every time this pulse comes like no the microcontroller has to execute some task which is called interrupt service routine and whatever these three operations I have mentioned here this all three operations they will be typically written in the interrupt service routine. So, they will be executed every sampling time now for them to get executed every sampling time this pulse needs to come on the pin at every sampling instances. So, typically these pulses are generated on some kind of a timer module will do that. So, counter timer module will have a job to develop this kind of a pulse every whatever sampling time instance and this pulse which is internally connected you do not need to do any connections nothing in your microcontroller like you know this modern microcontrollers you do not need to do nothing it is software only the software that you need to kind of write because these pins are already internally connected. So, that this pulse will get generated in the timer module and it will go on the interrupt pin and these pulses now are programmed to come regularly in some kind of you know the way you have a square waveform which is periodic these pulses are coming now in a periodic kind of a way and when these pulses are coming every pulse comes you do these three tasks or every pulse comes you execute your interrupt service routine will get executed and you keep on doing this task and how do you kind of program for this part of setting up sampling time or program or interrupt timer module to program to give you some kind of a facility to have something written in the interrupt service routine and getting executed every sampling time. So, you can do that first so that you can set up your sampling time properly and then now you can slowly start putting things in the interrupt service routine of this module. So, for example, if you have written PWM program you need to kind of see what are the proper parts of the PWM program which can go in the main program and what are the parts which should come in the interrupt service routine you can bifurcate for example, you just want PWM duty to be changed here. So, all other commands in the PWM routine should not like you know the other registers should not change. So, all other functionalities should not change. So, only the functionality which is giving you the handle over PWM duty cycle that can come in the interrupt service routine. Like that in you think what will be coming from the encoder interface into your first reading sensor kind of a part. Like that you need to start thinking and programming and you don't need to do any for loop this is very important don't set up any for loop for doing this once you are putting these things in the interrupt service routine you are rest assured that they will run every sampling instance. They will run continuously over and over again you don't need to put any for loop whatsoever for this purpose. We will discuss little more detail why we keep this as a interrupt based sampling time programming as we discuss sampling and other issues in more detail. So, we want every fixed known sampling time for the operation to happen every periodically and this job will be done. Now, so to put all these things together here we are using the encoder position you know feedback coming from the encoder. So, this is the first part here creating sensors in encoder in this case and then like we will use some kind of a proportional or proportional derivative control law. So, this expression typically will be of this kind of a form. So, you will have some kind of a desired position and you have actual position and these things will be this desired position as you see later like we will see what kind of a form we should have for the desired position. So, that we will keep on observing like you know once we have implemented we keep on observing our control is how our control is performing those kind of things we will see. Then how do we apply this to control law to the motor? So, see this control expression may give some negative value depending upon what are the values of this. So, how we when if the value is coming negative how do you see pdm duty is only positive value 0 to 100 then we need to kind of do something to control motor. So, we need to kind of give something to kind of reverse the direction of the motor if the direction here the computation is negative that is what we need to do. So, you know how to do the direction reversal. So, those are the things that we need to implement. So, that is what we are discussing what if this value is negative we need to use the in A in B pins of the driver to get reversed to change the direction of the function. Then again this question is what should be set as a desired position for this job to happen properly. So, because if we if we just put some kind of a constant value in desired position you think what will happen and then we want to observe this happening properly then we need to kind of set these values in the appropriate way. So, these are all the questions that you ponder over and think yourself and see what what solutions come to your mind for this and then you proceed to the next part. So, this pondering over for you is important here. So, now we will see this is a control feedback. So, now if what will happen if we keep the desired position constant then you imagine like know their program is start running in the interrupt service routine you will read the direction or you read the actual position and your desired position is constant and then like the actual position will start getting control to the desired position value in some way and then once it reaches desired position it will stop it will do nothing or it reaches close to the desired position it will stop there only. You start moving it you will find some resistance to move from that position if you externally start moving the motor, but otherwise it will do nothing after that. So, this is not a great way of like you know continuously observing how your control is performed you want to kind of have some kind of a handle over seeing that this control what I am implementing is continuously doing something. So, for that purpose we set up the desired position you have like if it reaches the final position then like after some time first steady state is reached you change it back to 0. So, you make it 0 again and again you kind of like let it come to 0 and again you take it to the desired. So, you can do that continuously. So, otherwise like if you want to observe the control log working multiple times same kind of a control log multiple times you will not be able to do that. So, you can do that by using the desired position. So, now how do you generate this kind of a desired position? Think about some algorithm to generate this. So, given that you have this now to be written in the interrupt service mode routine which is kind of a loop which will run continuously every sampling time. So, think about that suppose is if you want like you know the square waveform to have some certain kind of a time duration that this desired value will be held constant for this time duration and then it is brought to 0 for same time duration next and keep on doing that. So, what kind of algorithm comes to your mind? You need to kind of think about and you need to get used to now writing things in interrupt service routine which is executing every sampling instance. So, if the sampling time is t and this time for your square waveform is say t 1 then you need to kind of see how many number of sampling instance have to pass by before I change this value and like that when you do kind of these are some of the hints I am giving to for you to write this or develop this algorithm for the for the program and this kind of a square waveform you can generate for the desired position. So, this is so this desired position. So, this is not a tracking do not confuse that this is this is becoming tracking node this because we are suddenly changing this position we are not kind of like you know taking this smooth kind of a trajectory here it is a it is a multiple times you are observing the regulation here. Now, another question what should be amplitude of the square wave? So, this now that you can compute based on the encoder counts that you are like you are getting output from the encoder. So, say if you are getting whatever 1000 roughly 1000 counts for one revolution then you can plan I want to move one revolution or two revolution or 90 degree 80 degree like that based on that you can plan this number of counts and you can attach some kind of a small cardboard or plastic wheel to the motor maybe you can make it out of the visiting carts there are a kind of a stiff stuff there to attach and then you can put a put a pointer there or to kind of like know the position. So, like that you can do then what should be frequency? Frequency should be large enough so that you observe the controls. So, your control is happening like that it is controlling and then like no it is becoming steady state there there should be some duration left beyond that and then like no it should go like no change. So, this typically frequency of the square wave should be very very low so that you know you are able to observe your control nicely. So, we can observe the system response visually what should be what will be typical response of the motor if you can sketch on this desired response what is the actual response going to be you can get something of this sort. So, you can look for a way you will get some response of this kind when you view this square wave form and then we want to kind of get this data out of the microcontroller and maybe we should be able to put it in the MATLAB to observe this response this response you can observe by graphing functionality to see in the real time coming on the graph. So, of course we need to initialize some value to be 0 and then proceed. So, you may it may so happen that you know you will find that your response observed is exactly same as the desired position you may not observe any undulations here especially if you are observing for just a speed of the motor no open loop even some kind of a you know it is enough x desired it is like a PWM which is given some duty cycle not really PWM, but a speed sorry this is like a speed that is desired PWM desired will give you like no position continuously going in one direction till the time this PWM is getting to 0. But what I mean is here if you have a constant voltage given to the motor and we are observing this x dot desired then that response is going to be like that till go like a first order kind of a function, but if your inertia is too low then the sampling time that you have chosen might be too large for capturing the initial kind of a you know that typical for first order kind of a response capturing may not happen. So, you may find that okay oh my like you know the waveform for the speed now is coming as a straight. So, then like no you need to decrease the time sampling time to make sure that okay you are able to observe that transient that is going there. These are kind of small tricks that you need to learn as you build your you know practical understanding of the system. Okay, so this typical program you can use this following you know what you say hints to develop on your own. So, you need to initiate configure and enable interfaces all the interfaces that you are needing for the for this pro loop system operation to be working PWM interrupt then encoder QI module and any other variables you want to define you can define them to begin with. Okay, this all will this all can happen either in the main main program or in the in like I said some of the definitions can happen before main program. Okay, when you use the commands like include include something like that. Okay, then interrupt service routine is very important. Okay, we need to do these three operations that I was talking about. So, so in this you need to make sure like no no initiation or configuration kind of a functionalities. Okay, from Tiva should come into this interrupt service routine configuration or enabling kind of interfaces that should not come in the interrupt service routine. It should have just like no for example, from QEI interface, it should just read reading the value of encoder whatever function is there or reading the speed of encoder whatever function is there that can come up here because that is a thing that is be continuously changing all other things are going to be you know for constant for this particular program. Okay, so only the changing parts should come which are changing every interrupt cycle that should come now then you have to compute control by using the feedback law we have seen some law of proportion law or something like that and implement now implementation aspects will involve a couple of things here. Okay, then you need to be sensitive to say for first you know make sure that the variable definitions are appropriate. Okay, have you signed and unsigned integers or floats or whatever like these definitions are made in a way that is conducive say for example if you define for the control input the unsigned integer then like your negative part of the program control will not work at all. Okay, so those kind of things we should not be you should be careful about then control input should not exceed some maximum value in any case. Okay, so put the value with some kind of a limiting saturation function and then third part is like if computer control computer is negative then how do you kind of handle that what does mean it means that okay I need to reverse the direction of the motor with some same like so you will make it positive before applying to PWM duty. So PWM duty is like mod part of the control input command and the sign part like the positive or negative will be used to change the direction things. Okay, this is a very important kind of a baseline for like control implementation to happen. Okay, these two points are coming you can maybe kind of put some kind of a small function to make sure that this will happen before control is implemented. Okay, then you need to update any variables. Okay, so previous position needs to be updated for derivative computation and things like that or desired trajectory need to be updated now. Okay, so that that that functions will come here or they can come at the start. Okay, somewhere they should come in terms of this. Okay, so how do you compute derivative control action? Okay, do you know this difference difference formula, you can use that and do that. Okay, so it's very straightforward kind of a thing. Only thing with the question is how do you get this previous, how do you know what is previous X position? Okay, position X. I know current position but how do I know previous position? Since we don't in the interrupt service routine when we are working with, then we need to store the previous values. Okay, so we store the current values which will be available as say X previous for the for the next sampling instance to come. Okay, so that's how we store the previous value every, you know, in every sampling instance. Okay, so we update these values to kind of make sure our previous values are available. As I said here, you know, update any variables. Okay, so the variables need to be getting updated. So X previous will need to get updated to current X current to make sure that okay, it is now the current value will become previous value for the next sampling instance. That is a kind of a thinking philosophy that one should use. So if you want one or two or three previous values, you can do that by the same way. Okay, and so this is some additional tasks you can think about. Say if you want to generate sinusoidal waveform as a desired waveform instead of the square waveform that I have, like we have seen here, how do you kind of do it and implement in the interrupt service routine? Okay, so this is algorithm one can think about. Okay, so this is just a extra task that you can think about. Okay, so I think this is good enough kind of a primer for you to kind of get started and like implement practically any kind of control on your microcontroller. And then we'll have an interesting kind of things to talk about with the practical resource that you get of such implementation. So we will be able to talk about in the assignments as we progress. Okay, so maybe we'll stop here for now and then we'll have some more part to the lecture coming up. Okay.