Hi, I’m a begginer and I’m tring to create a room simulation with my TFmini Lidar, the device works with serial communication.
I diffeculties with my code, I’m trying to read from arduino and plot 3D as is.
</>
import processing.serial.;
import peasy.;
Serial serial;
PeasyCam cam;
ArrayList ptCloud;
float angle = 6.5f;
float angleIncrement = 0;
float xOffset = 3.0;
float xOffsetIncrement = 0;
float yOffset = 152.0f;
float yOffsetIncrement = 0;
float scale = 2.6f;
float scaleIncrement = 0;
void setup(){
size(800,640,P3D);
colorMode(RGB,255,255,255);
ptCloud = new ArrayList();
cam = new PeasyCam(this,800);
cam.rotateZ(-3.1415/4);
cam.rotateY(-3.1415/4);
//serial = new Serial(this,Serial.list()[0],115200);
//serial.bufferUntil(’\n’);
}
void draw(){
background(33);
stroke(255,255,255);
String input = serial.readStringUntil(’\n’);
if(input!=null){
String[] coordinates = split(input,’ ');
if(coordinates.length==3){
ptCloud.add(new PVector(float(coordinates[0]),float(coordinates[1]),float(coordinates[2])));
}
}
pushMatrix();
translate(width/2, height/2, -50);
rotateY(angle);
beginShape(POINTS);
for(int idx=0;idx<ptCloud.size();idx++){
PVector p = ptCloud.get(idx);
if(idx == ptCloud.size()-1){
stroke(255,0,0);
line(0,0,0,p.x,p.y,p.z);
}
stroke(255-p.x,255-p.y,255-p.z);
//point(p.x * scale + xOffset, -p.z * scale + yOffset, -p.y * scale);
vertex(p.x * scale + xOffset,-p.z * scale + yOffset, -p.y * scale);
}
endShape();
popMatrix();
angle += angleIncrement;
xOffset += xOffsetIncrement;
yOffset += yOffsetIncrement;
scale += scaleIncrement;
}
void keyPressed() {
if (key == ‘q’) {
// zoom in
scaleIncrement = 0.02f;
} else if (key == ‘z’) {
// zoom out
scaleIncrement = -0.02f;
} else if (key == ‘a’) {
// move left
xOffsetIncrement = -1f;
} else if (key == ‘d’) {
// move right
xOffsetIncrement = 1f;
} else if (key == ‘w’) {
// move up
yOffsetIncrement = -1f;
} else if (key == ‘s’) {
// move down
yOffsetIncrement = 1f;
} else if (key ==‘x’) {
// erase all points
ptCloud.clear();
} else if (key == CODED) {
if (keyCode == LEFT) {
// rotate left
angleIncrement = -0.015f;
} else if (keyCode == RIGHT) {
// rotate right
angleIncrement = 0.015f;
}
}
}
void keyReleased() {
if (key == ‘q’) {
scaleIncrement = 0f;
} else if (key == ‘z’) {
scaleIncrement = 0f;
} else if (key == ‘a’) {
xOffsetIncrement = 0f;
} else if (key == ‘d’) {
xOffsetIncrement = 0f;
} else if (key == ‘w’) {
yOffsetIncrement = 0f;
} else if (key == ‘s’) {
yOffsetIncrement = 0f;
} else if (key == CODED) {
if (keyCode == LEFT) {
angleIncrement = 0f;
} else if (keyCode == RIGHT) {
angleIncrement = 0f;
}
}
}