how to control stepper motor speed via USB port
    9 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
i really need some help over here..i have connected and run my stepper motor but how come i cannot control the speed of my motor?..can someone give me some hints?
below are my coding:
    function text_speed_m1_Callback(hObject, eventdata, handles)
  sliderValue = get(handles.text_speed_m1,'String');
%convert from string to number if possible, otherwise returns empty
sliderValue = str2nm(sliderValue);
%if user inputs something is not a number, or if the input is less than 0
%or greater than 100, then the slider value defaults to 0
if (isempty(sliderValue) || sliderValue < 0 || sliderValue > 256)
    set(handles.slider_speed_m1,'Value',0);
    set(handles.text_speed_m1,'String','0');
else
    set(handles.slider_speed_m1,'Value',sliderValue);
end
% --- Executes during object creation, after setting all properties.
function text_speed_m1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function slider_speed_m1_Callback(hObject, eventdata, handles)
sliderValue = get(handles.slider_speed_m1,'Value');
%puts the slider value into the edit text component
set(handles.text_speed_m1,'String', num2str(sliderValue));
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in pushbutton_speed_m1.
function pushbutton_speed_m1_Callback(hObject, eventdata, handles)
obj1 = instrfind('Type','serial','Port','COM8','Tag','');  
if isempty(obj1)
    obj1 = serial('COM8');
else
    fclose(obj1);
    obj1 = obj1(1)
end
fopen(obj1);
sv2 = get(handles.text_speed_m1,'String');
fprintf(obj1,'%s','rs')
fprintf(obj1,int2str(get(hObject,'Value')));
please give me some hints on what i do wrong..really need help now..thanks
1 comentario
  Walter Roberson
      
      
 el 27 de Mzo. de 2011
				Please reformat your code to be more readable. If you go in to the editor and select the code and press the '{} Code' button then it will be reformatted.
Please also check that the code output is what you wanted. For example one of your lines shows up as "if (isempty(sliderValue) sliderValue < 0 sliderValue > 256)" which is invalid syntax. I suspect that you had or-bar there but that your or-bar was interpreted by the forum as formatting instructions.
Respuesta aceptada
  Walter Roberson
      
      
 el 27 de Mzo. de 2011
        We can't tell why you cannot control the speed of the motor. You have not described what happens when you try.
Closing and reopening the serial port each time is not advisable. If you find a serial port and it is open, then you should use it.
In your code, if obj1 is empty after the instrfind, then when you associate the serial port with obj1, you have omitted setting anything like port speed. The default probably is not what you want; even if it happens to be, other people reading your code are going to be left wondering and the defaults might change in other versions, so it is better to set port characteristics explicitly.
0 comentarios
Más respuestas (17)
  fremond khoo
 el 27 de Mzo. de 2011
        6 comentarios
  Walter Roberson
      
      
 el 28 de Mzo. de 2011
				Doesn't this case correspond to you closing the existing port and opening it again? Or did you remove that code?
  fremond khoo
 el 29 de Mzo. de 2011
        3 comentarios
  Walter Roberson
      
      
 el 31 de Mzo. de 2011
				Is there a reference document that discusses the command format expected by the motor?
  fremond khoo
 el 31 de Mzo. de 2011
        4 comentarios
  Walter Roberson
      
      
 el 1 de Abr. de 2011
				It was through experience, Fremond. Without looking it up, I could see that CByte was obviously a "convert to byte" call, and the matlab equivalent of converting to byte is uint8()
  fremond khoo
 el 2 de Abr. de 2011
        5 comentarios
  Walter Roberson
      
      
 el 7 de Abr. de 2011
				In the above,
t1 = num2str(get(handles.text_track_m1,'String'));
should instead be
t1 = str2num(get(handles.text_track_m1,'String'));
or one of its equivalents. str2double() is better than str2num()
  fremond khoo
 el 6 de Abr. de 2011
        3 comentarios
  Walter Roberson
      
      
 el 7 de Abr. de 2011
				Sorry, I misused strcmp. I should have written
