Introduction to Simscape for Modeling Multidomain Physical Systems
Overview
In this session you will learn the basics of simulating physical systems in Simscape. The physical network approach in Simscape enables you to model physical systems by simply assembling a schematic of physical components. You can tailor level of model fidelity to match your engineering task, which makes your development process more efficient.
Highlights
- Modeling running robot with electric, mechanical, and multibody components
- Estimating motor parameters
- Target applications for Simscape
- How to get started modeling physical systems
About the Presenters
Karanjodh Singh Meen has a MS in Mechanical Engineering from Arizona State University and BE form Punjab Engineering College, India. He specializes in the area of control systems, modeling of physical systems and robotics. He has experience working with drones and Automobiles. In 2018, he joined Technical Support at MathWorks and later moved to the Application Engineering Group in 2019.
Ed Marquez is a Simulink Product Manager at MathWorks. He supports automated report generation and Model-Based Design applications. Ed is experienced in automotive system modeling and controls. He holds a MS and BS in Mechanical Engineering from Virginia Tech.
Recorded: 11 Nov 2022
Hello, everyone. Thank you for joining us for this introductory session on Simscape for modeling multidomain systems. Let's imagine for a moment that your task is to build this robot. As you design and engineer a system like this one, wouldn't it be great if you could understand and analyze how the robot behaves before building any physical prototypes? And how do you model the behavior and interactions of the electrical, mechanical, and multibody components to ensure the robot works as you expect? In this session, we'll show you the model of a walking robot. And you will see that modeling and simulation with Simscape can help us answer these questions.
My name is Ed Marquez. I'm a product manager for Simulink and model-based design at MathWorks. And today, Karan is with me. Hey, Karan.
Hello, everyone. My name is Karanjodh Singh. And I'm the application engineer at MathWorks. And I also focus on Simulink and model-based design workflow. So today, we will look at the example of this four-legged robot. And you can see this four-legged robot visualized on the left-hand side. And on the right-hand side, we have the model associated with this robot. So let's go ahead and run the simulation.
Now, the reason why we chose this particular example to highlight the tool is because this robot has components from multiple domains like electric batteries, electric actuators, mechanical linkages, thermal effects, and so on. And this example shows how you can basically put these components together and perform a system-level simulation of these multidomain systems.
So when you run a simulation like this, not only do you get a nice visualization of what's going on with the robot, we are collecting and analyzing some data associated with this robot on the right-hand side. So the data that you see on the top graph represents the torque output to the legs of the robot. And we are recording the torque at two of the legs. And at the bottom graph, you can see the power consumption of the robot.
Now, before we dive deeper into this example, let's look at the agenda for today. So today, we will start by answering the question, what is Simscape? After that, we will look at example models and how you can get started with Simscape. After that, we will share a little bit about different applications that Simscape is frequently used for. And finally, we will wrap up the session by talking about some resources that you guys can use to either learn about the tool on your own or reach out to us and engage more deeply about your applications. So Ed, why don't you tell us, what is Simscape, and where does it fit in the MathWorks product family?
Most definitely, Karan. Simscape is the platform that makes physical modeling easy in MATLAB and Simulink. And with Simscape, you create models of multidomain physical systems. So what that means is that your models incorporate aspects from electrical, mechanical, magnetic, thermal, and other domains.
And Simscape models are based on physical connections. So you don't need to derive any equations because Simscape takes care of that for you. And since you work with physical connections, it's easy to build and simulate models that look just like a schematic. And so here are some of the things that you can do with Simscape.
It helps you refine requirements for your systems, design control systems, optimize system-level performance, and test your embedded software without hardware prototypes. And examples of systems you can model include things like electric motors, mechanical systems with gears and transmissions, hydraulic actuators. And you can even create your own custom components, domains, and libraries with the Simscape language, which is MATLAB-based.
You also asked about, where does Simscape fit with MATLAB and Simulink? And to help you understand that, I want to also mention those two platforms. Because MATLAB is a programming environment that supports your algorithm development, data analysis, and numeric computations. And Simulink, which is the other platform, is a graphical environment that helps you design, simulate, and test dynamic systems before moving to handcoding or to hardware prototypes. And we also have more than 100 add-ons you can use on top of MATLAB and Simulink for more specialized tasks. So as we said before, Simscape is not just an add-on. It is the platform for physical modeling in MATLAB and Simulink
Cool. So Ed, can you show us how to build a simple Simscape model, maybe show us how to build a simple pendulum so that we get an idea of the tool?
Sure thing. Now let's jump into the demo portion of the session. So let's start with that pendulum model that you're referring to. So to open a simple Simscape model, we can do so from the MATLAB toolstrip and the Simulink icon. Or I can simply type the command smnew, which will give me a template model with multibody components in it.
And what you see here is I get some annotations and resources to help me get started. I wouldn't need those for now. And I also get some starting blocks. And I'll be deleting the ones that we won't be using today.
I also said that in Simscape, you build systems that can look a lot like the system that you're trying to model. So I can insert an image of a pendulum. And I can visualize that right next to the model as I build it. So I can now rotate these blocks. And I can do that in the toolstrip or with the keyboard, hitting Control-R.
And the first thing I want to do is start modifying this brick solid. So the first thing I'll do is I'll make sure that it's 10 centimeters in the Y and Z directions. And I can apply that. And hit F5 to update the visualization.
Now, I also want to change the viewing convention. I want the y-axis to be pointing up. So by changing that and now updating the angle view, I can confirm that the y-axis is pointing up. Next, let's update the frames or configure the frames for this brick solid. And I can do that by selecting geometric features on the body.
So I'll specify this first frame based on that selection. And I'll call this frame B because that is the base of my pendulum. I'll add another frame on the opposite side. And I'll call this frame E, as that's the end of my pendulum. So now I can select that feature as well, save that. And I can remove, now, the port R, or that reference port since we won't need it anymore.
So I can apply these changes. And you see that now my brick solid has those frames that we specified. So I'll rotate it so the base is pointing towards the world frame. And I make the connection here. So now if I update the model with Control-D on the keyboard, I get an animation of the model that I'm building.
So I can see the parts here in the Mechanics Explorer. Here is the world frame. And here's the brick solid with the two frames that we specified. So now I also want to change the viewing convention in the Mechanics Explorer. And I want the y-axis to be upward. I can also update the view. And then I'm confirming that those changes are made.
OK. The next thing that we need to do is we need to introduce a revolute joint so that my pendulum can rotate around that fixing point. So if I introduce this joins block, here, I see the two attachment points, which are the base frame and the forward frame. So I can rotate it and connect it.
And what you will see here is that if I run the model, it will not rotate. And that is because the last thing we need to do is to specify gravity to act in the negative-y direction. And we do that in this mechanism configuration block. So you see that gravity is specified as an XYZ vector. And right now, it's acting in the negative-z direction. So I'll change that to y. And now if I simulate the model, there, you see that we have our simple pendulum.
So Ed, I noticed that when you were constructing the model, you added the blocks using Quick Insert. Can you show us how you can add blocks using the Library Browser? And where do Simscape libraries exist?
Sure thing. If I go to the Simulink toolstrip and open the library browser, I can see all the blocks that I have available. And you see all the blocks that are available for Simscape and multibody. And you see here I have blocks for joints, for forces and torques, belts and cables, body elements, and so on.
So Ed, can you show us, how can we extend the simple pendulum to, maybe, a more complex, something like a double-pendulum?
Yes. Before, I mentioned that with Simscape, you don't have to drive system equations. It does it for you. So fortunately, to extend this pendulum, all I have to do is Copy-Paste what I've already built. And perhaps let's customize this before we simulate it. And I can change this to be blue so that we can differentiate it more easily in the animation.
So now if I run the model, there, you see I have my double-pendulum. And I can even extend that to be, maybe, a four-member pendulum by simply copy-pasting all of these pieces. And again, Simscape derives all of those equations for me.
So then if I simulate it, there I have my four-member pendulum. And I don't even want to imagine trying to derive the equations for this type of system. But for now, I'll stick to the double-pendulum since there are enough applications that I can think of for that type of system. So Karan, to go from this double-pendulum to that walking robot, we need to actuate these joints and determine how much torque is needed. How would I do that with Simscape?
So to actuate this double-pendulum and convert it to a robotic leg, let's use an electric motor to do that. So we are going to use this particular model to simulate an electric motor. And in this, we simply have an etched bridge and some input voltage commands coming in. And the third block is the DC electric motor.
Now, this DC electric motor block exists in the Simscape Electrical Library, which is a sublibrary of the Simscape. And this electric library provides you with a lot of blocks specifically for modeling electric systems like transformers, Brushless DC motors, and so on.
Great. So I can do the motor model and other electric components with Simscape. But if I modeling a motor, I would need to know that the model is realistic or useful. And if I compare simulation results to measure data, what can I do if these two things don't match, maybe because my motor parameters are off?
So that's a good question. So in this DC motor, if I double-click on the block, you can see there are a lot of parameters associated with it, like armature resistance, inductance, vacuum of constant, and so on. So right now, I've just pretty much guessed on the values of this constants. And if you actually simulate the system and compare the results of the simulation with experimental data, you can see there's a huge discrepancy between simulation and experimental data.
So simulation in this visualization is the yellow line. And the experimental data is represented by the purple line. So in order to get a better estimate of those parameters, we are going to use the Parameter Estimation tool. So basically, what happens in this Parameter Estimation tool is that we tell this app that this is the experimental data that we are trying to match. And on the top graph, it represents the measured velocity of the model. The bottom graph represents the input voltage applied to the motor.
And then we tell it, OK, these are the few parameters that we want to optimize. Right now, we are optimizing the proportionality constant and the resistance. And after that, all you have to do is click the Estimate button. And MATLAB automatically constructs a cost function in the background. And it will iteratively run the model and try to minimize the cost function, which is basically trying to minimize the error between measured data and the simulated data.
Karan, so did you just mention that this is an optimization problem and it's an iterative process? So I would imagine that this can take quite some time. Is there any way to speed up the process?
Yes. So Simulink offers a bunch of different ways that you can speed up the simulations. So one way to do that is by using Fast Restart. Or you can change the mode of the simulation. Or you can go back to the Parameter Estimation app. And in this app, from More Options, you can actually select the Parallel Options. And this is really useful when you have multiple cores available on your machine and you can run simulations in parallel.
So for this particular experiment, I know that the Fast Restart method is going to be sufficient to speed up the simulation. So let's go ahead and start the estimation process. So now, as the estimation starts, you can see it's running the Simulink model in the background. And it's running so fast that you don't even see the simulation bar showing you the progress of simulation.
And after a few iterations, you will be able to see that my simulated and measured results are starting to get pretty close. And once it's done with the estimating, it will automatically update those parameters in the model. So now, once it's done, I can go back here, run the simulation again. Now this model has the updated parameters. And you can verify those results.
So now it seems like my motor is behaving a lot more as expected. So finally, our goal is to take this electric motor and use that as an actuator for the double-pendulum so that we can convert it to a robotic leg. So now let's jump back into our main model that we saw in the beginning and see how we have implemented that in here.
So this is the model of the robot using Simscape. But let me go one level up to show that not only we are modeling the plant in this particular model, which represents the physical prototype. In addition to that, we have a model of the controller. So today, our focus of the conversation is basically on the plant model. But from this visualization, you can see that we are using that plant model to simulate, and test, and verify our controller design. And that is one of the main use case of Simscape.
So now, let's go inside the plant model and try to understand what's going on in here. So if you see, at the bottom, on the left-hand side, you have the power supply. And then the power supply, this is just basically a battery circuit with some measurements. And from here, the power supply-- if you follow the blue line, which represents the electrical connection, the power is supplied to each one of those legs.
And the robot itself is split into the body of the robot and four legs. Now, the four legs are pretty much identical to each other. So we can just go ahead and analyze just one of them. And if you actually look at the model of the leg, it's quite similar to the model of the double-pendulum. Just because it has similar two length, the upper leg and the lower leg. And in between those two lengths, we have an actuator called the knee actuator. And on the top, we have the hip actuator. Now, this hip actuator is the electric motor that we modeled earlier.
Karan, so I see that you have a controller subsystem here. And there was also a controller at the top level of the model. What is the difference between the two?
So in this particular model, we are using two levels of controllers. So the inner controller, the one that you're looking at right now, is basically a PID controller, which controls the electric motor and tells the electric motor how to go from position A to position B.
But the controller that we saw earlier decides the gate and the mode of operation of the motor, and how to coordinate the four different legs of the robot. So let's go ahead and run a simulation of this robot again. So one good thing about when you use multibody is that you get a nice visualization of the robot. And you can actually see if it's behaving as expected or not.
I see the geometries that we're using here. They're somewhat basic geometries. But what if I have something more sophisticated, perhaps a part that I built on CAD software? Can I import that into Simscape?
Yes. So let's say you're working and designing some mechanical components. And you end up designing those mechanical components in some CAD package. Now, Simscape multibody allows you to directly import those parts that you designed by using a simple block. And that's what I'm going to do right now. I'm going to replace this body of the robot with something that I designed using a CAD package.
I'm going to use the File Solid block. And in this file, all I need to do is specify the name of the file that I'm using. Or I can just browse to it. I'm using the body_SW. Once it's here, I can update the visualization by F5.
Let me zoom out a little bit. OK. So now I'm going to go ahead and define the two frames of reference where I'm going to connect this part to the rest of the robot. And this is very similar to what Ed did in the beginning with the double-pendulum. Take this, I'll just frame my base frame, use the Select a Feature, save, and another one on the opposite end.
Select a Feature. And I'm going to call it F2. And that should be good. So once I've defined those two frames, you can see two additional ports pop up on the block. And I can just go ahead and rotate the block.
Let me go ahead and match the size of the block with the existing one. This way, it makes it easier for me to replace this one with the file that we imported from the CAD package. I'll hit Control-D to update the model. And we can rerun the simulation. And you'll see that the simulation runs with the modified body.
And as you can see, the motion of the robot also changed a little bit. This is because when you import a CAD part like that from a CAD package, it automatically imports the material properties, the inertias, the mass, and everything from that file. And you don't need to worry about manually inputting those values in there.
Very good. Karan, another question is, when you gave us the overview of the power supply portion, an important consideration can be the thermal effects or maybe connecting to a thermal network for the electrical components. Is there a way to do that with Simscape?
Yes. So right now, we are not modeling any thermal effects of this robot. But let's say you want to model the thermal effects of the battery system. So you can simply just select the battery, go to this Additional tab, Simscape Block tab. And go to Block Choices. And it'll ask to show the thermal port. And if you noticed there, another port part pops up. And this is a thermal port which you can use to connect this and model your thermal circuit around the system.
So in order to summarize, we use Simscape to not only model and simulate these individual components-- like double-pendulum, electric motors-- we use it to put these components together and run system-level simulation to understand the behavior of these complex multidomain models. In addition to that, we use the Simscape models essentially as a prototype to validate our controller design, to test out and simulate our controller design.
And finally, one point I want to mention is that when you have models like these built up, you can easily do all sorts of what-if analysis. What if you want to use a different kind of a motor? What if you want to use a different kind of a battery which has some different specifications? What if you want to change the length of the robotic legs and so on? And this tool really makes it easier for you to do all those kind of analyses.
OK. So that wraps up the third demo. And this link here brings us to where you can find this demo in File Exchange in MATLAB Central. There, you see a description, and more details for this demo, and all the releases that it's available for. And you can also search, in File Exchange, other resources for Simscape. There's plenty of material available for you on this File Exchange. So with that, Karan, can you tell us more about other applications where Simscape is used?
All right. So today, we saw one application of Simscape. That is how you can design and simulate robots in the tool. In addition to that, Simscape is used for a lot of other applications like renewable energy, hybrid electric vehicles, drivelines, microgrids, and so on. If you're interested in finding about these applications, feel free to visit our website, where you can either navigate through these different example models in our documentation, or you can read about user stories, where different industries have used our tools to successfully model their applications.
And we also have a user-driven community like Ed showed you earlier. This user-driven community is MATLAB Central. And here, a lot of users from around the world in different professions, in different backgrounds come and share their experience using the tool to share the projects that they have been working on. And you can explore these existing projects here.
So in addition to that, we have training services. And the goal of training services is basically to help you ramp up on the tool as fast as you can so that you can more efficiently use it for your projects. In addition to that, we have a lot of free on-ramps, like Simulink, Stateflow, MATLAB, and so on. And finally, we have consulting services as well.
And when you engage with our consulting services, they will help you complete your project in a timely manner. And at the same time, they will make sure that you are proficient with the tool so that, at the end of the project, you don't need our help anymore. So with that, Ed, can you wrap up the session and summarize it?
Yes. So we saw today that Simscape is the platform that makes it easy to create models of multidomain physical systems in the MATLAB and Simulink environment. We also saw that you can use Simscape models to explore the design of physical systems. You can also use it to support your controller development and more. And Simscape is widely used for many applications. And there are also plenty of resources available to help you get started and succeed in your projects.