Shop OBEX P1 Docs P2 Docs Learn Events
Matlab and servo control using BS2px — Parallax Forums

Matlab and servo control using BS2px

ATATATAT Posts: 3
edited 2008-11-19 00:46 in BASIC Stamp
I've read all over the forums and have been unsuccessful at improving my current setup.

I am trying to have matlab (R14) just send a simple sine wave (absolute value) via the USB interface to my BS2px. The system is sending signals but they are rather noisy in the response. In matlab I sweep sine, and then inside of the BS2 I simply add the min pulse width of 490, then send that pulse width value back to matlab. The plot below is the inputs from matlab, and the respective returned signal. I've also posted all of the codes. What I want is to smooth out the communications. The final product I am looking for will be much more aggressive signals, so I would like to be able to remove all of this noise. Also I don't want to add a bunch of LONG waits just to make sure the signal is clean. This needs to be as fast as possible since this is just the coms part of a much larger system.

Thanks ahead of time,

ATAT

testsignal.jpg


Matlab code:

clear all
port_number = 5; % **(may need to change)**

port = [noparse][[/noparse]'COM5'];

s = serial(port); % Select COM port
s.terminator = 'CR';
% s.ReadAsyncMode = 'manual';
fopen(s) % Open COM port for reading
tic
t= toc 
message = 'clear'
length = 'short'
ii=0;
while t <= 30
    ii=ii+1;
    info = abs(round(2400*sin(10*t*pi/180)))
%     info = [noparse][[/noparse]num2str(s1)];
fprintf(s, '%d\n',info);
[noparse][[/noparse]echo] = fscanf(s);
pause(0.01)

[noparse][[/noparse]message] = fscanf(s, '%d\n');
if isnumeric(message)
pos(ii) = message;
else
    pos(ii)=0;
end
com(ii) = info;
message
% length
t= toc; 
end

fclose(s)
delete(s)
clear s

disp('End Program')
hold on
plot(com+490,'r')
plot(pos)





and here's the BS2 code



' {$STAMP BS2px}
' {$PBASIC 2.5}

PCpin       CON     16
baudrate    CON     16780
INFOnew     VAR     Word
INFOold     VAR     Word
PULSE       VAR     Word


Start:
INFOnew = 0000
INFOold = INFOnew

talk_in:

SERIN  PCpin, baudrate, 20, Pass, [noparse][[/noparse]DEC INFOnew]
INFOold = INFOnew

command:
PULSE =490 + INFOnew 'range seems to be 500 to 2900
PULSOUT 12, PULSE
PAUSE 20

talk_out:
SEROUT PCpin,baudrate,[noparse][[/noparse]DEC PULSE,CR]
                     PAUSE 20
GOTO talk_in

Pass:
INFOnew = INFOold
GOTO command





thanks again

Post Edited (ATAT) : 11/18/2008 5:38:44 PM GMT

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-18 19:52
    ATAT -

    I know nothing about Matlab, but here are a couple of things to consider. You're using the DEC formatter on your SERIN in the PBASIC program. This will slow things down, without question. Also, in the same SERIN, I'd look into your timeout parameter, and see what happens to your graph/servo pulse if a timeout does occur; which it very well may.

    Lastly, you may be able to cut back (time-wise) on your PAUSE command after the PULSEOUT instruction. Your program execution will absorb some time. How much time, you will have to figure out. Here is some timing assistance:
    http://emesystems.com/BS2speed.htm Thanks go to Dr. Tracy Allen for developing the instruction timings chart.

    Just some thoughts to ponder. Good luck with your project. It is really unique!

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When all else fails, try inserting a new battery.
  • ATATATAT Posts: 3
    edited 2008-11-18 23:04
    Thanks for the links to increase the MC's speed. That will help a great deal Bruce. Anyone have ideas on why I get the occational divation down to 490? Its obvious that something is preventing the line

    Pulse = 490 + INFOnew

    to be executed correctly, but I have no idea what. If there was a communications error it should use the value of INFOold in its place, but its not.


    Also could someone give me an example of a SEROUT with a timeout please? when I try to write SEROUT, pin, baudrate, XX, FUNC, [noparse][[/noparse]data] the pbasic compiles says it was expecting a [noparse][[/noparse] where the FUNC is. ie it is trying to treat the XX as a 'pace' command like the documentation says.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-18 23:35
    ATAT -

    Example of SERIN with timeout:

    SERIN Rpin {\Fpin}, Baudmode, {Plabel,} {Timeout, Tlabel,} [noparse][[/noparse]InputDat]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    result VAR Byte

    Main:
    SERIN 1, 16468, 2000, No_Data, [noparse][[/noparse]DEC result]
    DEBUG CLS, ? result
    STOP

    No_Data:
    DEBUG CLS, "Timed out"
    STOP

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    In the example above, taken directly from the PBASIC Help File, "\Fpin", and "Plabel" are absent. Thus you have remaining:

    SERIN Rpin, Baudmode, {Timeout, Tlabel,} [noparse][[/noparse]InputDat]

    which is exactly like the example above. Note: Only square brackets "[noparse][[/noparse] ]" are used with this command.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When all else fails, try inserting a new battery.
  • ATATATAT Posts: 3
    edited 2008-11-19 00:23
    Bruce Bates said...
    ATAT -

    Example of SERIN with timeout:

    SERIN Rpin {\Fpin}, Baudmode, {Plabel,} {Timeout, Tlabel,} [noparse][[/noparse]InputDat]

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    result VAR Byte

    Main:
    SERIN 1, 16468, 2000, No_Data, [noparse][[/noparse]DEC result]
    DEBUG CLS, ? result
    STOP

    No_Data:
    DEBUG CLS, "Timed out"
    STOP

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    In the example above, taken directly from the PBASIC Help File, "\Fpin", and "Plabel" are absent. Thus you have remaining:

    SERIN Rpin, Baudmode, {Timeout, Tlabel,} [noparse][[/noparse]InputDat]

    which is exactly like the example above. Note: Only square brackets "[noparse][[/noparse] ]" are used with this command.

    Regards,

    Bruce Bates



    I have that working fine for the SERIN command... its the SEROUT that I want. I know that the formats are nearly the exact same, but my comp just keeps insisting that I can't do a timeout on the SEROUT.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-11-19 00:46
    ATAT -

    Not so, if you read the documentation CAREFULLY. SEROUT timeouts apply ONLY to the Fpin, and NOT to the data pin. Go take a look, and you'll see what I mean. This is what you're looking for:

    Fpin is an optional variable/constant/expression* (0 - 15) that specifies the I/O pin to monitor for flow control status. This pin will be set to input mode. NOTE: Fpin must be specified to use the optional Timeout and Tlabel arguments in the SEROUT command.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When all else fails, try inserting a new battery.
Sign In or Register to comment.