Hello, this is my first post. I’ve used Arduino for a couple of years now but I am new to processing. I’m trying to create a fixture that turns a stepper motor with an Arduino while simultaneously reads the torque output using a Mark10 force/torque indicator. I’ve successfully run the stepper motor in the cycle that I planned and I can read the Mark10 torque values using serialEvent. I’ve run into trouble trying to graph the values from the Mark10. I believe it has something to do with serialEvent because a stripped down version of this code allows me to plot the real-time value from the Mark10. Below is my problematic code, the stripped down code, and the correct graph created with the stripped down code, respectively. Any help is appreciated. Sorry if my code sucks; like I said I’m new to processing.
import processing.serial.*;
import grafica.*;
import cc.arduino.*;
Arduino arduino;
Table rawtable;
Table summarytable;
Serial mark10; // Create object from Serial class
int dir = 5;
int en = 6;
int stepPin = 7;
float lockingTorque = -2.0;
float maximumlockingtorque = 0;
float initialsliptorque = 0;
float finalsliptorque = 0;
float[] initialSlipTorqueArray = new float[10000];
float[] finalSlipTorqueArray = new float[10000];
float[] lockTorqueArray = new float[10000];
int index = 0;
String passfail;
boolean permission1 = true;
boolean permission2 = false;
boolean permission3 = true;
boolean waitPermission = true;
float time1;
float time2;
float waitTimer;
float clearTime;
String val; // Data received from the serial port
int reading = 0;
int cycle = 0;
float y;
int level = 1;
int step = 0; //rotational steps
int fullRotation = 400;
float faststepDelay = 3;
float slowstepDelay = 10*faststepDelay;
float lockstepDelay = 200*faststepDelay;
float pause = 1000.0;
int time;
int clearDelay = 1000;
float xval = 0;
float yval = 0;
GPlot plot1;
int nPoints = 100000;
GPointsArray points = new GPointsArray(nPoints);
void setup() {
arduino = new Arduino(this, "COM15", 57600);
mark10 = new Serial(this, "COM17", 115200);
mark10.write("?C\r");
mark10.bufferUntil('\n');
rawtable = new Table();
rawtable.addColumn("Reading");
rawtable.addColumn("Torque");
rawtable.addColumn("Raw Data");
summarytable = new Table();
summarytable.addColumn("Cycle");
summarytable.addColumn("Status");
summarytable.addColumn("Initial Slip Torque");
summarytable.addColumn("Final Slip Torque");
summarytable.addColumn("Locking Torque");
arduino.pinMode(dir, Arduino.OUTPUT);
arduino.pinMode(en, Arduino.OUTPUT);
arduino.pinMode(stepPin, Arduino.OUTPUT);
arduino.digitalWrite(dir, Arduino.HIGH);
arduino.digitalWrite(en, Arduino.LOW);
plot1 = new GPlot(this);
size(700, 700);
background (150);
noLoop();
}
void draw() {
for (int c = 0; c < 1; c++) { // Number of reliability cycles
//if (cycle < 2) { // Number of reliability cycles
cycle++;
level = 1;
index = 0;
mark10.write("Z\r");
clearTime = millis();
while (millis() - clearTime <= clearDelay) {
//waiting to Clear
}
while (level < 8) {
xval++;
yval = y;
points.add(xval, yval);
plotting();
switch(level) {
case 1: //slow half slip cycle
stepFunc(slowstepDelay);
if (step == 0.25*fullRotation) {
wait(pause);
if (waitPermission) {
initialsliptorque = max(initialSlipTorqueArray);
level++;
step = 0;
index = 0;
}
}
break;
case 2: //fast slip cycle
stepFunc(faststepDelay);
if (step == 0.5*fullRotation) {
wait(pause);
if (waitPermission) {
level++;
step = 0;
arduino.digitalWrite(dir, Arduino.LOW);
}
}
break;
case 3: // lock cycle
if ((y > lockingTorque) && waitPermission) {
stepFunc(lockstepDelay);
} else {
wait(pause);
if (waitPermission) {
maximumlockingtorque = min(lockTorqueArray);
level++;
step = 0;
arduino.digitalWrite(dir, Arduino.HIGH);
index = 0;
}
}
break;
case 4: //back to zero
if (y < 0 && waitPermission) {
stepFunc(slowstepDelay);
} else {
if (waitPermission) {
level++;
step = 0;
}
}
break;
case 5: //slow half slip cycle
stepFunc(slowstepDelay);
if (step == 0.25*fullRotation) {
wait(pause);
if (waitPermission) {
finalsliptorque = max(finalSlipTorqueArray);
level++;
step = 0;
index = 0;
}
}
break;
case 6: //fast slip cycle
stepFunc(faststepDelay);
if (step == 0.5*fullRotation) {
if (waitPermission) {
level++;
step = 0;
}
}
break;
case 7: //back to zero
if (y < 0 && waitPermission) {
stepFunc(slowstepDelay);
} else {
if (waitPermission) {
level++;
step = 0;
}
}
break;
}
}
tablesave();
summarywrite();
}
mark10.stop();
summarytablesave();
exit();
}
void serialEvent(Serial mark10) {
val = mark10.readStringUntil('\n');
// Did we actually read anything in?
if (val != null)
{ // We did
reading++;
//print(reading);
//print("\t");
//println(val);
y = float(val);
if (level == 1) {
initialSlipTorqueArray[index] = y;
index++;
} else if (level == 5) {
finalSlipTorqueArray[index] = y;
index++;
} else if (level == 3) {
lockTorqueArray[index] = y;
index++;
}
//points.add(reading, y);
//plotting();
tablewrite();
mark10.write("?C\r");
}
}
void tablewrite()
{
TableRow newRow = rawtable.addRow();
newRow.setInt("Reading", reading);
newRow.setFloat("Torque", y);
newRow.setString("Raw Data", val);
}
void summarywrite()
{
if (abs(initialsliptorque-finalsliptorque) < 0.5) {
passfail = "Pass";
} else {
passfail = "Fail";
}
TableRow newRow = summarytable.addRow();
newRow.setInt("Cycle", cycle);
newRow.setString("Status", passfail);
newRow.setFloat("Initial Slip Torque", initialsliptorque);
newRow.setFloat("Final Slip Torque", finalsliptorque);
newRow.setFloat("Locking Torque", maximumlockingtorque);
}
void tablesave()
{
String path = "";
saveTable(rawtable, path);
reading = 0;
points = new GPointsArray(nPoints);
rawtable = new Table();
rawtable.addColumn("Reading");
rawtable.addColumn("Torque");
rawtable.addColumn("Raw Data");
}
void summarytablesave() {
String loc = "";
String filetype = ".csv";
String path = loc + "\\Reliability Summary" + filetype;
saveTable(summarytable, path);
}
void wait(float waitTime) {
if (waitPermission) {
waitTimer = millis();
waitPermission = false;
}
if (millis() - waitTimer >= waitTime) {
waitPermission = true;
}
}
void stepFunc(float stepTime) {
if (waitPermission) {
if (permission1) {
arduino.digitalWrite(stepPin, Arduino.HIGH);
permission1 = false;
time1 = millis();
} else if (permission2) {
arduino.digitalWrite(stepPin, Arduino.LOW);
permission2 = false;
permission3 = false;
time2 = millis();
}
if ((millis() - time1 >= faststepDelay) && (permission3 == true)) {
permission2 = true;
}
if ((millis() - time2 >= stepTime) && (permission3 == false)) {
permission1 = true;
permission2 = false;
permission3 = true;
step++;
}
}
}
void plotting()
{
println("here");
plot1 = new GPlot(this);
plot1.setPos(0, 0);
plot1.setDim(600, 600);
plot1.getTitle().setText("Reliability Torque");
plot1.getXAxis().getAxisLabel().setText("reading");
plot1.getYAxis().getAxisLabel().setText("Torque [lbFin]");
plot1.setPointSize(2);
plot1.drawGridLines(GPlot.BOTH);
plot1.setPoints(points);
plot1.defaultDraw();
}
import processing.serial.*;
import grafica.*;
Table table;
Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
int reading = 0;
int cycle = 0;
float y;
GPlot plot1;
int nPoints = 100000;
GPointsArray points = new GPointsArray(nPoints);
void setup() {
String portName = Serial.list()[0];
myPort = new Serial(this, "COM17", 115200);
table = new Table();
table.addColumn("Reading");
table.addColumn("Torque");
size(700, 700);
background (0);
}
void draw()
{
myPort.write("?\r");
while (myPort.available() > 0)
{
// Read a string in
val = myPort.readStringUntil('\n');
// Did we actually read anything in?
if (val != null)
{ // We did
reading = reading + 1;
print(reading);
print("\t");
println(val);
y = float(val);
points.add(reading, y);
}
}
plotting();
tablewrite();
}
void plotting()
{
plot1 = new GPlot(this);
plot1.setPos(0, 0);
plot1.setDim(600, 600);
plot1.getTitle().setText("Reliability Torque");
plot1.getXAxis().getAxisLabel().setText("reading");
plot1.getYAxis().getAxisLabel().setText("Torque [lbFin]");
plot1.setPointSize(2);
plot1.drawGridLines(GPlot.BOTH);
plot1.setPoints(points);
plot1.defaultDraw();
}
void tablewrite()
{
TableRow newRow = table.addRow();
newRow.setInt("Reading", reading);
newRow.setFloat("Torque", y);
}