function varargout = DesignGSP(varargin)
% DESIGNGSP M-file for DesignGSP.fig
%      DESIGNGSP, by itself, creates a new DESIGNGSP or raises the existing
%      singleton*.
%
%      H = DESIGNGSP returns the handle to a new DESIGNGSP or the handle to
%      the existing singleton*.
%
%      DESIGNGSP('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DESIGNGSP.M with the given input arguments.
%
%      DESIGNGSP('Property','Value',...) creates a new DESIGNGSP or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before DesignGSP_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to DesignGSP_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help DesignGSP

% Last Modified by GUIDE v2.5 25-Dec-2011 21:18:37

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DesignGSP_OpeningFcn, ...
                   'gui_OutputFcn',  @DesignGSP_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before DesignGSP is made visible.
function DesignGSP_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to DesignGSP (see VARARGIN)

% Choose default command line output for DesignGSP
handles.output = hObject;

%save handle to this FIGURE to hide 
hDesignGSP = gcf;
setappdata(0,'hDesignGSP',hDesignGSP);

table = zeros(6,3);
b = getappdata( 0 , 'Base_connection_points' );
if ( ~isempty(b) ) 
table = b;
end

for i=1:1:6
    for j=1:1:3
        if ( table(i,j) > -0.0000000001 && table(i,j) < 0.0000000001  ) 
            table(i,j)=0;
        end
    end
end

set(handles.BTable,'Data',table);
Rb = getappdata(0,'Base_radius');
set(handles.edit_Rb , 'String' , Rb );


table = zeros(6,3);
a = getappdata( 0 , 'Moving_connection_points' );
if ( ~isempty(a) ) 
    table = a;
end
for i=1:1:6
    for j=1:1:3
        if ( table(i,j) > -0.0000000001 && table(i,j) < 0.0000000001  ) 
            table(i,j)=0;
        end
    end
end

set(handles.PTable,'Data',table);
Rm = getappdata(0,'Moving_radius');
set(handles.edit_Rm , 'String' , Rm );

table = zeros(6,2);
set(handles.LegLimits,'Data',table);
 
table = zeros(6,4);
set(handles.JointLimits,'Data',table);

set(handles.JointLimits, 'Data', [ -60*ones(6,1) 60*ones(6,1) -45*ones(6,1) 45*ones(6,1) ] );

% LegLimit = str2num( get(handles.txtTz , 'String') ) ;
% set(handles.LegLimits, 'Data', LegLimit*ones(6,2) );
% LegLimits = get(handles.LegLimits,  'Data');
% if  ( sum(sum(LegLimits == zeros(6,2)))==12 )
if ( isempty(Rb) ) Rb=100; end
LegLimits = [ ceil(Rb/1.8) .* ones(6,1) ceil(Rb*3.5/2) .* ones(6,1) ] ;
% end

set(handles.LegLimits, 'Data' , LegLimits ) ;



 guidata(hObject, handles);

% UIWAIT makes DesignGSP wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = DesignGSP_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Save mechanism data to a file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pb_Save_Callback(hObject, eventdata, handles)
%SAVE MECHANISM FILE.
Base.BTable = get(handles.BTable,'Data'); 
Base.Rb = str2num ( get( handles.edit_Rb, 'String') );
% Base.Rb = getappdata(0,'Base_radius');

Platform.PTable = get(handles.PTable,'Data'); 
% Platform.Rm = getappdata(0,'Moving_radius');
Platform.Rm = str2num ( get( handles.edit_Rm, 'String') );

ConMatrix = get(handles.ConnectionTable,'Data');

LegLimits = get(handles.LegLimits, 'Data');
JointLimits = get(handles.JointLimits, 'Data');

Thickness_Base = str2num( get(handles.Thickness_Base, 'String') );
Thickness_Moving = str2num( get(handles.Thickness_Moving, 'String') );

Platform.Px = str2num( get(handles.txtTx, 'String' ));
Platform.Py = str2num(get(handles.txtTy, 'String' ));
Platform.Pz = str2num(get(handles.txtTz, 'String' ));
Platform.alpha = str2num(get(handles.alpha, 'String' ));
Platform.beta  = str2num(get(handles.beta, 'String' ));
Platform.gamma = str2num(get(handles.gamma, 'String'));

Platform.Translate = [ Platform.Px ; Platform.Py  ; Platform.Pz ];
Platform.Rotation = Rotation( Platform.alpha, Platform.beta, Platform.gamma , 'RPY' ) ;

Limitation = getappdata(0,'Limitation');   
   
[mechanism_file,path] = uiputfile('*.mat','Save Mechanism as a File');
if ( mechanism_file==0 ) return; end
filename = ['filename=' mechanism_file(1:(length(mechanism_file)-4) ) ];
set( handles.txt_Filename , 'String' , filename );

save([ path mechanism_file], 'Base' , 'Platform' , 'ConMatrix','LegLimits','JointLimits','Thickness_Base','Thickness_Moving', 'Limitation');

%DO NOT Use global.... GET&SET Appdata 
setappdata( 0 , 'Base' , Base );
setappdata( 0 , 'Platform' ,Platform );
setappdata( 0 , 'ConMatrix' , ConMatrix );
setappdata( 0 , 'LegLimits' , LegLimits );
setappdata( 0 , 'JointLimits' , JointLimits );
setappdata( 0 , 'Thickness_Base', Thickness_Base );
setappdata( 0 , 'Thickness_Moving', Thickness_Moving );
setappdata( 0 , 'Limitation' , Limitation);

%%%% Hide DesignGSP window and show StewSim (welcome screen).
hStewSIM= getappdata(0,'hStewSIM');

if ( ishandle(hStewSIM) == 1 ) 
    set(hStewSIM,'visible','off');
end

% hDesignGSP = getappdata(0,'hDesignGSP');
% set(hDesignGSP,'visible','off');
% delete(hDesignGSP);
%ters_kinematik_gui_anaprog
%%guidata(hObject, handles);

% AnalysisGSP;
%%openfig('AnalysisGSP.fig','new');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Load mechanism data from a file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pb_Load_Callback(hObject, eventdata, handles)
[ mechanism_file , pathname ]  = uigetfile('*.mat'); 

filename = ['filename=' mechanism_file(1:(length(mechanism_file)-4) ) ];
set( handles.txt_Filename , 'String' , filename );
% load(mechanism_file);
load([ pathname mechanism_file ]);

table = Base.BTable;
for i=1:1:6
    for j=1:1:3
        if ( table(i,j) > -0.0000000001 && table(i,j) < 0.0000000001  ) 
            table(i,j)=0;
        end
    end
end
Base.BTable = table;

set(handles.BTable,'Data', Base.BTable);
set(handles.edit_Rb,'String', num2str( Base.Rb ) ) ;

table = Platform.PTable;
for i=1:1:6
    for j=1:1:3
        if ( table(i,j) > -0.0000000001 && table(i,j) < 0.0000000001  ) 
            table(i,j)=0;
        end
    end
end
Platform.PTable = table;
set(handles.PTable,'Data', Platform.PTable);


set(handles.edit_Rm,'String', num2str( Platform.Rm ) ) ;
set(handles.ConnectionTable,'Data', ConMatrix);

set(handles.LegLimits, 'Data', LegLimits);
set(handles.JointLimits, 'Data', JointLimits);

set(handles.Thickness_Base, 'String' , num2str(Thickness_Base) );
set(handles.Thickness_Moving, 'String' , num2str(Thickness_Moving) );

set(handles.txtTx, 'String', num2str(Platform.Px));
set(handles.txtTy, 'String', num2str(Platform.Py));
set(handles.txtTz, 'String', num2str(Platform.Pz));
set(handles.alpha, 'String', num2str(Platform.alpha));
set(handles.beta,  'String', num2str(Platform.beta));
set(handles.gamma, 'String', num2str(Platform.gamma));

% handles.platform.currentPx = 0;
% handles.platform.currentPy = 0;
% handles.platform.currentPz = 25;
% handles.platform.currentAlpha = 0;
% handles.platform.currentBeta = 0;
% handles.platform.currentGamma = 0;
% %translation of mobile platform according to base
% handles.translation.Tx = handles.platform.currentPx;
% handles.translation.Ty = handles.platform.currentPy;
% handles.translation.Tz = handles.platform.currentPz;
Platform.Translate = [ Platform.Px ; Platform.Py  ; Platform.Pz ];

Platform.Rotation = Rotation( Platform.alpha, Platform.beta, Platform.gamma , 'RPY' ) ;

%DO NOT Use global.... GET&SET Appdata 
setappdata( 0 , 'Base' , Base );
setappdata( 0 , 'Platform' ,Platform );
setappdata( 0 , 'ConMatrix' , ConMatrix );
setappdata( 0 , 'LegLimits' , LegLimits );
setappdata( 0 , 'JointLimits' , JointLimits );
setappdata( 0 , 'Thickness_Base', Thickness_Base );
setappdata( 0 , 'Thickness_Moving', Thickness_Moving );
setappdata( 0 , 'Limitation' , Limitation);
   
guidata(hObject, handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Plot GSP model on axes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pb_Plot_Callback(hObject, eventdata, handles)
%getappdata is not adaptive.. if the editboxes altered and not click save
%button then getappdata is get the older datas
% Base = getappdata( 0 , 'Base' );
% Platform = getappdata( 0 , 'Platform' );
% ConMatrix = getappdata( 0 , 'ConMatrix' );
% LegLimits = getappdata( 0 , 'LegLimits' );
% JointLimits = getappdata( 0 , 'JointLimits' );
% Thickness_Base = getappdata( 0 , 'Thickness_Base' );
% Thickness_Moving = getappdata( 0 , 'Thickness_Moving' );

cla;
% figure( 3);

Base.BTable = get(handles.BTable,'Data');
if ( isempty(Base) == 1 ) ,     return;  end
Base.Rb = str2double( get(handles.edit_Rb, 'String') );

Platform.PTable = get(handles.PTable,'Data');
if ( isempty(Platform.PTable) == 1 ) ,     return;  end
Platform.Rm = str2double( get(handles.edit_Rm, 'String') );

ConMatrix = get(handles.ConnectionTable,'Data');
if ( isempty(ConMatrix) == 1 ) ,     return;  end

LegLimits = get(handles.LegLimits, 'Data');
if ( isempty(LegLimits) == 1 ) ,     return;  end

JointLimits = get(handles.JointLimits, 'Data');
if ( isempty(JointLimits) == 1 ) ,     return;  end

Thickness_Base = str2num( get(handles.Thickness_Base, 'String') );
Thickness_Moving = str2num( get(handles.Thickness_Moving, 'String') );

Platform.Px = str2num( get(handles.txtTx, 'String') );
Platform.Py = str2num( get(handles.txtTy, 'String') );
Platform.Pz = str2num( get(handles.txtTz, 'String') );
Platform.alpha = str2num( get(handles.alpha, 'String') );
Platform.beta  = str2num( get(handles.beta, 'String') );
Platform.gamma = str2num( get(handles.gamma, 'String') );

Platform.Translate = [ Platform.Px; Platform.Py; Platform.Pz ];
Platform.Rotation = Rotation( Platform.alpha, Platform.beta, Platform.gamma , 'RPY' ) ;
[ Leg, Platform.Pbase, ErrorMsg ]= inverse_kinematic(Base, Platform, ConMatrix, LegLimits, JointLimits );
          
% handles = Plot_Mechanism( handles );
               
%axes(handles.axis2);
%h=figure(2); ha = axes;
% grid on;
rotate3d on;zlabel('Z');ylabel('Y');xlabel('X');
%%axis([-50,50,-50,50,0,50,0,50]);
%%campos([ -30 , -60 , 60 ]); 
campos('auto');

try
    ishandle( handles.platform_handle );
    delete( handles.platform_handle);
    handles.platform_handle  = tabla_cizimi_genel( Platform.Pbase, -Thickness_Moving , [ 255/255, 242/255, 221/255] );
catch exception
    handles.platform_handle  = tabla_cizimi_genel( Platform.Pbase, -Thickness_Moving , [ 255/255, 242/255, 221/255] );
end

try      
    ishandle( handles.base_handle );
    delete( handles.base_handle );
    handles.base_handle  = tabla_cizimi_genel ( Base.BTable , Thickness_Base , [ 222/255, 235/255, 250/255 ] );
catch exception
    handles.base_handle  = tabla_cizimi_genel ( Base.BTable , Thickness_Base , [ 222/255, 235/255, 250/255 ] );
end

% try      
%     ishandle( handles.base_handle );
%     delete( handles.base_handle );
%     handles.base_handle  = tabla_cizimi_genel ( Base , Thickness_Base , [ 222/255, 235/255, 250/255 ]);
% catch exception
%     handles.base_handle  = tabla_cizimi_genel ( Base , Thickness_Base , [ 222/255, 235/255, 250/255 ] );
% end

try      
    ishandle( handles.Base.Btext );
    delete( handles.Base.Btext );
    handles.Base.Btext = 0;
catch exception
    handles.Base.Btext = 0;
end

try      
    ishandle( handles.Platform.Ptext );
    delete( handles.Platform.Ptext );
    handles.Platform.Ptext = 0;
catch exception
    handles.Platform.Ptext = 0;
end

try      
    ishandle( handles.Leg.Ltext );
    delete( handles.Leg.Ltext );
    handles.Leg.Ltext = 0;
catch exception
    handles.Leg.Ltext = 0;
end

try      
    ishandle( handles.Leg.Leg_handle );
    delete( handles.Leg.Leg_handle );
    handles.Leg.Leg_handle = 0;
catch exception
    handles.Leg.Leg_handle = 0;
end

    
for index = 1:1:6
       
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         
       %B nokta isimlerinin yazd?r?lmas?
       B = Base.BTable(index,:);
       txt = ['\bf\fontname{Tahoma}\fontsize{12}\color{black}B_' int2str(index)];
       handles.Base.Btext(index)=text(B(1),B(2),B(3)+10,txt );
       
       
       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       %P nokta isimlerinin yazd?r?lmas?
       P = Platform.Pbase(index,:);
       txt = ['\bf\fontname{Tahoma}\fontsize{12}\color{black}M_' int2str(index)];
       handles.Platform.Ptext(index)=text( P(1),P(2),P(3)+10,txt );
       

end


     
for index = 1:1:6
    if ~isnan(ConMatrix(1,index)) && ~isnan(ConMatrix(2,index))


        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        % Bacaklar?n çizilmesi
        % draw legs as line with the use of conMatrix
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
            handles.Leg.Leg_handle(index) = line(...
            'XData',[Base.BTable(ConMatrix(1,index),1); Platform.Pbase(ConMatrix(2,index),1)],...
            'YData',[Base.BTable(ConMatrix(1,index),2); Platform.Pbase(ConMatrix(2,index),2)],...
            'ZData',[Base.BTable(ConMatrix(1,index),3); Platform.Pbase(ConMatrix(2,index),3)],...
            'Color',[161/221,172/255,86/255],'LineWidth',5,'Marker','o','MarkerFaceColor','y','MarkerSize',15   );

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        %inverse kinematics = ters kinematik hesab?
        handles.Leg.L(index) = norm ( Platform.Pbase(ConMatrix(2,index),:) - Base.BTable(ConMatrix(1,index),:) );

       
       %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
       %L bacak isimlerinin yazd?r?lmas?

       %önce bacak vektörünün do?rultusundaki birim vektör hesaplan?r
       L_vector = Platform.Pbase(ConMatrix(2,index),:) - Base.BTable(ConMatrix(1,index),:);

       unit_Lvector =  L_vector / norm ( L_vector ) ;
        %bacak birim vektörün yar? uzunlu?undaki noktaya L ismi yazd?r?l?r.
        nokta = unit_Lvector * handles.Leg.L(index)/2 + ...
                Base.BTable(ConMatrix(1,index),:); 

        txt = ['\bf\fontname{Tahoma}\fontsize{12}\color{black}L_' int2str(index)];
        handles.Leg.Ltext(index)=text( nokta(1),nokta(2)+5,nokta(3), txt);
        
        
        nokta = unit_Lvector * handles.Leg.L(index) + ...
                 Base.BTable(ConMatrix(1,index),:);
             
             
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        % Bacaklar?n çizilmesi
        % draw legs as line with the use of conMatrix
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
            handles.Leg.Leg_handle2(index) = line(...
            'XData',[Base.BTable(ConMatrix(1,index),1); nokta(1)],...
            'YData',[Base.BTable(ConMatrix(1,index),2); nokta(2)],...
            'ZData',[Base.BTable(ConMatrix(1,index),3); nokta(3)],...
            'Color',[1/221,1/255,86/255],'LineWidth',3,'Marker','o','MarkerFaceColor','g','MarkerSize',5);

        

        
    else
        handles.Leg_handle(index) = 0;
    end
end


get(handles.txtNumber,'String');
[ sizeNaN , LegNumber ] = LegNumbers( ConMatrix );
msg = sprintf('Number of Leg=%d',LegNumber);
set(handles.txtNumber,'String', msg );


% 
% set(gca,'YTick',[])
% set(gca,'XTick',[])
% set(gca,'ZTick',[])



guidata(hObject, handles);

 
function pushbutton1_Callback(hObject, eventdata, handles)
set(handles.checkbox1,'Value',0);
set(handles.checkbox2,'Value',0);
set(handles.checkbox3,'Value',0);
set(handles.checkbox4,'Value',0);
set(handles.checkbox5,'Value',0);
set(handles.checkbox6,'Value',0);
%table = get(handles.BTable,'Data');
%table=-50+rand(6,2)*100; table(:,3)=0;
%table = sortrows(table);
table= zeros(6,3);
set(handles.BTable,'Data',table)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BASE NAN Checkbox
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function checkbox1_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.BTable,'Data');
if (checked == 1 )
    table(1,:)=NaN;
else table(1,:)=0;
end
set(handles.BTable,'Data',table)


function checkbox2_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.BTable,'Data');
if (checked == 1 )
    table(2,:)=NaN;
