Skip to content
Snippets Groups Projects
SCDclass_taskmdsloadprevETHCAT.m 4.79 KiB
Newer Older
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);
            
                switch(obj.node)
                    case 2
                        obj.actualizeNode(shot); 
                end
            catch
                fprintf('\n');
                warning('SCDclass_taskmdsloadprevETHCAT:init','Error initializing ETHCAT bus for node %d.',obj.node);
            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 loading functions
        
        function [obj, value] = getdataNodeETHCAT1(obj, shot)
            mdsnodename='node02';
            mdsopen('rtc',shot);
            T=timeseries;
            timebase=mdsvalue(['dim_of(\top.' mdsnodename '.ethcat1.in.int16_001)']);
            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