classdef Techno_DCP<handle
    properties
        Serial_Obj
        Kp; 
        Ki;
        Kd;
        legend_string;
        ls_index;
        plt_index;
        ref_deg;
        t_arr;
        deg_arr;
        max_time;
        overshoot;
        steady_state_error;
        rise_time;
        max_angle;
        test_data;
        start_angle;
        test_result;
        data_table;
        result_table;
        tacho_gain;
        plot_handles;
    end
    methods
        function obj=Techno_DCP(com_port)
            close_override();
            obj.Serial_Obj=serial_init(com_port);
            obj.Kp=0.5;
            obj.Ki=0.001;
            obj.Kd=0.5;
            obj.ls_index=1;
            obj.max_time=10;
            obj.legend_string={};
            set_deg(obj.Serial_Obj,100);
            obj.start_angle=100;
            obj.ref_deg=200;
            obj.t_arr=[];
            obj.deg_arr=[];
            obj.test_result=[];
            obj.test_data=[];
            obj.tacho_gain=0;
            obj.plt_index=1;
            obj.plot_handles=zeros(100,1);   
        end
        function reset(obj)
                close all;
                clc;
                obj.Kp=0.5;
                obj.Ki=0.001;
                obj.Kd=0.5;
                obj.ref_deg=200;
                obj.t_arr=[];
                obj.deg_arr=[];
                obj.max_time=10;
                obj.ls_index=1;
                obj.legend_string={};
                obj.start_angle=100;
                obj.set_angle(obj.start_angle);
                obj.test_result=[];
                obj.data_table=[];
                obj.result_table=[];
                obj.overshoot=0;
                obj.rise_time=0;
                obj.steady_state_error=0;
                obj.tacho_gain=0;
                obj.plot_handles=zeros(100,1);
                obj.plt_index=1;
        end
        
        function set_tacho(obj,value)
            obj.tacho_gain=set_variable(value);
        end
        function set_start(obj,value)
            obj.start_angle=round_val(set_variable(value),1);
            obj.set_angle(obj.start_angle);
        end
        function set_ref(obj,value)
                temp=set_variable(value);
                obj.ref_deg=round_val(temp,1);
                if(ishandle(1)==0 && obj.ls_index~=1)
                    obj.ls_index=1;
                    obj.legend_string=[];
                end
                str0=[strcat('Ref.Temp: ',num2str(value), ' degree')];
                obj.legend_string{obj.ls_index}=str0;
                obj.ls_index=obj.ls_index+1;
                graph_init(obj.legend_string,value,obj.max_time);
        end
        function set_max_time(obj,value)
            obj.max_time=set_variable(value);
        end
        
          function set_P(obj,val_P)
            obj.Kp=set_variable(val_P);
        end
        function set_I(obj,val_I)
            obj.Ki=set_variable(val_I);
        end
        function set_D(obj,val_D)
            obj.Kd=set_variable(val_D);
        end
        function curr_angle=read_angle(obj)
            [curr_tacho,curr_angle]=read_sig(obj.Serial_Obj);
        end
        function start_resp(obj)
            [obj.deg_arr,obj.t_arr,obj.test_data,obj.legend_string,obj.ls_index,obj.plot_handles,obj.plt_index]=store_sig(obj.Kp,obj.Ki,obj.Kd,obj.Serial_Obj,obj.ref_deg,obj.ls_index,obj.legend_string,obj.max_time,obj.tacho_gain,obj.plt_index);
            obj.set_angle(obj.start_angle);
            [obj.overshoot,t_max,maxim]=calc_overshoot(obj.deg_arr,obj.test_data);
            obj.steady_state_error=calc_steady_state_error(obj.deg_arr,obj.ref_deg);
            [obj.rise_time,temp_r]=calc_rise_time(obj.deg_arr,obj.test_data);
            obj.rise_time=obj.rise_time-obj.t_arr(1);
            display_result(obj.rise_time,obj.overshoot,obj.steady_state_error);
            txt0=sprintf('Rise Time: %0.1f sec',round_val(obj.rise_time,1));
            txt1=sprintf('Overshoot:%.2f percent',round_val(obj.overshoot,2));
            txt2=sprintf('Steady State error: %.2f deg ',round_val(obj.steady_state_error,2));
            str=sprintf('PID Response with Kp:%.4f Ki:%.4f Kd:%.4f Tacho: %0.4f',obj.Kp,obj.Ki,obj.Kd,obj.tacho_gain);
            obj.test_result=final_result(obj.Kp,obj.Ki,obj.Kd,obj.tacho_gain,obj.rise_time,obj.overshoot,obj.steady_state_error,obj.test_result,obj.deg_arr(1),obj.ref_deg,obj.deg_arr(length(obj.deg_arr)));
            obj.rise_time=obj.rise_time+obj.t_arr(1);
            plot_result(obj.deg_arr,obj.t_arr,obj.rise_time,temp_r,t_max,maxim,str,txt0,txt1,txt2,obj.max_time,obj.ref_deg);
            obj.data_table=array2table(obj.test_data,'VariableNames',{'Time_Sec','Deg'});
            obj.result_table=array2table(obj.test_result,'VariableNames',{'Serial_No','Start_Angle_Deg','Target_Angle_Deg','Final_Angle_Deg','Kp','Ki','Kd','Tacho','Rise_Time_sec','Overshoot_percent','Steady_State_Error'});
            disp(obj.result_table);
            obj.deg_arr=[];
            obj.t_arr=[];
            obj.test_data=[];
        end
        function set_angle(obj,value)

            set_deg(obj.Serial_Obj,value);
            
        end
        function close(obj)
            closing(obj.Serial_Obj);
        end
        function delete_latest(obj)
            [obj.result_table,obj.test_result,obj.ls_index,obj.plot_handles,obj.legend_string,obj.plt_index]=delete_entry(obj.result_table,obj.test_result,obj.ls_index,obj.plot_handles,obj.legend_string,obj.plt_index);
            
        end
            function export_data(obj)
                dname = uigetdir(matlabroot, 'Techno_DCP.xlsx');
                filenameData = 'Techno_DCP_Data.xlsx'
                filenameResult = 'Techno_DCP_Result.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