HOW DO I CONTROL SERVO MOTOR USING EDIT TEXT GUI MATLAB

Hello everyone,
Was wondering if you could help, I'm a bit of a newbie so this may be a very simple question.
how do i control multiple servo motors using edit text value.
Thank you

12 Comments

apri - have you designed and coded up your GUI? What do you have so far?
I have made the gui design and haven't made the program yet
I want to use the slider, edit text and push buttons to control the servo motor, I have been able to control the servo motor using the slider but edit text not yet
Is the idea that the user enter a value in the edit text field and then press the button (beside the edit field) which would then send that information to the arduino? If so, then you would need to use the push button callback function to do this work.
one more thing, how to make edittext as servo input as well as a slider indicator?
what I mean is an example of coding
yes...but does the user press a button to "send" the text entered into the edit control? Please clarify and/or attach your m-file.
how do i make 2 outputs (slider and static text) on one pushbutton
function varargout = DESAIN_ROBOT_FULL(varargin)
% DESAIN_ROBOT_FULL MATLAB code for DESAIN_ROBOT_FULL.fig
% DESAIN_ROBOT_FULL, by itself, creates a new DESAIN_ROBOT_FULL or raises the existing
% singleton*.
%
% H = DESAIN_ROBOT_FULL returns the handle to a new DESAIN_ROBOT_FULL or the handle to
% the existing singleton*.
%
% DESAIN_ROBOT_FULL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DESAIN_ROBOT_FULL.M with the given input arguments.
%
% DESAIN_ROBOT_FULL('Property','Value',...) creates a new DESAIN_ROBOT_FULL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before DESAIN_ROBOT_FULL_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to DESAIN_ROBOT_FULL_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 DESAIN_ROBOT_FULL
% Last Modified by GUIDE v2.5 20-Apr-2020 09:05:47
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DESAIN_ROBOT_FULL_OpeningFcn, ...
'gui_OutputFcn', @DESAIN_ROBOT_FULL_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 DESAIN_ROBOT_FULL is made visible.
function DESAIN_ROBOT_FULL_OpeningFcn(hObject, ~, 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 DESAIN_ROBOT_FULL (see VARARGIN)
% Choose default command line output for DESAIN_ROBOT_FULL
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes DESAIN_ROBOT_FULL wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = DESAIN_ROBOT_FULL_OutputFcn(~, ~, 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;
global a s1 s2 s3 s4 s5;
a = arduino;
s1=servo(a,'D3'); %servo1
s2=servo(a,'D5'); %servo2
s3=servo(a,'D6'); %servo 3
s4=servo(a,'D9'); %servo 4
s5=servo(a,'D10'); %servo 5
writePosition(s1,0.5);
pause(1);
writePosition(s2,0.5);
pause(1);
writePosition(s3,0.5);
pause(1);
writePosition(s4,0.5);
pause(1);
writePosition(s5,0.5);
pause(1);
% --- Executes on slider movement.
function slider1_Callback(~, ~, handles)
global s1;
pos=get(handles.slider1,'Value'); %same as pos=get(hObject,'Value');
pos1=pos/180;
pos1=1-pos1; %For position inversion
writePosition(s1,pos1);
readPosition(s1);
set(handles.text2,'String',num2str(int16(pos)));
function slider2_Callback(~, ~, handles)
global s2;
pos=get(handles.slider2,'Value');
pos2=pos/180;
%pos2=1-pos2; %For position inversion
writePosition(s2,pos2);
readPosition(s2);
set(handles.text3,'String',num2str(int16(pos)));
function slider3_Callback(~, ~, handles)
global s3;
pos=get(handles.slider3,'Value');
pos3=pos/180;
writePosition(s3,pos3);
readPosition(s3);
set(handles.text4,'String',num2str(int16(pos)));
function slider4_Callback(~, ~, handles)
global s4;
pos=get(handles.slider4,'Value');
pos4=pos/180;
writePosition(s4,pos4);
readPosition(s4);
set(handles.text5,'String',num2str(int16(pos)));
function slider5_Callback(~, ~, handles)
global s5;
pos=get(handles.slider5,'Value');
pos5=pos/180;
writePosition(s5,pos5);
readPosition(s5);
set(handles.text6,'String',num2str(int16(pos)));
function edit1_Callback(hObject, eventdata, handles)
function edit2_Callback(hObject, eventdata, handles)
function edit3_Callback(hObject, eventdata, handles)
function edit4_Callback(hObject, eventdata, handles)
function edit5_Callback(hObject, eventdata, handles)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global s1;
input=str2num(get(handles.edit1, 'String'));
input1=input/180;
writePosition(s1,input1);
set(handles.text2,'String',num2str(int16(input)));
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
global s2;
input=str2num(get(handles.edit2, 'String'));
input2=input/180;
writePosition(s2,input2);
set(handles.text3,'String',num2str(int16(input)));
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
global s3;
input=str2num(get(handles.edit3, 'String'));
input1=input/180;
writePosition(s3,input3);
set(handles.text4,'String',num2str(int16(input)));
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
global s4;
input=str2num(get(handles.edit4, 'String'));
input4=input/180;
writePosition(s4,input4);
set(handles.text5,'String',num2str(int16(input)));
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
global s5;
input=str2num(get(handles.edit5, 'String'));
input5=input/180;
writePosition(s5,input5);
Was wondering if you could help.
how do I display a pwm signal using a GUI, which controls a servo?
my point is to display the signal on the digital pin used by the servo.
apri, put an axes on your GUI, then get your signal somehow, then call plot() to display it in the axes control.
sorry I have not understood to get a signal from a servo motor.
I have not done that either. Good luck.
I have tried to make a simple coding, but I have a problem when I set slider1, why do axes2 react too?
function slider1_Callback(hObject, eventdata, handles)
global s1;
slider=get(hObject,'value');
set(handles.edit1,'string',num2str(slider));
guidata(hObject,handles);
pos1=slider/180;
writePosition(s1,pos1);
y1= [0 1 0 0 ];
x1= [0 0 1 20 ];
y2= [0 1 0 0 ];
x2= [0 0 1.5 20 ];
y3= [0 1 0 0 ];
x3= [0 0 2 20 ];
i=0;
i=i+1;
v(i)=readPosition(s1);
if (v(i)<0.5)
stairs(x1,y1,'LineWidth',2);
elseif (v(i)==0.5 || v(i)>0.5 && v(i)~=1)
stairs(x2,y2,'LineWidth',2);
elseif (v(i)==1)
stairs(x3,y3,'LineWidth',2);
end
axes(handles.axes1);
set(gca,'XTick', 0:1:20);
xlabel('lebar pulsa (ms)');
function slider2_Callback(hObject, eventdata, handles)
global s2;
slider=get(hObject,'value');
set(handles.edit2,'string',num2str(slider));
guidata(hObject,handles);
pos1=slider/180;
writePosition(s2,pos1);
y1= [0 1 0 0 ];
x1= [0 0 1 20 ];
y2= [0 1 0 0 ];
x2= [0 0 1.5 20 ];
y3= [0 1 0 0 ];
x3= [0 0 2 20 ];
i=0;
i=i+1;
v(i)=readPosition(s2);
if (v(i)<0.5)
stairs(x1,y1,'LineWidth',2);
elseif (v(i)==0.5 || v(i)>0.5 && v(i)~=1)
stairs(x2,y2,'LineWidth',2);
elseif (v(i)==1)
stairs(x3,y3,'LineWidth',2);
end
axes(handles.axes2);
set(gca,'XTick', 0:1:20);
drawnow;

Sign in to comment.

 Accepted Answer

I'm not seeing any static text labels on your figure so I don't know what handles.text2 refers to. All I see are edit text boxes. Anyway, to get the edit text box and the slider/scrollbar to sync up when you change either, you can do this:
%=======================================================================
% --- Executes on button press in pushbutton1.
% Edit field is the master value and we send that value to the slider.
function pushbutton1_Callback(hObject, eventdata, handles)
global s1;
% Get contents of the edit box.
editBoxContents = str2num(handles.edit1.String);
% Make static text label have the integer value of the edit field.
handles.text2.String = num2str(int16(editBoxContents));
% Make the slider have the same value as the edit box.
handles.slider1.Value = number; % The value before we divided it by 180.
if handles.slider1.Value > handles.slider1.Max
handles.slider1.Max = handles.slider1.Value;
end
% Now prepare value for sending to the servo:
% Send value to servo #1.
number = editBoxContents / 180;
writePosition(s1, number);
%=======================================================================
% Change in edit field is just like pushing the button.
% Edit field is the master value and we send that value to the slider.
function edit1_Callback(hObject, eventdata, handles)
pushbutton1_Callback(hObject, eventdata, handles)
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
global s1;
% Get value from slider.
sliderValue = handles.slider1.Value;
% Send the same value to edit box #1.
handles.edit1.String = sprintf('%d', round(sliderValue));
% Send the same value to static text label #2.
handles.text2.String = handles.edit1.String;
% Now prepare value for sending to the servo:
pos1 = pos / sliderValue;
number = 1 - pos1; %For position inversion
% Send value to servo #1.
writePosition(s1, number);
% Read it back.
readPosition(s1);
Repeat for the other sets of edit fields, sliders, static text labels, and buttons.

6 Comments

Thanks, I will check it out.
how about displaying the duty cycle of the servo, do I have to add axes?
Whatever you want. You can display it in some existing axes, or you can add a separate axes just for that.
when the slider is returned to '' 0 ", I get an error like this
function slider1_Callback(~, ~, handles)
global s1;
sliderValue = handles.slider1.Value;
handles.edit1.String = sprintf('%d', round(sliderValue));
handles.text2.String = handles.edit1.String;
pos=get(handles.slider1,'Value'); %same as pos=get(hObject,'Value');
pos1 = pos / sliderValue;
number = 1 - pos1; %For position inversion
% Send value to servo #1.
writePosition(s1, number);
% Read it back.
readPosition(s1);
set(handles.text2,'String',num2str(int16(pos)));
function edit1_Callback(hObject, eventdata, handles)
pushbutton1_Callback(hObject, eventdata, handles)
function pushbutton1_Callback(hObject, eventdata, handles)
global s1;
% Get contents of the edit box.
editBoxContents = str2num(handles.edit1.String);
% Make static text label have the integer value of the edit field.
handles.text2.String = num2str(int16(editBoxContents));
% Make the slider have the same value as the edit box.
number = editBoxContents / 180;
handles.slider1.Value = number; % The value before we divided it by 180.
if handles.slider1.Value > handles.slider1.Max
handles.slider1.Max = handles.slider1.Value;
end
% Now prepare value for sending to the servo:
% Send value to servo #1.
writePosition(s1, number);
hi, can you help me please,
how do i displaying motor servo output pwm signal
thanks,.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!