rs232 problem with matlab

hello

im trying to operate robotic snake that have 13 servos with the pololu 16 servo controler
using matlab. i already acomplished that with c# program that i built.
the conection threw matlab is by the following steps:
1.building a look up table
2.sending each value at a time after packing it as mini ssc2 mode

the problem is that program always stops after few values
i can send alot more values before it stops when i send it to 1 servo only
it seems that there is a memory problem or synchronization problem with the matlab and the pololu

function simulation(port,LUTable)

clc
%openning port

s=instrfind;
fclose(s);
try
disp(['Try connecting to port: ’ port]);
s = serial(port, ‘BaudRate’, 2400,‘InputBufferSize’,4096,‘OutputBufferSize’,4096)
fopen(s);
catch
disp(['Failed to connect with: ’ port]);
end
s.FlowControl = ‘none’;

%creating a packet according to the Look Up Table and writting it to the
%port

n=size(LUTable);
for i=1:n(1) %rows
for j=1:n(2) %columns
A=Create_Packet(j,LUTable(i,j)); %creating the data to servo controler (servo no,angel)
Write_To_Port(A,s);%writting the packet A to port object
end
end

fclose(s);
delete(s);


function A=Create_Packet(Servo_No,Servo_Pos)

A=[255,Servo_No,Servo_Pos];
end

I don’t have any c# experience to offer, but if you wanted to you could control the servos directly with Matlab commands. I’ve done this a few times and never had a problem. Here’s a simple example function:

function moveServo(s,x)
%moveServo(s,x) for Pololu serial servo controllers, using Pololu mode
%(remember to remove the blue mode-selection jumper!) absolute position
%command #4. You may need to edit the port variable in this file to match
%your com port.
%
%s=servo number
%x=absolute position, 500<=x<=5500

port = 'COM3';%Edit to your com port number

ser1 = serial(port);
set(ser1, 'InputBufferSize', 2048);
set(ser1, 'BaudRate', 9600);
set(ser1, 'DataBits', 8);
set(ser1, 'Parity', 'none');
set(ser1, 'StopBits', 1);

fopen(ser1);
fwrite(ser1, [128, 1, 4, s, binvec2dec(bitget(x,8:13)), binvec2dec(bitget(x,1:7))]);

fclose(ser1);
delete(ser1);

You can also download the m-file here.

The big part of this example is configuring and opening the com port. In actual use, you should only open the port once, send all the servo commands you want, then close the port when you’re done. You can also keep using MiniSSC-II mode by changing the byte string you send out, for example:

fwrite(ser1, [255,Servo_No,Servo_Pos]);

Keep in mind that your variables will have to be integers, and if you have any type errors you can just use the round function, as in round(Servo_Pos). There are a few more threads on using Matlab directly to control the Pololu servo controllers around the forum.

-Adam

P.S. Can you tell us more about your servo-snake robot (pictures maybe)? It’s amazing what you can achieve with a bunch of servos linked together and a little ingenuity!