Main Content

Control Vehicle Throttle Input Using a Powertrain Blockset Driver

R2026b

This example shows how to control throttle input to a Simscape™ Driveline™ vehicle model using a Powertrain Blockset™ Longitudinal Driver block. You add the driver to an open-loop model that uses a Signal Builder block for feedforward control. Adding the driver allows you to model closed-loop control by supplying a reference velocity and a feedback loop.

Open-Loop Simulation Using the Test - Hill Subcomponent Block

In the open-loop simulation, a subcomponent block is used to ramp up the throttle. Simulate the model to see the open-loop response.

  1. Open the model. At the MATLAB® command prompt, enter this code.

    openExample('sdl/VehicleWithFourWheelDriveExample')
    modelName = 'VehicleWithFourWheelDrive';

  2. Enable the signal that goes to the Motor RPM scope block for Simulink® data logging and viewing with the Simulation Data Inspector.

    %% PS-S Converter1 information
    pStoSConverter1Name = 'PS-Simulink Converter1';
    pStoSConverter1Path = [modelName,'/',pStoSConverter1Name];
    pStoSConverter1PortHandles = get_param(pStoSConverter1Path,'PortHandles');
    pStoSConverter1Inport = pStoSConverter1PortHandles.LConn(1,1);
    pStoSConverter1Outport = pStoSConverter1PortHandles.Outport(1,1);
    
    %% Enable the signal that goes to the Vehicle Speed scope block for   
    % Simulink(TM) data logging and viewing with the Simulation Data Inspector
    set_param(pStoSConverter1Outport,'DataLogging','on')

    The logging badge marks the signal in the model.

  3. Increase simulation time to get steady-state results. Simulate the model.

    %% Increase simulation time
    set_param(modelName,'StopTime','30')
    
    %% Simulate the model
    sim(modelName)
    % Plot the vehicle speed, the engine speed, and the throttle input.
    VehicleWithFourWheelDrivePlot1Speed

    Even though the throttle input is nonzero at simulation time 0, the vehicle rolls down the hill at first because the input is too small to overcome the gravitational force of the incline. When the input is large enough, the vehicle accelerates up the hill and settles at a velocity of ~20 km/h.

Closed-Loop Simulation Using a Longitudinal Driver Block

