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;…
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 servophidgetdouble pos;
// this is a type of number, which can store more decimal
// place values than the standard floating point value,
// check the referennce pageint 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 the line above to draw a vertical graph of the motor
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.
Pingback: Phidgets with Processing | IDHO 2011