else table(2,:)=0;
end
set(handles.BTable,'Data',table)


function checkbox3_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.BTable,'Data');
if (checked == 1 )
    table(3,:)=NaN;
else table(3,:)=0;
end
set(handles.BTable,'Data',table)

function checkbox4_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.BTable,'Data');
if (checked == 1 )
    table(4,:)=NaN;
else table(4,:)=0;
end
set(handles.BTable,'Data',table)

function checkbox5_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.BTable,'Data');
if (checked == 1 )
    table(5,:)=NaN;
else table(5,:)=0;
end
set(handles.BTable,'Data',table)

function checkbox6_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.BTable,'Data');
if (checked == 1 )
    table(6,:)=NaN;
else table(6,:)=0;
end
set(handles.BTable,'Data',table)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Random values for platform connection points data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pushbutton2_Callback(hObject, eventdata, handles)
set(handles.checkbox8,'Value',0);
set(handles.checkbox9,'Value',0);
set(handles.checkbox10,'Value',0);
set(handles.checkbox11,'Value',0);
set(handles.checkbox12,'Value',0);
set(handles.checkbox13,'Value',0);
table = get(handles.PTable,'Data');
table=-50+rand(6,2)*100; table(:,3)=0;
table = sortrows(table);
set(handles.PTable,'Data',table);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PLATFORM NAN Checkbox
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function checkbox8_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.PTable,'Data');
if (checked == 1 )
    table(1,:)=NaN;