tf = strcmp(t1, {'None (1 / 1)', '1 / 2', '1 / 5', '1 / 10'});
if any(tf)
stepfactor = stepfactors(idx);
fwrite(obj1, [uint8('M') uint8(stepfactor)];
else
%warn about the text not being what was expected
end
With a listbox, you would use this as the body of the callback:
stepfactors = [1 2 5 10];
idx = get(hObject,'Value');
if ~isempty(idx)
stepfactor = stepfactors(idx);
fwrite(obj1, [uint8('M') uint8(stepfactor)];
end
  fremond khoo
 el 7 de Abr. de 2011
        3 comentarios
  Walter Roberson
      
      
 el 7 de Abr. de 2011
				The 2 means to read two items; each item will be of type uint8.
  fremond khoo
 el 7 de Abr. de 2011
        4 comentarios
  Walter Roberson
      
      
 el 8 de Abr. de 2011
				Insufficient information. No correspondence has been documented between angles and the values sent for the 'T' command.
  fremond khoo
 el 8 de Abr. de 2011
        6 comentarios
  Walter Roberson
      
      
 el 10 de Abr. de 2011
				There are some suggestions about learning MATLAB in a previous Question, http://www.mathworks.com/matlabcentral/answers/1148-how-to-learn-matlab
It would be fairly difficult these days to get the same kind of experience that I got -- I've been programming for 35 years, from the days when I was the only person in my class who could afford to buy a computer that could fit as many as 99 instructions...
  fremond khoo
 el 11 de Abr. de 2011
        8 comentarios
  Walter Roberson
      
      
 el 11 de Abr. de 2011
				Put that fprintf() statement in a try/catch block, and when the exception occurs, display the output of get(obj2) in order to see what the internal status of the object was.
  fremond khoo
 el 12 de Abr. de 2011
        5 comentarios
  Walter Roberson
      
      
 el 12 de Abr. de 2011
				Where you have the
t = fread(obj2, 2, 'uint8');
change that to
[t,count,msg] = fread(obj2, 2, 'uint8');
if count < 2
 if ischar(msg)
 fprintf(1,'request_m2 obj2 read returned only %d bytes, saying that %s\n', count, msg);
 else
 fprintf(1,'request_m2 obj2 read returned only %d bytes for an unknown reason);
 end
 return
end
  fremond khoo
 el 12 de Abr. de 2011
        1 comentario
  Walter Roberson
      
      
 el 12 de Abr. de 2011
				Change the find_and_open function as follows and try again:
function obj=ODF_plot(port,wantbaud)
 needed_open = true;
 obj = instrfind('Type','serial','Port',port);
 if isempty(obj);
 fprintf(1,'find_and_open: note that instrfind did not find port %s\n', port);
 obj = serial(port);
 end
 curspeed = get(obj,'BaudRate');
 if curspeed ~= wantbaud
 if strcmpi(get(obj,'Status'),'open');
 fclose(obj);
 needed_open = false;
 fprintf(1,'find_and_open: note that port %s was already open but had the wrong speed, %d\n', port, curspeed)
 end
 set(obj,'BaudRate',wantbaud);
 end
 curmode = get(obj,'BytesAvailabelFcnMode');
 if ~strcmpi(curmode, 'byte')
 if strcmpi(get(obj,'Status'),'open');
 fclose(obj);
 needed_open = false;
 fprintf(1,'find_and_open: note that port %s was already open but had the wrong mode, %s\n', port, curmode)
 end
 set(obj,'BytesAvailabelFcnMode', 'byte');
 end
 if ~strcmpi(get(obj,'Status'),'open');
 fopen(obj);
 if needed_open
 fprintf(1,'find_and_open: needed to open port %s\n', port);
 end
 end
  keycamel villegas
 el 16 de Abr. de 2011
        hi fremond khoo..did you used a motor driver for your stepper motor?and may i know what are the connections?thank you!!.. im a newbie in matlab..and my thesis project involves the movement of a bipolar stepper motor. :)
  keycamel villegas
 el 19 de Abr. de 2011
        my project involves a bipolar stepper motor and im using L298 motor driver..would it be possible that your code above can be used in our project?
2 comentarios
  keycamel villegas
 el 19 de Abr. de 2011
				our professor said that our connection would be:
RS232 to serial to parallel converter(using 4 bit shift register) to l298 motor driver connected to bipolar stepper motor,,
would that be possible?or can you suggest other connection for our bipolar stepper motor because i have difficulty analyzing the connections of this components,,
by the way THANK YOU for your reply :)
  keycamel villegas
 el 19 de Abr. de 2011
        our professor said that our connection would be:
RS232 to serial to parallel converter(using 4 bit shift register) to l298 motor driver connected to bipolar stepper motor,,
would that be possible?or can you suggest other connection for our bipolar stepper motor because i have difficulty analyzing the connections of this components,,
by the way THANK YOU for your reply :)
6 comentarios
  keycamel villegas
 el 6 de Mayo de 2011
				can i asked you a question? how uart of a microcontroller communicate with the uart of matlab?.thank you
  Walter Roberson
      
      
 el 6 de Mayo de 2011
				The program running on the microcontroller uses system-specific methods to cause the microcontroller's UART to send the bytes out.
Please start a new question for these kinds of topics; this question is very long and difficult to follow.
Comunidades de usuarios
Más respuestas en Power Electronics Control
Ver también
Categorías
				Más información sobre Image Preview and Device Configuration en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


