import processing.serial.*;
import processing.core.PFont;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
Serial port;
ArrayList<PVector> dataPoints;
float graphX; // X-coordinate of the graph
float graphY; // Y-coordinate of the graph
float graphWidth; // Width of the graph
float graphHeight;
// x axis label range
int xMin = 0;
int xMax = 100;
int xStep = 10;
// y axis label range
int yMin = 0;
int yMax = 5;
int yStep = 1;
PFont boldFont; // Declare a variable for the bold font
// Swing components
JFrame frame;
JTable dataTable;
DefaultTableModel tableModel;
void setup() {
size(750, 500); // Set the size of the Processing window
graphX = width * 0.2; // Set the initial X-coordinate of the graph
graphY = height * 0.8; // Set the initial Y-coordinate of the graph
graphWidth = width * 0.6; // Width of the graph
graphHeight = height * 0.6; // Height of the graph
printArray(Serial.list()); // Print available serial ports in the console
port = new Serial(this, "COM5", 9600); // Replace "COM3" with the appropriate port name
port.bufferUntil('\n'); // Set the character to buffer until newline
// Initialize data points list
dataPoints = new ArrayList<PVector>();
// Load a bold font
boldFont = createFont("Arial Bold", 12); // Replace "Arial Bold" with the name of your desired bold font
// Initialize Swing components
frame = new JFrame("Data Table");
tableModel = new DefaultTableModel();
tableModel.addColumn("Angle (θ)");
tableModel.addColumn("Kinematics of Mechanism (m / s)");
dataTable = new JTable(tableModel);
frame.getContentPane().add(new JScrollPane(dataTable));
frame.setSize(400, 300);
frame.setVisible(true);
}
void draw() {
background(255); // Set the background color to white
// Set the stroke color and weight for axes and origin
stroke(0); // Set the stroke color to black
strokeWeight(1); // Set the stroke weight to 1
// Update the position of the graph based on mouse movement
if (mousePressed) {
graphX += mouseX - pmouseX; // Update the X-coordinate of the graph based on mouse movement on the X-axis
graphY += mouseY - pmouseY; // Update the Y-coordinate of the graph based on mouse movement on the Y-axis
}
// Draw the x-axis
line(graphX, graphY, graphX + graphWidth, graphY);
// Draw the arrowhead at the end of the x-axis
float arrowSize = 8; // Size of the arrowhead
triangle(graphX + graphWidth - arrowSize, graphY - arrowSize/2,
graphX + graphWidth - arrowSize, graphY + arrowSize/2,
graphX + graphWidth, graphY);
textAlign(RIGHT);
fill(0);
textFont(boldFont); // Set the text font to bold
textSize(12); // Set the font size for "Angle"
text("Angle(θ)", graphX + graphWidth/2 + 24, graphY + 42);
// Draw the y-axis
line(graphX, graphY, graphX, graphY - graphHeight);
// Draw the arrowhead at the end of the y-axis
triangle(graphX - arrowSize/2, graphY - graphHeight + arrowSize,
graphX + arrowSize/2, graphY - graphHeight + arrowSize,
graphX, graphY - graphHeight);
textAlign(RIGHT);
fill(0);
textFont(boldFont); // Set the text font to bold
textSize(12); // Set the font size for "Velocity of Mechanism"
text("Kinematics of Mechanism (m / s)", graphX - 25, graphY - graphHeight + 152);
// Write x-axis values
textAlign(CENTER);
fill(0);
stroke(0);
for (int i = xMin; i <= xMax; i += xStep) {
float xPos = map(i, xMin, xMax, graphX, graphX + graphWidth );
line(xPos, graphY, xPos, graphY + 8);
text(i, xPos, graphY + 25);
int numSubDivisions = 10; // Number of smaller divisions within each major division
float majorDivisionWidth = graphWidth / (float)(xMax - xMin) * xStep;
float subDivisionWidth = majorDivisionWidth / numSubDivisions;
for (int j = 0; j < numSubDivisions; j++) {
float subXPos = xPos + (j * subDivisionWidth);
if (subXPos < graphX + graphWidth ) {
line(subXPos, graphY, subXPos, graphY + 4);
}
}
}
// Write y-axis values
textAlign(RIGHT, CENTER);
fill(0);
stroke(0);
for (int i = yMin; i <= yMax; i += yStep) {
float yPos = map(i, yMin, yMax, graphY, graphY - graphHeight);
line(graphX - 8, yPos, graphX, yPos);
text(i, graphX - 12, yPos);
int numSubDivisions = 10; // Number of smaller divisions within each major division
float majorDivisionWidth = graphHeight / (float)(yMax - yMin) * yStep;
float subDivisionWidth = majorDivisionWidth / numSubDivisions;
for (int k = 0; k < numSubDivisions; k++) {
float subYPos = yPos + (k * subDivisionWidth);
if (graphY > subYPos) {
line(graphX - 4, subYPos, graphX, subYPos );
}
}
}
// Draw latest point
if (dataPoints.size() > 0) {
PVector latestPoint = dataPoints.get(dataPoints.size() - 1);
float x = map(latestPoint.x, xMin, xMax, graphX, graphX + graphWidth);
float y = map(latestPoint.y, yMin, yMax, graphY, graphY - graphHeight);
fill(255, 0, 0);
ellipse(x, y, 8, 8);
}
// Draw data points
noFill();
stroke(0, 0, 255);
for (int i = 1; i < dataPoints.size(); i++) {
PVector prevPoint = dataPoints.get(i - 1);
PVector point = dataPoints.get(i);
float prevX = map(prevPoint.x, xMin, xMax, graphX, graphX + graphWidth);
float prevY = map(prevPoint.y, yMin, yMax, graphY, graphY - graphHeight);
float x = map(point.x, xMin, xMax, graphX, graphX + graphWidth);
float y = map(point.y, yMin, yMax, graphY, graphY - graphHeight);
line(prevX, prevY, x, y);
}
}
void serialEvent(Serial port) {
String data = port.readStringUntil('\n'); // Read the received data until newline character
if (data != null) {
data = data.trim();
println("Data received: " + data);
// Parse the data string
int commaIndex = data.indexOf(',');
if (commaIndex != -1) {
String angleStr = data.substring(0, commaIndex);
String hDotStr = data.substring(commaIndex + 1);
// Convert the parsed values to floats
float angle = float(angleStr);
float h_dot = float(hDotStr);
// Create a PVector with angle and h_dot values
PVector point = new PVector(angle, h_dot);
// Add the point to the data points list
dataPoints.add(point);
// Add the data to the JTable
tableModel.addRow(new Object[]{angle, h_dot});
// Print the parsed values
println("Parsed Angle: " + angle);
println("Parsed h_dot: " + h_dot);
}
}
}
i have made the code with Jtable the table generate but both angle and velocity mechanism values repeated continuosly like scorll down a lot not stable