2010 The MathWorks, Inc. Real-Time Simulation of Simscape Models Vehicle Dynamics Motor Planetary Gear Electric Motor Electric Generator DC-DC Converter Battery Controller Hybrid Electric Vehicle Wit ursilo Application Engineer, Physical Modeling MathWorks
Challenge Limited power Max real-time simulation speed of the with acceptable accuracy within hardware capability calculation device Increase model fidelity Require addt l power for numerical calculation 2
Key Points explicit ode14x Solver Choice Backward Euler Trapezoidal Rule Small Step Size Large To move from desktop to real-time simulation, there are four areas where adjustments can be made Few # of Iterations Many Low High Model Fidelity Settings must be found that permit fixed-step, fixed-cost simulation with acceptable accuracy and speed Advances in Simscape make it easier to achieve real-time execution with accurate results Fixed-step explicit solver Controller Maximum Possible Step Size Execution Time Simulation Execution Chosen Step Size Other Tasks Step Size Safety Margin Idle Local fixed-step implicit solver #1 Mechanical Electrical Local fixed-step implicit solver #2 3
Agenda Overview of Application Example (Hybrid Electric Vehicle) Uses of Real-Time Simulation Challenge of Moving from Desktop to Real-Time Configuring Models for Real-Time Simulation Selecting fixed-step solver Configuring fixed-cost simulations 4
Configuring Simscape Models For Real-Time Simulation Model: Vehicle Dynamics Motor Planetary Gear Electric Motor Electric Generator DC-DC Converter Battery Controller Hybrid Electric Vehicle Problem: Configure the model used for desktop simulation so it can be used for HIL testing explicit ode14x Solver Choice Backward Euler Trapezoidal Rule Small Step Size Large Solution: Use Simscape local solvers and other settings to make the model real-time capable Few # of Iterations Many Low High Model Fidelity 5
Uses and Benefits of Real-Time Simulation Uses Hardware-in-the-Loop (HIL) testing Benefits Controller Hardware Ability to test conditions that would damage equipment or personnel Ability to test systems where no prototypes exist Reduced costs in the later phases of development Ability to test 24 hours a day, 7 days a week Real-Time Computer (physical system) 6
Hardware-in-the-Loop Testing S3 S1 S2 Hardware In the Loop (HIL) 7
Challenge of Moving From Desktop to Real-Time Simulation Must find combination of model fidelity and solver settings that permits real-time execution and delivers accurate results Maximum Possible Step Size Larger step sizes result in poor accuracy and robustness Affected by solver choice, # of nonlinear iterations, and model fidelity Chosen Step Size Small explicit ode14x Step Size Backward Euler Solver Choice Large Trapezoidal Rule Minimum Possible Step Size Smaller step sizes result in overrun Execution Time Affected by solver choice, # of iterations and model fidelity Safety Margin Affected by step size Few # of Iterations Many Simulation Execution Processing I/O and Other Tasks Step Size Idle Low High Model Fidelity 8
Steps For Moving to Real-Time Simulation Flowchart describes steps engineers take 1. Obtain reference results 2. Check if real-time capable 3. Configure for fixed-step, fixed cost simulation 4. Find settings that deliver acceptable results and speed 5. Test on real time platform 6. Adjust if necessary 6 Adjust model to make it real-time capable 1 Obtain reference results and estimate step size with variable-step solver Y 2 Is the solver taking too many small steps? 3 Simulate with fixed-step, fixed-cost solver ew results match reference results? Y Acceptable simulation speed? Y 5 Simulate on real-time platform Executes in real time with accurate results? Y Increase iterations and/or decrease step size 4 Success Decrease iterations and/or increase step size 9
Obtaining Reference Results With Variable-Step Solver ecessary to determine if configuration for real-time simulation delivers acceptable results For Simscape models, ode15s or ode23t are recommended 10
Determining If Model Is Real-Time Capable >> semilogy(tout(1:end-1),diff(tout),'-x') Examine step size during simulation to determine: Rough idea of step size for accurate results umber and type of events When step size decreases, an event has occurred Slow recovery indicates dynamics that may require a smaller step size for the fixed-step solver For much of the simulation, the step size is 0.1~0.2s Events Slow Recovery Quick Recovery 11
Adjusting Model To Be Real-Time Capable Low High Model Fidelity Examine simulation results to determine when events occur Find elements model that will be difficult for fixed-step solvers Discontinuities Hard-stops, stick-slip friction Small time constants Small masses with undamped, stiff springs Hydraulic circuits with small, compressible volumes Eliminate or soften these effects Events 12
Recap 6 Adjust model to make it real-time capable 1 Obtain reference results and estimate step size with variable-step solver Y 2 Is the solver taking too many small steps? 3 Simulate with fixed-step, fixed-cost solver ew results match reference results? Y Acceptable simulation speed? Y Increase iterations and/or decrease step size 4 Decrease iterations and/or increase step size 5 Simulate on real-time platform Executes in real time with accurate results? Y Success 13
Explicit vs. Implicit Methods For Choosing a Fixed-Step Solver explicit ode14x Solver Choice Backward Euler Trapezoidal Rule Physical System simulation typically requires a Stiff System Stiff system: A stiff differential equation causes numerical methods to be unstable unless the step size is extremely small. Stiff System is solved better with Implicit Solver. Simscape provides implicit solvers for handling numerically stiff systems Explicit: n 1 yn y hf t, n y n Implicit: y y hf n 1 n n 1, n 1 t y Explicit Implicit Explicit Implicit 14
Choosing a Fixed-Step Solver (Explicit vs. Implicit) explicit ode14x Solver Choice Backward Euler Trapezoidal Rule Choose fixed-step solver based on umerical stiffness of system Computational cost of solver Stiff systems simulate best with an implicit solver Can take larger steps and maintain accuracy Explicit solver requires smaller time step to achieve accuracy comparable to a implicit solver Implicit solvers often require more computational effort per iteration 15
Relative Computational Cost of Solvers explicit ode14x Solver Choice Backward Euler Trapezoidal Rule Test all fixed-step solvers on a nonlinear Simscape model Same step size Same total iterations Most explicit solvers require less cost than ode14x, but will require smaller step size for accurate results Local solvers in Simscape provide best combination of computational cost and maximum step size 16
Configuring Local Solvers In Simscape Configure per physical network Choose solver and sample time Sample rates can be different Must be integer multiple of global sample time Fixed-step explicit solver Controller Local fixed-step implicit solver #1 Simscape Physical etwork Simscape Physical etwork Local fixed-step implicit solver #2 Backward Euler Designed for robustness Tends to damp oscillations Trapezoidal Rule Designed for accuracy Tends to capture oscillations 17
Solution Preventing Overruns by Limiting the umber of Iterations Few # of Iterations Many To prevent overruns, the amount of time spent calculating the solution during each step must be less than the time step Fixed-step solver makes computations per iteration roughly constant Fixed cost sets a maximum number of iterations per time step x x x x x x x x x x x Time required to calculate solution per iteration. Iteration 1 Iteration 2 Iteration 3 Simulation Time True Solution Simulation overrun because too many iterations are required. Real Time 18
Configuring Fixed-Cost Simulations Few Many # of Iterations For Simscape models, fixed-cost simulation requires setting onlinear Iterations in Solver Configuration block for all fixed-step solvers Start with 2 or 3 Increase as necessary For ode14x, additional setting in Solver Configuration panel, umber ewton s iterations If Simscape Local Solvers are used, ode14x is only necessary if continuous equations outside of physical network are numerically stiff 19
Solver Recommendations For Real-Time Simulation of Simscape Models 1. Always try using local solvers a. Backward Euler for robustness b. Trapezoidal Rule for accuracy 2. Always enable fixed-cost simulation a. onlinear iterations in Solver Configuration block b. Additional setting for ode14x in Solver Configuration panel 3. Start with number of nonlinear iterations at 2 or 3 20
Finding Real-Time Settings explicit ode14x Backward Euler Solver Choice Trapezoidal Rule Maximum Possible Step Size Larger step sizes result in poor accuracy and robustness Affected by solver choice and # of nonlinear iterations Chosen Step Size Minimum Possible Step Size Smaller step sizes result in overrun Execution Time Affected by solver choice and # of nonlinear iterations Safety Margin Affected by step size Few Many # of Iterations Small Large Step Size Simulation Execution Processing I/O and Other Tasks Idle Step Size 21
Recap 6 Adjust model to make it real-time capable 1 Obtain reference results and estimate step size with variable-step solver Y 2 Is the solver taking too many small steps? 3 Simulate with fixed-step, fixed-cost solver ew results match reference results? Y Acceptable simulation speed? Y Increase iterations and/or decrease step size 4 Decrease iterations and/or increase step size 5 Simulate on real-time platform Executes in real time with accurate results? Y Success 22
Applying Process To Other Models in Physical Modeling Tools Models Verify the process with 20 models Hydraulic, electrical, mechanical, pneumatic, thermal elements 18 nonlinear, up to 117 states after equation reduction Intel Core 2 Duo E6700 (2.66GHz) running xpc Target Plenty of time for I/O, other tasks, and safety margin Execution Time Simulation Execution Chosen Step Size Other Tasks Safety Margin Idle % of time step spent in simulation execution Max = 18% Average = 3.9% Min = 6e-4% Step Size 23
Key Points explicit ode14x Solver Choice Backward Euler Trapezoidal Rule Small Step Size Large To move from desktop to real-time simulation, there are four areas where adjustments can be made Few # of Iterations Many Low High Model Fidelity Settings must be found that permit fixed-step, fixed-cost simulation with acceptable accuracy and speed Advances in Simscape make it easier to achieve real-time execution with accurate results Fixed-step explicit solver Controller Maximum Possible Step Size Execution Time Simulation Execution Chosen Step Size Other Tasks Step Size Safety Margin Idle Local fixed-step implicit solver #1 Mechanical Electrical Local fixed-step implicit solver #2 24
What is xpc Target? Host PC with MATLAB & Simulink Target PC running a Real-Time kernel Ethernet or RS 232 A Host-Target environment that allows for real-time execution of Simulink models on a PC platform. 25
What is xpc Target? Desktop PC Compact PCI Rack Mounted PC Single Board Computer PC/104 and PC/104+ Open Architecture: A target PC can be almost any PC compatible system with a 32 bit Intel or AMD processor. 26
What is xpc Target? Host PC Target PC Host PC with MATLAB Ethernet or RS 232 xpc Target PC Ethernet or RS 232 An environment that provides interactive access between the real-time application and the host PC. 27
What is xpc Target? Host PC Target PC Ethernet or RS 232 An environment that provides interactive access between the real-time application and the host PC. 28
What is xpc Target? Host PC Target PC Ethernet or RS 232 An environment with numerous I/O device drivers that are easily configurable within the Simulink model to allow for communication with hardware in real-time. 29
What is xpc Target? Target PC An environment with numerous I/O device drivers that are easily configurable within the Simulink model to allow for communication with hardware in real-time. 30
Are White Paper and Model available? http://www.mathworks.com/matlabcentral/fileexchange/26351-real-time-simulation-of-a-hybrid-electric-vehicle http://www.mathworks.com/mason/tag/proxy.html?dataid=12628&fileid=61384 31