Sunday, January 2, 2011

Using Phidgets on a Chumby/Infocast 8


I had been kicking around the idea of a system that retrieves the status of various devices and displays that state on on a set of lamps.  I wanted some type of embedded system that already had network support and a standard tool chain. I also needed some simple way to interface with my indicator lights.
Chumby and it's Infocast derivatives are really slick customizable internet aware devices with built in touch screens, a linux kernel, USB ports and available GPIO pins.  Chumby also provides a development toolchain, documentation and a nice user forum. Best Buy had these on sale during the 2010 Christmas rush for less than almost any DIY system could be built.
Phidgets are USB connected peripherals that let you interface to sensors, I/O, servos and other real world devices without having to write micro-controller code.  This is a picture of my 8" Infocast and Phidget 0/16/16 board.



The Chumby comes with Linux Phidget kernel divers. The Phidget folks recommend using their user space drivers available on their web site instead of the kernel drivers. Binary versions are not a vailable which means we have to build the phidget user space drivers
We have to compile the phidgets library libphidget21 which requires libhid and libusb. They have to be built in the order libusb, libhid and then libphidget21. The chumby comes with some libhid and libusb code but not all of the header files required to buld libphidget21. The best thing to do is to build and install libusb , libhid and then libphidget21.

Device Preparation
  1. Enable sshd through the control panel
  2. SSH into the box using a terminal window under linux or putty under Windows
  3. You can enable sshd across reboots by creating /psp/startsshd  with the command touch /psp/start_sshd
  4. Optional: This isn't required and has nothing to do with this project but I'm kind of detail oriented. Set the hardware clock from the software clock. Enter hwclock -s
  5. Run make to install the tool chain if it isn't already installed. The system will ask you if you want it to try and install make. Enter “Y” and hit return at the prompt. This installs all the compiler tools
Directory Preparation
The kernel and include search paths on the Chumby include /mnt/storage directories that correspond to the usual /usr. So there is a /mnt/storage/local/bin in the same way that a linux server /has /usr/local/bin. /mnt/storage directories are writeable on the Chumby while /usr directories are read/only.
  1. Make the root director for locally compiled software and libraries mkdir /mnt/storage/local
  2. I normally make a temp directory /mnt/storage/software on the chumby for all of my downloading and compilation.  I then cd into that directory for all my downloads and unpacking.
Compiling and Installing libusb
 The system comes with the libusb runtime but none of the compile time headers. We have to download, compile and install the libusb libraries and headers.
  1. download libusb 0.1.12  from http://files.chumby.com/source/ . The direct link for this file is http://files.chumby.com/source/falconwing/build2370/libusb-0.1.12.tar.gz You can download the files directly to the Chumby/Infocast using the wget command in the terminal/ssh window.
  2. Uncompress the .tar.gz file with gunzip.  Un-tar the .tar file with tar -xvf
  3. cd into libusb-0.1.12 created by the unpacking operation.
  4. Run the configure script telling it where we intend to install the library and header files. Run the command ./configure -prefix=/mnt/storage/local
  5. Compiling libusb at this point will fail because the chumby comes wih a misconfigured libstdc++.la file. We need to rename (or move) /usr/lib/libstdc++.la to some other name like /usr/lib/libstdc++.la.badpaths. Just use the mv command mv /usr/lib/libstdc++.la /usr/lib/libstdc++.la.badpaths
  6. Run make to build the library
  7. Run make install to install the library and headers.

Compiling and installing libhid
libphidget21 is dependent on libhid 0.2.16 libraries and header files. There isn't any libhid source jar on the chumby download site so you'll have to grab it from the main libhid download site.

  1. Download the source tar ball from a libhid site. I used links available off of http://libhid.alioth.debian.org/ The direct download link (at that time) was http://alioth.debian.org/frs/download.php/1958/libhid-0.2.16.tar.gz. You can use the wget command to directly download to the chumby. From the command prompt run wget <url_for_package>
  2. Unzip the .gz file with gunzip. Untar the resulting .tar file with tar (tar xvf <filename_.tar>).
  3. Change directory into libhid-0.2.16
  4. Run the configure command and tell it about the Chumby “special places”  ./configure -prefix=/mnt/storage/local
  5. run the make command to build the project
  6. run make install to install the library and headers in /mnt/storage/local
