[libusb] linux/windows set position with unmanaged C++

Facepalm! I found out what the problem was, I forgot to include libusb_exit(ctx); at the bottom of setTarget(), I’ve updated the code in my first post so try it out and it should now work :smiley:

I’m glad you were able to fix it!

You were probably running out of memory. Most of the libusb functions have return codes (such as LIBUSB_ERROR_NO_MEM) that tell you whether there was an error or not. I recommend that you check those return codes in your program and if there is an error code then you should print it and exit. This will make it easier to debug future problems.


Thank you guys
I really appreciate your help

I will test the code right now.

I also would like to share
some code I generated using Perl. Basically
It does the same as in the C++ version
(see above cosine.zip)

Ubuntu-Linux to Mini-Maestro 12 (/dev/ttyACM0)

serial.zip (902 Bytes)


Thanks for the code. That is exactly what I needed. I am trying to port the code over to a gumstix and I am having problems with the recipe. Any chance you could give me a few pointers on what you did with your recipe and with the cross compiler? I’ve spent hours trying to figure it out without any success.


First off bitbake libusb, then extract the maestro folder in the attached zip to your user recipes and bitbake maestro. In the bitbake file replace with your own.
Remember to increase the revision number in the bitbake file every time you make a modification.
maestro.zip (4.18 KB)

Hello, Brown.

In another thread, ChisBob has posted his kernel module for sending commands the Maestro. He wrote it specifically for a Gumstix. I thought this might help you.


Hi guys,

our application is finally working :slight_smile:
We build a little Wrapper around the C+±Code so you have a C+±Class to work with.

Here are the source-files:

and you of course need libusb-1.0.

Here you find a little video about our finished project - it was for so called “Studienarbeit” :wink:

Greetings from Germany,

Hello, Steven.

I’m glad that things worked out for you. Thanks for sharing your code and the video!


Im sorry , but im having trouble setting up libusb in Visual C++ Express . If anyone could give me any instructions on how to get it running I would be incredibly in his/her debt.

Cheers .

Hello, blinpa.

Using libusb in Windows is probably more trouble than it is worth. I recommend downloading the Pololu USB SDK and using the Visual C++ Maestro example included with it, which is based on UsbWrapper.dll, which uses WinUSB. Instructions are in the README.txt file of the SDK.


Hello, I tried your code and I get a segmentation fault error on this line:

libusb_control_transfert(device_handle, 0x40, …

What’s the problem ?

Probably the call to libusb_open on the previous line failed, leaving the device_handle in an invalid or undefined state. To debug this, he first thing I would do is add error checking to the code; almost all the libusb functions have a return value that let you see if an error happened. If it did, then your program should print the error message and exit.


Yes it seems to be that, I have the segmentation fault on libusb_close if I remove the line with libusb_control_transfert.
I checked and libusb_open is returning -3.

Okay. Then the next step is to look up that error code on this page:


Also, here is the documentation of libusb_open:

libusb.sourceforge.net/api-1.0/g … 7fa81c89d1

I think you need to download the Pololu USB SDK and follow the part of the instructions that helps you set a udev rule. That should give you permission to access the device.


Yes I saw it’s a access denied error…
A bout the rule you mean copying 99-polulu-rule , I already did that also the UscCmd works to control my servo, then the problem must be somewhere else :frowning:

I just tried to run my program by launching directly my bin file from terminal with sudo and it’s working this way. Is there a way to avoid that ?

You should be able to get the permission right so you can access the device as a normal user. I am not sure why UscCmd would work while your program does not, but could you try unplugging the Maestro and plugging it back in to make sure the latest udev rules have been applied? --David

I looked at the 99-pololu-rule in the etc/… and it was empty ! Then now it works, thank you.

Hello again, is there a way to retreive the servo position ?

Yes. The Maestro has a native USB command for getting an array of structs where each struct holds data about one channel. This struct has a “position” field that holds the current width of pulses that the Maestro is sending. The command is implemented in the Pololu USB SDK in the Usc class. The method you should look at is getVariables(out ServoStatus[] servos). The exact protocol you need to use depends on whether you have a Mini Maestro or a Micro Maestro and you can see those details in the source code.