Please Help me
I currently bought a parallax servo motor controller board (part number #28823).·It is·said that the board·can be·controlled by a VB program. I have no idea·of how to program. Is there anyone having such·programming experience?·Could anyone provide me some reference or sample codes ? Thank you for your attention!.··
Comments
As of the command for reading the position of servo motor, I wrote my codes as followed.
According to the documentation, the device will automatically reply the servo motor position to the input buffer after the command for reading motor position is sent. However, when I trace the variables, I find the BytesAvailable for input buffer is always 0 which means there is no reply from device.
Is there anyone who also encounted such problem? Thank you for your attention.
%~~~~~~~~~~~~~~ SMC CMD4 Read the Position of Motor ~~~~~~~~~~~~~~
% In:"!SCRSP"x (x is the channel number) => Out: x y z (where x is
%% ~~~~~~~~~ 1.Input Command ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cmd=uint8('!SCRSP');
cmd=[noparse][[/noparse]cmd 1 13]
fwrite(s_port,cmd,'async');
%% ~~~~~~~~ 2. Input Buffer ready to read ~~~~~~~~~~~~~~~~~~~~
while s_port.BytesAvailable~=3 % (3 stands for the bytes of reply)
end
stopasync(s_port);
%% ~~~~~~~~~ 3. Read Data from Input Buffer ~~~~~~~~~~~~~~~~~~~~~~~~~
M_pos=fread(s_port,s_port.BytesAvailable)
See if this post helps any: http://forums.parallax.com/showthread.php?p=598635
command at first and both of them work well; then it is the command for reading the servo motor position and there is no reply from the device.· Thank you for your reply.
%~~~~~~~~~~~~~~ SMC CMD4 Read the Position of Motor ~~~~~~~~~~~~~~
% In:"!SCRSP"x (x is the channel number) => Out: x y z (where x is
% channer_no, z:y represents the position in binary number
s_port=serial('COM18','BaudRate',2400,'Parity','none','DataBits',8,'StopBits',1);
s_port.timeout=0.001;
fopen(s_port);
%~~~~~~~~~~ Command 1 ping the device ~~~~~~~~~~~~~~~~~~~~~~~~
fprintf(s_port,'%s\r','!SCVER?','async');
while (s_port.ValuesSent~=8)
end
echoedInput=s_port.ValuesSent;
while s_port.BytesAvailable~=11
end
stopasync(s_port);
fread(s_port,s_port.BytesAvailable);
% ~~~~~~~~ Command 3 set/write the SMotor position~~~~~~~~~~~~~~~~
%% ~~~~~~~~1. Input Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
cmd=uint8('!SC');
ch=1;
ra=0; %fastest rate
pwl=50;
pwh=0;
cr=13;
cmd=[noparse][[/noparse]cmd ch ra pwl pwh cr];
fwrite(s_port,cmd,'async');
%% ~~~~~~~~~ 2. Output buffer finish transmission ~~~~~~~~~~~~~~~~~
while (s_port.ValuesSent~=echoedInput+8)
end
echoedInput=s_port.ValuesSent; % The device can automatically echo input to output
stopasync(s_port);
%~~~~~~~~~ Command 4 Read the position of Motor ~~~~~~~~~`~
%% ~~~~~~~~~ 0.Input Command ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%cmd=uint8('!SCRSP');
%cmd=[noparse][[/noparse]cmd 1 13]
%fwrite(s_port,cmd,'async');
cmd=[noparse][[/noparse]'!SCRSP' 1];
fprintf(s_port,'%s\r',cmd,'async');
%% ~~~~~~~~~ 1. Output buffer finish transmission ~~~~~~~~~~~~~~~~~
while (s_port.ValuesSent~=echoedInput+8)
end
echoedInput=s_port.ValuesSent; % The device can automatically echo input to output
%% ~~~~~~~~· 2. Input Buffer ready to read ~~~~~~~~~~~~~~~~~~~~
while s_port.BytesAvailable~=0 % (8 bytes in +3 bytes out)
end
stopasync(s_port);
%% ~~~~~~~~~ 3. Read Data from Input Buffer ~~~~~~~~~~~~~~~~~~~~~~~~~
c_pos=fread(s_port,s_port.BytesAvailable) % quite same as fscanf
% ~~~~~~~~~~~ Program End ~~~~~~~~~~~~~~~~~~~~~~~~~~~
stopasync(s_port);
echoedInput=0;
fclose(s_port);
delete(s_port);
clear s_port;
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com