Need help with Serial control of Mini Maestro (Matlab)

Hi all!

I’m currently trying to use Matlab to control servos through the Mini Maestro-18 servo controller. I’ve been using the sample code that I’ve found in these forums:

function moveServo(s,x,port)
    x = max(500,x);
    x = min(5500,x);
    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);    %initialize
    AA = [170,12,4,s, binvec2dec(bitget(x,8:13)), binvec2dec(bitget(x,1:7))];
    fwrite(ser1, AA);
    
    fclose(ser1);
    delete(ser1);
end

Pretty standard stuff, with the parameters modified to match the Mini Maestro user guide. When I send the command through Matlab, the green led indicator flashes to show that the message was sent, but the servo doesn’t move at all.

For the longest time I thought it was just some issue with the Matlab code, but then I also used the Serial Transmitter Tool and even tried some sample byte sequences as shown in the user guide, but the same thing would happen. I can’t even get the serial transmitter to read the servo position.

Has anyone else had issues with serial communication with the Mini Maestro-18 board? The only other issue I can think of is that there might be something wrong with how I connected the board, but it works fine through the Maestro Control Center, and all I’m doing is sending commands through the USB connection. Some older forum posts mention that I should be using the jumper to specify the code protocol, but with these Maestro boards, that should be done automatically based on the first byte that gets sent, right?

Also, I’m a bit confused about how serial control will work. Once I send the target move command (assuming that I’m able to at some point), does the servo hold this position actively? I feel as though there should be some “enable” command much like how the Control Center can enable/disable servos.

Thanks for everyone’s time!
Ray

Once you send a Set Target command with a non-zero value to the Maestro, the Maestro will start sending pulses to the servo, which should cause it to actively maintain its position if the servo is powered. If you send a Set Target command with a value of 0, it will stop sending pulses so the servo will stop being active.

There are several things that appear to be wrong with your code. Your range of allowed parameters values (500-5500) is strange; 4000-8000 would make more sense for the Maestro. It looks like you are sending the target bits in the wrong order. How about you massively simplify your code by just trying to send the example bytes from the user’s guide: 132, 0, 112, 46. That’s the compact protocol command for setting the target of channel 0 to 1500us.

Another thing to try is sending those example bytes with the Pololu Serial Transmitter to make sure there is nothing wrong the virtual serial port.

–David

Hello,

Please also make sure that you have selected an appropriate serial mode as described in the user’s guide, and tell us how you selected the right COM port.

-Paul

Thank you David/Paul for such quick responses!

Got everything to work with the following adjustments:

  • Changed Serial settings to USB Dual Port through the Control Center
  • Reversed the target byte sequence from 8:14 and then 1:7 to 1:7 and then 8:14
  • Used the secondary COM port option that was given (not sure what the standards are for most computers, but mine returned COM3 and COM4 as possible connections. The latter is the only one that worked)

Accompanying Matlab code:

function moveServo(s,x,port)
    if (x ~= 0)
        x = max(500,x);
        x = min(5500,x);
        x = 4*x;
    end
    
    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);    %initialize
    AA = [170,12,4,s, binvec2dec(bitget(x,1:7)), binvec2dec(bitget(x,8:14))];
        %0xAA = 170
        %device number 12 by default
        %4 = action (set target)
    fwrite(ser1, AA);
    
    fclose(ser1);
    delete(ser1);
end

So, the previous posts on using Matlab code to control servos with older control boards had the bit sequences reversed and different command byte headers, and they also didn’t account for the factor of 4 difference for the new Mini Maestro boards.

Thanks again!

Hey,
I am trying to control my mini maestro using matlab,
is there anyone who could send me some example code to get me started,
I am very new to programming and am having great difficulty finding information
regarding the maestro and matlab.
My email is skuldgier@hotmail.com
Thank you
Kai

Hello.

Did you read this thread? Toothpickguy is saying the code in his last post works. Did you try it? Does it not work for you?

- Ryan

Yes I tried the code out,
I get several errors. First is says that x is not defined…
so I added a script at the start saying x=1. Then it says com is not defined.
I am not sure how to define the com. My maestro is hooked up to com 3 at the moment.
I was working with similar code and ran into the same com error

Here is were I am at:

s=0;
port=('COM4');
set(ser1, 'InputBufferSize', 2048);
    set(ser1, 'BaudRate', 9600);
    set(ser1, 'DataBits', 8);
    set(ser1, 'Parity', 'none');
    set(ser1, 'StopBits', 1);
    
    fopen(ser1);    %initialize
    AA = [170,12,4,s, binvec2dec(bitget(x,1:7)), binvec2dec(bitget(x,8:14))];
        %0xAA = 170
        %device number 12 by default
        %4 = action (set target)
    fwrite(ser1, AA);
    
    fclose(ser1);
    delete(ser1);

This is what I ended up with. S is the channel that my servo is connected to, I believe which is zero in my case.
I do not know how to get my x value.

Hello.

Take a look at the user’s guide section on serial servo commands. Here is the description of the bytes being created by x:

Basically x is an integer representing the width of the servo pulse in 1/4 microseconds.

- Ryan

Hello, I used The Cod but there is Error at “if (x==0)”,
Is there any other line missing here?