To control throttle input, add a Powertrain Blockset Longitudinal Driver block.

  1. Add a Longitudinal Driver block to the model.

    1. Expand the model window to fit the Longitudinal Driver block.

      %% Update the model canvas
      modelWindowNewLocation = [47 100 1072 723];
      
      % Increase the size of the model window
      set_param(modelName,'location',modelWindowNewLocation);

    2. Add the Longitudinal Driver block.

      %% Longitudinal Driver block name, path,%
      %   and position information
      longDriverName = 'Driver';
      longDriverLibPath = 'autolibscenario/Longitudinal Driver';
      longDriverPath = [modelName,'/Longitudinal Driver'];
      longDriverPos = [710 15 905 135];
      
      % Add a driver block
      add_block(longDriverLibPath,longDriverPath,...
          'position',longDriverPos)
      
      % Longitudinal Driver block port information
      longDriverPortHandles = get_param(longDriverPath,'PortHandles');
      longDriverPortVelRef = longDriverPortHandles.Inport(1,1);
      longDriverPortVelFdbk = longDriverPortHandles.Inport(1,2);
      longDriverPortGrade = longDriverPortHandles.Inport(1,3);
      longDriverPortInfo = longDriverPortHandles.Outport(1,1);
      longDriverPortAccelCmd = longDriverPortHandles.Outport(1,2);
      longDriverPortDecelCmd = longDriverPortHandles.Outport(1,3);

    3. Change the source of throttle input from the Test - Hill block Throttle port to the Longitudinal Driver block AccelCmd port and terminate the unconnected Throttle port with a Terminator block.

      %% Test - Hill block information
      signalTestName = 'Test - Hill';
      signalTestPath = [modelName,'/',signalTestName];
      signalTestPortHandles = get_param(signalTestPath,'PortHandles');
      signalTestPortIncline = signalTestPortHandles.Outport(1,1);
      signalTestPortWind = signalTestPortHandles.Outport(1,2);
      signalTestPortThrottle = signalTestPortHandles.Outport(1,3);
      
      %% Throttle Goto block information
      goToThrottleName = 'Goto';
      goToThrottlePath = [modelName,'/',goToThrottleName];
      goToThrottlePortHandle = get_param(goToThrottlePath,'PortHandles');
      goToThrottleInport = goToThrottlePortHandle.Inport(1,1);
      goToThrottleNewPos = [950 66 985 84];
      
      % Delete the connection line from the Signal Builder to the Throttle Goto
      delete_line(modelName,signalTestPortThrottle,goToThrottleInport)
      
      % Move the throttle Goto to the Longitudinal Driver
      set_param(goToThrottlePath, 'position', goToThrottleNewPos)
      
      % Add a connection line from the Longitudinal Driver AccelCmd 
      %    port to the Throttle goto
      add_line(modelName,longDriverPortAccelCmd,goToThrottleInport)
      
      %% Terminator0 Block Information
      % Terminator0 block name, path, and position information
      terminator0Name = 'Terminator0';
      terminatorLibPath = 'simulink/Sinks/Terminator';
      terminator0Path = [modelName,'/',terminator0Name];
      terminator0Pos = [200 5 220 25];
      
      % Add Terminator0 block
      add_block(terminatorLibPath,terminator0Path,...
          'position',terminator0Pos)
      
      % Terminator0 port information
      terminator0PortHandle = get_param(terminator0Path,'PortHandles');
      terminator0Inport = terminator0PortHandle.Inport(1,1);
      
      % Connect Longitudinal Driver AccelCmd Outport to the Thr Goto inport
      add_line(modelName,signalTestPortThrottle,terminator0Inport)
      

    4. Terminate the Info and DeclCmd outports on the Longitudinal Driver block

      %% T1
      % Terminator1 block name, path, and position 
      %    information
      terminator1Name = 'Terminator1';
      terminatorLibPath = 'simulink/Sinks/Terminator';
      terminator1Path = [modelName,'/',terminator1Name];
      terminator1Pos = [955 105 975 125];
      
      % Add Terminator1 block
      add_block(terminatorLibPath,terminator1Path,...
          'position',terminator1Pos)
      
      % Terminator1 port information
      terminator1PortHandle = get_param(terminator1Path,'PortHandles');
      terminator1Inport = terminator1PortHandle.Inport(1,1);
      
      % Connect Longitudinal Driver DecelCmd Outport 
      %    to Terminator1 inport
      add_line(modelName,longDriverPortDecelCmd,terminator1Inport)
      
      %% T2
      % Terminator2 block name, path, and position information
      terminator2Name = 'Terminator2';
      terminatorLibPath = 'simulink/Sinks/Terminator';
      terminator2Path = [modelName,'/',terminator2Name];
      terminator2Pos = [955 25 975 45];
      
      % Add Terminator2 block
      add_block(terminatorLibPath,terminator2Path,...
          'position',terminator2Pos)
      
      % Terminator2 port information
      terminator2PortHandle = get_param(terminator2Path,'PortHandles');
      terminator2Inport = terminator2PortHandle.Inport(1,1);
      
      % Connect Longitudinal Driver Info Outport to Terminator2 inport
      add_line(modelName,longDriverPortInfo,terminator2Inport)

    5. Input a 5.84 reference velocity to the Longitudinal Driver block VelRef port using a Constant block.

      %% Reference Velocity
      ref_vel = '5.84';
      constantLibPath = 'simulink/Commonly Used Blocks/Constant';
      referenceVelocityName = 'Constant';
      referenceVelocityPath = [modelName,'/',referenceVelocityName];
      referenceVelocityPos = [655 20 685 50];
      add_block(constantLibPath,referenceVelocityPath,...
          'position',referenceVelocityPos)
      set_param(referenceVelocityPath,'Value',ref_vel)
      
      % Reference Velocity port information
      refernceVelocityPortHandle = get_param(referenceVelocityPath,'PortHandles');
      refernceVelocityOutport = refernceVelocityPortHandle.Outport(1,1);
      
      add_line(modelName,refernceVelocityOutport,longDriverPortVelRef)
      

    6. Input the incline angle signal from the Signal Builder block to the Longitudinal Driver block by connecting the Incline outport to the Grade inport.

      %% Connect incline output port to Grade input port
      add_line(modelName,signalTestPortIncline,...
          longDriverPortGrade,'autorouting','on')

    7. Input the velocity feedback signal to the Longitudinal Driver block using a Unit Conversion block to convert from km/hr to m/s.

      %% Unit Conversion
      % UnitConvert0 block name, path, and position information
      unitConvert0Name = 'Unit Conversion';
      unitConvert0LibPath = 'simulink/Signal Attributes/Unit Conversion';
      unitConvert0Path = [modelName,'/',unitConvert0Name];
      unitConvert0Pos = [565 54 635 96];
      
      % Add block
      add_block(unitConvert0LibPath,unitConvert0Path,...
          'position',unitConvert0Pos)
      
      %% UunitConvert0 port information
      unitConvert0PortHandle = get_param(unitConvert0Path,'PortHandles');
      unitConvert0Inport = unitConvert0PortHandle.Inport(1,1);
       unitConvert0Outport = unitConvert0PortHandle.Outport(1,1);
      
      %% Connect Longitudinal Driver AccelCmd Outport to Goto Throttle inport
      add_line(modelName,unitConvert0Outport,...
          longDriverPortVelFdbk,'autorouting','on')
      
      %% Connect PS-S Converter Outport to Longitudinal Driver AccelCmd Inport
      add_line(modelName,pStoSConverter1Outport,...
          unitConvert0Inport,'autorouting','on')

  2. Simulate the closed-loop model using the Simple Tire blocks and plot the results.

    %% Simple
    VehicleWithFourWheelDriveSetTires(bdroot,'Simple');
    sim(modelName)
    % Plot the vehicle speed, the engine speed, and the Throttle input.
    VehicleWithFourWheelDrivePlot1Speed
    

    The drive block increases the throttle input rapidly at the beginning of simulation due to the difference between the velocity feedback and reference signals.

