View Full Version : BS2p + Matlab + Servo (communication & control)

08-19-2005, 02:30 AM
I was wondering if there is any way to have the stamp do two things at once.· Right now I have the stamp connected to MATLAB via a·RS-232 connection and to a servo.· I have the communication between MATLAB and the·BS2p working adequately (although if anyone has any suggestions on doing this it would be appreciated).· The problem is that while I am communicating with MATLAB to get the updated control parameters, I need the servo to either hold its current position or execute a particular movement.· Currently, as soon as the stamp finishes sending the pulse train and is waiting for the next input, the servo goes limp since there is no control signal.· I am trying to use only one STAMP.·

I need my code to loop as fast as possible.· As of now the BS2p takes spends most of its time waiting for the "ZZ" flag (refer to code below) and then receiving the updated pulse.· I realize that right now I have a very simple control law that could be easily implemented right on the stamp but the goal is to implement a control law that is much more advanced and highly nonlinear (which would be difficult to do onboard).· Is there any way to reduce the downtime involved with the communication?· I have reviewed the MATLAB interface example http://www.parallax.com/dl/docs/article/BS2MatlabPaper.pdf·but that did not address timing or handshaking issues.· Does anyone have any suggestions on how to streamline this?· I have attached a simplified version of my MATLAB code and BS2 code for reference.

************************************************** **********
· LOW 5························ 'turn off status LEDs
· LOW 6
· LOW 7
· LOW 8

· SERIN 9, 240, [WAIT("ZZ")]··· 'flag to sync with MATLAB
· PAUSE 50
· HIGH 5······························· 'indicate flag received

· SERIN 9, 240, [DEC pulse]····· 'input updated pulse width
· GOSUB servo······················· 'move servo
· HIGH 6······························· 'turn on LED 6 to indicate pulse received

· GOSUB ReadStrain······················· 'read strain gage
· SEROUT 10, 240, [DEC Strain, CR]·· 'output strain measurement to MATLAB
· HIGH 7
· GOSUB servo······························ 'hold servo position

· GOSUB ReadPot·························· ·'read potentiometer
· SEROUT 10, 240, [DEC Pot, CR]······ 'output potentiometer measurement to MATLAB
· HIGH 8······································· 'indicator LED
· GOSUB servo······························· 'hold servo position

GOTO start
************************************************** ***

Thanks in advance!

08-19-2005, 02:53 AM
Typically, servos need to be pulsed every 20ms or so.· Waiting forever for serial data to arrive could throw the timing off significantly, depending on how often and well syncronized matlab sends "ZZ".· Also, why do you wait 50ms after receiving ZZ?· The first change·I would make is adding a timeout parameter to the SERIN so that it will continue to pulse the servo if the serial data isn't received within·a certain amount of time (see SERIN docs).· Also, why not combine your two SERIN instructions into a single instruction - that will save time on both the transmitting end and receiving end (no 50ms pause).· Lastly, you could look into using the handshaking pins the BS supports (see SERIN docs), if you can get matlab to work with handshaking.· This would eliminate the need for the ZZ flag entirely, thus cutting down on waiting and transmission time.