Controlling a Servo via Matlab

Hello,
I have a Micro Maestro 6-Channel USB Servo Controller, an external power supply and a Modelcraft WG-90MG Servo. The command port is COM3, the TTL port is COM4. My Servo is connected to channel 5. Controlling the Servo via the “Pololu Maestro Control center” works fine. So the first hurdle is overcome.

Now I would like to control my Servo via Matlab (for the first trial, just bring it into a specific position). Unfortunately, I’m an absolute newbie regarding electro-technical stuff. By reading some code snippets in the internet, I think I have at least now found out how to establish a connection between Matlab and the Pololu (though I’m not sure whether the parameters are correct :frowning: ):

port = 'COM3';      % Opens COM Port associated with Pololu 

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); % Make communication between MATLAB and Pololu

% fwrite(ser1, [128, 5, 5, 0, binvec2dec(bitget(4500,8:13)), binvec2dec(bitget(4500,1:7))]);
fwrite(ser1, ???what comes here???);


fclose(ser1); % Closes communication line between MATLAB and Pololu
delete(ser1); 

However, where I get stuck is the line where it comes to actually send a servo command to the Pololu. I just don’t know what I have to put into this line (I mean the second input parameter of the fwrite() function)? How do I tell Pololu “Move the Servo on channel 5 to position X”? I have already looked at the User guide here: pololu.com/docs/0J40/5.e, but unfortunately I just do not understand it, sorry.

I would be very happy to get some advice from you.

Hello.

I have not used Matlab before, but it sounds like you are mostly confused about the Maestro’s protocol with serial commands. Using the compact protocol, as it states in the section of the user’s guide that you linked to, the first byte should be 0x84 (132 in decimal) to start the command, the second byte designates the channel number of the servo (you specified channel 5), and the third and fourth bytes represent the target position.

The target position is configured by splitting the binary representation of the target into two separate pieces, which are each represented with a byte (the third and fourth bytes of the serial command). The byte that goes in the third position represents the lower seven bits of the binary representation of your target position (designated by the red text in the user’s guide). The fourth byte holds the other half of the binary representation of the target position (designated by the green text in the user’s guide). Using the guide’s example, setting a neutral target value of 6000 (1500 µs) is done by first looking at the binary representation of your target position, and splitting it up into two pieces. The lower seven bits when converted back into decimal is 112, and the other half converted back into decimal is 46.

So your fwrite function should be fwrite(ser1, [ 132, 5, 112, 46] ); to send the servo on channel 5 to its neutral position.

There is a thread about controlling a Maestro using Matlab that you might find helpful. It includes an example of the fwrite() function that sets the servo to its neutral position.

-Brandon

Thank you very much for your reply and your explanations. Now it works: What i fogot to do, is to set the Maestro’s serial mode to USB Dual Port.