Simulation Comparison

Compare the open and closed-loop results using the Simulation Data Inspector.

%% Get Simulation (TM) Data Inspector run IDs for 
%    the last two runs
runIDs = Simulink.sdi.getAllRunIDs;
runBaseline = runIDs(end - 1);
runSimple = runIDs(end);

% Open the Simulation Data Inspector
Simulink.sdi.view

compBaselinePartition = Simulink.sdi.compareRuns(runBaseline,...
    runSimple);

To see the results in the Simulation Data Inspector, click the Compare icon and then, under Filter Comparisons, click PS-Simulink Converter1:1.

The first plot overlays the results from the open and closed-loop simulations. It shows how much faster the controlled vehicle goes to steady state.

The second plot shows the numerical difference in the results from the two simulations. It shows how much the two signals differ at the beginning of the simulation and how they eventually reach the same steady state.

You can also examine the results for other tire blocks.

%% Magic
VehicleWithFourWheelDriveSetTires(bdroot,'Mformula');
sim(modelName)
% Plot the vehicle speed, the engine speed, and the throttle input.
VehicleWithFourWheelDrivePlot1Speed

%% Friction
VehicleWithFourWheelDriveSetTires(bdroot,'Friction');
sim(modelName)
% Plot the vehicle speed, the engine speed, and the throttle input.
VehicleWithFourWheelDrivePlot1Speed

See Also

| | | | |

Topics