Intel Do-It-Yourself Challenge Lab 6: USB and servo motors Nicolas Vailliet www.Intel-Software-Academic-Program.com paul.guermonprez@intel.com Intel Software 2014-02-01 Prerequisites and objectives We assume that: - You are able to download an archive, to unzipped it and run a program under the OS you’re familiar to. - You are able to read pieces of code and comments written in C-like language. Our objective is: - Given a Galileo board and a laptop connected on the same network, a Pololu Micro Maestro 6 USB Servo controller, and a servo motor, you will be able to control the motor from Galileo. You will use C++ binaries. In general, Our goal is to show you more Galileo possibilities and to develop your imagination on IoT world. Before you start What do you need? Desktop OS We assume you’re under Microsoft Windows. Linux and Mac users should not have any problem to do the same. Hardware An Intel Galileo Development Board. The box comes with power supply and cable. A Wi-Fi adaptor (N-2200 Mini-PCIe) A micro SD card (8GB with the full Linux image we provide) A Pololu Micro Maestro 6 USB controller - A USB cable Type A – Type B - A servo motor - A USB cable to power up the motor Software A SSH client like Putty for Windows. Mac and Linux users only need a terminal. http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html Binaries we provide: « set_acceleration », « set_target », « set_speed » Controlling servo motors Preparing hardware Read before doing anything! It could damaged Galileo and laptop! Connecting hardware - Plug and boot your board. - Connect the Pololu card to Galileo on USB host connector. - Connect a servo motor on Pololu. - Connect the USB power wire to Pololu - And plug it to your laptop. Important - See next slide to know how to connect the USB power wire to the Pololu. - Connect the servo signal connector (yellow-ish wire) to the pin closest to the USB port of the Pololu card. Preparing hardware How to connect the hardware? Vin #0 #5 Bash script to control a servo motor We provide you three binaries to control: - The targeted position of the servo motor - The speed of the servo motor (0 is max speed) - The acceleration of the servo motor (0 is max acceleration) How to run this files # ./set_target channel value # ./set_accel 0 0 # ./set_speed 0 0 # ./set_target 0 8000 The order is automatically sent to the Pololu card via USB. These binaries are C++ program using libusb-1.0. Bash script to control a servo motor A simple bash script to control a servo motor #! /bin/bash while : # infinite loop do ./set_target 0 4000 # channel 0 targeted position is set to 4000 sleep 1s # we wait a second ./set_target 0 8000 sleep 1s done You can now use these three binaries to write your own script/program to control servo motors! Optionally: look at the C++ source code on next slides! Set_target source code A C++ main function using libusb #include <iostream> #include <string> #include <libusb-1.0/libusb.h> #include <unistd.h> #include <stdlib.h> using namespace std; int main(int argc, char *argv[]){ if(argc < 3) cout << "Usage: binary servo_number target_value" << endl; int servo = atoi(argv[1]); int value = atoi(argv[2]); Set_target source code Creating libusb structures libusb_device **devs; libusb_context *ctx = NULL; libusb_device_descriptor descr; libusb_device *pololu; string idPololu ("137"); int r; int found = 0; ssize_t cnt, i; Initializing libusb r = libusb_init(&ctx); libusb_set_debug(ctx, 3); Seeking devices cnt = libusb_get_device_list(ctx, &devs); Set_target source code For each device found for(i = 0; i < cnt; i++) { r = libusb_get_device_descriptor(devs[i],&descr); //pololu id is 137, so let’s find it if(descr.idProduct == 137) { pololu = (devs[i]); found = 1; break; Stop looking for devices and don’t unreference the pololu } } libusb_unref_device(devs[i]); Set_target source code Sending target command if(found) { libusb_device_handle **handle; r = libusb_open(pololu,handle); r = libusb_detach_kernel_driver(*handle,0); if(r < 0 && r != LIBUSB_ERROR_NOT_FOUND) { cout<<"Error: detach kernel driver failed"<<endl; return 1; } r = libusb_claim_interface(*handle,0); r = libusb_control_transfer( *handle,0x40,//request type 0x85, //request value, //value servo, //servo number NULL,0,5000); Set_target source code Closing USB connection } } r = libusb_release_interface(*handle,0); libusb_close(*handle); libusb_free_device_list(devs, 1); libusb_exit(ctx); return 0; What’s next? Optionally, integrating Galileo with Android? An IoT project which need a remote? Use your Android smartphone to control Galileo! After that, your mission will be… …to implement your own idea! …to realize the project we suggest! License Creative Commons – By 3.0 You are free: • to Share — to copy, distribute and transmit the work • to Remix — to adapt the work • to make commercial use of the work Under the following conditions: • Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). With the understanding that: • Waiver — Any of the above conditions can be waived if you get permission from the copyright holder. • Public Domain — Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license. • Other Rights — In no way are any of the following rights affected by the license: – – – • Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations; The author's moral rights; Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights. Notice — For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. http://creativecommons.org/licenses/by/3.0/