Phidget Servo motor output via Processing

I'm currently preparing for a short piece of processing tutoring in the Bachelor of Architecture course, commencing tomorrow.  The course is a 1st year construction subject, in which the year group is designing modular structures from found objects (read; whatever they can find in large amounts at Reverse Garbage), with a  12-15 unsuspecting students are going to be shown the slippery slope that is processing, all in the aim of augmenting small construction projects with responsive elements.

I've used the standard phidget servo motors with max/msp before, but I've decided to switch focus to processing so such solutions will no longer suffice.  There are some benefits to not using max/msp in the university context;

  • Runtime - i can't count the number of times we've built test prototypes and final projects only to see them fall over due to the small number of licences we've access to on campus.  The runtime solution is acceptible for last minute, last chance, last straw moments but it's just not good enough for day to day use or experimentation.
  • The extraordinary cost of licensing - the licences bought by the university come at a ridiculous cost, not to mention the involved installation process.  Processing by comparison is such a simple install - for both the core components and the additional libraries.
  • Extensibility - max/msp additional objects are always a welcome addition to the program, however the objects themselves tend to be closed off, limited in how much they reveal of their inner workings and can be fairly slow.

To this end I've enjoyed the processing learning curve, there's been more than enough learning resources available online and in book form, so I'm definitely pushing for its' inclusion in the syllabus in the arch. faculty.  Anthony Burke has been teaching processing in the master of architecture course this semester (with assistance from the computation whiz-kid Ben Coorey), so along with the arduino hardware the transition from proprietary to open-source projects is well under way in the DAB.

So to proceed with the real agenda of this post, I was searching for simple code to interface with the PhidgetServo motor output units we've been using, this time working in processing.  I couldn't find any decent examples online so had to cobble together one myself.

Read on for more;
...

[updated code]

import com.phidgets.*;
import com.phidgets.event.*;
 
// Declare Variables - a ServoPhidget object called 'sp', a Double called 'pos' and an integer called 'xPos'
ServoPhidget sp; // Declare the servophidget
double pos; // this is a type of number, which can store more decimal place values than the standard floating point value, check the referennce page
int xPos;
 
// Set size, video framerate, connect to and open phidget object
void setup()
{
frameRate(20);
size(400,300);
try
{
// Create & Assign the servophidget
sp = new ServoPhidget();
sp.openAny();
println("Waiting for Phidget");
sp.waitForAttachment();
// if you do not see this below in the console, your phidget is not connected
println("OK ready to go");
}
catch(Exception e)
{
println("ERROR");
System.out.println(e);
}
}
 
void draw()
{
background(200);
// map the mouse x coordinate to the available range of the servophidget
pos = map(mouseX,0,width,-22.9,232.0);
// read the data from the phidget
try
{
sp.setPosition(0,pos);
sp.getPositionMax(0);
// checking the max and min values for the servophidget
double helloMax = sp.getPositionMax(0);
double helloMin = sp.getPositionMin(0);
// print a line to the console to display the data as it arrives
println("min is: " + helloMin + ", max is: " + helloMax + ", current position is: " + pos);
}
catch(Exception e)
{
println("ERROR");
System.out.println(e);
}
 
// the double variable is not used in processing, but is needed to send to the servophidget.
// to display this data as a graph onscreen you need to convert it to a floating point number
 
float fPos = (float)pos;
fPos = map(fPos,-22.9,232.0,0,height);
stroke(0);
 
//  line(xPos,0,xPos,height-fPos); // Un-comment out this line to draw a vertical graph of the motor movement
stroke(50);
line(mouseX,0,mouseX,height);
 
// this controls the graph position onscreen, it animates the graph and refreshes to the 0 position when the graph goes offscreen
if (xPos >= width)
{
xPos = 0;
}
else {
xPos++;
}
}

note this requires the phidget21.jar core library file, which is included on the phidgets programming download page, just look for the Java examples download.

[Update] Download the phidget jar archive here.

When downloaded, copy+paste the above code into a new sketch, then select Sketch>Add File... to add the phidget21.jar file to your sketch.  After that, plug in the phidget servo + a motor and you're ready to go.

Related Posts

architecture com dab data float gallery google information interface j line pixeltag processing   39

Comments

