Simulink is a graphical environment for modeling dynamic systems that is
systems that change over time Simulink takes care of the simulation so you can
focus on the engineering you can use it to model simple things like a home
thermostat or complex systems like a fully autonomous vehicle or a surgical
robot this video will show you the basics of Simulink and give you an idea
of what working in Simulink looks like stay tuned to the end to find out where
to go to learn more about how to use Simulink let's get started at the
mathworks headquarters in Natick Massachusetts we use solar panels to
generate electricity these panels face south and are fixed in
place that means they produce more electricity when the sun is shining
directly on them in the middle of the day and less power when the Sun is to
the east or west early and late in the day what if you had solar panels that
rotated to track the Sun so that you could produce as much electricity as
possible in this video we'll use simulate to design a tracking system to
keep a solar panel aligned with the Sun if you want to follow along as we build
the model you can download it using the link below the physical system consists
of a panel and a motor will model those first and then we'll add a controller to
track the sun's position once we're happy with the design we'll test it to
see how well it does tracking actual Sun data the physical system has two main
components with some basic physics we can write out the equations of motion
for each let's model the panel first you start Simulink by clicking the Simulink
button in the MATLAB tool strip this opens the start page where you can
create new models find examples and even find basic training we're starting our
model from scratch so we'll choose blank model Simulink models are built up from
blocks and signals open the library browser to see all the blocks available
to model the equation for the panel we'll start with the integrator block
let's click and drag an integrator from the library into our model so why an
integrator block well the integrator block takes an input and integrates it
over time if we integrate acceleration we get velocity and if we integrate
velocity we get position this is the basis for modeling differential equation
in Simulink our equation has acceleration and velocity terms so we
need at least one integrator let's add another one to also get the position we
connect blocks together with signals by clicking and dragging between blocks
don't worry about these red lines we'll connect them up in a second it's a good
idea to label signals to keep things organized so we'll double click the
signals and type a name let's name this one theta dot dot for acceleration then
theta dot for velocity and theta for the position of the panel next let's model
the right-hand side of this equation first the torque term let's use a
constant block we'll replace this with the motor later you can double-click a
block to change its parameters let's change the value to 10 we need to
perform a subtraction so let's also grab a subtract block the damping term
depends on the panels velocity the theta dot signal we can branch this signal by
right clicking and dragging to connect it to the subtract block and don't
forget we still need to multiply that by KD let's use a gain block for that
rather than hard coding the gain value we can also use a variable or even
MATLAB code so let's just say the gain is KD that red box is telling us that KD
hasn't been defined yet so click the three dots and select create we'll give
it a value of five and store it in MATLAB space work space
let's check MATLAB and yep the variable KD has been created to finish up the
equation we need to divide by the inertia J we'll do that with another
gain block but now we know the name of the block so we can just double-click in
the model and start typing the block name then use the drop down to find the
right block and hit enter we'll set the gain to 1 over J and again let's define
the variable J in the base workspace with a value of 8.6 let's add an
annotation to show the equation being modeled so when we come back to it later
will easily know what the equation is well that should be all we need to model
the panel but to check if everything is working correctly we'll want to
visualize some signals Simulink has a bunch of visualization tools for a quick
check of a signal the easy option is to use a scope block and connect it to the
signal we want to view let's add a second scope to see the velocity signal
okay now we're ready to simulate the model we can set the simulation stop
time in the simulation tab but we'll leave it as 10 for now to run the
simulation just click the Run button the simulation finished but what you didn't
see was that simulate numerically solved the differential equation through time
double-click on a scope to check out what happened in the position scope we
see the panel's angular position is increasing in the velocity scope the
velocity starts from 0 and levels off so with a constant torque the panel starts
turning and then settles to rotating at a fixed rate oh that makes sense let's
do a quick sanity check and see what happens if we switch the sign of the
torque we can edit the constant value directly on the block rerun and that
looks good we see the panel now spins in the opposite direction now that we have
the panel model working let's group those blocks together to keep things
organized just select the blocks you want to
include we'll leave the constant block and the Scopes out for now and then
click create subsystem in the modeling tab now all those blocks are contained
within this subsystem let's name it panel you can double click to see this
system contents these oval-shaped blocks are in ports and out ports that's the
data going in and out of the subsystem let's adjust the port names go back up
to the top level and there are the port names we just entered okay we still need
a motor remember that motor equation well we can
follow the exact same process to model it and we've got our motor will supply a
voltage to the motor to generate a torque and move the panel let's check
what happens we can see that the panel spins when a voltage is applied to the
motor so everything looks good so far okay we've modelled the panel and the
motor now we need a controller to set the correct voltage so that the panel
tracks the Sun from our model we know where the panel is pointing and let's
say the Sun is over here we want the panel pointing at the Sun so the
difference is the error we'll add a controller that applies a voltage to the
motor to make that error as small as possible and if the Sun moves the
controller will react accordingly to keep the panel pointing at the Sun okay
back to our Simulink model we have the position of the panel here to get the
error we need the position of the Sun while we're designing the controller
we'll use a unit step input that's pretty common in control design we'll
test it with some actual Sun position data later now to calculate the error
we'll use a sunblock it's already got this nice circular shape common in
control schematics we just need to change the second port to be a minus
instead of a plus next we need a controller there are a lot of options
but a common approach is some form of PID control which stands for
proportional integral derivative because the control output is some function of
the error the integrated error and the derivative of the error but we don't
need to build all that ourselves we'll just add a PID controller block we
connect the input to the error signal and the output will drive the motor you
can see there are a lot of ways to customize the controller we'll switch to
a PI controller the D term helps respond to quick changes which we don't need
because the Sun moves steadily across the sky there are two gains to adjust
one for the proportional term and one for the integral term these effect the
controller's response will set the proportional gain to 240 and the
integral gain to 180 to see how well the controller performs let's use the same
scope to show both the Sun position and the panel position notice that a new
port is automatically added let's run the model and add a legend to the scope
so we can tell the signals apart let's also make the position line dashed we
can see that the controller overshoots a little then settles to the reference
value of 1 that's good enough for our application ok we designed our
controller but can it actually track the movement of the Sun well let's see how
it performs when we provide it with some real data let's load some Sun position
data into the MATLAB base workspace this file has two variables a vector for time
spanning 15 hours and a vector of the sun's position at each point in time
let's plot it you can see that the Sun rises in the northeast at about 60
degrees from due north and sets in the Northwest at about 300 degrees we can
bring the Sun position data into the model by replacing the step block with
an import we need to select which data to use click model settings in the
modeling tab then navigate to the data import export pane there are a lot of
settings in here if you're ever unsure about anything just right click and
select what's this this input option is what we need when specifying input data
the first column should always be time after that you can add a column vector
for each import in your model since we now have 15 hours of data will change
the simulation stop time let's run the model and we can see it's tracking the
sun's position quite well so we modeled our solar panel system developed a
controller and tested the system to ensure it will track the motion of the
Sun and it looks like our design works but that's just the beginning
if we want to make these panels a reality we can incorporate proper design
specs into our model we can bring in other tools like sim scape to model the
mechanical and electrical systems without needing to derive any equations
and with stateflow we can add logic to make the panels
smart so they spin back to the east at the end of the day and they know what to
do if conditions change then when we're ready we can automatically generate code
from the model and deploy it directly to hardware at each step of the way we can
continuously test the design to ensure it's error-free and meeting
specifications with the model at the center of our design process we can
tackle all kind of design questions and so can you now that you have a feel for
what working in Simulink is like it's time to learn it the best way to learn
Simulink is to work with it so startup Simulink on-ramp which will teach the
basics of Simulink interactively it's free and takes just a couple of hours
welcome to Simulink