Main Content

Deploy Virtual Vehicle Composer Model to Speedgoat Real-Time Target Machine

Configure a copy of the Conventional Vehicle Reference Application to use Simulink® Real-Time™ and deploy the model to a Speedgoat® real-time target machine.

For Simscape™-configured vehicle models, you can simulate using Simulink Real-Time only for models with longitudinal vehicle dynamics. You cannot use Simulink Real-Time to simulate Simscape-configured vehicle models with combined longitudinal and lateral dynamics.

Build Conventional Vehicle Model

Create and open a working copy of the Conventional Vehicle Reference Application project.

autoblkConVehStart();

Define a name for the model.

modelName='ConfiguredConventionalVirtualVehicle';

Set System Target Files

The Virtual Vehicle Composer model uses configuration sets stored in data dictionaries. These configuration sets use the default system target file grt.tlc. Programmatically change the system target files in the configuration sets to use the Speedgoat compatible target file speedgoat.tlc.

Open the data dictionaries that contain the configuration sets.

dd1 = Simulink.data.dictionary.open('VehicleConfig.sldd');
cs1 = dd1.getSection('Configurations');


dd2 = Simulink.data.dictionary.open('VirtualDynoConfig.sldd');
cs2 = dd2.getSection('Configurations');

From the list of target files, select the Speedgoat system target file speedgoat.tlc.

newSystemTargetFile = 'slrealtime.tlc';

Get all the configuration set entries in the section and iterate over each configuration set to change the system target file.

entries1 = find(cs1);
entries2 = find(cs2);
allEntries = [entries1; entries2];

for i = 1:numel(allEntries)
    % Get the name of the Configuration Set
    configSetName = allEntries(i).Name;
    
    % Load the Configuration Set object
    configSetObj = getValue(allEntries(i));
    
    % Change the system target file of the Configuration Set
    set_param(configSetObj, 'SystemTargetFile', newSystemTargetFile);
    
    % Save the modified Configuration Set back to the data dictionary
    setValue(allEntries(i), configSetObj);
end

Save the changes in the data dictionaries and close the data dictionaries.

saveChanges(dd1);
saveChanges(dd2);

close(dd1);
close(dd2);

Update Code Mapping for Vehicle Models with a Battery Management System (BMS)

For vehicle models with a BMS, update the Code Mapping settings in the top model and all model references. These settings update automatically for vehicle models without a BMS.

modelRefs = find_mdlrefs( ...
    modelName, 'MatchFilter', @Simulink.match.allVariants);

for i = 1:length(modelRefs)

    load_system(modelRefs{i});

    codeMapping = coder.mapping.api.CodeMapping( ...
        modelRefs{i}, 'SimulinkCoderC');
    codeMapping.setDataDefault( ...
        'ExternalParameters', ...
        'StorageClass', ...
        'PageSwitching (slrealtime)');
    codeMapping.setDataDefault( ...
        'ModelParameters', 'StorageClass', 'PageSwitching');

    save_system(modelRefs{i});

end

Change Global Solver Settings

To generate code and deploy the model to the Speedgoat target, the top-level solver must be set to a fixed-step solver.

activeConfig = getActiveConfigSet(modelName);
activeConfig.SourceName = 'VehicleFixed1ms';

If you configure a Simscape vehicle model with longitudinal dynamics, turn on the local solver, set the local sample time to 1 ms, and use fixed-cost runtime consistency iterations.

variantCheck = get_param([modelName '/Vehicle'], 'VariantChoices');
isActive = strcmp(variantCheck.Name, 'ConfiguredSimscapePlantModel');

if isActive
    SimscapeBlockPath = [modelName '/Vehicle/ConfiguredSimscapePlantModel/Solver Configuration/Solver Configuration'];
    set_param(SimscapeBlockPath, 'UseLocalSolver', 'on', ...
        'LocalSolverSampleTime', '0.001', ...
        'DoFixedCost', 'on');
end

Connect to Target Machine

You must configure a link between the development computer and the Speedgoat target machine. For more information, see Set Up and Configure Simulink Real-Time (Simulink Real-Time).

After the link is configured, to run the commands to connect the development computer and target machine, select the checkbox.

if false
tg = slrealtime;
tg.connect;
end

Run Model on Target Machine

When you are ready to build the application, deploy the model to the target, and run the simulation in external mode, select the checkbox.

if false
slbuild(modelName);

load(tg, modelName);

set_param(modelName, 'SimulationMode', 'external'); 
set_param(modelName, 'SimulationCommand', 'Connect');

set_param(modelName, 'SimulationCommand', 'start');
end

Alternatively, from the Simulink Real-Time tab, click Run on Target to build, deploy, and run the model on the target machine.

Alternatively, to execute each step individually, click the Run on Target arrow button and follow the order of the buttons displayed.

For more information, see Execute Real-Time Application in Simulink External Mode by Using Step-by-Step Commands (Simulink Real-Time).

View Simulation Output in Real-Time

For this example, the virtual vehicle model simulates the FTP75 drive cycle.

To open the Simulink Data Inspector, select the checkbox.

if false
Simulink.sdi.view;
end

Select output signals to view the simulation results in real-time.

External Websites