else table(1,:)=0;
end
set(handles.PTable,'Data',table)

function checkbox9_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.PTable,'Data');
if (checked == 1 )
    table(2,:)=NaN;
else table(2,:)=0;
end
set(handles.PTable,'Data',table)

function checkbox10_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.PTable,'Data');
if (checked == 1 )
    table(3,:)=NaN;
else table(3,:)=0;
end
set(handles.PTable,'Data',table)

function checkbox11_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.PTable,'Data');
if (checked == 1 )
    table(4,:)=NaN;
else table(4,:)=0;
end
set(handles.PTable,'Data',table)

function checkbox12_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.PTable,'Data');
if (checked == 1 )
    table(5,:)=NaN;
else table(5,:)=0;
end
set(handles.PTable,'Data',table)

function checkbox13_Callback(hObject, eventdata, handles)
checked = get(hObject,'Value');
table = get(handles.PTable,'Data');
if (checked == 1 )
    table(6,:)=NaN;
else table(6,:)=0;
end
set(handles.PTable,'Data',table)


function pushbutton3_Callback(hObject, eventdata, handles)
table = get(handles.ConnectionTable,'Data');
table=ceil(rand(2,6)*6);
table = sortrows(table);
set(handles.ConnectionTable,'Data',table)


function ConnectionTable_CreateFcn(hObject, eventdata, handles)
table = get(hObject,'Data');
table=ones(2,6)*NaN;
set(hObject,'Data',table)

