Video length is 19:32

What Is Sliding Mode Control?

From the series: Robust Control

Brian Douglas, Mathworks

Sliding mode control is a nonlinear control law that has a few nice properties, such as robustness to uncertainties and disturbances, and is relatively straightforward to implement and tune. However, one drawback is chattering where the system state, and therefore, the actuators trying to maintain the state, jitter back and forth quickly, which might not be desirable. This tech talk covers mitigations for this chattering. Overall, sliding mode control is worth understanding because it is a really interesting controller, and you might find that it’s right for your application.

This tech talk is divided into three parts. In the first part, be walked through a graphical explanation of the controller to try to get some intuition into how it works. For the second part, see a derivation of it so you understand the mathematics. And in the third part, you’ll see an example of a controller in MATLAB® and Simulink®.

To learn more, see this MATLAB example for sliding mode control design for a mass-spring-damper system: Sliding Mode Control Design for Mass-Spring-Damper System

Published: 2 Sep 2024

Sliding mode control is a nonlinear control law that has a few nice properties like robustness to uncertainties and disturbances and is relatively straightforward to implement and tune. However, one drawback is chattering where the system state, and therefore, the actuators trying to maintain the state, jitter back and forth quickly which might not be desirable. But there are mitigations for this chattering. Overall I think sliding mode control is worth understanding because for one is kind of neat controller, and two you might find that it’s right for your application.

So, this video is divided into three parts. In the first part, we’ll walk through a graphical explanation of the controller to try to get some intuition into how it works. For the second part, we’ll walk through a derivation of it so you understand the mathematics. And in the third part, we’ll show a quick example in MATLAB and Simulink. I think it’ll be really interesting and informative, so I hope you stick around. I’m Brian and welcome to a MATLAB Tech Talk.

To build up some intuition into what sliding mode control is and how it works, we’re going to look at an extremely simple system, just a mass and a spring. Let’s impart some initial velocity onto the mass and get the system moving. As you would expect with a mass spring system, it’s just going to oscillate forever because there is no damping so there's no energy being lost.

When we discuss sliding mode control for 2nd order systems like this one, it’s helpful to visualize the dynamics with a phase plane diagram.

In a phase plane diagram the two states are plotted on two orthogonal axes and for this system they represent position and velocity.

So, if I pause the system, at this moment the position is 0 and the velocity is a large positive value. So, the current state of the system is right here. Now, as we move forward in time, the velocity goes down and the position increases to the right. Then the velocity becomes negative, and the mass moves back. This eventually completes a circle in the phase plane, and like we said earlier, since there is no damping, we’ll just go around this circle forever. This path that is traced out is called a trajectory.

Now, one way to change this trajectory is by changing the system itself. Here I’ll add some damping and give the system the same initial velocity. Once again, the mass moves to the right and the velocity reduces, however, due to damping, the system follows a trajectory that spirals down, until the system comes to rest as the 0 position, 0 velocity state.

Ok let’s just look at the phase plane diagram.  Instead of looking at an individual trajectory, we can plot arrows representing the direction of the entire phase plane field. So, hopefully, with these arrows you can visualize the trajectory for any arbitrary starting state. For example, if we start the system here, you can see how the trajectory will follow the arrows and once again come to rest at the origin.

This is the uncontrolled behavior of the system - you know just it’s natural response. But we can also change this by adding a feedback controller. Feedback changes the dynamics of the system and therefore changes the shape of the trajectories - preferably to something that is desired.

So with that being said, let’s talk about sliding mode control and specifically how it affects the trajectories in a phase plane.    

The mass-spring-damper system is second order, which means that in general the trajectories can bend and circle around in two axes across the entire phase plane - like we see here. Sliding mode control tries to constrain the trajectories so that they can only exist along a slice within the plane. That’s this yellow line here.

Now the question might be why could we possibly want that kind of constrained behavior?  Well, one reason is that the controlled system will now behave like a simple first order linear system.

And we can see why that’s the case. The equation for this yellow line can be written as b * x1 + x2 = 0.  Where x1 is position and x2 is velocity, and b is the parameter that adjusts the slope of the line. Since x2 is x1 dot, we can rewrite this equation as x1 dot equals -b X1. This is a first order system. So, if the sliding mode controller ensures that the trajectories stay along this line, then our 2nd order system will behave exactly like this first order system.  We can choose the orientation of this yellow line, to describe the 1st order response that we’re looking for.

This equation is the called the switching function, S, and when we set S to 0, we get the sliding surface, which is the yellow line itself. It’s called that because the states slide along that line.

Now, I want to be clear that often when you see sliding mode control described, it’s done with second order systems and a phase plane diagram like I’m showing here, but this is only because second order systems are easy to visualize and draw. In general, we could have a higher order system and still describe a lower order switching function that cuts through a cross section of the original system.

