Shop OBEX P1 Docs P2 Docs Learn Events
Using PBASIC serin with MATLAB to control servos — Parallax Forums

Using PBASIC serin with MATLAB to control servos

burgessburgess Posts: 3
edited 2005-04-04 15:35 in BASIC Stamp
I am using a basic stamp 2 with a PicoPic servo controller card to control 20 servos. I have a 1D array in matlab that I pass to Pbasic using the SERIN command. The Matlab and Pbasic code are below.

MATLAB:

ser_obj=serial('COM1','Baudrate',9600);········ --This·sends the data from·matrix(i) in 0.3 second intervals
····· for i=1:20
fopen(ser_obj);
fprintf(ser_obj,'%d\n',[noparse][[/noparse]matrix(i)]);
fclose(ser_obj);
pause(.3)
end··

where matrix() is the 20 element array.

PBASIC:

pos··· VAR··· Word
poss··· VAR··· Word
serial·· CON··· 16
Baudrate CON 84
Portnum VAR Byte
i VAR Byte
FOR PortNum = 1 TO 20
SEROUT 8, 84, [noparse][[/noparse]120, PortNum, $05, $DC, 0]········ -- puts all servos at mid-position
NEXT
·FOR i=1 TO 20
· SERIN serial,Baudrate,[noparse][[/noparse]DEC4 pos]····················· -- recieves matlab matrix(i) data and moves·each servo
· PAUSE 50
· SEROUT 8, 84, [noparse][[/noparse]120, i, pos.HIGHBYTE, pos.LOWBYTE, 0]
· PAUSE 200
··NEXT

The problem I am having is that at best, only 19 of the servos move. The ones that do move are going to the correct position.· I believe the problem is with the timing of the data transfer between the two programs. Experimenting with diferent·pauses I realized that the pause length effects how many servos move. It seems that the pbasic program is waiting at the SERIN command for the next value from the matrix, but it is either not being sent or is being sent at the wrong time.· Anyone have any ideas as to what the problem could be?
·

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2005-04-03 19:53
    Yes. The best scenario you can have for this has the BS2 'pending' in the SERIN, waiting for data. When the data comes, the BS2 grabs it, does its thing to transfer it, then immediately goes back to 'pending' in the SERIN routine.

    Thus you need neither of the PAUSE statements, instead you should let the PC 'meter' out it's commands to the BS2. If the PC sends the position string, then the PC waits for 200 mSec, this will guarantee that the BS2 will sit waiting in the SERIN for those 200 mSec -- and your servo's will have time to move.

    If the BS2 is not sitting in the SERIN command when the PC starts transmitting, then some data is not going to be recieved.

    P.S. and now I see you DO have matlab doing a pause(0.3) up there -- this is good.· It does mean at 250 mSec of pauses in the BS2 that you were kind of on the hairy edge of what would work.· Remove the pauses in the BS2, and just use the MatLab pause(0.3).· This will guarantee that the BS2 is in the SERIN statement before you start sending data.

    You might want to pause your first send of data to insure the BS2·gets the first string correctly.


    Post Edited (allanlane5) : 4/3/2005 7:57:10 PM GMT
  • burgessburgess Posts: 3
    edited 2005-04-03 20:45
    allanlane5:

    ···· I tried your suggestion of removing the pauses in my pbasic code. The results are the same. I still get a seemingly random number of servos to operate. Sometimes 12 or 14 operate and I have yet to get all 20 to work. I thought that the method I used to·for this code would not allow any time based errors because the Pbasic would already be waiting for data each time matlab sent out a value.· This was the purpose of the pause in matlab.· There seems to be an accumulation of time error.·Any other ideas?
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-04-04 15:35
    This almost sounds like a grounding problem. For reliable communication, you MUST have the pin-5 ground connected to both sides in the PC to BS2 connection -- that's the RS-232 SERIAL connection.

    Also, a ground must be connected between the BS2 and the PICO Servo board. Also, the Servo grounds and the PICO logic ground probably need to be connected -- I'd say MUST be, but I don't know the PICO board.

    It also sounds like a power problem, that something is being 'browned out' by the servo movement current use.

    I highly recommend you put the 'pause(0.3)' FIRST in the MatLab code, not last. This will hopefully guarantee that the BS2 will have entered its SERIN before MatLab sends its first byte.

    I don't understand why you 'close' and 'open' the port for every parameter sent. Can't you open the port, THEN do the 'FOR' loop sending parameters, THEN close the port?
Sign In or Register to comment.