Setting up Raspberry Pi with Robot Arm

Submitted by code_admin on Mon, 07/23/2018 - 09:27

Install libusb

  1. sudo apt-get install libusb-1.0-0-dev

You can check it is there

  1. ls /usr/include/libusb-1.0

Write Scan C++ Program

Write a program to scan for devices.

Create a file scanusb.cpp

  1. #include <iostream>
  2. #include <libusb-1.0/libusb.h>
  3. using namespace std;
  4.  
  5. void printdev(libusb_device *dev);
  6.  
  7. int main()
  8. {
  9.   std::cout << "Hello World!" << std::endl;
  10.  
  11.         libusb_device **devs; //pointer to pointer of device, used to retrieve a list of devices
  12.         libusb_context *ctx = NULL; //a libusb session
  13.         int r; //for return values
  14.         ssize_t cnt; //holding number of devices in list
  15.         r = libusb_init(&ctx); //initialize a library session
  16.         if(r < 0) {
  17.                 cout<<"Init Error "<<r<<endl; //there was an error
  18.                                 return 1;
  19.         }
  20.         libusb_set_debug(ctx, 3); //set verbosity level to 3, as suggested in the documentation
  21.         cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
  22.         if(cnt < 0) {
  23.                 cout<<"Get Device Error"<<endl; //there was an error
  24.         }
  25.         cout<<cnt<<" Devices in list."<<endl; //print total number of usb devices
  26.  
  27.         ssize_t i; //for iterating through the list
  28.         for(i = 0; i < cnt; i++) {
  29.                                 printdev(devs[i]); //print specs of this device
  30.                 }
  31.                 libusb_free_device_list(devs, 1); //free the list, unref the devices in it
  32.                 libusb_exit(ctx); //close the session
  33.  
  34.   return 0;
  35. }
  36.  
  37. void printdev(libusb_device *dev) {
  38.         libusb_device_descriptor desc;
  39.         int r = libusb_get_device_descriptor(dev, &desc);
  40.         if (r < 0) {
  41.                 cout<<"failed to get device descriptor"<<endl;
  42.                 return;
  43.         }
  44.         cout<<"Number of possible configurations: "<<(int)desc.bNumConfigurations<<"  ";
  45.         cout<<"Device Class: "<<(int)desc.bDeviceClass<<"  ";
  46.         cout<<"VendorID: "<<desc.idVendor<<"  ";
  47.         cout<<"ProductID: "<<desc.idProduct<<endl;
  48.         libusb_config_descriptor *config;
  49.         libusb_get_config_descriptor(dev, 0, &config);
  50.         cout<<"Interfaces: "<<(int)config->bNumInterfaces<<" ||| ";
  51.         const libusb_interface *inter;
  52.         const libusb_interface_descriptor *interdesc;
  53.         const libusb_endpoint_descriptor *epdesc;
  54.         for(int i=0; i<(int)config->bNumInterfaces; i++) {
  55.                 inter = &config->interface[i];
  56.                 cout<<"Number of alternate settings: "<<inter->num_altsetting<<" | ";
  57.                 for(int j=0; j<inter->num_altsetting; j++) {
  58.                         interdesc = &inter->altsetting[j];
  59.                         cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | ";
  60.                         cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | ";
  61.                         for(int k=0; k<(int)interdesc->bNumEndpoints; k++) {
  62.                                 epdesc = &interdesc->endpoint[k];
  63.                                 cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | ";
  64.                                 cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<" | ";
  65.                         }
  66.                 }
  67.         }
  68.         cout<<endl<<endl<<endl;
  69.         libusb_free_config_descriptor(config);
  70. }

Compile and run

Find g++ options for libusb:

  1. pkg-config --libs libusb-1.0

Build with g++

  1.  g++ scanusb.cpp -lusb-1.0

Run

  1. ./a.out

USB Permission

The USB device can only be opened by root.

  1. lsusb -v -d 1267:0000

This gives permission error in output (Couldn't open device, some information will be missing)

Add a line to
sudo nano /etc/udev/rules.d/40-scratch.rules

  1. ATTRS{idVendor}=="1267", ATTRS{idProduct}=="0000", SUBSYSTEMS=="usb", ACTION=="add", MODE="0666", GROUP="plugdev"

Repeast the lsusb test and there will be no couldn't open device message!

RJM Article Type
Work Notes