Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Please Help with GUIs: Issue: Automatic initializing of Hardware when starting GUI
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everybody,
at this time I have not programmed so much with Matlab GUI.
Therefore, I have some problems and hope you can help me.
The Issue: I have a stepper motor that can be controlled by ActiveX in Matlab. When using m-file script, the stepper motor works fine.
But when I am trying to control the motor by a GUI, it does not really work.
The code, which should automatically run when the GUI starts is:
fig = figure('Position', [0 100 1 1], 'HandleVisibility', 'off', 'IntegerHandle', 'off', 'MenuBar', 'none', 'Visible', 'off');
h_Ctrl = actxcontrol('MGMOTOR.MGMotorCtrl.1', [10 10 20 20], fig); % Start ActiveX-control
h_Ctrl.HWSerialNum = 90828811; % Serial Number
Channel_ID = 0; % should be a variable which is used in other functions
h_Ctrl.invoke('StartCtrl'); % Start connection to Hardware
It is important that h_Ctrl is global in order to use it with other commands in different functions.
The entire code is written here:
---------------------------------------------------- function varargout = ui_stepper_motor_0100vers(varargin) % UI_STEPPER_MOTOR_0100VERS M-file for ui_stepper_motor_0100vers.fig % UI_STEPPER_MOTOR_0100VERS, by itself, creates a new UI_STEPPER_MOTOR_0100VERS or raises the existing % singleton*. % % H = UI_STEPPER_MOTOR_0100VERS returns the handle to a new UI_STEPPER_MOTOR_0100VERS or the handle to % the existing singleton*. % % UI_STEPPER_MOTOR_0100VERS('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in UI_STEPPER_MOTOR_0100VERS.M with the given input arguments. % % UI_STEPPER_MOTOR_0100VERS('Property','Value',...) creates a new UI_STEPPER_MOTOR_0100VERS or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before ui_stepper_motor_0100vers_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to ui_stepper_motor_0100vers_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 ui_stepper_motor_0100vers
% Last Modified by GUIDE v2.5 10-Mar-2011 09:27:44
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ui_stepper_motor_0100vers_OpeningFcn, ...
'gui_OutputFcn', @ui_stepper_motor_0100vers_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
fig = figure('Position', [0 100 1 1], 'HandleVisibility', 'off', 'IntegerHandle', 'off', 'MenuBar', 'none', 'Visible', 'off');
h_Ctrl = actxcontrol('MGMOTOR.MGMotorCtrl.1', [10 10 20 20], fig);
h_Ctrl.HWSerialNum = 90828811;
pause(0.2);
Channel_ID = 0;
h_Ctrl.invoke('StartCtrl');
% --- Executes just before ui_stepper_motor_0100vers is made visible.
function ui_stepper_motor_0100vers_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 ui_stepper_motor_0100vers (see VARARGIN)
% Choose default command line output for ui_stepper_motor_0100vers
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ui_stepper_motor_0100vers wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ui_stepper_motor_0100vers_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 angle_startText_Callback(hObject, eventdata, handles)
% hObject handle to angle_startText (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 angle_startText as text
% str2double(get(hObject,'String')) returns contents of angle_startText as a double
input = str2num(get(hObject, 'String'));
guidata(hObject, handles);
function end_angleText_Callback(hObject, eventdata, handles)
% hObject handle to end_angleText (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 end_angleText as text
% str2double(get(hObject,'String')) returns contents of end_angleText as a double
input = str2num(get(hObject, 'String'));
guidata(hObject, handles);
function minvel_text_Callback(hObject, eventdata, handles)
% hObject handle to minvel_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 minvel_text as text
% str2double(get(hObject,'String')) returns contents of minvel_text as a double
input = str2num(get(hObject, 'String'));
guidata(hOject, handles);
function maxvel_Text_Callback(hObject, eventdata, handles)
% hObject handle to maxvel_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 maxvel_Text as text
% str2double(get(hObject,'String')) returns contents of maxvel_Text as a double
input = str2num(get(hObject, 'String'));
guidata(hObject, handles);
function acc_vel_Text_Callback(hObject, eventdata, handles)
% hObject handle to acc_vel_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 acc_vel_Text as text
% str2double(get(hObject,'String')) returns contents of acc_vel_Text as a double
input = str2num(get(hObject, 'String'));
guidata(hObject, handles);
% --- Executes on button press in startangle_move_button.
function startangle_move_button_Callback(hObject, eventdata, handles)
% hObject handle to startangle_move_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
startposition = str2num(get(handles.angle_startText, 'String'));
h_Ctrl.invoke('SetAbsMovePos', Channel_ID, startposition);
pause(0.2);
h_Ctrl.invoke('MoveAbsolute', Channel_ID, 0);
% --- Executes on button press in endangle_move_button.
function endangle_move_button_Callback(hObject, eventdata, handles)
% hObject handle to endangle_move_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if (h_Ctrl.invoke('GetHWCommsOK')) == 1 %and (h_Ctrl.invoke('MoveComplete', Channel_ID)== 1 OR h_Ctrl.invoke('MoveStopped', ChannelID) == 0)
else
h_Ctrl.StartCtrl;
pause(0.2);
end
endposition = str2num(get(handles.end_angleText, 'String'));
h_Ctrl.invoke('SetAbsMovePos', Channel_ID, endposition);
pause(0.2);
h_Ctrl.invoke('MoveAbsolute', Channel_ID, 0);
------------------------------------------------
So my question is about how can I manage it - that at GUI-Start the initialization of hardware runs once - to use variables like h_Ctrl in order to use something like that: h_Ctrl.invoke('Startnumber', Channel_ID, 0);
If you have some (more) questions about my problem, please inform me!
Thanks to the people who support me!
0 commentaires
Réponses (0)
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!