Internet of Things with Intel Edison Servo motors with Pololu Controller Pierre Collet www.Intel-Software-Academic-Program.com paul.guermonprez@intel.com Intel Software 2014-10-14 Objectives Objective Servo motors are controlled with a signal called PWM. To interact correctly with a PWM device, you need to generate a strict signal. Obvious for a basic microcontroller, not so much for a general purpose processor with a regular linux. Two solutions : o Use a Real Time OS. On Edison the second processor is running a Real Time OS so we can do it on Edison itself. o Use an external PWM card, connected with USB. A good example : Pololu Maestro card. Today, we’ll use the Pololu Maestro with 6 PWMs. Setup LibUSB LibUSB We are connected to the Pololu card with USB, using USB messages. We need libusb-1.0-dev and we’ll use a package for Yocto, the Linux by default on Edison. To install : echo "src/gz all http://repo.opkg.net/edison/repo/all" > /etc/opkg/base-feeds.conf echo "src/gz edison http://repo.opkg.net/edison/repo/edison" >> /etc/opkg/base-feeds.conf echo "src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32" >> /etc/opkg/base-feeds.conf echo "src mraa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/mraa-upm.conf opkg update opkg install libusb-1.0-dev Using the Master USB port Master/Client When you plug Edison to your PC with a USB cable, you see Edison as a serial-over-USB device and a mass storage device. That’s the client mode of USB. Switch But Edison is also a PC, acting as master. To use the full size USB port on the board as a master, operate the switch. Remember to switch it back after this lab. Hardware Connecting hardware Plug and boot your board. Connect the Pololu card to Edison on USB host connector using a standard mini USB Cable. Connect a servo motor on Pololu channel #1. In the next slide we’ll see how to power the Pololu from a USB port. Hardware – Power from USB Power ? The Pololu has 2 power requirements : o Very minimal for the microcontroller. This power comes from the USB cable. o High for the servos. We need to get this power from an external source and connect it to the 2 power pins of the Pololu. Two methods Usually, we would get a 6V battery with the right connector. If you have one, use it. If you don’t, it’s possible to : o cut a USB cable (it’s not reversible, obviously), separate wires o get the 2 power wires (red and black) o connect manually the 2 wires to the Pololu power pins Hardware Regular USB cable DATA Modified USB cable POWER ONLY Vin #0 #5 Script Shell script to control a servo Binaries We provide you 3 binaries to control 3 parameters : o The targeted position of the servo motor o The speed of the servo motor (0 is max speed) o The acceleration of the servo motor (0 is max acceleration) For each binary, the first argument is the channel number and the second parameter is the value. How to run this files # ./set_accel 0 # ./set_speed 0 # ./set_target 0 0 0 2000 The order is automatically sent to the Pololu card via USB. These binaries are C++ program using libusb-1.0. Shell script to control a servo A simple bash script to control a servo motor #! /bin/bash while : do ./set_target 0 1000 sleep 1s ./set_target 0 2000 sleep 1s Done # infinite loop # channel 0 targeted position is set to 1000 # we wait a second It’s just moving the servo from 1000 to 2000 and back, with 1s pauses. C Code 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; Conclusion Conclusion Edison being a PC, you can expect to find a lot of great USB solutions to all your problems. Same thing for webcams. For PWM, you could do without the Pololu Maestro, both are good solutions. Idea : Integrate Edison and Android ? An IoT project which need a remote ? Use your Android smartphone to control Edison ! We’ve seen how to create a web form with NodeJS, you can also create an Android app with a native look and feel. 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/