Teaching / ProcessingPhidgets


Teaching.ProcessingPhidgets History

Hide minor edits - Show changes to markup

October 30, 2009, at 03:33 PM by 136.159.7.119 -
Changed lines 198-199 from:
int serial()return serial number of phidget if attached, 0 otherwise
int version()return phidget version number if attached, 0 otherwise
to:
int serial()return serial number of phidget if attached, -1 otherwise
int version()return phidget version number if attached, -1 otherwise
October 30, 2009, at 03:27 PM by 136.159.7.119 -
Changed line 62 from:
  1. It is not strictly necessary, but I like to wait for the phidget to attach in setup().
to:
  1. It is not strictly necessary, but I like to wait for the phidget to attach in setup(). When the phidget is attached it means it is plugged in by USB and ready for the program to use it.
Changed line 75 from:
  1. In draw(), check to see if the phidget is attached, i.e., plugged in by USB and talking to your program. If it is, display a green circle, otherwise display a red circle.
to:
  1. In draw(), check to see if the phidget is attached. If it is, display a green circle, otherwise display a red circle.
October 30, 2009, at 03:25 PM by 136.159.7.119 -
Changed line 74 from:
This allows one second (1000 ms) for the phidget to attach before proceeding.
to:
This allows one second (1000 ms) for the phidget to attach before proceeding. If the phidget does not attach within this time (i.e., times out), the program carries on without the phidget attached.
October 30, 2009, at 03:24 PM by 136.159.7.119 -
Changed line 40 from:
  1. You need to create an instance of the accelerometer object. The example does this in setup().
to:
  1. You need to create an instance of the accelerometer object. The example does this in setup().
Changed line 62 from:
  1. It is not strictly necessary, but I like to wait for the phidget to attach in setup().
to:
  1. It is not strictly necessary, but I like to wait for the phidget to attach in setup().
Changed lines 74-75 from:
This allows one second (1000 ms) for the phidget to attach before proceeding.
  1. In draw(), check to see if the phidget is attached, i.e., plugged in by USB and talking to your program. If it is, display a green circle, otherwise display a red circle.
to:
This allows one second (1000 ms) for the phidget to attach before proceeding.
  1. In draw(), check to see if the phidget is attached, i.e., plugged in by USB and talking to your program. If it is, display a green circle, otherwise display a red circle.
Changed line 102 from:
  1. Display the acceleration data as a bar graph. The code uses the getAxisCount to determine the number of axes to display acceleration data for, allowing a for loop to iterate through the axes.
to:
  1. Display the acceleration data as a bar graph. The code uses the getAxisCount() to determine the number of axes to display acceleration data for, allowing a for loop to iterate through the axes.
October 30, 2009, at 03:20 PM by 136.159.7.119 -
Changed line 3 from:

Source Code with Objects for Operating Phidgets from Processing

to:

Source Code with Classes for Operating Phidgets from Processing

October 30, 2009, at 03:20 PM by 136.159.7.119 -
Added line 208:
  
Added line 215:
  
Added line 222:
  
October 30, 2009, at 03:19 PM by 136.159.7.119 -
Changed line 228 from:
to:
  
October 30, 2009, at 03:19 PM by 136.159.7.119 -
Added line 228:
October 30, 2009, at 03:16 PM by 136.159.7.119 -
Changed lines 203-204 from:

Accelerometer

to:
Accelerometer 
Changed lines 208-212 from:

AdvancedServo

to:
AdvancedServo 
Changed lines 214-219 from:

Encoder

to:
Encoder 
Deleted lines 219-221:
Changed line 228 from:
Motor Control 
to:
Motor Control 
October 30, 2009, at 03:15 PM by 136.159.7.119 -
Changed lines 233-234 from:

Interface Kit

to:
Interface Kit 
Changed lines 241-243 from:

Motor Control

to:
Motor Control 
October 30, 2009, at 03:14 PM by 136.159.7.119 -
Added lines 242-252:

Motor Control

