User Tools

Site Tools


courses:intro.prototyping.spring.2012.apr10

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
courses:intro.prototyping.spring.2012.apr10 [2012/04/04 07:07]
130.239.234.49
courses:intro.prototyping.spring.2012.apr10 [2012/04/04 07:52] (current)
130.239.234.49
Line 1: Line 1:
 +=== Phidgets with Processing ===
 +
 +
 +* [[Intro.Prototyping.spring.2012.Apr10.Daniel_example|Daniel'​s early example]]
 +
 +There are a couple of solutions out there to connecting Phidgets to processing. I have stripped down three examples to the bare necessities,​ which I will explain. After that it's up to you to expand on that using what you know of Processing but triggered by Phidget hardware.
 +
 +Use images, sounds, text, video. Search the processing website and internet for inspiration!
 +
 +The source for the examples are from [[http://​pages.cpsc.ucalgary.ca/​~boyd/​pmwiki/​pmwiki.php?​n=Teaching.ProcessingPhidgets|here]],​ [[http://​www.jasonmcdermott.net/​2009/​08/​phidget-interface-processing/​|here]] and [[http://​thegeekmovement.com/​blog/?​p=42|here]].
 +
 +==A good preparation for this that I copied from the first source==
 +(1).Install phidget drivers for your computer from http://​www.phidgets.com/​ - drivers tab.
 +(You probably did this last time)
 +
 +2.Make a folder to keep your phidget-related files in. This will contain files that you will re-use for phidget projects, not your Processing sketches.
 +
 +3.Download phidget21.jar from http://​www.phidgets.com/​ - programming tab. Put it in your phidget folder.
 +
 +4.Download the Processing code for the phidgets you plan to use from [[http://​pages.cpsc.ucalgary.ca/​~boyd/​pmwiki/​pmwiki.php?​n=Teaching.ProcessingPhidgets|this]] page. Put them in your phidget folder.
 +
 +5.Create a new Processing sketch. Save the sketch, giving it a suitable name. 
 +
 +6.From the Sketch menu in Processing, select Add File ... From the file selection dialogue, select phidget21.jar from your folder of phidget-related files. This will add a folder name code to your Processing sketch folder, and a copy phidget21.jar will appear there.
 +
 +7.From the Sketch menu in Processing, select Add File ... From the file selection dialogue, select the Processing code for your phidget, e.g., phidgetIK.pde. You will see a new file tab appear for your sketch with the Processing code, and the file will be copied to your sketch folder.
 +
 +
 +
 +
 +==Accelerometer example==
 +<code java>
 +phidgetAccelerometer accelerometer;​
 +float accelValue;
 +void setup()
 +{
 +  // set screen size
 +  size(400, 300);
 +  fill(0,​0,​255);​
 +  ​
 +  // get phidget IK started and wait up to 1s for attach
 +  accelerometer = new phidgetAccelerometer();​
 +  ​
 +  accelerometer.waitForAttachment( 1000 );
 +  ​
 +}
 +
 +void draw()
 +{
 +  background( 200 );
 +  ​
 +  // show acceleration. our accelerometer has two axes -> getAcceleration(0) and getAcceleration(1).
 +  //​accelerometer.getAccelerationMin( 0 ), accelerometer.getAccelerationMax( 0 )
 +    accelValue = accelerometer.getAcceleration(0);​
 +    float x = map(accelValue,​ -1.0,1.0 ,0, 400 );
 +    accelValue = accelerometer.getAcceleration(1);​
 +    float y =  map(accelValue,​ -1.0,0.8 ,0, 300 );
 +    println(accelValue);​
 +    ellipse( x, y, 10, 10 );
 +  ​
 +  if (keyPressed) {
 +    accelerometer.close();​
 +    exit();
 +  }
 +}
 +</​code>​
 +
 +
 +==Interface kit example==
 +<code java>
 +import com.phidgets.*;​
 +import com.phidgets.event.*;​
 +
 +InterfaceKitPhidget IK;// swap these labels for the actual sensors you have
 +float sensorVal;
 +
 + 
 +void setup() {
 +  size(400, 300);
 +  fill(255, 0, 0);
 +  setupIK();
 +}
 + 
 +void draw() {
 +  // the main interfaces for data input / output
 +  readIK();
 + 
 +  // clear the previous frame,
 +  // draw a new background of greyscale value 200
 +  background(200);​
 + 
 +  sensorVal = map (sensorVal, 0, 1000, 0, height); ​
 +  ellipse(width/​2,​ sensorVal, 10, 10);  ​
 +  println(sensorVal);​
 +}
 + 
 +void readIK() {
 +  try {
 +    // the ports are considered an array, with 8 ports in total
 +    // the first port is array number 1, but it is addressed as port 0
 +    // since it is 0 elements away from the start of the array.
 +    sensorVal = (float)IK.getSensorValue(0);//​
 +  }
 +  catch (Exception e) {
 +    println(e.toString());​
 +  }
 +
 + 
 +void setupIK() {
 +  try {
 +    IK = new InterfaceKitPhidget();​
 +    IK.openAny();​
 +    println("​Waiting for Phidget"​);​
 +    IK.waitForAttachment();​
 +    println("​OK ready to go");
 +  }
 +  catch(Exception e) {
 +    println("​ERROR"​);​
 +    System.out.println(e);​
 +  }
 +}
 + 
 +</​code>​
 +
 +
 +== RFID example ==
 +<code java>
 +import com.phidgets.*;​
 +import com.phidgets.event.*;​
 +
 +RFIDPhidget rfid = null;
 +
 +String tag = "";​
 +
 +void setup()
 +{
 +  size(550,​450);​
 +
 +  // setup Reader
 +  setupReader();​
 +
 +  // close Reader: Should add a button or some other trigger to call this method
 +  //​closeReader();​
 +  ​
 +}
 +
 +void draw()
 +{
 +  if(tag.equals("​1e00bcf721"​))
 +  {
 +    println("​Hi mom!"​);​
 +  }
 +}
 +
 +void setupReader() {
 +  try {
 +    rfid = new RFIDPhidget();​
 +
 +    rfid.addAttachListener(new AttachListener() {
 +      public void attached(AttachEvent ae)
 +      {
 +        try
 +        {
 +          ((RFIDPhidget)ae.getSource()).setAntennaOn(true);​
 +          ((RFIDPhidget)ae.getSource()).setLEDOn(true);​
 +        }
 +        catch (PhidgetException ex) {
 +        }
 +        println("​attachment of " + ae);
 +      }
 +    }
 +    );
 +
 +    rfid.addDetachListener(new DetachListener() {
 +      public void detached(DetachEvent ae) {
 +        System.out.println("​detachment of " + ae);
 +      }
 +    }
 +    );
 +
 +    rfid.addErrorListener(new ErrorListener() {
 +      public void error(ErrorEvent ee) {
 +        System.out.println("​error event for " + ee);
 +      }
 +    }
 +    );
 +
 +    rfid.addTagGainListener(new TagGainListener()
 +    {
 +      public void tagGained(TagGainEvent oe)
 +      {
 +        System.out.println(oe);​
 +        tag = oe.getValue();​
 +      }
 +    }
 +    );
 +
 +    rfid.addTagLossListener(new TagLossListener()
 +    {
 +      public void tagLost(TagLossEvent oe)
 +      {
 +        System.out.println(oe);​
 +        tag = "";​
 +      }
 +    }
 +    );
 +
 +    rfid.addOutputChangeListener(new OutputChangeListener()
 +    {
 +      public void outputChanged(OutputChangeEvent oe)
 +      {
 +        System.out.println(oe);​
 +      }
 +    }
 +    );
 +
 +    rfid.openAny();​
 +    println("​waiting for RFID attachment…"​);​
 +    rfid.waitForAttachment(1000);​
 +
 +    System.out.println("​Serial:"​ + rfid.getSerialNumber());​
 +    System.out.println("​Outputs:​ " + rfid.getOutputCount());​
 +
 +    System.out.println("​Outputting events."​);​
 +
 +  }
 +  catch (PhidgetException ex)
 +  {
 +    System.out.println(ex);​
 +  }
 +}
 +
 +void closeReader(){
 +  try {
 +    System.out.print("​closing…"​);​
 +    rfid.close();​
 +    rfid = null;
 +    System.out.println("​ ok");
 +    if (false) {
 +      System.out.println("​wait for finalization…"​);​
 +      System.gc();​
 +    }
 +  }
 +  catch (PhidgetException ex)
 +  {
 +    System.out.println(ex);​
 +  }
 +}
 +</​code>​
  
courses/intro.prototyping.spring.2012.apr10.txt · Last modified: 2012/04/04 07:52 by 130.239.234.49