Home / Community
Home / Community

Coding hardware with Ruby + Phidgets

D2d66b4e429c04b16e74d65f44c92815by Fernando Castellanos|

Jan 22, 2013


Ever wondered if you can take Ruby to the next level? Maybe program some hardware with it? Well, me too! And Phidgets is a great tool for accomplishing this goal.

Phidgets (Physical Widgets), as the website stands are '... a set of "plug and play" building blocks for low cost USB sensing and control from your PC.' This means that we have a wide variety of sensors and controllers for us to choose to play with. Phidgets works with C/C++, C#, Cocoa, Delphi, Flash AS3, Flex AS3, Java, LabVIEW, MATLAB, Max/MSP, MRS, Python, REALBasic, Visual Basic.NET, Visual Basic 6.0, Visual Basic for Applications, Visual Basic Script, and Visual C/C++/Borland.NET... and now Ruby!

In order to start working with Phidgets + Ruby + OSX there's this really good wiki, but if you want to start right away then I suggest to begin by installing the OSX driver and the phidgets-ffi ruby gem.

After doing that, we can go to 'System Preferences' and under the 'Other' group we will find the Phidgets pane.

system preferences

Enter the Phidgets pane and see the Phidgets devices connected to the computer. In my case I have a RFID reader and the Phidget InterfaceKit 8/8/8, with 8 digital inputs, 8 digital outputs and 8 analog inputs with configurable data acquisition rates on the board.

phidgets pane

One of the coolest thigs about this pane is that if you double click on the interface kit you get a window where you can test it, see its information and read its inputs, here in the image you can see that I have two analogue sensors, a rotation sensor and a slider sensor (both potentiometers) inputs 0 and 5. If you move those sensors you can see the numbers change in real time.

interfacekit pane

Now that we can confirm that everything is working as it should, let's get down to writing some code.

I'm going to show you how to code a small example of the PhidgetRFID reader. The first thing that you need to do is install the Phidgets gem.

gem install phidgets-ffi

As the documentation of the gem states, phidgets-ffi is dependent on the ffi(>=1.0.9) gem. So, if the above command doesn't automatically install the gem for you, then enter:

gem install ffi

The example code is too small so I'm going to paste it in full and then explain:

Line 2 will include the libraries included in the Phidgets gem, Line 6 is interesting because we are creating a Phidgets RFID object which we can configure in Lines 12 and 13 in the on_attach hook from the RFID object we just created. Line 12 is critical because we're activating the antenna and without it we wouldn't be able to read tags, Line 13 is simply to turn on the LED in the reader. We need to have the sleep call in that configuration in order to give time for the reader to connect.

Events on Line 17 and Line 21, 'on_tag' and 'on_tag_lost' get called when we add or remove a RFID tag in the reader. And that's pretty much it!

Much of the coding for Phidgets follows the line of configuring the board and hooking up to events. Those events are called when the sensors activate by either a change of the brightness of the light, temperature variations, the flip of a switch, touching something, etcetera. And it's then when we can do stuff with our code, either logging those events or taking action using actuators, including servos, DC and stepper motors.

If you want to know more, you can find more examples of how to program Phidgets with Ruby by looking at the Phidget's gem source code, have fun!

blog comments powered byDisqus