int getMotorCount()return number of motors that phidget can control
float getAcceleration( int index )return acceleration setting for indexed motor normalized to [0 .. 1]
float getCurrent( int index )return current for indexed motor
int getInputCount( )return number of digital inputs
boolean getInputState( int index )return state for indexed digital input
float getVelocity( int index )return velocity for indexed motor
void setAcceleration( int index, float accel )set acceleratin for indexed motor in range [0 .. 1]
void setVelocity( int index, float velocity )set velocity of indexed motor in range [-100 ... 100]
October 30, 2009, at 03:11 PM by 136.159.7.119 -
October 30, 2009, at 03:11 PM by 136.159.7.119 -
Changed lines 225-232 from:
int getEncoderCount()return encoder count
int getInputCount()return number of digital inputs
int getPosition( int index )return position of indexed encoder
void setPosition( int index, int position )set position of indexed encoder
boolean getInputState( int index )return state of indexed digital input
to:
int getEncoderCount()return encoder count
int getInputCount()return number of digital inputs
int getPosition( int index )return position of indexed encoder
void setPosition( int index, int position )set position of indexed encoder
boolean getInputState( int index )return state of indexed digital input
Added lines 234-242:
void setRatiometric( boolean state )set ratiometric mode for interface kit
int getSensorCount()return number of A/D converters (sensor channels) available
int getSensorValue( int index )return sensor value for A/D converter corresponding to index
int getInputCount()return number of digital inputs available
boolean getInputState( int index )return state of digital input corresponding to index
int getOutputCount()return number of digital outputs available
void setOutputState( int index, boolean state )sets state of digital output corresponding to index
October 30, 2009, at 03:08 PM by 136.159.7.119 -
Added lines 224-231:
int getEncoderCount()return encoder count
int getInputCount()return number of digital inputs
int getPosition( int index )return position of indexed encoder
void setPosition( int index, int position )set position of indexed encoder
boolean getInputState( int index )return state of indexed digital input
October 30, 2009, at 03:04 PM by 136.159.7.119 -
Added lines 213-221:
int getMotorCount()return number of motors available
boolean getEngaged( int index )return true if indexed motor is engaged
void setEngaged( int index, boolean state )engage/disengage indexed motor (state=true for engaged)
float getPosition( int index )returns position of indexed motor in range [-1 ... 1]
void setPosition( int index, float position )set position of indexed motor in range [-1 ... 1]
October 30, 2009, at 03:00 PM by 136.159.7.119 -
Added lines 201-216:

The following are methods specific to each phidget type.

Accelerometer

int getAxisCount()returns number of axes
float getAcceleration( int axis )returns acceleration for axis x=0 y=1 z=2
float getAccelerationMax( int axis )returns maximum acceleration for axis x=0 y=1 z=2
float getAccelerationMin( int axis )returns minimum acceleration for axis x=0 y=1 z=2

AdvancedServo

Encoder

Interface Kit

