Last active
August 29, 2015 14:12
-
-
Save kenkawakenkenke/f9e7458717cc8f8fc67f to your computer and use it in GitHub Desktop.
Hardware Prototyping Speed Test: Phidgets vs Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
Serial.begin(9600); | |
pinMode(7,OUTPUT); | |
} | |
void loop() { | |
digitalWrite(7,HIGH); | |
delay(500); | |
digitalWrite(7,LOW); | |
delay(500); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ken.phidgets.challenge; | |
import com.ken.phidgets.PhidgetsUtil; | |
import com.phidgets.InterfaceKitPhidget; | |
public class Blink { | |
public static void main(String[] args) throws Exception{ | |
InterfaceKitPhidget ik=PhidgetsUtil.getAnyInterfaceKit(); | |
while(true){ | |
ik.setOutputState(0, true); | |
Thread.sleep(500); | |
ik.setOutputState(0, false); | |
Thread.sleep(500); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//low pass filter | |
float queue[100]; | |
int window=100; | |
float total=0; | |
int currentIdx=0; | |
void setup() { | |
Serial.begin(9600); | |
for(int i=0;i<window;i++) | |
queue[i]=0; | |
for(int i=2;i<=9;i++) | |
pinMode(i,OUTPUT); | |
} | |
float magThresh=0.3; | |
bool isAbove=false; | |
int stepCount=0; | |
void loop() { | |
double x=(analogRead(3)-512)/200.; | |
double y=(analogRead(4)-512)/200.; | |
double z=(analogRead(5)-512)/200.; | |
float mag=sqrt(x*x+y*y+z*z); | |
//high pass filter | |
{ | |
total-=queue[currentIdx]; | |
queue[currentIdx]=mag; | |
total+=mag; | |
currentIdx++; | |
if(currentIdx==window) | |
currentIdx=0; | |
mag-= total/(float)window; | |
} | |
//zero crossing | |
if(isAbove){ | |
if(mag<-magThresh){ | |
isAbove=false; | |
stepCount++; | |
int outIdx=map(stepCount,0,50,2,9); | |
for(int j=2;j<=9;j++) | |
digitalWrite(j, j<outIdx?HIGH:LOW); | |
Serial.println(mag); | |
} | |
}else{ | |
if(mag>magThresh){ | |
isAbove=true; | |
} | |
} | |
// Serial.print(x); | |
// Serial.print(" "); | |
// Serial.print(y); | |
// Serial.print(" "); | |
// Serial.print(z); | |
// Serial.println(" "); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ken.phidgets.challenge; | |
import java.io.IOException; | |
import com.ken.phidgets.PhidgetsUtil; | |
import com.ken.phidgets.SpatialEventDataListener; | |
import com.phidgets.InterfaceKitPhidget; | |
import com.phidgets.PhidgetException; | |
import com.phidgets.SpatialEventData; | |
import com.phidgets.SpatialPhidget; | |
import common.gui.graph.ScatterPlot; | |
import common.util.MathUtil; | |
import common.util.WindowedAverager; | |
public class Pedometer { | |
public static void main(String[] args) throws IOException { | |
final InterfaceKitPhidget ik=PhidgetsUtil.getAnyInterfaceKit(); | |
for(int i=0;i<8;i++){ | |
try { | |
ik.setOutputState(i, false); | |
} catch (PhidgetException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
SpatialPhidget spatial=PhidgetsUtil.getAnySpatial(); | |
spatial.addSpatialDataListener(new SpatialEventDataListener() { | |
ScatterPlot plot=ScatterPlot.getPlotFrame(); | |
{ | |
plot.setShowLine(true); | |
} | |
WindowedAverager avger=new WindowedAverager(100); | |
double magThresh=0.3; | |
boolean isAbove=false; | |
int stepCount=0; | |
@Override | |
public void eventData(SpatialEventData data) { | |
double x=data.getAcceleration()[0]; | |
double y=data.getAcceleration()[1]; | |
double z=data.getAcceleration()[2]; | |
double mag=Math.sqrt(x*x+y*y+z*z); | |
//calc low pass | |
avger.add(mag); | |
//high pass | |
mag-=avger.mean(); | |
if(isAbove){ | |
if(mag<-magThresh){ | |
isAbove=false; | |
stepCount++; | |
int outIdx=(int)MathUtil.map(stepCount, 0, 50,0,8); | |
for(int i=0;i<8;i++) | |
try { | |
ik.setOutputState(i, i<outIdx); | |
} catch (PhidgetException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
System.out.println("step: "+stepCount); | |
} | |
}else{ | |
if(mag>magThresh){ | |
isAbove=true; | |
} | |
} | |
plot.add(System.currentTimeMillis(),mag); | |
} | |
}); | |
System.in.read(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
Servo mServo; | |
void setup() { | |
Serial.begin(9600); | |
mServo.attach(9); | |
} | |
void loop() { | |
int val=analogRead(0); | |
val=map(val,0,1024,0,180); | |
Serial.print("write "); | |
Serial.println(val); | |
mServo.write(val); | |
delay(30); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ken.phidgets.challenge; | |
import java.io.IOException; | |
import com.ken.phidgets.PhidgetsUtil; | |
import com.phidgets.AdvancedServoPhidget; | |
import com.phidgets.InterfaceKitPhidget; | |
import com.phidgets.PhidgetException; | |
import com.phidgets.event.SensorChangeEvent; | |
import com.phidgets.event.SensorChangeListener; | |
import common.util.MathUtil; | |
public class ServoTest { | |
public static void main(String[] args) throws IOException { | |
InterfaceKitPhidget ik=PhidgetsUtil.getAnyInterfaceKit(); | |
final AdvancedServoPhidget servo=PhidgetsUtil.getAnyAdvancedServo(); | |
ik.addSensorChangeListener(new SensorChangeListener() { | |
@Override | |
public void sensorChanged(SensorChangeEvent event) { | |
if(event.getIndex()==0){ | |
double angle=MathUtil.map(event.getValue(), 0, 1000,0,180); | |
System.out.println(event.getValue()+" "+angle); | |
try { | |
servo.setPosition(0, angle); | |
} catch (PhidgetException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}); | |
System.in.read(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment