classdef Techno_DCS<handle
    properties
        Serial_Obj;
        Kp_values;
        Ki;
        ls_index;
        ls_index_cl;
        legend_string;
        legend_string_cl;
        max_time;
        max_time_cl;
        start_voltage;
        ref_voltage;
        plt_index;
        plt_index_cl;
        plot_handles;
        plot_handles_cl;
        test_data;
        test_result;
        test_result_cl;
        result_table;
        result_table_cl;
        volt_rpm;
    end
    methods
        function obj=Techno_DCS(com_port)
            clc;
            close all;
            close_override();
            obj.Serial_Obj=serial_init(com_port);
            obj.Kp_values
            obj.Kp_values=[2, 3, 4];
            obj.Ki=0;
            obj.ls_index=1;
            obj.ls_index_cl=1;
            obj.max_time=25;
            obj.max_time_cl=20;
            obj.legend_string={};
            obj.legend_string_cl={};
            obj.start_voltage=0;
            obj.ref_voltage=0.5;
            obj.write_voltage(obj.start_voltage);%obj.set_rpm(obj.start_voltage);
            obj.test_result=[];
            obj.test_result_cl=[];
            obj.test_data=[];
            obj.plt_index=1;
            obj.plt_index_cl=1;
            obj.plot_handles=zeros(100,1);
            obj.plot_handles_cl=zeros(100,1);
            obj.result_table=[];
            obj.result_table_cl=[];
            
        end
        function write_voltage(obj,value)
            write_volt(obj.Serial_Obj,value);
        end
        function reset(obj)
            obj.Kp_values=[2 2.5 3 3.5 4];
            obj.Ki=0;
            obj.ls_index=1;
            obj.ls_index_cl=1;
            obj.max_time=25;
            obj.max_time_cl=20;
            obj.legend_string={};
            obj.legend_string_cl={};
            obj.start_voltage=0;
            obj.ref_voltage=0.5;
            obj.write_voltage(obj.start_voltage);
            obj.test_result=[];
            obj.test_result_cl=[];
            obj.test_data=[];
            obj.plt_index=1;
            obj.plt_index_cl=1;
            obj.plot_handles=zeros(100,1);
            obj.plot_handles_cl=zeros(100,1);
            obj.result_table=[];
            obj.result_table_cl=[];
                
        end
        function set_start(obj,value)
            obj.start_voltage=round_val(set_variable(value),1);
        end
        function motor_open_loop(obj)
                [obj.test_result,obj.legend_string,obj.ls_index,obj.plot_handles,obj.plt_index] = open_loop(obj.Serial_Obj,obj.Kp_values,obj.ref_voltage,obj.max_time,obj.legend_string,obj.ls_index,obj.plt_index,obj.test_result);
                obj.result_table=array2table(obj.test_result,'VariableNames',{'Serial_No','Drive_V','SS_Tacho_Output_V','SS_Speed_N_rpm','Time_Constant_T_sec','Motor_Gain_Km_rpm_per_volt'})
                obj.volt_rpm=fit(obj.test_result(:,3),obj.test_result(:,4),'poly1');
        end
        
        function motor_closed_loop(obj)
                [obj.test_result_cl,obj.legend_string_cl,obj.ls_index_cl,obj.plot_handles_cl,obj.plt_index_cl] = closed_loop(obj.Serial_Obj,obj.Kp_values,obj.Ki,obj.ref_voltage,obj.max_time_cl,obj.legend_string_cl,obj.ls_index_cl,obj.plt_index_cl,obj.test_result_cl,obj.volt_rpm);
                obj.result_table_cl=array2table(obj.test_result_cl,'VariableNames',{'Serial_No','Kp','Ki','SS_Tacho_Output_V','SS_Speed_N_rpm','Rise_Time_T_sec','SS_Error_volt'})
        end
        function set_ref(obj,value)
                temp=set_variable(value);
                obj.ref_voltage=round_val(temp,1);        
        end
        function set_max_time(obj,value)
            obj.max_time=set_variable(value);
        end
        
        function set_P(obj,val_P)
            obj.Kp_values=set_variable(val_P);
        end
        function set_I(obj,val_I)
            obj.Ki=set_variable(val_I);
        end
       
        function close(obj)
            closing(obj.Serial_Obj);
        end
            function export_data(obj)
            
                dname = uigetdir(matlabroot, 'Techno_DCS.xlsx');
                filenameData = 'Techno_DCS_Result_Open_Loop.xlsx'
                filenameResult = 'Techno_DCS_Results_Closed_Loop.xlsx'
                dnameData = fullfile(dname,filenameData);
                dnameResult = fullfile(dname,filenameResult);
                writetable(obj.data_table,dnameData,'Sheet',1,'Range','D5');
                writetable(obj.result_table,dnameResult,'Sheet',1,'Range','D5');
               
            
            end
            
        
        
    end
        
    
end