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

Hello ,
the files cosine works well on my Ubuntu20.0, I can build it with g++ and then control the maestro board with the compiled file.
but, when I adapted the cosine.cpp into ROS package, there is an error which says:
fatal error: libusb.h: No such file or directory,
I have already installed the libusb library by : sudo spt-get install libusb1.0-dev
and I added the headers “#include <libusb.h>” and “#include <libusb-1.0/libusb.h>”

do you have any clue about this?

thank you.

If you write #include <libusb-1.0/libusb.h> in Ubuntu after installing libusb1.0-dev, that should work. You can remove the line that is giving you the error, which is probably #include <libusb.h>.

–David

thank you David. yes, that worked.

now, another problem: I added “#include <libusb-1.0/libusb.h>”,

when building in ROS the errors come :slight_smile:
[ 95%] Built target publisher_set_position
/usr/bin/ld: CMakeFiles/subscriber_get_position.dir/src/maestro_getpost_sub.cpp.o: in function deviceMatchesVendorProduct(libusb_device*, unsigned short, unsigned short)': maestro_getpost_sub.cpp:(.text+0x1600): undefined reference to libusb_get_device_descriptor’
/usr/bin/ld: CMakeFiles/subscriber_get_position.dir/src/maestro_getpost_sub.cpp.o: in function setTarget(int, int)': maestro_getpost_sub.cpp:(.text+0x1691): undefined reference to libusb_init’
/usr/bin/ld: maestro_getpost_sub.cpp:(.text+0x16a4): undefined reference to libusb_get_device_list' /usr/bin/ld: maestro_getpost_sub.cpp:(.text+0x1718): undefined reference to libusb_open’
/usr/bin/ld: maestro_getpost_sub.cpp:(.text+0x174c): undefined reference to libusb_control_transfer' /usr/bin/ld: maestro_getpost_sub.cpp:(.text+0x175c): undefined reference to libusb_close’
/usr/bin/ld: maestro_getpost_sub.cpp:(.text+0x1781): undefined reference to libusb_free_device_list' /usr/bin/ld: maestro_getpost_sub.cpp:(.text+0x178d): undefined reference to libusb_exit’

To fix undefined reference errors to libusb functions, you need to add the -lusb-1.0 option to the linking step of your build process. That is an option that your build system needs to pass to GCC or your linker when it is producing the executable or shared library that uses libusb. I am not familiar with how you are building your software, but you might look for a place to add libraries, “ldflags”, or “linker flags”.

–David

thank you David.

I resolved the problems.

On ROS it’s about redefining the CMakelist.txt file.

Hi, David.

I’m coming back again with new problems. :laughing:

I’m using the library ‘libusb-1.0’ to control the maestroboard on Ubuntu 20.0.

With this library, I can set the target position to the motor with the function ‘libusb_control_transfer’ in the form of ‘libusb_control_transfer(device_handle, 0x40, REQUEST_SET_TARGET, position, servo, 0, 0, (ushort)5000);’ where REQUEST_SET_TARGET is defined as 0x85

I reied to set speed to the motor with ‘libusb_control_transfer(device_handle, 0x40, REQUEST_SET_SPEED, target speed, servo, 0, 0, (ushort)5000);’ where REQUEST_SET_SPEED is defined as 0x87 according to the Maestro user guide. but the speed didn’t change when running this function.

I also found out that in the user guide, for setting target the Compact protocol is : 0x84, but in the code for setting target the ‘REQUEST_SET_TARGET is defined as 0x85’ works well. I’m a little confused about this.

So, the question is how to set motor speed with the function ‘libusb_control_transfer’ ?

Thank you in advance.

– Lisheng

Hello, Lisheng.

I think you were looking at the documentation of the Maestro’s serial interface in the user’s guide. The Maestro’s serial interface is different from the native USB interface that you are accessing when you run libusb_control_transfer. To learn the format of the Maestro’s native USB commands, I recommend referring to the source code of the Usc class in the Pololu USB SDK:

https://github.com/pololu/pololu-usb-sdk/blob/master/Maestro/Usc/Usc.cs

That code uses some constants that are defined here:

https://github.com/pololu/pololu-usb-sdk/blob/master/Maestro/Usc/Usc_protocol.cs

To set the speed, you should be able to do something like this:

libusb_control_transfer(device_handle, 0x40, 0x84, speed, servo, 0, 0, 5000);

–David

Hi David,

thank you for the reply.

I just realized that I am using the ‘Mini Maestro 6’ board, the user guide says ‘speed control apply to Mini Maestro 12, 18, and 24 only’, so with the Mini Maestro 6 board, the motor speed can not be controlled/redefined (even with the native USB interface). am I right for this?

All of the Maestros support servo speed and acceleration limits. You can try out those features using the “Status” tab or “Channel Settings” tab of the Maestro Control Center. The command I mentioned above for setting the speed works on all the Maestros.

Could you send a screenshot showing the part of the user’s guide you are referring to?

–David

thank you David.

I tried, the function ‘libusb_control_transfer(device_handle, 0x40, 0x84, speed, servo, 0, 0, 5000);’ can set the motor speed, on my Mini maestro 6-channel board, which requires input a feasible value of ‘speed’.

Below is the screenshot which shows the context I mentioned above.