RUNNİNG MOTORS ISSUES İN GUİDE

조회 수: 1 (최근 30일)
Franck paulin Ludovig pehn Mayo
Franck paulin Ludovig pehn Mayo 2022년 3월 16일
이동: Sabin 2023년 1월 11일
İ have a problem it seems that no one is maybe getting what i am saying.
İ am trying to run some motors when they entered a particular and stop from running when it is out of that region.
To try to understand the issue, ı have designed a new GUİ wıth just 2 push buttons . RUN and STOP. Guess what , they are working as expected. But when i am trying to ımplement the same code ın my main guı it is not working at all. İ am not having any error in the command window btw. My motors just vibrate and stop vibrating randomly.
The motors are connected to driving motors and driving motors are connected to an arduino uno
The line corresponding to the issue is at the very end of the code.
writeline(handles.arduinoObj, '4&MOTOR_1_2&0!'); This line means vibrating
writeline(handles.arduinoObj, '0&MOTOR_1_2&0!'); This line means No vibration
All thıs is done in GUİDE.
İ wıll post the main code here and i will reply with the Test code.
function varargout = franck_guide(varargin)
% FRANCK_GUIDE MATLAB code for franck_guide.fig
% FRANCK_GUIDE, by itself, creates a new FRANCK_GUIDE or raises the existing
% singleton*.
%
% H = FRANCK_GUIDE returns the handle to a new FRANCK_GUIDE or the handle to
% the existing singleton*.
%
% FRANCK_GUIDE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FRANCK_GUIDE.M with the given input arguments.
%
% FRANCK_GUIDE('Property','Value',...) creates a new FRANCK_GUIDE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before franck_guide_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to franck_guide_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 franck_guide
% Last Modified by GUIDE v2.5 22-Feb-2022 13:54:14
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @franck_guide_OpeningFcn, ...
'gui_OutputFcn', @franck_guide_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 franck_guide is made visible.
function franck_guide_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 franck_guide (see VARARGIN)
% Choose default command line output for franck_guide
handles.output = hObject;
% Plot patch on uiaxes
%hold on
% Read experiment data from a CSV file
load_folder = "C:\Users\student\Desktop\FRANCK\thesis\excel_data\";
load_name = "excel_data.xlsx";
load_addr = load_folder + load_name;
handles.T = readtable(load_addr,'NumHeaderLines',1);
handles.exp_counter = 1;
handles.v_thickness_1 = handles.T.Var1;
handles.v_thickness_2 = handles.T.Var2;
handles.h_thickness_1 = handles.T.Var3;
handles.h_thickness_2 = handles.T.Var4;
handles.amplitude_array = handles.T.Var5;
handles.v_or_h_array = handles.T.Var6;
handles.v_thick1 = handles.v_thickness_1(handles.exp_counter);
handles.v_thick2 = handles.v_thickness_2(handles.exp_counter);
handles.h_thick1 = handles.h_thickness_1(handles.exp_counter);
handles.h_thick2 = handles.h_thickness_2(handles.exp_counter);
handles.v_or_h = handles.v_or_h_array(handles.exp_counter);
handles.region1 = [];
% Create the Arduino serial object
handles.arduinoObj = serialport('COM3', 9600);
configureTerminator(handles.arduinoObj,'CR/LF');
%flush(handles.arduinoObj);
%
for i=1:8
handles.message = readline(handles.arduinoObj);
disp(handles.message)
end
create_patch(handles);
% Update handles structure
%guidata(hObject, handles);
% UIWAIT makes Hapticfinal wait for user response (see UIRESUME)
% uiwait(handles.finger);
function create_patch(handles)
if ishandle(handles.region1)
delete(handles.region1);
end
v_or_h = handles.v_or_h_array(handles.exp_counter);
if v_or_h == 0 % Vertical line
v_thick1 = handles.v_thickness_1(handles.exp_counter);
v_thick2 = handles.v_thickness_2(handles.exp_counter);
handles.region1 = patch( ...
'Parent',handles.axes1, ...
'XData',[v_thick1 v_thick2 v_thick2 v_thick1], ...
'YData',[-10 -10 10 10], ...
'FaceColor','red');
set(handles.axes1,'XLim',[-5 0],'YLim',[-10 10]);
else % Horizontal line
h_thick1 = handles.h_thickness_1(handles.exp_counter);
h_thick2 = handles.h_thickness_2(handles.exp_counter);
handles.region1 = patch( ...
'Parent',handles.axes1, ...
'XData',[-10 10 10 -10], ...
'YData',[h_thick1 h_thick1 h_thick2 h_thick2], ...
'FaceColor','red');
set(handles.axes1,'XLim',[0 5],'XLim',[-10 10]);
end
set(handles.axes1,'XGrid','on','YGrid','on');
axis(handles.axes1,'equal');
% Update handles structure
guidata(handles.finger,handles);
% call the button motion fcn to update the new patch's color:
finger_WindowButtonMotionFcn(handles.finger);
% UIWAIT makes franck_guide wait for user response (see UIRESUME)
% uiwait(handles.finger);
% --- Outputs from this function are returned to the command line.
function varargout = franck_guide_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 Start_button.
function Start_button_Callback(hObject, eventdata, handles)
% hObject handle to Start_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.fileID = fopen('exp.txt','w');
handles.t = timer('ExecutionMode', 'fixedRate', ...
'Period', 0.5, ...
'TasksToExecute', Inf, ...
'TimerFcn', {@timerCallback, handles.fileID});
start(handles.t);
set(handles.Start_button,'Enable','off'); % -> Disable the button
guidata(hObject,handles);% -----> do this to save the updated handles object
function timerCallback(~,~,fileID)
fprintf(fileID,'(X, Y, time) = (%g, %g, %s)\n', get(0, 'PointerLocation'), datetime('now'));
fprintf('calling timer callback\n');
% --- Executes on button press in Next_button.
function Next_button_Callback(hObject, eventdata, handles)
% hObject handle to Next_button (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);
f = msgbox('Operation Completed,3 seconds please.','NEXT');
% Read experiment data from a CSV file
% region1 = patch(axes1,[-10 10 10 -10],[-5 -5 -4.4 -4.4],'r','FaceAlpha',1,...
%'LineWidth',0.01,'LineStyle','-','tag','region1');
handles.exp_counter = handles.exp_counter + 1;
if handles.exp_counter > numel(handles.v_thickness_1)
return
end
% delete the old patch and create a new one:
create_patch(handles);
% --- Executes on button press in Yes_button.
function Yes_button_Callback(hObject, eventdata, handles)
% hObject handle to Yes_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
answer = questdlg('ARE YOU SURE?','Confirm close',...
'OK',@(src,event)mycallback(handles,src,event));
clear
clc
fileID= fopen('exp.txt2','a');
YES = "I FEEL IT";
fprintf (fileID, "%s\n",YES);
fclose(fileID);
% --- Executes on button press in Stop_button.
function Stop_button_Callback(hObject, eventdata, handles)
% hObject handle to Stop_button (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);
stop(handles.t) %whenever we want to stop.
fclose(handles.fileID);
set(handles.Start_button,'Enable','on'); % -> Enable the button
guidata(hObject,handles);
% --- Executes on button press in No_button.
function No_button_Callback(hObject, eventdata, handles)
% hObject handle to No_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
answer1= questdlg('ARE YOU SURE?','Confirm Close',...
'OK',@(src,event)mycallback(handles,src,event));
clear
clc
fileID= fopen('exp.txt2','a');
NO = "I DON'T FEEL IT";
fprintf (fileID, "%s\n",NO);
fclose(fileID);
% --- Executes on mouse motion over figure - except title and menu.
function finger_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject handle to finger (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);
pos = get(hObject, 'currentpoint'); % get mouse location on figure
global x;
global y;
x = pos(1);
y = pos(2); % assign locations to x and y
set(handles.xloc, 'string', ['x loc:' num2str(x)]); % update text for x loc
set(handles.yloc, 'string', ['y loc:' num2str(y)]); % update text for y loc
% Determine if mouse is within the region
p_x = get(handles.region1,'XData');
p_x = p_x([1 2]);
p_y = get(handles.region1,'YData');
p_y = p_y([1 3]);
ax_xl = get(handles.axes1,'XLim');
ax_yl = get(handles.axes1,'YLim');
ax_units = get(handles.axes1,'Units');
if ~strcmp(ax_units,'pixels')
set(handles.axes1,'Units','pixels')
end
ax_pos = get(handles.axes1,'Position'); % axes1 position in pixels
if ~strcmp(ax_units,'pixels')
set(handles.axes1,'Units',ax_units);
end
% convert the patch XData and YData from axes coordinates to figure coordinates in pixels
p_x = (p_x-ax_xl(1))/(ax_xl(2)-ax_xl(1))*ax_pos(3)+ax_pos(1);
p_y = (p_y-ax_yl(1))/(ax_yl(2)-ax_yl(1))*ax_pos(4)+ax_pos(2);
if x >= p_x(1) && x <= p_x(2) && y >= p_y(1) && y <= p_y(2)
set(handles.region1,'FaceColor','g');
%run the motors
writeline(handles.arduinoObj, '4&MOTOR_1_2&0!');
drawnow;
else
set(handles.region1,'FaceColor','r');
writeline(handles.arduinoObj, '0&MOTOR_1_2&0!');
drawnow;
end
% Create the Arduino serial object
%handles.arduinoObj = serialport("COM3", 9600);
%configureTerminator(handles.arduinoObj,"CR/LF");
%flush(handles.arduinoObj);
%
%%for i=1:8
%handles.message = readline(handles.arduinoObj);
%disp(handles.message)
%end
  댓글 수: 30
Franck paulin Ludovig pehn Mayo
Franck paulin Ludovig pehn Mayo 2022년 3월 23일
@Rik I do appreciate your input a lot but unfurtunately i cannot followw that line .Starting all over again a third time, will play against me. I do not have that much time. The gui i am Trying to do have many pushbuttons and also with time i have to add other things depending on how the experiment goes. I am not that too good with matlab . I have tried my best to learn how to use appdesigner and at the end it did not work as i wanted then i went into guide that is giving some small issues while i have completed already 95 of the job.
Since it is a lil hard to answer the previous question that is why i have changed the strategy by using keyboard key in order to run and stop the motors. I can run and stop the motors whenever i want in a test gui (guide). I just want to implement that in my Gui.
Rik
Rik 2022년 3월 23일
You will be able to recycle many of the parts you have already written. It is not the same as going from AppDesigner to GUIDE.
The point is to start with something small where you can see what works and what doesn't. When you're sure one part works, you can add the next part. You don't have to write those parts from scratch, you can copy them from what you currently have.
Re-writing something that doesn't work can be a worthwhile time investment. You can continue spending days on debugging GUIDE, or you can build up your program part by part, where you know what each part does. I strongly encourage you to read the link I gave you. I suspect GUIDE is holding you back. I know it did for me.

댓글을 달려면 로그인하십시오.

답변 (1개)

Franck paulin Ludovig pehn Mayo
이동: Sabin 2023년 1월 11일
@Rik i have solved the issue. The problem was that the communication between arduino and matlab was flooded. To solve it, i have added a ''status_check'' before the cursor enters the area and after it does

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by