Assstance for mini maestro usb

Hello forum,

I try to interface my mini maestro with perl code but I’m doing something wrong.

Could anyone help with this mess I wrote:

--------------------test.pl------------------------
use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/ttyS0");

$port->baudrate(9600);
$port->parity(“none”);
$port->handshake(“none”);
$port->databits(8);
$port->stopbits(1);
$port->read_char_time(0);
$port->read_const_time(1);

my $pack = pack “CCCCCC”, 128, 1, 4, 0, 112, 0;
print “Position: $position\n”;
$ob->write($pack);
undef $pack;
--------------------test.pl------------------------

The servos don’t move the green led flashes which tells me that this is the correct usb/serial port.

I read the documentation and I can see that there are different command protocols and I tried them all in this script but to no avail.
Could anyone write the simplest perl code which sends a move servo to some position command to get me started.

Thank you in advance.

Hello,

Does your Maestro work in the Control Center?

How did you pick /dev/ttyS0? It is very unlikely that this is correct - but if you run “dmesg” or look in /var/log/messages you should see exactly which ports the Maestro is connected on. The Command Port is probably on /dev/ttyACM0.

Have you configured the Maestro for Dual-Port or Chained mode?

Oh, and the bytes you tried sending were also totally wrong. Were you looking at the documentation for some other controller? You could try the first example give here if you want something guaranteed to work.

-Paul

Thanks for the reply Paul.

The controller is indeed connected to ttyACM0 I pasted older version of my script :slight_smile:
It works properly in the control center.
I tried with:
my $pack = pack “CCCC”, 132, 2, 112, 46;
as per the example you linked me to but it still didn’t moved or initialized the servo.

“Have you configured the Maestro for Dual-Port or Chained mode?” – How do I do this and which one do I need in order to interact with perl.

Thanks for the help and sorry for the noobish questions but I’m new to serial communication :slight_smile:

Hello,

Either mode will work, and you configure it in the Control Center. Please read about serial modes to learn more.

-Paul

I read the correct instructions for setting a target and the modes in the documentation and now it works perfectly.
Thanks for the help Paul.

Hello,

I am happy to hear that it works! Can you post the code that finally worked for you, in case someone else wants to try using the Maestro with perl?

-Paul

Sure I still have some issues and because it’s easier I use the MicroSSC protocol.

My problem now is that I cant figure out how to calculate and form the high and low bits according to this:


Set Target (Pololu/Compact protocol)
Compact protocol: 0x84, channel number, target low bits, target high bits
Pololu protocol: 0xAA, device number, 0x04, channel number, target low bits, target high bits


If someone can help here it will be greatly appreciated.

Here is my working code using the MicroSSC protocol:


use Device::SerialPort;
#
# usage perl servo.pl 0 234  
# this will send servo 0 to position 234 



my $port = Device::SerialPort->new("/dev/ttyACM0");

$port->baudrate(9600);
$port->parity("none");
$port->handshake("none");
$port->databits(8);
$port->stopbits(1);
$port->read_char_time(0);
$port->read_const_time(1);

$servo = shift; # servo number
$pos = shift;   # 1-255 according to MiniSSC protocol

my $pack = pack "CCC", 255,$servo, $pos;     #packing command
$port->write($pack);
$port->close;

Okay, did you see the example C code given below? I expect the same expressions to work in perl (with the addition of a few dollar signs on the variable names, of course).

-Paul

Unfortunately I can’t translate it correctly to perl.
I’m still tinkering around with the code.
In the meantime if someone knows how the high/low bytes are calculated in perl please assist.

To pack the high and low bits in perl:

$servo = shift; # servo number
$pos = shift; # picoseconds * 4

my $pack = pack("CCCC", 0x84, 2, $pos & 0x7F, $pos >> 7 & 0x7F);

If you want to doublecheck your pack, this will spit back the values in hex:

print unpack("H*", $pack) . "\n";