The switching function can be any function of system states, even nonlinear, however, it’s most common to just set it to a linear combination of states. Where the number rows of the matrix C is the number of states in the system, and the number of columns is the number of inputs, U, in the system. So for our mass spring damper, C would be 2 by 1.

Ok, so we have our sliding surface and the first job of the controller should be to keep the states on that line - that’s priority number one. However, obviously, the system could physically be in any state at any time. There’s no guarantee that the system states are initialized along this surface.

Therefore, the controller has another job. It has to make sure that the system can reach the sliding surface.  So for example, for any state above the yellow line, the controller needs to adjust the phase plane in such a way that all of those arrows push down towards the surface, and for any state below the yellow line, the controller has to drive the states up to the surface.

In this way, no matter where the system starts, the sliding mode controller will first drive the states to the sliding surface - during which the system will behave like a 2nd order system, and then once it gets to the surface it will slide along it and behave like a 1st order system.

Now it might not be obvious at this point why this is actually a robust control strategy. But with this diagram we can start to understand it. Let’s zoom into the phase plane right at the sliding surface and let’s look at these arrows that are pointing in towards the yellow line.

From their starting point, both blue lines are pointing in the correct direction just like we want. But let’s say that there’s some external disturbance that we didn’t account for. That disturbance is going to have some delta contribution to the existing phase plane which I’m showing here in orange. And the combined effect will be this green arrow. So, even with this disturbance, the sliding mode controller still causes the states on either side to tend toward the switching surface. And this is true no matter which direction the disturbance is in.

Now, if the disturbance is larger than the controller can handle, then the states might not reach the sliding surface. In this case, if you know the disturbances are going to be this large, you can increase the gain in the controller, which has the effect or lengthening the blue line, and now the combined result points in the correct direction again.

Effectively, what we’re doing here is using very high gain and switching back and forth between two different controllers to make sure the system stays along the sliding surface. And with that high gain, we get the benefit and not being susceptible to system uncertainties and external disturbances because the high gains will just over power them. Which is good for robustness.

However, the down side of this switching high gain approach is that we introduce chattering into the system. Chattering is when the system states bounce back and forth on either side of the switching line and it occurs because of the finite time between switching. With high gain, this means that for one time step, the system is really driving the state hard in one direction, which jumps it over the line and then the very next time step it drives it hard in the other. And delay in the system only exacerbates this chattering.

Now, if we lower the gain, the controller drives the system less hard around this sliding surface, which will reduce chattering but as we saw earlier, this affects the robustness of the system. So there is that tradeoff between the two. And since robustness is important, we want to find other ways to address chattering.

And one way to do this is to define a boundary layer near the surface, within which you change how the system switches between the two different controllers. For example, one type of boundary layer option is saturation, where outside of the boundary layer you use full gain but inside, the reaching law gain is proportional to the distance to the surface. In this way, as long as the system stays within the boundary layer the control is smooth and chattering is reduced, and it’s only when the state nears the edge of the boundary layer does that heavy gain really come into effect to push the state back.

This is called saturation, because graphically it looks like the symbol for saturation. Where outside of the boundary, the gain is capped at the gain of the controller, whereas inside of the boundary it’s scaled linearly. However, saturation is just one boundary layer option of many that are out there. And so the point I want to make is that there are methods for reducing the chattering of a sliding mode controller without impacting robustness too much.

Ok, so that is the sort of a graphical introduction to what sliding model control is doing. Now to design one, there are several things we need to consider. One, we need to define a sliding surface. This represents the reduced order dynamics that we want for our system. Two, we need to come up with a control scheme that ensures that no matter where the system starts, it’s always being pushed toward the sliding surface. And three, we need to come up with a method for dealing with chattering that is acceptable for the hardware and the system requirements that you’re working with.

So, with all that being said, let’s move on to looking at the actual equations for sliding mode control - and try to figure out exactly how we accomplish all of this.

Ok, let’s consider the mass spring damper system from earlier which has this differential equation.

We can define the state variables, x1 and x2, and rearrange this into state space representation. This is now in the form, x dot = some state function f + some input function g *u which a general form that can be applied to many systems - even nonlinear systems.  So, for this derivation, we’re going to use this equation for the system model. Alright, let’s design a sliding mode controller for it.

To begin, we need to define a switching function, S. recall from earlier that I said that the switching surface is a linear combination of all of the states. So, S equals C transpose times x.

When S=0, this defines the sliding surface. And if s is not zero, this means that the system states are not on the sliding surface - it could be anywhere else in the phase plane. And remember, we want our controller to ensure that the system states always reach that line. So how can we do that?

Well, let’s think about it this way, if S is positive, then we want S dot to be negative. And if S is negative we want to S dot to be positive. If that is the case, then no matter where the states are they will always be moving toward S=0. This means we can define the reachability condition that S times S dot must be less than 0. Or another way of putting is that the sign of S must always be the opposite of S dot. Pretty simple idea.