function BTable_CreateFcn(hObject, eventdata, handles)
table = get(hObject,'Data');
table=-50+rand(6,2)*100; table(:,3)=0;
set(hObject,'Data',table)


function PTable_CreateFcn(hObject, eventdata, handles)
table = get(hObject,'Data');
table=-50+rand(6,2)*100; table(:,3)=0;
set(hObject,'Data',table)

function pushbutton1_CreateFcn(hObject, eventdata, handles)
function pushbutton2_CreateFcn(hObject, eventdata, handles)
function pushbutton3_CreateFcn(hObject, eventdata, handles)
function pb_Save_CreateFcn(hObject, eventdata, handles)






function pushbutton8_Callback(hObject, eventdata, handles)


function Thickness_Base_Callback(hObject, eventdata, handles)
% get(hObject,'String') returns contents of txtTx as text
% str2double(get(hObject,'String')) returns contents of txtTx as a double

function Thickness_Base_CreateFcn(hObject, eventdata, handles)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function Thickness_Moving_Callback(hObject, eventdata, handles)


function Thickness_Moving_CreateFcn(hObject, eventdata, handles)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function Jointmax_Callback(hObject, eventdata, handles)
% hObject    handle to Jointmax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Jointmax as text
%        str2double(get(hObject,'String')) returns contents of Jointmax as a double