3 Comments so far. Leave a comment below.
  1. Andrew,

    Hello,
    I am currently a physical computing student. I am trying to use your sample code as a base point for studying Processing and Phidgets. Due to lack of documentation on the subject I am asking for your help. Currently I am bouncing between 2 different machines, 1 is running linux the other Mac Os X Snow Leopard. With your code I can’t get very far on my Linux machine. On my Mac I am getting farther however my device is still not functional in processing. I was wondering if you could take a look at my code and let me know what is causing problems.

    The error I receive is unexpected token: void. That error falls upon the void draw line.

    import com.phidgets.*;
    import com.phidgets.event.*;

    // Declare Variables – a ServoPhidget object called ’sp’, a Double called ‘pos’ and an integer called ‘xPos’
    ServoPhidget sp; // Declare the servophidget
    double pos; // this is a type of number, which can store more decimal place values than the standard floating point value, check the referennce page
    int xPos;

    // Set size, video framerate, connect to and open phidget object
    void setup()
    {
    frameRate(20);
    size(400,300);
    try
    {
    // Create & Assign the servophidget
    sp = new ServoPhidget();
    sp.openAny();
    println(”Waiting for Phidget”);
    sp.waitForAttachment();
    // if you do not see this below in the console, your phidget is not connected
    println(”OK ready to go”);
    }
    catch(Exception e)
    {
    println(”ERROR”);
    System.out.println(e);
    }

    void draw(){
    background(200);
    // map the mouse x coordinate to the available range of the servophidget
    pos = map(mouseX,0,width,-22.9,232.0);
    // read the data from the phidget
    try
    }

    {
    sp.setPosition(0,pos);
    sp.getPositionMax(0);
    // checking the max and min values for the servophidget
    double helloMax = sp.getPositionMax(0);
    double helloMin = sp.getPositionMin(0);
    // print a line to the console to display the data as it arrives
    println(”min is: ” + helloMin + “, max is: ” + helloMax + “, current position is: ” + pos);
    }
    catch(Exception e)
    {
    println(”ERROR”);
    System.out.println(e);
    }

    // the double variable is not used in processing, but is needed to send to the servophidget.
    // to display this data as a graph onscreen you need to convert it to a floating point number

    float fPos = (float)pos;
    fPos = map(fPos,-22.9,232.0,0,height);
    stroke(0);

    // line(xPos,0,xPos,height-fPos); // Un-comment out this line to draw a vertical graph of the motor movement
    stroke(50);
    line(mouseX,0,mouseX,height);

    // this controls the graph position onscreen, it animates the graph and refreshes to the 0 position when the graph goes offscreen
    if (xPos >= width)
    {
    xPos = 0;
    }
    else {
    xPos++;
    }
    }

    Thanks,
    Andrew

  2. Hi Andrew,

    I agree the phidget documentation is sorely lacking, if you have the chance to look into arduino hardware instead I would recommend it – the supporting community and documentation well and truly make up for the learning curve. Phidgets aren’t impossible to get working in processing, the java library does exist and it should help point you in the right direction, just go to the java section of the phidgets website.

    I had a look at your code, it seems like the copy+paste between processing and wordpress isn’t the cleanest method – some commented lines get wrapped and smart quotes are another problem. I cleaned that up a little bit, it was just a simple matter of checking each of the open and close braces { and }. Give this a try (and don’t forget to download the phidget21.jar file or nothing will happen.

    I hope this helps, drop me a line if you have any more hassles. Good luck!

    import com.phidgets.*;
    import com.phidgets.event.*;

    // Declare Variables – a ServoPhidget object called ’sp’, a Double called ‘pos’ and an integer called ‘xPos’
    ServoPhidget sp; // Declare the servophidget
    double pos;
    int xPos;

    // Set size, video framerate, connect to and open phidget object
    void setup()
    {
    frameRate(20);
    size(400,300);
    try {
    // Create & Assign the servophidget
    sp = new ServoPhidget();
    sp.openAny();
    println(”Waiting for Phidget”);
    sp.waitForAttachment();
    // if you do not see this below in the console, your phidget is not connected
    println(”OK ready to go”);
    }
    catch(Exception e)
    {
    println(”ERROR”);
    System.out.println(e);
    }
    }

    void draw(){
    background(200);
    // map the mouse x coordinate to the available range of the servophidget
    pos = map(mouseX,0,width,-22.9,232.0);
    // read the data from the phidget
    try {
    sp.setPosition(0,pos);
    sp.getPositionMax(0);
    // checking the max and min values for the servophidget
    double helloMax = sp.getPositionMax(0);
    double helloMin = sp.getPositionMin(0);
    // print a line to the console to display the data as it arrives
    println(”min is: ” + helloMin + “, max is: ” + helloMax + “, current position is: ” + pos);
    }
    catch(Exception e) {
    println(”ERROR”);
    System.out.println(e);
    }
    // the double variable is not used in processing, but is needed to send to the servophidget.
    // to display this data as a graph onscreen you need to convert it to a floating point number

    float fPos = (float)pos;
    fPos = map(fPos,-22.9,232.0,0,height);
    stroke(0);

    // line(xPos,0,xPos,height-fPos); // Un-comment out this line to draw a vertical graph of the motor movement
    stroke(50);
    line(mouseX,0,mouseX,height);

    // this controls the graph position onscreen, it animates the graph and refreshes to the 0 position when the graph goes offscreen
    if (xPos >= width)
    {
    xPos = 0;
    }
    else {
    xPos++;
    }
    }

Add Your Comments

Required
Required
Tips

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <ol> <ul> <li> <strong>

Your email is never published nor shared.

Ready?