Your sketch is now ready for you to start writing code that uses the phidgets.
The following example illustrates how to write Processing code that uses phidgets. It is based on testPhidgetAccelerometer.zip.
phidgetAccelerometer accelerometer;
void setup() { ... // get phidget IK started and wait up to 1s for attach accelerometer = new phidgetAccelerometer(); ... }
void setup() { ... // get phidget IK started and wait up to 1s for attach accelerometer = new phidgetAccelerometer( 12345 ); ... }
void setup() { ... // get phidget IK started and wait up to 1s for attach accelerometer = new phidgetAccelerometer(); accelerometer.waitForAttachment( 1000 ); ... }
void draw() { ... // show attach status if ( accelerometer.isAttached() ) fill( 0, 255, 0 ); else fill( 255, 0, 0 ); noStroke(); ellipse( 20, 20, 10, 10 ); ... }
void draw() { ... // show serial number and version fill(0); text( accelerometer.serial(), 40, 25 ); text( accelerometer.version(), 40, 35 ); ... }
void draw() { ... // show accelerations for( int i = 0; i < accelerometer.getAxisCount(); i++ ) { ... } ... }
void draw() { ... // show accelerations for( int i = 0; i < accelerometer.getAxisCount(); i++ ) { float xc = 30.0 + i * 60.0; float yc = 140.0; ... } ... }
void draw() { ... // show accelerations for( int i = 0; i < accelerometer.getAxisCount(); i++ ) { ... float y = map( accelerometer.getAcceleration( i ), accelerometer.getAccelerationMin( i ), accelerometer.getAccelerationMax( i ), 230.0, 50.0 ); ... } ... }
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 ); } ... }
void draw() { ... if (keyPressed) { accelerometer.close(); exit(); } }
The phidget classes currently available are:
Each phidget class has two constructors as given below (TYPE is one of Acclerometer, AdvancedServo, Encoder, IK, or MotorControl).
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 |
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, -1 otherwise |
int version() | return phidget version number if attached, -1 otherwise |
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 | |
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] |
Encoder | |
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 |
Interface Kit | |
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 |
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] |