% --- Executes during object creation, after setting all properties.
function Jointmax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Jointmax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Jointmin_Callback(hObject, eventdata, handles)
% hObject    handle to Jointmin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Jointmin as text
%        str2double(get(hObject,'String')) returns contents of Jointmin as a double



% --- Executes during object creation, after setting all properties.
function Jointmin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Jointmin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
%%%% Hide DesignGSP window and show StewSim (welcome screen).

delete(hObject);
hStewSIM= getappdata(0,'hStewSIM');
if ( ishandle( hStewSIM ) == 1 ) 
    set(hStewSIM,'visible','on');
else
   close;   
   StewSIM_1;   
end




% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%%%% Hide DesignGSP window and show StewSim (welcome screen).
hStewSIM= getappdata(0,'hStewSIM');
set(hStewSIM,'visible','on');
hDesignGSP = getappdata(0,'hDesingGSP');
set(hDesignGSP,'visible','off');
%delete(hDesignGSP);
%ters_kinematik_gui_anaprog

% --- Executes when entered data in editable cell(s) in ConnectionTable.
function ConnectionTable_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to ConnectionTable (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)


% --- Executes when selected cell(s) is changed in ConnectionTable.
function ConnectionTable_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to ConnectionTable (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function text3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes when entered data in editable cell(s) in LegLimits.
function LegLimits_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to LegLimits (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)



function txtTz_Callback(hObject, eventdata, handles)
% hObject    handle to txtTz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtTz as text
%        str2double(get(hObject,'String')) returns contents of txtTz as a double

Pz = str2num ( get(hObject,'String') );
% 
% 
% LegLimits = get(handles.LegLimits,  'Data');
% % if  ( sum(sum(LegLimits == zeros(6,2)))==12 )
%     LegLimits = [ ceil(Pz/1.8) .* ones(6,1) ceil(Pz*3.5/2) .* ones(6,1) ] ;
% % end
% 
% set(handles.LegLimits, 'Data' , LegLimits ) ;



function alpha_Callback(hObject, eventdata, handles)
% hObject    handle to alpha (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of alpha as text
%        str2double(get(hObject,'String')) returns contents of alpha as a double



function beta_Callback(hObject, eventdata, handles)
% hObject    handle to beta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of beta as text
%        str2double(get(hObject,'String')) returns contents of beta as a double



function gamma_Callback(hObject, eventdata, handles)
% hObject    handle to gamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of gamma as text
%        str2double(get(hObject,'String')) returns contents of gamma as a double



function txtTx_Callback(hObject, eventdata, handles)
% hObject    handle to txtTx (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtTx as text
%        str2double(get(hObject,'String')) returns contents of txtTx as a double



function txtTy_Callback(hObject, eventdata, handles)
% hObject    handle to txtTy (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtTy as text
%        str2double(get(hObject,'String')) returns contents of txtTy as a double



function edit_Rb_Callback(hObject, eventdata, handles)
% hObject    handle to edit_Rb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_Rb as text
%        str2double(get(hObject,'String')) returns contents of edit_Rb as a double
Rb = str2double(get(hObject,'String'));
setappdata(0, 'Rb', Rb );

Base = getappdata(0, 'Base' );
Base.Rb = Rb;
setappdata( 0 , 'Base' , Base );




% --- Executes during object creation, after setting all properties.
function edit_Rb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_Rb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_Rm_Callback(hObject, eventdata, handles)
% hObject    handle to edit_Rm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_Rm as text
%        str2double(get(hObject,'String')) returns contents of edit_Rm as a double
Rm = str2double(get(hObject,'String'));
setappdata(0, 'Rm', Rm );

Platform = getappdata(0, 'Platform' );
Platform.Rm = Rm;
setappdata( 0 , 'Platform' , Platform );


% --- Executes during object creation, after setting all properties.
function edit_Rm_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_Rm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