October 30, 2009, at 02:55 PM by 136.159.7.119 -
Changed lines 194-199 from:
void close()close the phidget Accelerometer
boolean isAttached()returns true if Accelerometer is attached
void waitForAttachment( int timeout )wait for Accelerometer to attach with timeout in miliseconds
void waitForAttachment( )wait indefinitely for Accelerometer to attach
int serial()return serial number of Accelerometer if attached, 0 otherwise
int version()return Accelerometer revision no if attached, 0 otherwise
to:
void close()close the phidget
boolean isAttached()returns true if phidget is attached
void waitForAttachment( int timeout )wait for phidget to attach with timeout in miliseconds
void waitForAttachment( )wait indefinitely for phidget to attach
int serial()return serial number of phidget if attached, 0 otherwise
int version()return phidget version number if attached, 0 otherwise
October 30, 2009, at 02:54 PM by 136.159.7.119 -
Changed lines 189-191 from:
phidgetTYPE()open with first available phidget of TYP{E
phidgetTYPE( int serial)open phidget with given serial number
to:
phidgetTYPE()open with first available phidget of TYP{E
phidgetTYPE( int serial)open phidget with given serial number
Changed lines 194-200 from:
||  void close() - close the phidget Accelerometer
//  boolean isAttached() - returns true if Accelerometer is attached
//  void waitForAttachment( int timeout ) - wait for Accelerometer to attach with timeout in miliseconds
//  void waitForAttachment( ) - wait indefinitely for Accelerometer to attach
//  int serial() - return serial number of Accelerometer if attached, 0 otherwise
//  int version() - return Accelerometer revision no if attached, 0 otherwise
to:
void close()close the phidget Accelerometer
boolean isAttached()returns true if Accelerometer is attached
void waitForAttachment( int timeout )wait for Accelerometer to attach with timeout in miliseconds
void waitForAttachment( )wait indefinitely for Accelerometer to attach
int serial()return serial number of Accelerometer if attached, 0 otherwise
int version()return Accelerometer revision no if attached, 0 otherwise
October 30, 2009, at 02:52 PM by 136.159.7.119 -
Changed lines 189-190 from:
phidgetTYPE()open with first available phidget of TYP{E
phidgetTYPE( int serial)open phidget with given serial number
to:
phidgetTYPE()open with first available phidget of TYP{E
phidgetTYPE( int serial)open phidget with given serial number

Every phidget class has these methods.

||  void close() - close the phidget Accelerometer
//  boolean isAttached() - returns true if Accelerometer is attached
//  void waitForAttachment( int timeout ) - wait for Accelerometer to attach with timeout in miliseconds
//  void waitForAttachment( ) - wait indefinitely for Accelerometer to attach
//  int serial() - return serial number of Accelerometer if attached, 0 otherwise
//  int version() - return Accelerometer revision no if attached, 0 otherwise
October 30, 2009, at 02:50 PM by 136.159.7.119 -
Added lines 189-190:
phidgetTYPE()open with first available phidget of TYP{E
phidgetTYPE( int serial)open phidget with given serial number
October 30, 2009, at 02:49 PM by 136.159.7.119 -
Added lines 186-187:

Each phidget class has two constructors as given below (TYPE is one of Acclerometer, AdvancedServo, Encoder, IK, or MotorControl).

October 30, 2009, at 02:42 PM by 136.159.7.119 -
Changed lines 176-187 from:

@]

to:

@]

Documentation

The phidget classes currently available are:

  • phidgetAccelerometer
  • phidgetAdvancedServo
  • phidgetEncoder
  • phidgetIK
  • phidgetMotorControl
October 30, 2009, at 02:40 PM by 136.159.7.119 -
Changed lines 130-131 from:
to:
Compute where the endpoint of the bar should be. Use the getAcceleration() method to get the acceleration. The getAccelerationMax() and getAccelerationMin() methods give you the range of values for the accelerometer that you are using. Scale the the acceleration into the range of bar sizes using the map() function.
Changed lines 137-138 from:
    float xc = 30.0 + i * 60.0;
    float yc = 140.0;
to:
    ...
Changed lines 140-146 from:
    noFill();
    stroke( 0 );
    line( xc, yc, xc, y );
    fill( #0000ff );
    noStroke();
    ellipse( xc, y, 10, 10 );
to:
    ...
Added lines 146-176:
Draw the bar.

void draw() {
  ... 
  // show accelerations
  for( int i = 0; i < accelerometer.getAxisCount(); i++ ) {
    ...
    noFill();
    stroke( 0 );
    line( xc, yc, xc, y );
    fill( #0000ff );
    noStroke();
    ellipse( xc, y, 10, 10 );
  }
  ...
}

  1. It is best to use the close() method for the phidget object explicitly. It closes the phidget connection nicely, which will not happen automatically when Processing/Java deletes your phidget object. I used a keyPressed event to trigger the close() method and to shut down the program.

void draw() {
  ... 
  if (keyPressed) {
    accelerometer.close();
    exit();
  }
}

October 30, 2009, at 02:33 PM by 136.159.7.119 -
Changed line 115 from:
Compute where the bar should be placed for the i th axis, center at xc,yc.
to:
Compute where the bar should be placed for the i th axis, center at xc,yc.
October 30, 2009, at 02:33 PM by 136.159.7.119 -
Deleted lines 115-116:
October 30, 2009, at 02:31 PM by 136.159.7.119 -
Changed lines 115-117 from:
to:
Compute where the bar should be placed for the i th axis, center at xc,yc.

void draw() {
  ... 
  // show accelerations
  for( int i = 0; i < accelerometer.getAxisCount(); i++ ) {
    float xc = 30.0 + i * 60.0;
    float yc = 140.0;
    ...
  }
  ...
}

October 30, 2009, at 02:28 PM by 136.159.7.119 -
Changed line 44 from:

...

to:
  ...
Changed line 47 from:

...

to:
  ...
Changed line 55 from:

...

to:
  ...
Changed line 58 from:

...

to:
  ...
Changed line 66 from:

...

to:
  ...
Changed line 70 from:

...

to:
  ...
Changed line 79 from:

...

to:
  ...
Changed line 85 from:

...

to:
  ...
Changed line 93 from:

...

to:
  ...
Changed line 98 from:

...

to:
  ...
Changed line 106 from:

...

to:
  ... 
Changed line 109 from:

...

to:
    ...
Changed line 111 from:

...

to:
  ...
Changed line 121 from:

...

to:
  ... 
Changed line 136 from:

...

to:
  ...
October 30, 2009, at 02:27 PM by 136.159.7.119 -
Changed line 102 from:
to:
  1. Display the acceleration data as a bar graph. The code uses the getAxisCount to determine the number of axes to display acceleration data for, allowing a for loop to iterate through the axes.
Deleted lines 108-119:
    float xc = 30.0 + i * 60.0;
    float yc = 140.0;
    float y = map( accelerometer.getAcceleration( i ),
        accelerometer.getAccelerationMin( i ), accelerometer.getAccelerationMax( i ), 230.0, 50.0 );

    noFill();
    stroke( 0 );
    line( xc, yc, xc, y );
    fill( #0000ff );
    noStroke();
    ellipse( xc, y, 10, 10 );
  }
Added lines 110-111:
  }

...

Changed lines 114-139 from:

@]

to:

@]


void draw() {
... 
  // show accelerations
  for( int i = 0; i < accelerometer.getAxisCount(); i++ ) {
    float xc = 30.0 + i * 60.0;
    float yc = 140.0;
    float y = map( accelerometer.getAcceleration( i ),
        accelerometer.getAccelerationMin( i ), accelerometer.getAccelerationMax( i ), 230.0, 50.0 );

    noFill();
    stroke( 0 );
    line( xc, yc, xc, y );
    fill( #0000ff );
    noStroke();
    ellipse( xc, y, 10, 10 );
  }
...
}

October 30, 2009, at 02:23 PM by 136.159.7.119 -
Changed lines 103-124 from:
to:

void draw() {
... 
  // show accelerations
  for( int i = 0; i < accelerometer.getAxisCount(); i++ ) {
    float xc = 30.0 + i * 60.0;
    float yc = 140.0;
    float y = map( accelerometer.getAcceleration( i ),
        accelerometer.getAccelerationMin( i ), accelerometer.getAccelerationMax( i ), 230.0, 50.0 );

    noFill();
    stroke( 0 );
    line( xc, yc, xc, y );
    fill( #0000ff );
    noStroke();
    ellipse( xc, y, 10, 10 );
  }
...
}

October 30, 2009, at 02:22 PM by 136.159.7.119 -
Changed line 102 from:
to:
October 30, 2009, at 02:22 PM by 136.159.7.119 -
Added lines 85-97:

... }

@]

  1. Find and display the serial and version numbers for the accelerometer phidget.

[@

void draw() { ...

  // show serial number and version
  fill(0);
  text( accelerometer.serial(), 40, 25 );
  text( accelerometer.version(), 40, 35 );
October 30, 2009, at 02:20 PM by 136.159.7.119 -
Changed lines 75-90 from:
to:
  1. In draw(), check to see if the phidget is attached, i.e., plugged in by USB and talking to your program. If it is, display a green circle, otherwise display a red circle.

void draw() {
...
  // show attach status
  if ( accelerometer.isAttached() ) fill( 0, 255, 0 );
  else fill( 255, 0, 0 );
  noStroke();
  ellipse( 20, 20, 10, 10 );
...
}

October 30, 2009, at 02:16 PM by 136.159.7.119 -
Changed line 75 from:
to:
October 30, 2009, at 02:15 PM by 136.159.7.119 -
Changed lines 40-43 from:
  1. next item
What happened with the last example? Why? If you were able to correctly anticipate the computer’s response in all but the last one, it is time to move on. If not, take time now to make up examples of your own. Explore the modulus operator until you are confident you understand how it works.
to:
  1. You need to create an instance of the accelerometer object. The example does this in setup().

void setup() {
...
  // get phidget IK started and wait up to 1s for attach
  accelerometer = new phidgetAccelerometer();
...
}

This will work if you use only one phidget accelerometer. If you use more, you should use the alternate constructor to specify which phidget to use with the serial number.

void setup() {
...
  // get phidget IK started and wait up to 1s for attach
  accelerometer = new phidgetAccelerometer( 12345 );
...
}

  1. It is not strictly necessary, but I like to wait for the phidget to attach in setup().

void setup() {
...
  // get phidget IK started and wait up to 1s for attach
  accelerometer = new phidgetAccelerometer();
  accelerometer.waitForAttachment( 1000 );
...
}

This allows one second (1000 ms) for the phidget to attach before proceeding.
October 30, 2009, at 02:09 PM by 136.159.7.119 -
Added line 36:
Added line 38:
Changed lines 40-41 from:
  1. next item
to:
  1. next item
What happened with the last example? Why? If you were able to correctly anticipate the computer’s response in all but the last one, it is time to move on. If not, take time now to make up examples of your own. Explore the modulus operator until you are confident you understand how it works.
October 30, 2009, at 02:07 PM by 136.159.7.119 -
Changed line 37 from:

@/

to:

@]

October 30, 2009, at 02:06 PM by 136.159.7.119 -
Changed lines 34-38 from:
  1. Every phidget needs an object to operate it, so create the object variable.
to:
  1. Every phidget needs an object to operate it, so create the object variable:

[@ phidgetAccelerometer accelerometer; @/

  1. next item
October 30, 2009, at 02:02 PM by 136.159.7.119 -
Added lines 31-34:

The following example illustrates how to write Processing code that uses phidgets. It is based on testPhidgetAccelerometer.zip.

  1. Every phidget needs an object to operate it, so create the object variable.
October 30, 2009, at 01:56 PM by 136.159.7.119 -
Changed lines 27-32 from:
  1. 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.
to:
  1. 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.
  2. Repeat the previous step for each type of phidget that you will use.

Your sketch is now ready for you to start writing code that uses the phidgets.

October 30, 2009, at 01:53 PM by 136.159.7.119 -
Added lines 26-28:
  1. 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.
  2. 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.
October 30, 2009, at 01:47 PM by 136.159.7.119 -
Changed lines 21-26 from:
  1. Install Phidget drivers for your computer from http://www.phidgets.com/ .
to:
  1. Install phidget drivers for your computer from http://www.phidgets.com/ - drivers tab.
  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 this page. Put them in your phidget folder.
  5. Create a new Processing sketch. Save the sketch, giving it a suitable name.
October 30, 2009, at 01:37 PM by 136.159.7.119 -
Changed line 21 from:
  1. Install Phidget drivers for your computer from http://www.phidgets.com/
to:
  1. Install Phidget drivers for your computer from http://www.phidgets.com/ .
October 30, 2009, at 01:37 PM by 136.159.7.119 -
Added lines 19-21:

General Instructions

  1. Install Phidget drivers for your computer from http://www.phidgets.com/
October 30, 2009, at 01:34 PM by 136.159.7.119 -
Changed lines 9-18 from:
  • Accelerometer

testPhidgetAccelerometer.zip testPhidgetAdvancedServo.zip testPhidgetEncoder.zip testPhidgetIK.zip testPhidgetMotorControl.zip

to:
  • Motor Controller

Sample Sketches

  • testPhidgetAccelerometer.zip
  • testPhidgetAdvancedServo.zip
  • testPhidgetEncoder.zip
  • testPhidgetIK.zip
  • testPhidgetMotorControl.zip
October 30, 2009, at 01:31 PM by 136.159.7.119 -
Changed lines 5-7 from:
  • phidgetAccelerometer.pde
to:
  • Accelerometer
  • Advanced Servo
  • Encoder
  • Interface Kit
  • Accelerometer
Deleted lines 12-15:

phidgetAdvancedServo.pde phidgetEncoder.pde phidgetIK.pde phidgetMotorControl.pde

October 30, 2009, at 01:29 PM by 136.159.7.119 -
October 30, 2009, at 01:29 PM by 136.159.7.119 -
Added lines 2-18:

Source Code with Objects for Operating Phidgets from Processing

  • phidgetAccelerometer.pde

phidgetAdvancedServo.pde phidgetEncoder.pde phidgetIK.pde phidgetMotorControl.pde testPhidgetAccelerometer.zip testPhidgetAdvancedServo.zip testPhidgetEncoder.zip testPhidgetIK.zip testPhidgetMotorControl.zip

October 30, 2009, at 01:04 PM by 136.159.7.119 -
Added line 1:

Processing with Phidgets

Page last modified on October 30, 2009, at 03:33 PM