[MATLAB] Répéter en fin de programme un but de code

Le problème exposé dans ce sujet a été résolu.

Bonjour,

Je poste ce message car j’ai un petit problème sur MATLAB : je suis entrain de faire une interface homme machine communiquant entre MATLAB et Arduino. J’ai réussi à faire la partie MATLAB vers Arduino mais je n’arrive pas à faire l’inverse, je sais comment faire grâce à ça : http://allaboutee.com/2011/07/04/how-to-send-data-from-the-arduino-to-matlab/ mais je n’arrive pas à l’implémenter dans mon cas…

En faite j’aimerais ajouter à la fin de mon programme (que vous trouverez ci dessous) une fonction qui s’executeras disons toute les 1 secondes et qui contiendrais toute les manipulations concernant la partie MATLAB vers Arduino (vérification si Arduino envoie quelque-chose, si oui récupérer ce quelque chose etc…).

Sauriez-vous comment faire ?

Cordialement et Merci d’avance !

Jupiter41.

EDIT : En faite une autre petite question secondaire :) : Connaîtriez-vous la valeur du retour de fread(arduino, 'Value’) si Arduino n’envoie aucune valeur ? -1, peut-être ?

Mon programme :

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

% Last Modified by GUIDE v2.5 13-May-2019 12:43:12

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = GUI_Drone_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 slider movement.
function Angle_Callback(hObject, eventdata, handles)
% hObject    handle to Angle (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slide
set(handles.Angle, 'Max', 130);
set(handles.Angle, 'Min', 70);
set(handles.Angle, 'SliderStep', [1/(105-75),0.1]);
Valeur_angle=get(hObject,'Value');
set(handles.Valeur_a,'String',num2str(Valeur_angle));
guidata(hObject,handles);


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

% Hint: slider controls usually have a light gray background.

if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObjet,'BackgroundColor',[.9 .9 .9]);
end

function Valeur_a_Callback(hObject, eventdata, handles)
edit=get(hObject,'String');
set(handles.Angle,'value',str2num(edit));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function Valeur_a_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Valeur_a (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 Valider.
function Valider_Callback(hObject, eventdata, handles)
% hObject    handle to Valider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Valeur_angle=get(handles.Angle,'Value');
Valeur_sliderVM=get(handles.sliderVM,'Value');

Valeur_Kp = get(handles.valKp, 'Value');
Valeur_Ki = get(handles.valKi, 'Value');
Valeur_Kd = get(handles.valKd, 'Value');

%set(handles.text8,'String',num2str(a));
fprintf(handles.s, "%d;", Valeur_angle); % Consigne
fprintf(handles.s, "%d;", Valeur_sliderVM); % Poussée maximale
fprintf(handles.s, "%d;", Valeur_Kp);
fprintf(handles.s, "%d;", Valeur_Ki);
fprintf(handles.s, "%d;", Valeur_Kd);

function ValMoteurVM_Callback(hObject, eventdata, handles)
% hObject    handle to ValMoteurVM (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 ValMoteurVM as text
%        str2double(get(hObject,'String')) returns contents of ValMoteurVM as a double
Valeur_sliderVM=get(hObject,'String');
set(handles.sliderVM,'value',str2num(Valeur_sliderVM));
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function ValMoteurVM_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ValMoteurVM (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 slider movement.
function sliderVM_Callback(hObject, eventdata, handles)
% hObject    handle to sliderVM (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
set(handles.sliderVM, 'Max', 255);
set(handles.sliderVM, 'Min', 130);
set(handles.sliderVM, 'SliderStep', [1/(255-130),0.1]);
Valeur_sliderVM=get(hObject,'Value');
set(handles.ValMoteurVM,'String',num2str(Valeur_sliderVM));
guidata(hObject,handles);

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


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

fclose(handles.s);

clear all ;
close all;


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

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu
delete(instrfind);
contents = cellstr(get(hObject,'String'))
popChoise = contents{get(hObject,'Value')}
global s;
handles.s=s;
handles.s=serial(popChoise,'BaudRate',115200);
guidata(hObject, handles);
%display(popChoise);

fopen(handles.s) ; % Ouvrir la connexion avec Arduino

% --- Executes during object creation, after setting all properties.
function popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu (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



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


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


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


% --- Executes during object creation, after setting all properties.
function valKd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to valKd (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
+0 -0
Connectez-vous pour pouvoir poster un message.
Connexion

Pas encore membre ?

Créez un compte en une minute pour profiter pleinement de toutes les fonctionnalités de Zeste de Savoir. Ici, tout est gratuit et sans publicité.
Créer un compte