0% found this document useful (0 votes)
5 views2 pages

Processing Radar

The document is a Processing sketch that reads data from a serial port to visualize radar-like data on a canvas. It displays angles and distances, plotting points based on distance measurements while allowing for a reset of points when specific angles are detected. The sketch includes functions for drawing the radar, points, sweep lines, and displaying text information about the current angle and distance.

Uploaded by

thuongnv2209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Processing Radar

The document is a Processing sketch that reads data from a serial port to visualize radar-like data on a canvas. It displays angles and distances, plotting points based on distance measurements while allowing for a reset of points when specific angles are detected. The sketch includes functions for drawing the radar, points, sweep lines, and displaying text information about the current angle and distance.

Uploaded by

thuongnv2209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import processing.serial.

*;
import java.util.ArrayList;

Serial myPort;
String angle = "";
String distance = "";
String data = "";
int iAngle = 0, iDistance = 0;
ArrayList<PVector> points;
PFont myFont;
boolean resetPoints = false;

void setup() {
size(800, 600);
smooth();
myPort = new Serial(this, "COM5", 9600);
myPort.bufferUntil('.');

points = new ArrayList<PVector>();


myFont = createFont("Consolas", 16);
textFont(myFont);
}

void draw() {
background(0, 100, 0); // xanh đậm
drawRadar();
drawObjects();
drawSweep();
drawText();

// Nếu vừa reset


if (resetPoints) {
points.clear();
resetPoints = false;
}
}

void serialEvent(Serial myPort) {


data = myPort.readStringUntil('.');
if (data != null) {
data = data.substring(0, data.length() - 1);
int index = data.indexOf(',');
if (index > 0) {
angle = data.substring(0, index);
distance = data.substring(index + 1);
iAngle = int(angle);
iDistance = int(distance);

// Khi quét về 0 độ => xoá điểm


if (iAngle == 15 || iAngle == 165) {
resetPoints = true;
}

if (iDistance < 40) {


float r = map(iDistance, 0, 40, 0, width/2 * 0.9);
float realAngle = radians(iAngle);
float x = r * cos(realAngle);
float y = -r * sin(realAngle);
points.add(new PVector(x, y));
}
}
}
}

void drawRadar() {
pushMatrix();
translate(width/2, height*0.95);
stroke(0, 255, 0);
strokeWeight(2);
noFill();

for (int r = 1; r <= 4; r++) {


ellipse(0, 0, r * width/2 * 0.45, r * width/2 * 0.45);
}

for (int a = 0; a <= 180; a += 30) {


float x = (width/2 * 0.9) * cos(radians(a));
float y = -(width/2 * 0.9) * sin(radians(a));
line(0, 0, x, y);
}

popMatrix();
}

void drawObjects() {
pushMatrix();
translate(width/2, height*0.95);
noStroke();
fill(255, 153, 0); // cam
for (int i = 0; i < points.size(); i++) {
ellipse(points.get(i).x, points.get(i).y, 6, 6);
}
popMatrix();
}

void drawSweep() {
pushMatrix();
translate(width/2, height*0.95);
stroke(255);
strokeWeight(2);
float x = (width/2 * 0.9) * cos(radians(iAngle));
float y = -(width/2 * 0.9) * sin(radians(iAngle));
line(0, 0, x, y);
popMatrix();
}

void drawText() {
fill(0, 200);
noStroke();
rect(0, 0, width, 40);

fill(255);
textAlign(LEFT);
text("Angle: " + iAngle + "°", 10, 25);
text("Distance: " + iDistance + " cm", 200, 25);
}

You might also like