Ok, so this is the reachability condition that our controller needs to satisfy. So, let’s talk about how we do that. We know S, it’s C transpose X, so to meet this condition we need to define S dot so that it’s the opposite sign. The easiest way to do that is to just say that S dot is the negative sign of S. Now we’re guaranteed that the reachability condition is met. And if we want to adjust the speed of S dot, that is the speed with which the states approach S=0, then we can add a constant, Eta. Eta is that gain that changes the length of the arrows in the phase plane and is responsible for the tradeoff between robustness and chattering that we talked about.

Alright, so this particular equation is called the constant rate reaching law. Another popular one is the exponential reaching law.  But there are many many different kinds of reaching laws. Basically, anything that satisfies s*s dot < 0 will work as a reaching law. Choosing one or another comes down to how you want the system to reach the switching surface, how robust you want it to be, and what kind of chattering can you handle. Alright, instead of picking a specific one for this derivation, I’m going to set it to the function h.

Therefore, s dot needs to equal the reaching law H. But s dot also has to equal the time rate of change of s - you know the switching surface we defined. If we take the derivative of s, we get the partial derivative of s with respect to x times the derivative of x with respect to t. Well, the partial of S with respect to x is C transpose, and dx/dt is just x dot and we know what that is from the dynamics of the system. So, putting this all together, we get C transpose times the dynamics of the system has to equal the reaching law H.

Now we just have to ask, what is the u that makes this equation true? Which we can find by solving for u. And this is the standard sliding mode control compensator formula.

And here’s what I think is interesting about it. This one control law defines the switching surface, C transpose, and because of the reaching law, h, ensures that the system states reach that surface from any direction. Pretty cool right! Something to note here is that in addition to choosing H and C, you also have to have an estimate of your system dynamics, that’s the F and G here. But the nice thing is that since this is a robust controller, it can handle some uncertainty in the system model, you know because it’s just going to overpower it with high gain.

Now the third step is to address chattering but it’s taken care of also within the reaching law. Instead of using the sign function, which switches harshly between -1 and 1, we can replace it with a new function theta. Now, for theta, we define a boundary layer with phi being the distance the boundary is from the switching line.  If S is outside of the boundary layer, then theta is just 1 or -1 like we had with the sign function. But if S is inside the boundary layer, then Phi is proportional to the distance to the switch line. So, our new reaching law would be this bit of logic, times - eta. Again this particular one is called saturation, but there numerous different boundary layer approaches.

Alright, so that’s sliding mode control. Let’s now jump over to MATLAB and Simulink and I’ll show you a quick example of controlling a mass spring damper system.

I have a mass-spring-damper subsystem which takes in input u which is a force acting on the mass, and the output are the two states, position and velocity. And if I just run the model now, we can see the uncontrolled response of this system. Notice that is takes well over 10 seconds for this system to settle.

So, let’s fix that by using the sliding mode controller block in Simulink. If I click on the block I can set the sliding surface and the reaching law. Now, I’m going to keep this in regulation mode which drives the states to zero, but there is the option of tracking mode where you define a reference and the controller drives the error to zero. Alright, I’m going to set the sliding coefficient matrix, C, to 1 1 which means S equals x1 plus x2.

And then on the reaching law tab, I’m going to keep the constant rate reaching law set the gain eta to 1. And for now, I’ll keep the boundary layer as just a simple sign function. Ok, this controller needs the system states, as well as the g and f functions. Here, F is the  A matrix which is a function of the spring mass and tamer parameters, times the state vector. And with that, we’re set.

Now I’ll run this again and you can see the difference in the output. At first the control input looks smooth, and this is because the system is in the process of reaching the sliding surface, where it behaves like a 2nd order system. And then notice, at around 2 seconds, it reaches the surface and begins to slide along it. At this point, the response looks like a first order system. But as you can see, the downside is that there is a huge amount of chattering on the control input.

So, to address this, we can go back to our controller and change the boundary layer from sign to saturation. And I’ll leave the boundary layer, phi, at 0.01. I’ll rerun the simulation again, and check this out. The response looks pretty much the same, but we’ve reduced the chattering in the system a great deal which is fantastic.

Now, this was a quick overview of using sliding mode control in Simulink. I’ve left a link to an mass-spring-damper example that goes into more detail, including showing the how this controller is robust to disturbances. There is also a link to an example showing a more practical application of controlling a robotic arm.  I hope you check them out and I hope that you found this explanation useful.

Alright, don’t forget to subscribe to this channel so you don’t miss any future MATLAB Tech Talks. Also you can find all of the Tech Talk videos, across many different topics, nicely organized at mathworks.com.  And if you liked this video then you might be interested to learn about other robust controllers like H infinity and Mu synthesis. Alright, thanks for watching, and I’ll see you next time.