function varargout = Cbot2DGUI(varargin)
% CBOT2DGUI M-file for Cbot2DGUI.fig
%      CBOT2DGUI, by itself, creates a new CBOT2DGUI or raises the existing
%      singleton*.
%
%      H = CBOT2DGUI returns the handle to a new CBOT2DGUI or the handle to
%      the existing singleton*.
%
%      CBOT2DGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CBOT2DGUI.M with the given input arguments.
%
%      CBOT2DGUI('Property','Value',...) creates a new CBOT2DGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Cbot2DGUI_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Cbot2DGUI_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

% Copyright 2002-2003 The MathWorks, Inc.

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

% Last Modified by GUIDE v2.5 22-Feb-2005 17:23:06

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Cbot2DGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @Cbot2DGUI_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 Cbot2DGUI is made visible.
function Cbot2DGUI_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 Cbot2DGUI (see VARARGIN)

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

handles.ConfigFile = 'Cbot2D_Config';
handles.q = linspace(0,pi/2,100)';
handles.F = 0;
handles.N = 0;
handles.L = 0;
handles.Lp = 0;
handles.limit = 0;

axes(handles.Model)
axis auto
hold on
title('Model')

axes(handles.Normal)
axis auto
hold on
title('Normal')

axes(handles.Tangent)
axis auto
hold on
title('Tangent')

axes(handles.Stability)
axis auto
hold on
title('Stability')

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Cbot2DGUI_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;



function m_Callback(hObject, eventdata, handles)
% hObject    handle to m (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 m as text
%        str2double(get(hObject,'String')) returns contents of m as a double
handles=guidata(hObject);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function m_CreateFcn(hObject, eventdata, handles)
% hObject    handle to m (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 y1_Callback(hObject, eventdata, handles)
% hObject    handle to y1 (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 y1 as text
%        str2double(get(hObject,'String')) returns contents of y1 as a double
handles=guidata(hObject);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function y1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to y1 (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 y2_Callback(hObject, eventdata, handles)
% hObject    handle to y2 (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 y2 as text
%        str2double(get(hObject,'String')) returns contents of y2 as a double
handles=guidata(hObject);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function y2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to y2 (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 h_Callback(hObject, eventdata, handles)
% hObject    handle to h (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 h as text
%        str2double(get(hObject,'String')) returns contents of h as a double
handles=guidata(hObject);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function h_CreateFcn(hObject, eventdata, handles)
% hObject    handle to h (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 mu1_Callback(hObject, eventdata, handles)
% hObject    handle to mu1 (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 mu1 as text
%        str2double(get(hObject,'String')) returns contents of mu1 as a double
handles=guidata(hObject);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function mu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mu1 (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 mu2_Callback(hObject, eventdata, handles)
% hObject    handle to mu2 (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 mu2 as text
%        str2double(get(hObject,'String')) returns contents of mu2 as a double
handles=guidata(hObject);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function mu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mu2 (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 alpha1_Callback(hObject, eventdata, handles)
% hObject    handle to alpha1 (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 alpha1 as text
%        str2double(get(hObject,'String')) returns contents of alpha1 as a double
handles=guidata(hObject);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function alpha1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to alpha1 (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 alpha2_Callback(hObject, eventdata, handles)
% hObject    handle to alpha2 (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 alpha2 as text
%        str2double(get(hObject,'String')) returns contents of alpha2 as a double
handles=guidata(hObject);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function alpha2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to alpha2 (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 on button press in Load.
function Load_Callback(hObject, eventdata, handles)
% hObject    handle to Load (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
rehash
string=strcat(get(handles.Load_text,'String'),'.m');
if(fopen(string,'r')==-1)
    makeConfigFile(hObject,handles);
end
rehash
eval(get(handles.Load_text,'String'));
set(handles.m,'String',num2str(m));
set(handles.y1,'String',num2str(y1));
set(handles.y2,'String',num2str(y2));
set(handles.h,'String',num2str(-z1));
set(handles.mu1,'String',num2str(mu1));
set(handles.mu2,'String',num2str(mu2));
set(handles.alpha1,'String',num2str(alpha1));
set(handles.alpha2,'String',num2str(alpha2));
handles.ConfigFile = get(handles.Load_text,'String');
guidata(hObject,handles);
plotModel(hObject,handles);
guidata(hObject,handles);

% --- Executes on button press in Save.
function Save_Callback(hObject, eventdata, handles)
% hObject    handle to Save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
clear handles.ConfigFile;
handles.ConfigFile=get(handles.Save_text,'String');
guidata(hObject,handles);
makeConfigFile(hObject,handles);
plotModel(hObject,handles);
guidata(hObject,handles);


function Load_text_Callback(hObject, eventdata, handles)
% hObject    handle to Load_text (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 Load_text as text
%        str2double(get(hObject,'String')) returns contents of Load_text as a double
handles=guidata(hObject);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function Load_text_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Load_text (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 Save_text_Callback(hObject, eventdata, handles)
% hObject    handle to Save_text (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 Save_text as text
%        str2double(get(hObject,'String')) returns contents of Save_text as a double
handles=guidata(hObject);
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function Save_text_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Save_text (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 on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject    handle to Calculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
handles.ConfigFile = 'Cbot2D_Config';
guidata(hObject,handles);
makeConfigFile(hObject,handles);
rehash
[handles.F,handles.L,handles.N,handles.Lp]=Cbot2D(handles.q,handles.ConfigFile);
handles.limit=NaN;
for i=2:size(handles.L(2,:),2)
    if(handles.L(2,i)<0)
        m = (handles.L(2,i)-handles.L(2,i-1))/(handles.q(i)-handles.q(i-1));
        handles.limit=handles.q(i-1)+((0-handles.L(2,i-1))/m);
        break;
    end
end
guidata(hObject,handles);
plotModel(hObject,handles);
plotNormal(hObject,handles);
plotTangent(hObject,handles);
plotStability(hObject,handles);
guidata(hObject,handles);


% --- Executes on button press in Clear.
function Clear_Callback(hObject, eventdata, handles)
% hObject    handle to Clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
axes(handles.Normal)
cla
axes(handles.Tangent)
cla
axes(handles.Stability)
cla
axes(handles.Model)
cla
guidata(hObject,handles);

function makeConfigFile(hObject,handles)
string=strcat(handles.ConfigFile,'.m');
if(fopen(string,'r')~=-1)
    delete(string);
end
File=fopen(string,'w+');
fprintf(File,'m=%d;\r\n',str2num(get(handles.m,'String')));
fprintf(File,'y1=%d;\r\n',str2num(get(handles.y1,'String')));
fprintf(File,'y2=%d;\r\n',str2num(get(handles.y2,'String')));
fprintf(File,'z1=%d;\r\n',-str2num(get(handles.h,'String')));
fprintf(File,'z2=%d;\r\n',-str2num(get(handles.h,'String')));
fprintf(File,'mu1=%d;\r\n',str2num(get(handles.mu1,'String')));
fprintf(File,'mu2=%d;\r\n',str2num(get(handles.mu2,'String')));
fprintf(File,'alpha1=%d;\r\n',str2num(get(handles.alpha1,'String')));
fprintf(File,'alpha2=%d;\r\n',str2num(get(handles.alpha2,'String')));
fclose(File);

function plotNormal(hObject,handles)
handles=guidata(hObject);
axes(handles.Normal)
axis auto
set(handles.Normal,'XLim',[handles.q(1) handles.q(size(handles.q,1))]);
plot(handles.q,handles.F(2,:),'b-',handles.q,handles.F(4,:),'r-')
y=get(handles.Normal,'YLim');
line([handles.limit handles.limit],[y(1) y(2)],'Color',[0 0 0])
guidata(hObject,handles);

function plotTangent(hObject,handles)
handles=guidata(hObject);
axes(handles.Tangent)
axis auto
set(handles.Tangent,'XLim',[handles.q(1) handles.q(size(handles.q,1))]);
plot(handles.q,handles.F(1,:),'b-',handles.q,handles.F(3,:),'r-')
y=get(handles.Tangent,'YLim');
line([handles.limit handles.limit],[y(1) y(2)],'Color',[0 0 0])
guidata(hObject,handles);

function plotStability(hObject,handles)
handles=guidata(hObject);
axes(handles.Stability)
axis auto
set(handles.Stability,'XLim',[handles.q(1) handles.q(size(handles.q,1))]);
plot(handles.q,handles.L(2,:),'b-')
string=sprintf('Stability, Limit = %.2f deg',handles.limit*(180/pi));
title(string)
line([handles.q(1) handles.q(size(handles.q,1))],[0 0],'Color',[0 0 0])
y=get(handles.Stability,'YLim');
line([handles.limit handles.limit],[y(1) y(2)],'Color',[0 0 0])
guidata(hObject,handles);

function plotModel(hObject,handles)
handles=guidata(hObject);
persistent x;
persistent y;
rehash
eval(handles.ConfigFile);
axes(handles.Model)
plot(0,-z1,'ko','MarkerSize',15,'MarkerFaceColor',[0 0 0]);
plot(y1,0,'bo','MarkerSize',15,'MarkerFaceColor',[0 0 1]);
plot(y2,0,'ro','MarkerSize',15,'MarkerFaceColor',[1 0 0]);
line([0 0; y1 y2],[-z1 -z1; 0 0],'Color',[0 0 0]);
if(isempty(x))
    x=[y2 y1];
    y=[0 -z1];
else
    x=[min([y2 x(1)]) max([y1 x(2)])];
    y=[min([0 y(1)]) max([-z1 y(2)])];
end
xd=x(2)-x(1);
yd=y(2)-y(1);
line([x(1)-.1*xd x(2)+.1*xd],[0 0],'Color',[0 0 0]);
axis tight
axis equal

%rotate(h1,[0 0 1],45,[0 0 0]);
%rotate(h2,[0 0 1],45,[0 0 0]);
%rotate(h3,[0 0 1],45,[0 0 0]);
%rotate(h4,[0 0 1],45,[0 0 0]);
%rotate(h5,[0 0 1],45,[0 0 0]);

%axis([x(1)-.1*xd x(2)+.1*xd y(1)-.1*yd y(2)+.1*yd])

guidata(hObject,handles);

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

% Hint: get(hObject,'Value') returns toggle state of Z
handles=guidata(hObject);
z=get(hObject,'Value');
if(z)
    zoom on
else
    zoom off
end
guidata(hObject,handles);