Compiling and Installing libphidget21
Now we can finally build and install the phidget library.
  1. Download the linux phidgets drivers from the downloads page at http://www.phidgets.com/drivers.php to some place on /mnt/storage. I usually create a /mnt/storage/software directory for this type of work. The actual download file is http://www.phidgets.com/downloads/libraries/libphidget_2.1.7.20101222.tar.gz . You can use the wget command on the chumby/infocast to download them directly to the device.
  2. Unpack the archive with gunzp.
  3. Unpack the tar archive with tar -xvf <name_of_tar_file>
  4. cd into the unpacked libphidget21 directory
  5. The phidget compile process doesn't know about those special Chumby include or library directories so we'll tell it about them when we run the configure command. Run ./configure --prefix=/mnt/storage/local  CFLAGS=-pthread CPPFLAGS=-I/mnt/storage/local/include
  6. Build the library.  Run make. You may have to run it a couple times if it dies with random out of memory errors. A restart on my machine eliminated the problem. You could also try turning off the control panel and other processes before running make. I just ignored it and kept running make on one of my machines.
  7. Install the library and headers by running make install  libphidget21 will install the tools into /mnt/storage/local
Enabling the User Mode Device Access
You now have to enable user mode access to phidget devices. You should look on the Internet for a more thorough explanation of this. The udev rules file in the Read/Only filesystem is sym-linked to a copy in /mnt/storage/psp. This means changes made to /mnt/storage/psp/udev/chumby.rules actually show up in /etc/udev/rules/50-udev.rules.
  1. Add the following line to /mnt/storage/psp/udev/chumby.rules on silvermoon devices or /psp/udev/chumby.rules on falconwing devices
    SUBSYSTEM=="usb_device", SYSFS{idVendor}=="06c2", MODE="0666"
    Verify
    Verify that all this works using the example programs built in the libphidget21/examples directory
    1. Plug in the phidget device (in my case a 0/16/16) and run lsusb. Verify there is a line containing “ID 06c2:xxxx”. The 06c2 is the Phidgets vendor id.
    2. cd into the examples directory located in the phidgets source tree where you build the libraries.
    3. Build the examples by running make
    4. Run ./manager. It should log out the connected phidget device. You should be able to unplug and plug in phidget boards and see it log each insertion and removal. Press ctrl-c to kill the program.
    5. The example ifkit.c has a bug in it so I made some minor changes. Folks with one of the interface boards can: make the following changes to ifkit.c, rebuild and run the program.  The Phidget indicator lights should flash as the program cycles through the channels.
    Change
    while(1)
    {
    CPhidgetInterfaceKit_setOutputState(IFK, 7, 1);
    CPhidgetInterfaceKit_setOutputState(IFK, 7, 0);
    }
    to
    int increment = 1;
    int i = 0;
    while(i>=0)
    {
       
    CPhidgetInterfaceKit_setOutputState(IFK, i, 1);
       
    sleep(1);
       
    CPhidgetInterfaceKit_setOutputState(IFK, i, 0);
       
    sleep(1);
        i+=increment;
       
    if (i == 7) increment = -1;
    }

    Additional Work
    The Phidgets folks also have custom web service lets you communicate with devices on remote machines using the same libphidgets21 libraries.  A source package is available on the Phidgets web site.  The following configure command worked for me.

    ./configure --prefix=/mnt/storage/local LIBS=/mnt/storage/local/lib/libphidget21.so CFLAGS=-pthread CPPFLAGS=-I/mnt/storage/local/include
    I verified it worked from a Windows machine but didn't go any farther than that. I instead decided to talk add cgi scripts to my Chumby web server that call Phidget command line programs.  That makes it easy for anyone to talk to or read data from my Phidget devices.

    Controlling with Java Programs on the Chumby
    Oracle has a version of the standard J2SE runtime available for ARM v5 processors.  It it is a full function JRE that does not come with development tools.  You can actually run the Phidgets Java example programs on the Chumby using this VM. 
    1. Download the VM from Oracle (ejre1.6.0_21 at the time of this writing).  Copy to the Chumby and unpack the .tar.gz file.  I put mine into /mnt/storage so that I ended up with java on the path /mnt/storage/ejre1.6.0_21.
    2. Download a version of Phidget21.jar to your chumby and copy to somewhere on the LD_LIBRARY_PATH. I put it in /mnt/storage/local/lib, the same place I put the phidgets shared library.
    3. Compile a sample phidget program on your PC to test it with your interface board.  I have an Interface kit so I played with OpenIFKitExample.
    4. Copy the .class file to the chumby.
    5. Set your JAVA_HOME and PATH in the shell.  You could set it in a .bashrc or something but for testing it is simpler to just enter

      export JAVA_HOME=/mnt/storage/ejre1.6.0_21
      export PATH=$JAVA_HOME/bin:$PATH

    6. Run the program adding the current directory (where the .class file is) and the Phidget21.jar to the classpath.  Here is an example that runs OpenIFKitExample which is a Java program that talks JNI to the libphidget21 which talks to the device.

      java -classpath .:Phidget21.jar OpenIFKitExample


    Final Notes
    Phidgets and Chumby are a great combination for folks wanting a package that lets them integrate a network, physical devices and user interfaces.

    1 comment:

    1. It would be a great service if you were to add this to the Chumby Wiki.

      http://wiki.chumby.com/mediawiki/index.php/Main_Page

      ReplyDelete