Tutorial: Interface Kit 8/8/8 and EZ1 sensors using Linux

  1. rbhutani rbhutani is offline Relay
    How To, DIY

    Interface Kit 8/8/8 and EZ1 sensors using Linux

    Starting with the Interface kit 8/8/8

    Interface kit 8/8/8

    This interface kit makes it possible to avoid all the complications involved with using the serial port communication through microcontrollers for controlling the sensors. In this section we will be talking mostly about the sonar sensors which we have used with phidgets. Starting with the kit

    Firstly to start off as a normal windows user- With the manual available for the kit on the www.phidgets.com , the phidget is connected to the PC and the interface available for the windows on the same site was used as a test program. It has 8 analog inputs, and 8 digital input and output ports.
    The analog ports were tested using a 5V adapter as an analog sensor and it was noted that the interface detected the sensor successfully (By showing a value of 1000 corresponding to voltage value 5V in the box on the interface). Also the digital output ports were checked using the LED’s. The exact procedure for these simple tests is again mentioned in the manual.
    After this the next step was to get this phidget running through a program in C or C++. Although documentation is available for the programming in the downloads section of the site but it is not in a very detailed form. Looking at the examples available on the site could be a better option for that.
    For compiling the C programs, a header (phidget21.h) and a library corresponding to that is also needed.
    So in all to start with the things you need to download the following from the site www.phidgets.com

    • A product manual for the kit
    • Interface for your operating system
    • Phidget21.h and libphidget.so and libphidget.a libraries. With the Linux download they are included in it.
    • An examples folder for the programming language

    Unfortunately we had everything for the windows but when it was tried to run the ‘Interfacekit-simple’ program from the folder using a TURBO C++ compiler, it did not work. Even when the phidget21.h file was in the include directory and the working folder, the same error message was flashed every time- “unable to include phidget21.h”. All the possible combinations mentioned on sites and forums, like removing “.h” and all other stuff were tried but there was no success. The reason for that is still not known. May be it was the problem with the compiler.

    InterfaceKit with Linux

    With all the downloads for the Linux, the kit was connected to the PC.
    Steps to be followed :

    • Download and extract the Linux Source folder available on the site

    • Open a new terminal and go to the extracted folder path

     cd ....................../Phidgetlinux/phidget21
    • Run the makefile by running "make" in the same directory
    • Then run- "sudo make install"
    • This will itself copy the phidget21.h and phidget libraries to your system.
    • Now next thing to do is to make all phidget devices writable via USB for Linux as in Ubuntu these settings are not by default as in windows.
    • For this just copy the 51-phidgets.rules file from the udev folder to /etc/udev/rules.d Directory of your system.

    sudo cp ......./Phidgetlinux/phidget21/udev/51-phidgets.rules /etc/udev/rules.d
    • Now there is no direct interface software available for the Linux and the phidget can be accessed only via a code written in C\C++ or some other programming language
    • First thing to do is to check if the interface kit is being detected or not
    • The "InterfaceKit-simple.c" code available in the Downloaded Examples folder can be used for that.
    • Usually the Linux system has gcc and g++ installed in the system. If not use :-

    sudo aptitude install gcc
    • Now from the terminal window go to the examples folder. It is named as "standardC".
    • Run :- make
    • Now for sure all the executables for all the C files would be created if they were not earlier.
    • Now run the "Interfacekit-simple" executable by writing in the terminal window (make sure the kit is connected, for that the LCD on the back will light up)

     sudo ./InterfaceKit-simple
    Note- Some people might say that running it as 'sudo' might not be needed. But for me it did not work. Without running it as 'sudo' my PC never detected the InterfaceKit. It just gave a timeout error.
    • Now if everything works fine, you will get an output on your terminal window showing that the Interface kit has been attaced, displaying all its properties and the currrent state.
    • You can also run Manager-simple program from the folder. It will just display the the list of devices attached but do not forget to run it as sudo if it does not work on your system.

    Understanding and modifying the code

    The next best thing to do would be to understand how this 'Interfacekit-simple' program works so that it can be modified later to run the sensors. For me, as a starting, rather than writing the whole program by yourself it is better to use this existing program and to modify it STEP-BY-STEP in order to add elements to its functionality. It is the easiest and most reliable way to start as you start with a working program.
    Understanding the working of the code takes some time but once it is done, each step becomes faster than the previous as with each step your understanding improves.

    To start with it, I would suggest to read the C API manual available on the site although it is not very detailed one, but it increases the understanding to an extent. I dont think so it is needed to understand each subroutine or function in the program in great depth. There should be just a picture or an algorithm of the program in your mind while you are working on it.

    And yes for now onwards I would be using C++ instead of C. There is not much difference, but I am just used to working on C++.

    For compiling a C++ Program please note the command :
    g++ anyfile.cpp -o outfilename -lphidget21
    This "-lphidget21" is important as without this the g++ compiler does not recognize the phidgets. We have to explicitly provide the compiler the phidget libraries by writing '-lphidget21'.

    Modifications to control digital output

    With just a statement inserted in the InterfaceKit-simple example we can easily control the digital output port of the phidget.
    The statement

    where index=0 to 7 ,state=0 or 1 (OFF or ON)
    will change the state of the Digital output port of the interest.

    So with the following statements insterted in the interfacekit_simple() function after the call to the display_properties() function and with an LED connected to the digital port zero, we can switch on an LED for say 5 seconds.

    • Open the folder containing the Interface-kit simple file.
    • Open the file using Gedit or any other text editor.
    • Insert the folowing code as mentioned above

    CPhidgetInterfaceKit_setOutputState (ifKit,0,1);
    CphidgetInterfaceKit_setOutputState (ifKit,0,0);
    • After inserting this piece of code save the file and open a terminal window
    • Go to the directory standardC

    cd /home/username/Desktop/standardC
    cd /home/username/Desktop/standardC
    • Run
    • This will compile the changes you made to the code. You should not get any error message with these simple changes. If you get some check for the uppercase and lowercase letters carefully in the inserted code.
    • Run

    sudo ./InterfaceKit-simple
    • You should see the LED switchin on and then switchin off after 5 seconds.

    This function used "CPhidgetInterfaceKit_setOutputState(..)"is again an inbuilt function provided by the phidget library.
    By this you must be having an idea of how we can switch off and on EZ1 sensors via digital output ports if we are able to connect the sensors to these ports.

    Toggling the sensor from C code

    A sensor can be controlled in a similar way a LED is controlled from a digital output port of the 8/8/8/ Interfacekit. Connect the sensor as explained in the manual available on site to connect these sensors and include the same piece of code in the interfaceKit-simple example :

    CPhidgetInterfaceKit_setOutputState (ifKit,0,1);
    CphidgetInterfaceKit_setOutputState (ifKit,0,0);
    This 5 seconds time will include time to switch on the sensor from the port, time it takes to callibrate itself and for the remaining time the sensor will fire the ultrasonic pulses at 20Hz frequency.

    You will see scrolling sensor values on your terminal window. If you want you can record these sensor values from the sensor change handler function.
    Example output :

    Sensor: 0 > Value: 11
    Sensor: 0 > Value: 14
    Sensor: 0 > Value: 33
    Sensor: 0 > Value: 50
    Sensor: 0 > Value: 42
    In case of worst problems
    In case of any problems with the phidgets using any example program (specially the unchanged examples)and you cannot find any solution to the problem, the best thing to do would be create a log file by inserting :
    Insert this statement after the "CPhidgetInterfaceKit_create(&ifKit);" statement in the 'interfacekit_simple()' function.

    Send this logfile to the administrator of the www.Phidgets.com along with description of the problem or post it on the TRC forum on the site. You will get all the possible help with the product.

    Rohit Bhutani

Replies to Tutorial: Interface Kit 8/8/8 and EZ1 sensors using Linux
  1. Join Date
    Sep 2006
    Carol Stream, Illinois

    Re: Interface Kit 8/8/8 for beginners and EZ1 sensors

    Excellent tutorial Rohit! Great job at breaking everything down. Although, I'm not too sure if this is for beginners

    �In the long history of humankind (and animal kind, too) those who learned to collaborate and improvise most effectively have prevailed�
    - Charles Darwin

Closed Tutorial