iat 351 week 9 lecture 2 04.03 - sfu.ca · phidgets | iat351 | 04.03.2009 related work • tools...
TRANSCRIPT
Phidgets™ programming framework
IAT 351 Week 9 Lecture 2
04.03.2009
Phidgets | IAT351 | 04.03.2009
traditional physical UI : examples
Walking pad (DIUF)
Phidgets | IAT351 | 04.03.2009
traditional physical UI : examples
Lego Mindstorms
Phidgets | IAT351 | 04.03.2009
traditional physical UI : examples
SmartHome
Phidgets | IAT351 | 04.03.2009
What is Context?
• Any information that can be used to characterize the situation of an entity • Who, what, where, when
• Why is it important? information, usually implicit, that applications do not have access to It’s input that you don’t get in a GUI
Phidgets | IAT351 | 04.03.2009
How to Use Context
• To present relevant information to someone Mobile tour guide
• To perform an action automatically Print to nearest printer
• To show an action that use can choose Want to phone the number in this email?
Phidgets | IAT351 | 04.03.2009
Related Work
• Tools for working with physical input/output devices
• iRX Board • Digital I/O boards • Tini boards • Arduino
Phidgets | IAT351 | 04.03.2009
Why is this hard?
• Steps Acquisition Representation Interpretation Storage Delivery Reaction
• Most of these steps repeated in all development.
Assignment 4
• Sensor input and drawing
• energyView panel
• Graph with two lines: external temperature (set with slider) Energy consumption ( compute from 3 inputs)
• Inputs: Internal temperature : rotation sensor Lights: precision light sensor Appliances: touch sensor
Phidgets | IAT351 | 04.03.2009
Assignment 4
• Graph with two lines: Energy consumption = sum of 1. Whether heating or cooling is needed
• kwH = ABS(internal temp-ext temp)* 2 • Turn red if heating, blue if cooling
2. Lights (full on=2kwH, partly on=1 ,off=0) 3. Appliances: on = 3kwH, off = 0
Phidgets | IAT351 | 04.03.2009
Assignment 4
• Graph with two lines: Energy consumption = sum of 1. Whether heating or cooling is
needed 2. Lights (full on=2kwH, partly
on=1 ,off=0) 3. Appliances: on = 3kwH, off = 0
• Turn boxes on or off according to input
Phidgets | IAT351 | 04.03.2009
H appliance lights AC
Phidgets | IAT351 | 04.03.2009
traditional physical UI : problems
1. Electronics (Difficult to write/debug w/o actual devices) 2. missing API 3. unsuited API (wrong abstraction level) 4. commercial target (oriented to different markets) 5. lack of simulation capabilities
• Hard to build
Phidgets | IAT351 | 04.03.2009
phidgets : goals
• simple devices • easy to program
… just as widgets make GUIs easy to develop, so could phidgets make the new generation of physical user interfaces easy to develop.
Phidgets | IAT351 | 04.03.2009
phidgets : definition
• widget (1931) small mechanical device (today) ‘windows gadget’
• phidget (aka phydget) ‘physical widget’
Phidgets | IAT351 | 04.03.2009
phidgets : definition
Phidgets™, or physical widgets, are building blocks that help a developer construct physical user interfaces.
Phidgets | IAT351 | 04.03.2009
phidgets : architecture
Device
Device manager
API
Simulation
Phidget
Phidgets | IAT351 | 04.03.2009
phidgets : architecture
Device
Device manager
API
Simulation
Phidgets | IAT351 | 04.03.2009
phidgets : API
Int Count Phidget[] Item
OnAttach() OnDetach() OnError()
DeviceType IsAttached
SerialNumber
Phidget-specific properties, e.g.
NumMotors Phidget-specific
events, e.g. OnTag
Generic phidget
Specific phidget
Phidgets manager
actuator
sensor
Phidgets | IAT351 | 04.03.2009
phidgets : java API
Phidgets | IAT351 | 04.03.2009
phidgets : java API
Phidgets | IAT351 | 04.03.2009
phidgets : programming (output)
Phidget COM
Object
Application
creates
Device
opens
sets
sets
Phidgets | IAT351 | 04.03.2009
phidgets : programming (input)
Phidget COM
Object
Listener
Application
creates
Device
opens
reads fire events
starts
Phidgets | IAT351 | 04.03.2009
Handling the Phidget
• 3 important basic events • ATTACH (USB connected) OnAttach(),attached() • DETACH (USB disconnected) OnDetach(),detached() • ERROR (help!) OnError(), error()
• Standard Java approach of listeners and adapters
• Specific events for each type of device E.g. rfid.OnTag() for detecting an RFID tag in range
Phidgets | IAT351 | 04.03.2009
phidgets : programming
Implement OnEvent methods
Create OS Object Attach Listener
Open connection to device
Extend adapter
Start listening
Phidgets | IAT351 | 04.03.2009
public class OpenIFKitExample
{ public static final void main(String args[]) throws Exception {
InterfaceKitPhidget ik; System.out.println(Phidget.getLibraryVersion());
ik = new InterfaceKitPhidget();
ik.addAttachListener(new AttachListener() { public void attached(AttachEvent ae) { System.out.println("attachment of " + ae); } });
ik.addDetachListener(new DetachListener() { public void detached(DetachEvent ae) { System.out.println("detachment of " + ae); } });
ik.addErrorListener(new ErrorListener() { public void error(ErrorEvent ee) { System.out.println("error event for " + ee);
} });
Basic listeners
Phidgets | IAT351 | 04.03.2009
ik.addInputChangeListener(new InputChangeListener() {public void inputChanged(InputChangeEvent oe) {System.out.println(oe);}});
ik.addOutputChangeListener(new OutputChangeListener() {public void outputChanged(OutputChangeEvent oe) {System.out.println(oe);}});
ik.addSensorChangeListener(new SensorChangeListener() {public void sensorChanged(SensorChangeEvent se) {System.out.println(se);}});
IK listeners
Phidgets | IAT351 | 04.03.2009
ik.openAny();System.out.println("waiting for InterfaceKit attachment...");ik.waitForAttachment();
System.out.println(ik.getDeviceName());
Thread.sleep(500);
ik.getOutputCount()Ik.getSensorCount()
ik.getOutputState(i);ik.setOutputState(i,<true/false>);
Ik.getSensorRawValue(i)Ik.getSensorValue(i)
Waits until a device is plugged in
Phidgets | IAT351 | 04.03.2009
Simple temperature example
import com.phidgets.*; import com.phidgets.event.*;
public class TemperatureExample {
public static final void main(String args[]) throws Exception { TemperatureSensorPhidget tempsensor;
tempsensor = new TemperatureSensorPhidget();
Phidgets | IAT351 | 04.03.2009
Simple temperature example : add IK listeners
tempsensor.addAttachListener(new AttachListener() { public void attached(AttachEvent ae){
System.out.println("attachment of " + ae); } });
tempsensor.addDetachListener(new DetachListener() { public void detached(DetachEvent ae) { System.out.println("detachment of " + ae); }
}); tempsensor.addErrorListener(new ErrorListener() { public void error(ErrorEvent ee) { System.out.println("error event for " + ee);
} });
Phidgets | IAT351 | 04.03.2009
Simple temperature example : add device listener
tempsensor.addTemperatureChangeListener(new TemperatureChangeListener()
{ public void temperatureChanged(TemperatureChangeEvent oe) {
System.out.println(oe); } });
Phidgets | IAT351 | 04.03.2009
Simple temperature example : attach device
tempsensor.openAny();
System.out.println("waiting for TemperatureSensor attachment..."); tempsensor.waitForAttachment();
System.out.println("Serial: " + tempsensor.getSerialNumber());
tempsensor.setTemperatureChangeTrigger(0, 0.1);
System.out.println("trigger: " + tempsensor.getTemperatureChangeTrigger(0));
Set trigger and wait for sensor input
Phidgets | IAT351 | 04.03.2009
Simple temperature example : finish and tidy up
System.out.println("Outputting events. Input to stop."); System.in.read();
System.out.print("closing..."); tempsensor.close(); tempsensor = null; System.out.println(" ok");
if (false) { System.out.println("wait for finalization..."); System.gc(); }
} }
Any console input will stop program
Phidgets | IAT351 | 04.03.2009
Simple temperature example : wait for input
System.out.println("Serial: " + tempsensor.getSerialNumber());
tempsensor.setTemperatureChangeTrigger(0, 0.1); System.out.println("trigger: " + tempsensor.getTemperatureChangeTrigger(0));
System.out.println("Outputting events. Input to stop.");
System.in.read(); Any console input will stop program
Phidgets | IAT351 | 04.03.2009
Simple LED example : turn on and off
for (int j = 0; j < 10; j++)
{ for (int i = 0; i < led.getLEDCount(); i++) {
led.setDiscreteLED(i, 100); } for (int i = 0; i < led.getLEDCount(); i++) {
led.setDiscreteLED(i, 0); } }
Phidgets | IAT351 | 04.03.2009
Simple LED example
import com.phidgets.*; import com.phidgets.event.*;
public class LEDExample {
public static final void main(String args[]) throws Exception { LEDPhidget led;
led = new LEDPhidget();
Phidgets | IAT351 | 04.03.2009
Simple LED example : add IK listeners
led.addAttachListener(new AttachListener() { public void attached(AttachEvent ae){
System.out.println("attachment of " + ae); } });
led.addDetachListener(new DetachListener() { public void detached(DetachEvent ae) { System.out.println("detachment of " + ae); }
}); led.addErrorListener(new ErrorListener() { public void error(ErrorEvent ee) { System.out.println("error event for " + ee);
} });
Phidgets | IAT351 | 04.03.2009
Simple LED example : attach devices
led.openAny();
System.out.println("waiting for LED attachment..."); led.waitForAttachment();
System.out.println("Serial: " + led.getSerialNumber()); System.out.println("LEDs: " + led.getLEDCount());
Phidgets | IAT351 | 04.03.2009
Simple LED example : turn on and off
for (int j = 0; j < 10; j++)
{ for (int i = 0; i < led.getLEDCount(); i++) {
led.setDiscreteLED(i, 100); } for (int i = 0; i < led.getLEDCount(); i++) {
led.setDiscreteLED(i, 0); } }
Phidgets | IAT351 | 04.03.2009
Simple LED example
Set up listener
Phidgets | IAT351 | 04.03.2009
Phidgets Requirement
• Connection manager • ID • Simulation mode
Phidgets | IAT351 | 04.03.2009
Phidget Manager
• onAttach() • onDetach()
• Count • Item
• DeviceType • isAttached() • SerialNumber
Phidgets | IAT351 | 04.03.2009
Phidget Servo
• MotorPosition • NumMotors • onPositionChanged()
Phidgets | IAT351 | 04.03.2009
Attaching the interface kit
// create the interface kit object InterfaceKitPhidget ik = new InterfaceKitPhidget();
// look for an attachment ik.openAny();
// don’t continue until an attachment is found ik.waitForAttachment();
Phidgets | IAT351 | 04.03.2009
Digital Input
• Use the wires to hook one of the switches up to a digital input and one of the grounds (on either end of the row)
• The parameter is the index of the input • The result is a boolean (true for on, false for off)
// read the value of input 3 ik.getInputState(3);
Phidgets | IAT351 | 04.03.2009
Analog Input
• Parameter is index of the device • The result is a value between 0 and around 1000 (I’m guessing the
max is 1024)
// get the value of the first analog device ik.getSensorValue(0);
Phidgets | IAT351 | 04.03.2009
Digital Output
• Boolean values are used to set the output • Each output has an index: 0 - 7 • The two slots at either end are grounds
// sets output 2 to “high” ik.setOutputState(2, true);
Phidgets | IAT351 | 04.03.2009
Ugly Java Stuff: Exceptions
• Almost everything in the phidget package throws exceptions • Solution #1: add “throws Exception” at the end of every method
signature, including the main method • Solution #2: wrap phidget calls in try/catch blocks
Phidgets | IAT351 | 04.03.2009
Pausing in Java
• If you want to pause execution of your program • The parameter is time in milliseconds
// pause for 2 seconds Thread.sleep(2000);
Phidgets | IAT351 | 04.03.2009
Drawbacks
• Need PC • Not mobile • Not easy to deploy
Phidgets | IAT351 | 04.03.2009
references : bibliography • Greenberg S. and Fitchet C., Phidgets : Easy developement of physical interfaces through
physical widgets. Proceedings of the ACM UIST 2001 Symposium. • Greenberg S. and Boyle M., Customizable physical interfaces for interacting with conventional
applications. Proceedings of the ACM UIST 2002 Symposium. • www.phidgets.com, www.phidgetsusa.com • www.wikipedia.org (phidget, widget) • www.smarthome.com, www.legomindstorms.com • Laroussi Bouguila, Evequoz Florian, Michèle Courant, Béat Hirsbrunner. Active Walking
Interface for Human-Scale Virtual Environment. HCII’05.
Phidgets | IAT351 | 04.03.2009
references: programming resources • documentation on www.phidgets.com :
C_API_Manual : basic architecture • http://phidgets.com/documentation/C_API_Manual.pdf
COM_API_Manual : object model (VB) • http://phidgets.com/documentation/COM_API_Manual.pdf
Javadoc : java API, quite similar to COM • http://phidgets.com/documentation/JavaDoc.zip
For each particular phidget see its « hardware description » : • Section labelled « Phidgets » on www.phidgets.com >> Documentation
• software introduction and tutorials : • www.phidgets.com >> Documentation • http://www.phidgetsusa.com/tutorials/tutorialhome.asp
• code examples : • www.phidgets.com >> Download >> Beta >> Examples.zip • www.phidgets.com >> Project / Examples