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

% Last Modified by GUIDE v2.5 02-Jul-2009 11:55:02

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @motor_controller_OpeningFcn, ...
                   'gui_OutputFcn',  @motor_controller_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
%Establish variables



% --- Executes just before motor_controller is made visible.
function motor_controller_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 motor_controller (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


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


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(handles.popupmenu1, 'String')
%set(handles.text6, 'String', str(get(handles.popupmenu1, 'Value')))
sp = serial(str(get(handles.popupmenu1, 'Value')), 'BAUD', 19200)
fopen(sp)
setappdata(hObject, 'serialport', sp)
set(handles.text3, 'String', sp.Status)



% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


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

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
items = instrhwinfo('serial')
s = items.SerialPorts
set(hObject, 'String', s);

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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


% --- Executes during object creation, after setting all properties.
function velocity_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to velocity_tb (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 set_velocity_pb.
function set_velocity_pb_Callback(hObject, eventdata, handles)
% hObject    handle to set_velocity_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%
%Function to Set velocity
%Assume slave address of 0xA0
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 201, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
vel = str2double(get(handles.velocity_tb, 'String'))
fwrite(sp, vel, 'uint16')




% --- Executes on button press in get_data_pb.
function get_data_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_data_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 110, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
in = fscanf(sp)
length = str2double(strtrim(in))
%length = str2double(strtrim(fscanf(sp)))-1
for i =1:1:length
    d(i) = str2double(strtrim(fscanf(sp)))
    e(i) = str2double(strtrim(fscanf(sp)))
    %d{i} = fscanf(sp)
    %e{i} = fscanf(sp)
end
i = 1:1:length
plot(i,d)
hold on
plot(i,e)
hold off


%x = 0:.01:2*pi;
%y = sin(x);
%plot(x,y)
%send message to send slave data
% SENDDATA | SLAVEADDRESS
%sp = getappdata(handles.pushbutton1, 'serialport')
%fprintf(sp, char(55));
%Have termination character
%for i = 1:1:60
%    d(i) = str2double(fscanf(sp))
%    e(i)=  str2double(fscanf(sp))
%end
%hold on
%i = 1:1:60;
%plot(i, d)
%plot(i,e)


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fclose(sp)
set(handles.text3, 'String', sp.Status)
clear sp
rmappdata(handles.pushbutton1, 'serialport')



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


% --- Executes during object creation, after setting all properties.
function position_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to position_tb (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 set_position_pb.
function set_position_pb_Callback(hObject, eventdata, handles)
% hObject    handle to set_position_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 202, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
fwrite(sp, str2double(get(handles.position_tb, 'String')), 'uint32')


% --- Executes on button press in get_velocity_pb.
function get_velocity_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_velocity_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 101, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
vel = strtrim(fscanf(sp))
set(handles.velocity_tb, 'String', vel)


% --- Executes on button press in get_position_pb.
function get_position_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_position_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 102, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
pos = strtrim(fscanf(sp))
set(handles.position_tb, 'String', pos)

%Utility Functions
%These funcitons will be called by the gui to 
function set_slave_velocity(slave_addr, velocity)
%Code is 201

function set_slave_position(slave_addr, position)
%Code is 202

function set_slave_max_velocity(slave_addr, max_velocity)
%Code is 207

function get_slave_velocity(slave_addr)
%Code is 101

function get_slave_position(slave_addr)
%Code is 102

function get_all_slave_velocity()
%No specific code, loop through

function get_all_slave_position()

function get_master_info()

function get_motor_data(slave_addr)
%Code is 110
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 110, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')


function slave_address = get_slave_addr(handles)
%gets the slave address either from the textbox or drop down box
%is only from 
disp('function works')
slave_address = str2double(get(handles.slave_addr_tb, 'String'));



% --- Executes on selection change in slave_popup_menu.
function slave_popup_menu_Callback(hObject, eventdata, handles)
% hObject    handle to slave_popup_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns slave_popup_menu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from slave_popup_menu


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

% Hint: popupmenu 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 get_slaves_pb.
function get_slaves_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_slaves_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in add_slave_pb.
function add_slave_pb_Callback(hObject, eventdata, handles)
% hObject    handle to add_slave_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sa = get_slave_addr(handles)
%set(handles.text6, 'String', sa)
vals = get(handles.slave_popup_menu, 'String')
if(iscell(vals))
    vals{length(vals)+1} = num2str(sa);
else
    newvals = {num2str(sa)};
    vals = newvals;
end
set(handles.slave_popup_menu, 'String', vals);



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


% --- Executes during object creation, after setting all properties.
function add_slave_addr_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to add_slave_addr_tb (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 slave_addr_tb_Callback(hObject, eventdata, handles)
% hObject    handle to slave_addr_tb (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 slave_addr_tb as text
%        str2double(get(hObject,'String')) returns contents of slave_addr_tb as a double


% --- Executes during object creation, after setting all properties.
function slave_addr_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slave_addr_tb (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 target_velocity_tb_Callback(hObject, eventdata, handles)
% hObject    handle to target_velocity_tb (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 target_velocity_tb as text
%        str2double(get(hObject,'String')) returns contents of target_velocity_tb as a double


% --- Executes during object creation, after setting all properties.
function target_velocity_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to target_velocity_tb (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 target_position_tb_Callback(hObject, eventdata, handles)
% hObject    handle to target_position_tb (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 target_position_tb as text
%        str2double(get(hObject,'String')) returns contents of target_position_tb as a double


% --- Executes during object creation, after setting all properties.
function target_position_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to target_position_tb (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 kp_tb_Callback(hObject, eventdata, handles)
% hObject    handle to kp_tb (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 kp_tb as text
%        str2double(get(hObject,'String')) returns contents of kp_tb as a double


% --- Executes during object creation, after setting all properties.
function kp_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to kp_tb (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 kd_tb_Callback(hObject, eventdata, handles)
% hObject    handle to kd_tb (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 kd_tb as text
%        str2double(get(hObject,'String')) returns contents of kd_tb as a double


% --- Executes during object creation, after setting all properties.
function kd_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to kd_tb (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 time_interval_tb_Callback(hObject, eventdata, handles)
% hObject    handle to time_interval_tb (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 time_interval_tb as text
%        str2double(get(hObject,'String')) returns contents of time_interval_tb as a double


% --- Executes during object creation, after setting all properties.
function time_interval_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to time_interval_tb (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 set_target_velocity_pb.
function set_target_velocity_pb_Callback(hObject, eventdata, handles)
% hObject    handle to set_target_velocity_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 201, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
fwrite(sp, str2double(get(handles.target_velocity_tb, 'String')), 'uint16')


% --- Executes on button press in get_target_velocity_pb.
function get_target_velocity_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_target_velocity_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 103, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
pos = strtrim(fscanf(sp))
set(handles.target_velocity_tb, 'String', pos)


% --- Executes on button press in set_target_position_pb.
function set_target_position_pb_Callback(hObject, eventdata, handles)
% hObject    handle to set_target_position_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 202, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
fwrite(sp, str2double(get(handles.target_position_tb, 'String')), 'uint32')


% --- Executes on button press in get_target_position_pb.
function get_target_position_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_target_position_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 104, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
target_pos = strtrim(fscanf(sp))
set(handles.target_position_tb, 'String', target_pos)


% --- Executes on button press in set_kp_pb.
function set_kp_pb_Callback(hObject, eventdata, handles)
% hObject    handle to set_kp_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 205, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
fwrite(sp, str2double(get(handles.kp_tb, 'String')), 'uint16')


% --- Executes on button press in get_kp_pb.
function get_kp_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_kp_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 105, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
kp = strtrim(fscanf(sp))
set(handles.kp_tb, 'String', kp)


% --- Executes on button press in set_kd_pb.
function set_kd_pb_Callback(hObject, eventdata, handles)
% hObject    handle to set_kd_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 206, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
fwrite(sp, str2double(get(handles.kd_pb, 'String')), 'uint16')


% --- Executes on button press in get_kd_pb.
function get_kd_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_kd_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 106, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
kd = strtrim(fscanf(sp))
set(handles.kd_tb, 'String', kd)


% --- Executes on button press in set_time_interval_pb.
function set_time_interval_pb_Callback(hObject, eventdata, handles)
% hObject    handle to set_time_interval_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 208, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
fwrite(sp, str2double(get(handles.time_interval_tb, 'String')), 'uint16')


% --- Executes on button press in get_time_interval_pb.
function get_time_interval_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_time_interval_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 108, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
time_interval = strtrim(fscanf(sp))
set(handles.time_interval_tb, 'String', time_interval)



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


% --- Executes during object creation, after setting all properties.
function max_velocity_tb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to max_velocity_tb (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 set_max_velocity_pb.
function set_max_velocity_pb_Callback(hObject, eventdata, handles)
% hObject    handle to set_max_velocity_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 207, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
fwrite(sp, str2double(get(handles.max_velocity_tb, 'String')), 'uint16')


% --- Executes on button press in get_max_velocity_pb.
function get_max_velocity_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_max_velocity_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 107, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')
max_vel = strtrim(fscanf(sp))
set(handles.max_velocity_tb, 'String', max_vel)


% --- Executes on button press in get_all_data_pb.
function get_all_data_pb_Callback(hObject, eventdata, handles)
% hObject    handle to get_all_data_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%
% Should have had functions to do individual tasks. Would be easy to
% implement though, not going to make this until that happens.


% --- Executes on button press in go_pb.
function go_pb_Callback(hObject, eventdata, handles)
% hObject    handle to go_pb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
sp = getappdata(handles.pushbutton1, 'serialport')
fwrite(sp, 111, 'uint8')
fwrite(sp, get_slave_addr(handles), 'uint8')



