In this project, a device will be designed which will allow controlling a servo motor from the desktop computer on USB interface. For controlling a servo motor, PWM output needs to be generated from the microcontroller. The length of the ON time of the PWM signal at the control pin of the servo determines the angle the servo should rotate. The project device receives data from the host computer on USB interface which will determine the ON time of the PWM signal and according a PWM signal will be sent out to the control pin of the servo using in-built timer.
For this, the device will be configured as HID Class Device again. An HID Class device has USB communication over two types of transfers – Control transfer and Interrupt transfer. The control transfer will be used for enumeration of the device. After enumeration, data packets will be read from the computer for determining rotation angle. The Tower Pro SG-90 servo motor is used in the project which can rotate about 180 degrees. The rotation angle is denoted from negative 90 degree to positive 90 degree.
The project will need a controller chip to handle USB data from the computer and to operate servo motor accordingly. The 8-bit USB AVR – Atmega 32u4 is used as the device controller chip in the project. The project uses AVR based Lightweight USB Framework (LUFA) as the firmware which will be responsible for implementation of the USB Protocol. The device is tested on a Linux system using Python based Pyusb and Libusb frameworks for sending data to the peripheral device from the host computer.
The Generic HID device driver class of the LUFA firmware is used and modified to program the project. With the use of LUFA firmware, the device driver code to implement USB protocol is not needed to be written explicitly. Modifying the firmware code will be sufficient to implement the USB protocol.
This project is based on Arduino Pro Micro which has the USB AVR – Atmega 32u4 as the sitting MCU. In order to understand this project, one must have basic knowledge of the AVR microcontrollers and the embedded C programming for AVRs. WinAVR Studio is used to write, edit and compile the project code, so closely following the project shall require familiarizing with the above stated IDE as well. Though LUFA framework takes care of implementing the USB protocol and has APIs to abstract the lower level codes, understanding USB protocol is recommended to understand how actually the project is working. In fact, if anyone has already worked on some other microcontroller, it will not be much pain to understand and follow this project as the project code is more or less about modifying the LUFA device driver to work as generic HID device and generating PWM output from the Arduino board. One must have additional knowledge of Linux operating system (Ubuntu) and should be knowing basic Linux commands. One having knowledge of Python programming language is recommended but not mandatory for implementing the project.
1. Arduino Pro Micro
2. Connecting wires
3. Tower Pro SG-90 servo motor
4. Micro USB cable
SOFTWARE TOOLS REQUIRED
The project uses Arduino Pro Micro as the USB controller chip and uses Tower Pro SG-90 servo motor. The servo motor has three wires – VCC, Ground and Control. The VCC and Ground wires are connected to the common VCC and ground respectively. The Control wire of the servo is connected to the pin 6 of Port C which is pin 5 on the Arduino Pro Micro. Therefore, the Arduino and servo motor will have connections according to the following table -:
The Program code for the project is burnt to the Arduino Pro Micro using AVR Dude. The Arduino board is connected to the USB port of a PC by a USB cable.
HOW THE PROJECT WORKS
In this project the USB protocol is implemented by the LUFA framework. For configuring the controller chip to work as Generic HID device, the HID Class Driver of the LUFA framework will be used. The Human Interface Device (HID) class takes care of the transfers between the host device and the human controlled USB peripherals like USB Keyboard, Mouse or Joystick.
When a USB device is attached to the host (PC), the host sends request for configuration details in the form of control transfer. The connected device has to respond with appropriate descriptors to get configured and ready for further operations. Only after configuration, the device can communicate with the host in the form of interrupt, isochronous or bulk transfers for executing the operations for which the device has been made. This process of identification and configuration of the device with the host is called enumeration. . The device designed in this project uses control transfer to enumerate with the host computer and send data to the microcontroller. The data will be exchanged using Class-Specific Requests. These Requests are sent by Host to Device via Control Transfer. The Set_Report is the request by which Host can send data to the device. Check out the Atmega 32u4 Based LED Series Project for learning about the Set_Report Class specific request.
The project is based HID class driver of USB and LUFA framework has HID class related module in the LUFA-Source-Folder /LUFA/Drivers/USB/Class/Device folder. Other device class related module are also in the same folder. The LUFA framework has demo projects for different USB device classes in the LUFA-Source-Folder\Demos\Device\ClassDriver folder. For implementing the project, demo project for Generic HID devices provided in the LUFA framework will be modified and complied. The demo project for Generic HID devices is in the LUFA-Source-Folder\Demos\Device\ClassDriver\GenericHID folder. The folder contains GenericHID.c file which will be modified to implement the project.
How GenericHID.c identifies HID device being Generic HID device
The GenericHID.c uses Generic_HID_Interface interface in HID_Device_USBTask() function which is being imported from the HIDDeviceClass.c (from LUFA-Source-Folder \LUFA\Drivers\USB\Class\Device) to configure the device as generic HID device. The interface abstracts the low-level descriptor codes and identifies the device as generic HID device through an InterfaceNumber variable.
Generic HID Device Specific Report Descriptors
Any HID device has to exchange data with the host which should be structured in the form of reports. The report descriptor defines the report structure. A report descriptor contains the information needed by host to determine the data format and how the data should be processed by the host. Therefore, a report descriptor basically structure the data that needs to be exchanged with the host according to the USB protocol.
For working like a generic USB HID device, the device needs to send usage report and data input report descriptors specific to Generic HID Class to the host while it itself needs to interpret data output report specific to Generic HID Class received from the host device. The Usage Report informs the Host about the features or functionality of the USB device. The Data Output Report is used to receive data from the host while Data Input report is used to send data to the host.
From Where GenericHID.C gets the USAGE and Data Reports Descriptors
In the LUFA framework’s demo project for GenericHID, GenericHID.h is imported. The GenericHID.h imports descriptor.c file which defines the relevant usage and data reports descriptors for the host device. The descriptor.c defines a GenericReport structure to generate generic HID usage and data reports descriptors. Inside descriptor.c the GenericReport structure has the values returned by HID_DESCRIPTOR_VENDOR () function. The HID_DESCRIPTOR_VENDOR () is defined in HIDClassCommon.h (located in LUFA-Source-Folder\LUFA\Drivers\USB\Class\Common folder).
The GenericHID.c imports GenericHID.h which imports usb.h. USB.h imports HIDCLass.h. In HIDClass.h is imported HIDClassDevice.h if the USB_CAN_BE_DEVICE is true for the controller chip to being a USB device not the host. The HIDClassDevice.h imports HIDClassCommon.h where the HID device specific descriptor fields have been defined.
Read Moore: Atmega 32u4 Based USB Controlled Servo Motor