simulate dc motor step response using local solver -凯发k8网页登录
this example shows how using a local solver can improve simulation performance for a model hierarchy that contains components that operate on different time scales. the model in this example simulates the step response of a dc motor driving a mechanical load.
subcomponents of dynamic systems can operate on different time scales. for example, in this dc motor model, the electrical physics of the step response operate on a time scale close to a microsecond, while the mechanical motion occurs on a time scale of miliseconds.
when you use referenced models to represent system components, you can configure the referenced models to use local solvers. for example, the referenced model that represents the mechanical motion can run with a larger solver step size than the rest of the dc motor model.
analyze system dynamics
to determine whether your system might benefit from using a local solver, analyze the dynamics of your system. local solvers can provide performance improvements by increasing the step size for components with slower dynamics compared to other parts of the system or allowing you to use a different solver that is better suited to that component.
open the model dcmotorlocalsolver
. the model implements the system using two referenced models:
the model
dcmotorelectrical
models the electrical response of the motor.the model
dcmotormechanical
models the mechanical response of the motor.
the electrical and mechanical systems are coupled by the motor current and the angular speed of the motor and load.
topmdl = "dcmotorlocalsolver"; mechref = "dcmotormechanical"; elecref = "dcmotorelectrical"; open_system(topmdl) load_system(mechref) load_system(elecref)
to analyze the electrical component, open the referenced model named dcmotorelectrical
in the simulink™ editor by double-clicking the model block named electrical
. the electrical dynamics of the motor are modeled as a series rl circuit.
the input voltage can be expressed as the sum of the voltage on the motor resistance , the voltage on the motor inductance , and the voltage due to the motor electrical constant and the angular velocity .
the electrical component in the model calculates the value of the current, which the mechanical system uses to calculate the speed of the motor and load.
the motor inductance and resistance determine the electrical time constant for the system.
the motor in this model has an inductance of and a resistance of , resulting in an electrical time constant of .
to analyze the mechanical component, navigate back to the top model by clicking the back arrow . then, open the referenced model named dcmotormechanical
in the simulink editor by double-clicking the model block named mechanical
.
the mechanical system models the rotational motion that results from the torque produced by the motor current. the total rotational moment of inertia from the motor and the load times the angular acceleration is equal to the sum of the forces in the system. the motor current and the magnetic field in the armature coil create a force scaled by the motor torque constant . for this example, assume the magnetic field is constant. the angular speed creates a force scaled by the viscous friction of the motor .
the mechanical component in the model integrates the angular acceleration to calculate the angular speed of the motor and load, which the electrical system uses to calculate the current.
the motor resistance, the inertia of the motor and load, the electrical constant, and the torque constant for the motor determine the mechanical time constant [1].
the motor in this model has a resistance of , an electrical constant of , and a torque constant of . the total moment of inertia for the system is , resulting in a mechanical time constant of .
run baseline simulation
simulate the model using a single solver to create a baseline for the simulation outputs and timing. using a single solver, the whole system must run using the same time step. the electrical time constant drives the choice of the step size. the step size must be small enough to capture the dynamics of the electrical response.
for this example, the model uses the ode3
fixed-step solver with a step size equal to the electrical time constant, . simulate the model to create a baseline for the simulation results and execution time.
set_param(mechref,"usemodelrefsolver","off") baselineout = sim(topmdl);
to see the amount of time required to execute the simulation, check the executionelaspedwalltime
field of the timinginfo
structure in the simulation metadata. the simulink.simulationoutput
object baselineout
contains a simulink.simulationmetadata
object with information about the model and simulation, including the structure timinginfo
.
using a single configuration for the ode3
solver with a step size of , the execution phase of the simulation runs for several seconds.
baseexec = baselineout.simulationmetadata.timinginfo.executionelapsedwalltime
baseexec = 3.6396
the record block in the model logs the position and speed data to the simulation data inspector. access the run created in the simulation data inspector and change the name to baseline: dcmotorlocalsolver
.
baserun = simulink.sdi.run.getlatest;
baserun.name = "baseline: dcmotorlocalsolver";
simulate using local solver
to see the performance benefit and impact on the model outputs, configure the mechanical model to use a local solver. the dynamics of the mechanical system operate on a longer time scale. when you configure the model to use a local solver, you can specify a larger time step so that the mechanical system calculations do not occur more frequently than required for capturing the system response.
in the top model, continue to use the step size. configure the dcmotormechanical
model to use the ode3
solver as a local solver with a step size of 2ms. you can configure the settings for the model reference from the top model, using the property inspector or the block parameters dialog box.
in the top model, select the model block that references the
dcmotormechanical
model.open the property inspector. on the modeling tab, expand the design section and select property inspector, or press ctrl shift i.
in the property inspector, expand the solver section.
click the hyperlink next to the use local solver parameter. the configuration parameters dialog box for the referenced model opens.
in the configuration parameters dialog box for the
dcmotormechanical
model, on the model referencing pane, select use local solver when referencing model.to configure the solver settings for the referenced model, select the solver pane.
set the solver to
ode3 (bogacki-shampine)
.set the fixed step size (fundamental sample time) to
0.002
.click ok.
in the model, the model block indicates the local solver selected for the referenced model.
alternatively, you can configure the model settings programmatically using the set_param
function.
set_param(mechref,"usemodelrefsolver","on","solvertype","fixed-step",... "solver","ode3","fixedstep","0.002");
the model block parameters input signal handling and output signal handling specify how the local solver processes inputs from the top model and provides outputs to the top model. for this simulation, use the default values.
mdlblkpath = append(topmdl,"/mechanical"); set_param(mdlblkpath,"inputsignalhandling","auto",... "outputsignalhandling","use solver interpolant")
simulate the model using the local solver.
localout = sim(topmdl);
check the simulation metadata to see the amount of time the simulation spent in the execution phase. when you use a local solver to execute the mechanical system at a slower rate, the simulation runs faster.
localexec = localout.simulationmetadata.timinginfo.executionelapsedwalltime
localexec = 2.4772
access the run created in the simulation data inspector. change the name to local solver: dcmotorlocalsolver
.
localrun = simulink.sdi.run.getlatest;
localrun.name = "localsolver: dcmotorlocalsolver";
analyze simulation results
to see whether using a local solver affected the simulation results, plot the position and speed data from both simulations in the simulation data inspector.
on the simulation tab, under review results, click data inspector. alternatively, use the simulink.sdi.view
function.
simulink.sdi.view
in the simulation data inspector, plot the speed and position signals from the two runs. alternatively, use the simulink.sdi.loadview
function to load the saved view for this example, which is named dcmotorlocalsolver1
.
simulink.sdi.loadview("dcmotorlocalsolver1");
the results from the local solver simulation are nearly the same as the results from the baseline simulation that used a single solver and step size. the solid line shows the baseline result. the dotted line shows the local solver result.
the biggest difference occurs in the angular speed
signal at the start of the simulation. the local solver determines required state and signal values from the top model using extrapolation. the first time the local solver takes a step, the local solver does not have enough history for extrapolation. on the second step, the local solver can extrapolate values from the top model and compensate in the output value.
use zero-order hold input and output handling
because the execution of the local solver is decoupled from the top solver, the local solver extrapolates values it uses from the top model and provides interpolated values for the top solver to use. you can use the input signal handling and output signal handling to configure how the local solver extrapolates the incoming and inteprolates outgoing values.
by default, the input signal handling parameter value is auto
. the software determines how to extrapolate the data that the local solver uses from the top model. the default value for the output signal handling parameter is use solver interpolant
, which means that the local solver provides output values that are compensated using the solver interpolant.
you can specify each parameter as zero-order hold
instead. configure the model block that references the dcmotormechanical
model to use zero-order hold
input and output signal handling.
set_param(mdlblkpath,"inputsignalhandling","zero-order hold",... "outputsignalhandling","zero-order hold")
to see the effect of the input and output signal handling on the simulation results, simulate the model again.
zohout = sim(topmdl);
check the execution time for the simulation. using the simpler extrapolation method further reduces the execution time.
zohexec = zohout.simulationmetadata.timinginfo.executionelapsedwalltime
zohexec = 3.3558
access the run created in the simulation data inspector. change the name to local solver zoh: dcmotormechanical
.
zohrun = simulink.sdi.run.getlatest;
zohrun.name = "local solver zoh: dcmotormechanical";
the simulation data inspector replaces the signals from the first local solver run with the signals from the zero-order hold run. the signal values are similar between the baseline run and the zero-order hold run. the signals from the zero-order hold run have steps that reflect the zero-order hold and the step size for the local solver.
reference
[1] younkin, george w. "electric servo motor equations and time constants." https://support.controltechnologycorp.com/customer/elearning/younkin/drivemotorequations.pdf.
see also
blocks
model settings
- | |