PMEC-OSU / FOSTWIN

Open Source MATLAB & Simulink Code for modeling and control of FOSWEC Wave Energy Converter
https://pmec-osu.github.io/FOSTWIN/
5 stars 0 forks source link

Stop Target Timeouts #10

Closed Graham-EGI closed 2 years ago

Graham-EGI commented 2 years ago

When running the simulation in realtime, there are often issues when running the pTg.stop line to stop the simulation on the speedgoat.

Symptoms:

Adding Simulink.sdi.setRecordData(false); didn't seem to fix the issue

johannes-spinneken commented 2 years ago

@Graham-EGI To stop the SDI from interfering with the file transfer (e.g. by deleting files after a run is complete), start the target with the following command:

app.tg.start('AutoImportFileLog',false,'ExportToBaseWorkspace',false,'FileLogMaxRuns',100)
johannes-spinneken commented 2 years ago

@Graham-EGI

This is some example code to retrieve the file and open in SDI:

% post processing script to retrieve data from the target

clearvars; close all; clc;

%% === parameters =========================================================
tgName = 'EGIBaseline';
mdlName = 'sweptCtrlMdl';
dataDir = fullfile('D:','simulink_data');

matFileName = 'simdata.mat';
% =========================================================================

%% === test Speedgoat connection ==========================================
tg = slrealtime(tgName);

try 
   tg.connect
catch ME
   fprintf('\n*** Target %s not connected. Stopping program. Check connection.\n',tgName)
   fprintf('\n*** Matlab error \n %s \n\n',ME.getReport)   
   return  
end

if tg.isConnected 
   fprintf('\n*** Target %s is connected at IP address %s. \n\n',tg.TargetSettings.name,tg.TargetSettings.address)
end

ipAddress = tg.TargetSettings.address;
% =========================================================================  

%% === copy data from target ==============================================
newFolder = fullfile(dataDir,mdlName);
mkdir(newFolder);

system(['pscp -r slrt@', ipAddress, ':/home/slrt/applications/', mdlName, '/* ' ,newFolder])
% =========================================================================  

%% === Import Logged Data into MATLAB and view in Simulation Data Inspector
importLogData(newFolder)
Simulink.sdi.view;
% =========================================================================  

%% === Export to .MAT file from Simulation Data Inspector =================
runIDs = Simulink.sdi.getAllRunIDs; % get run ID
runID = runIDs(end);

rtRun = Simulink.sdi.getRun(runID); % get data for last run
SignalData = rtRun.export;

Simulink.sdi.exportRun(runID,'to','file','filename',matFileName); % export to .mat
% =========================================================================  
johannes-spinneken commented 2 years ago

@Graham-EGI

The importLogData function was something Mathworks provided us early on for beta testing. I believe this is now replaced by

slrealtime.fileLogImport().
Graham-EGI commented 2 years ago

Have updated the system to use the logic above.

Seems to work perfectly when running from an interactive MATLAB session (matlab UI), but there are errors when running from the python server.

Python errors:

Stop error message:

Error using slrealtime.Target/throwError
Cannot stop application on target 'EGIBaseline2': Timed-out waiting for application to stop.

Error in slrealtime.Target/stop

Error in stoptarget (line 3)
        pTg.stop

Logging err message:

Error using slrealtime.internal.Application
Enter a valid application name

Error in slrealtime.Application

Error in slrealtime.internal.logging.Importer/checkFileLogDataVersion

Error in slrealtime.internal.logging.Importer/importLocal

Error in importLogData

Error in FOSTWINctrlPost (line 30)
         importLogData(loggingDataDir)
Graham-EGI commented 2 years ago

@johannes-spinneken Update on the logging without using the data inspector during the simulation:

Graham-EGI commented 2 years ago

Issues resolved, timeouts related to using a constant block for the inport data and using the data inspector. Updates fixed the use of root level inport blocks in non realtime (removing the need for the constant block) and added in system to copy log data from the host machine directly and turn off the data inspector.

Closing