HOW DO I CONTROL SERVO MOTOR USING EDIT TEXT GUI MATLAB

14 views (last 30 days)
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 zulham
apri zulham on 19 May 2020
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

Image Analyst
Image Analyst on 21 Apr 2020
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
apri zulham
apri zulham on 25 Apr 2020
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);
apri zulham
apri zulham on 2 May 2020
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!