4 changed files with 82 additions and 30 deletions
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 42 KiB |
@ -1,6 +1,6 @@ |
|||||
# 3Dmodel_motion_tracking |
# 3Dmodel_motion_tracking |
||||
|
|
||||
Visualizing 3D motion of lolypad adxl335 sensor |
Visualizing 3D motion of lolypad adxl335 sensor |
||||
|
|
||||
## Connecting Lolypad ADXL335 to arduino uno |
## Connecting Lolypad ADXL335 to arduino uno |
||||
 |
 |
@ -0,0 +1,55 @@ |
|||||
|
// these constants describe the pins. They won't change:
|
||||
|
const int xpin = A1; // x-axis of the accelerometer
|
||||
|
const int ypin = A2; // y-axis
|
||||
|
const int zpin = A3; // z-axis (only on 3-axis models)
|
||||
|
//
|
||||
|
int sampleDelay = 500; //number of milliseconds between readings
|
||||
|
void setup() |
||||
|
{ |
||||
|
// initialize the serial communications:
|
||||
|
Serial.begin(9600); |
||||
|
//
|
||||
|
//Make sure the analog-to-digital converter takes its reference voltage from
|
||||
|
// the AREF pin
|
||||
|
pinMode(xpin, INPUT); |
||||
|
pinMode(ypin, INPUT); |
||||
|
pinMode(zpin, INPUT); |
||||
|
} |
||||
|
void loop() |
||||
|
{ |
||||
|
int x = analogRead(xpin); |
||||
|
//
|
||||
|
//add a small delay between pin readings. I read that you should
|
||||
|
//do this but haven't tested the importance
|
||||
|
delay(1); |
||||
|
//
|
||||
|
int y = analogRead(ypin); |
||||
|
//
|
||||
|
//add a small delay between pin readings. I read that you should
|
||||
|
//do this but haven't tested the importance
|
||||
|
delay(1); |
||||
|
//
|
||||
|
int z = analogRead(zpin); |
||||
|
//
|
||||
|
//zero_G is the reading we expect from the sensor when it detects
|
||||
|
//no acceleration. Subtract this value from the sensor reading to
|
||||
|
//get a shifted sensor reading.
|
||||
|
float zero_G =512; |
||||
|
//
|
||||
|
//scale is the number of units we expect the sensor reading to
|
||||
|
//change when the acceleration along an axis changes by 1G.
|
||||
|
//Divide the shifted sensor reading by scale to get acceleration in Gs.
|
||||
|
float scale =102.3; |
||||
|
//
|
||||
|
Serial.print(((float)x - zero_G)/scale); |
||||
|
Serial.print("\t"); |
||||
|
//
|
||||
|
Serial.print(((float)y - zero_G)/scale); |
||||
|
Serial.print("\t"); |
||||
|
//
|
||||
|
Serial.print(((float)z - zero_G)/scale); |
||||
|
Serial.print("\n"); |
||||
|
//
|
||||
|
// delay before next reading:
|
||||
|
delay(sampleDelay); |
||||
|
} |
@ -1,48 +1,45 @@ |
|||||
import processing.serial.*; |
import processing.serial.*; |
||||
String row; |
String row; |
||||
int xpin; |
float xpin; |
||||
int ypin; |
float ypin; |
||||
int zpin; |
float zpin; |
||||
Serial myPort; |
Serial myPort; |
||||
|
|
||||
void setup(){ |
void setup(){ |
||||
String portName = Serial.list()[0]; //open COM4 port |
String portName = Serial.list()[0]; //open COM4 port |
||||
myPort = new Serial(this, portName, 9600); |
myPort = new Serial(this, portName, 9600); |
||||
|
|
||||
size(640,360,P3D); |
size(640, 360, P3D); |
||||
background(0); |
|
||||
lights(); |
|
||||
|
|
||||
pushMatrix(); |
|
||||
translate(130, height/2, 0); |
|
||||
rotateY(1.25); |
|
||||
rotateX(-0.4); |
|
||||
noStroke(); |
noStroke(); |
||||
box(100); |
fill(204); |
||||
popMatrix(); |
|
||||
|
|
||||
pushMatrix(); |
|
||||
translate(500, height*0.35, -200); |
|
||||
noFill(); |
|
||||
stroke(255); |
|
||||
sphere(280); |
|
||||
popMatrix(); |
|
||||
|
|
||||
} |
} |
||||
|
|
||||
void draw(){ |
void draw(){ |
||||
|
|
||||
|
|
||||
|
|
||||
if ( myPort.available() > 0){ // If data is available, |
if ( myPort.available() > 0){ // If data is available, |
||||
|
|
||||
row = myPort.readStringUntil('\n'); // read it and store it in variable |
row = myPort.readStringUntil('\n'); // read it and store it in variable |
||||
if (row != null){ |
if (row != null){ |
||||
String splittedRow[] = split(row, '\t'); |
String splittedRow[] = split(row, '\t'); |
||||
if (splittedRow.length > 0){xpin = Integer.parseInt(splittedRow[0].trim());} |
if (splittedRow.length > 0){xpin = Float.parseFloat(splittedRow[0].trim());} |
||||
if (splittedRow.length > 1){ypin = Integer.parseInt(splittedRow[1].trim());} |
if (splittedRow.length > 1){ypin = Float.parseFloat(splittedRow[1].trim());} |
||||
if (splittedRow.length > 2){zpin = Integer.parseInt(splittedRow[2].trim());} |
if (splittedRow.length > 2){zpin = Float.parseFloat(splittedRow[2].trim());} |
||||
|
|
||||
println(xpin + ", " + ypin + ", " + zpin); //print it out in the console |
println(xpin + ", " + ypin + ", " + zpin); //print it out in the console |
||||
|
|
||||
|
|
||||
|
background(0); |
||||
|
lights(); |
||||
|
//if(xpin < 360){rotateX(radians(xpin));} |
||||
|
//if(ypin < 360){rotateY(radians(ypin));} |
||||
|
//if(zpin < 360){rotateZ(radians(zpin));} |
||||
|
camera(xpin, ypin, zpin, width/2, height/2, 0, 0, 1, 0); |
||||
|
translate(width/2, height/2, 0); |
||||
|
stroke(255); |
||||
|
box(160); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
} |
} |
||||
|
Loading…
Reference in new issue