Something went wrong on our end
-
Cristian Galperti authored
this is a very nice approach, to be replicated into all other try - catch based loading
Cristian Galperti authoredthis is a very nice approach, to be replicated into all other try - catch based loading
SCDclass_taskmdsloadprevETHCAT.m 5.47 KiB
classdef SCDclass_taskmdsloadprevETHCAT < SCDclass_task
% This is a special class which loads previous
% values of the EtherCAT1 real-time network
properties
modelbus
node
workspacedatabasestructure
end
properties(Access=private)
logdecimate
nint16
nfloat32 % not yet implemented
end
methods
function obj=SCDclass_taskmdsloadprevETHCAT(id, varargin)
obj@SCDclass_task(id, varargin);
obj.cparser.addRequired('modelbus',@(x) ischar(x));
obj.cparser.addParameter('node', 0, @(x) (isnumeric(x) && ((x==1) || x==2)));
obj=obj.parseconstructorcommon(id, varargin);
obj.modelbus=obj.cparser.Results.modelbus;
obj.node=obj.cparser.Results.node;
switch(obj.node)
case 2
obj.workspacedatabasestructure='SCDsimdata.SCDnode02simdata.ethercat';
otherwise
error('SCDclass_taskmdsloadprevETHCAT:constructor','Class not implemented for node %d',obj.node);
%obj.workspacedatabasestructure='';
end
obj.classname=mfilename;
obj.logdecimate=2;
obj.nint16=20;
obj.nfloat32=10; % not yet implemented
end
function init(obj, shot)
mdsconnect(obj.mdsserver);
mdsopen(obj.mdstree, shot);
try
switch(obj.node)
case 2
obj.actualizeNode(shot);
end
catch ME
warning('SCDclass_taskmdsloadprevETHCAT:init','Error initializing ETHCAT bus for node %d. Error is:\n%s',obj.node,getReport(ME));
end
end
function [obj, value] = getdata(obj,shot)
switch(obj.node)
case 2
[~,value]=obj.getdataNodeETHCAT1(shot);
end
end
function printinfo(obj)
obj.printinfocommon;
fprintf(' Init model bus: %s, node: %d, ws base struct: %s\n',obj.modelbus, obj.node, obj.workspacedatabasestructure);
end
function entrystring = genMARTe2entry(obj, shot)
% TODO: this is a workaround which works by chance, need a more accurate fix
entrystring = '';
end
end
methods(Access = private)
function obj = actualizeNode(obj, shot)
dd=SCDconf_getdatadict(obj.datadictionary);
adcbus=dd.getEntry(obj.modelbus).getValue;
[~,data]=obj.getdata(shot);
T=timeseries;
if obj.verbose==1
fprintf('Actualizing data for bus: ''%s'' (%s, shot %d) 0%% ', obj.modelbus, obj.classname, shot);
end
for ii=1:obj.nint16
T.Time=data.Time;
T.Data=int16(data.Data(:,ii));
assignin('base','temp',T);
assigncmd=sprintf('%s.%s=temp;',obj.workspacedatabasestructure, adcbus.Elements(ii).Name);
evalin('base',assigncmd);
if obj.verbose==1 && mod(ii,obj.logdecimate)==0
fprintf('%.0f%% ', ii/obj.nint16*100);
end
end
evalin('base','clear temp');
if obj.verbose==1
fprintf('100%% \n');
end
end
%%
% Node 02/03 loading functions
function [obj, value] = getdataNodeETHCAT1(obj, shot)
% node02 and 03 store an equal copy of EtherCAT1 input data
% on MDS. Here we take data from the first ok node on MDS.
% This to allow loading in cases where one of the two nodes
% has not stored.
mdsopen('rtc',shot);
mdsnodename='node02';
T=timeseries;
timebase=mdsvalue(['dim_of(\top.' mdsnodename '.ethcat1.in.int16_001)']);
if ~isnumeric(timebase)
mdsnodename='node03';
T=timeseries;
timebase=mdsvalue(['dim_of(\top.' mdsnodename '.ethcat1.in.int16_001)']);
end
if ~isnumeric(timebase)
warning('getdataNodeETHCAT1:NoData', ...
'Could notget EtherCAT1 int16 data either from node02 or node03');
value=[];
return;
end
d_time = double(mdsvalueraw(['\top.' mdsnodename '.params:d_time']))*1.0e-6;
%timebase = round(timebase.*1/d_time)*d_time-d_time/2;
timebase = round(timebase.*1/d_time)*d_time-d_time/100;
T.Time=timebase;
T.Data=zeros(numel(timebase),obj.nint16);
datamatrix=zeros(numel(timebase),obj.nint16);
if obj.verbose==1
fprintf('Getting data for bus: ''%s'' (%s, shot %d) 0%% ', obj.modelbus, obj.classname, shot);
end
for ii=1:obj.nint16
channelstr=sprintf(['\\top.' mdsnodename '.ethcat1.in.int16_%03d.raw'],ii);
datamatrix(:,ii)=int16(mdsvalueraw(channelstr));
if obj.verbose==1 && mod(ii,obj.logdecimate)==0
fprintf('%.0f%% ', ii/192*100);
end
end
T.Data=datamatrix; % this speeds up a lot the loading
value=T;
if obj.verbose==1
fprintf('100%% \n');
end
end
end
end