Skip to content

Commit

Permalink
move CSV to a central class
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Nov 5, 2019
1 parent e57bc0a commit 8cb66e1
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 61 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ shadowJar {
mainClassName = 'edu.wpi.rbe.rbe2001.fieldsimulator.gui.Main'
baseName = 'FieldControl'
classifier = null
version = '0.1.7'
version = '0.1.8'
mergeServiceFiles()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package edu.wpi.rbe.rbe2001.fieldsimulator.gui;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Date;
public class CSVManager {
long timestampLast;
ArrayList<double[]> hashMap= new ArrayList<double[]>();

public void addLine(long timestamp, double pos0, double pos1, double pos2, double vel0, double vel1, double vel2,
double hw0, double hw1, double hw2, double velsetpoint0, double velsetpoint1, double velsetpoint2,
double setpoint0, double setpoint1, double setpoint2, double Azimuth) {
if(timestampLast+50>timestamp)
return;
double[] line = new double[] { timestamp,pos0, pos1, pos2, vel0, vel1, vel2, hw0, hw1, hw2, velsetpoint0, velsetpoint1,
velsetpoint2, setpoint0, setpoint1, setpoint2,Azimuth };
hashMap.add( line);
if(hashMap.size()>100000)
writeToFile();

}

public void writeToFile() {
new Thread(()->{
File desktop = new File(System.getProperty("user.home")+"/Desktop/");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String timestamp =dateFormat.format(new Date());
System.out.println(timestamp);
String filename = desktop.getAbsolutePath()+"/Motor-Data_"+timestamp+".csv";
System.out.println(filename);
File exportFile = new File(filename);
if(!exportFile.exists())
try {
exportFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String content = "Timestamp,Current Value 1,Current Value 1,Current Value 1,Target Value,Hardware Value\n";
for(int j=0;j<hashMap.size();j++) {
double[] line=hashMap.get(j);
for(int i=0;i<line.length;i++)
content+=line[i]+",";

}
PrintWriter out;
try {
out = new PrintWriter(filename);
out.println(content);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

hashMap.clear();
}).start();
}

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package edu.wpi.rbe.rbe2001.fieldsimulator.gui;

import java.io.File;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
@SuppressWarnings("restriction")
public class GraphManager {
private ArrayList<XYChart.Series> pidGraphSeries=new ArrayList<>();
private LineChart<Double, Double> pidGraph;
private double start = ((double) System.currentTimeMillis()) / 1000.0;
private long lastPos;
private long lastSet;
private long lastHw;
private HashMap<Integer,ArrayList<Double>> posExp = new HashMap<>();
private HashMap<Integer,ArrayList<Double>> setExp = new HashMap<>();
private HashMap<Integer,ArrayList<Double>> hwExp = new HashMap<>();
private HashMap<Integer,ArrayList<Double>> timeExp = new HashMap<>();
// private HashMap<Integer,ArrayList<Double>> posExp = new HashMap<>();
// private HashMap<Integer,ArrayList<Double>> setExp = new HashMap<>();
// private HashMap<Integer,ArrayList<Double>> hwExp = new HashMap<>();
// private HashMap<Integer,ArrayList<Double>> timeExp = new HashMap<>();
private int currentIndex=0;
private int numPid=0;
public GraphManager(LineChart<Double, Double> g, int num ) {
Expand All @@ -32,10 +28,10 @@ public GraphManager(LineChart<Double, Double> g, int num ) {

pidGraphSeries.add(i, e);
pidGraph.getData().add(e);
posExp.put(i,new ArrayList<>());
setExp.put(i,new ArrayList<>());
hwExp.put(i,new ArrayList<>());
timeExp.put(i,new ArrayList<>());
// posExp.put(i,new ArrayList<>());
// setExp.put(i,new ArrayList<>());
// hwExp.put(i,new ArrayList<>());
// timeExp.put(i,new ArrayList<>());
}
pidGraph.getXAxis().autoRangingProperty().set(true);

Expand All @@ -59,16 +55,16 @@ public void updateGraph(double pos, double set, double hw) {
pidGraphSeries.get(0).getData().add(new XYChart.Data(now, pos));
pidGraphSeries.get(1).getData().add(new XYChart.Data(now, set));
pidGraphSeries.get(2).getData().add(new XYChart.Data(now , hw));
posExp.get(currentIndex).add(pos);
setExp.get(currentIndex).add(set);
hwExp.get(currentIndex).add(hw);
timeExp.get(currentIndex).add(now);
if(posExp.get(currentIndex).size()>5000) {
posExp.get(currentIndex).remove(0);
setExp.get(currentIndex).remove(0);
hwExp.get(currentIndex).remove(0);
timeExp.get(currentIndex).remove(0);
}
// posExp.get(currentIndex).add(pos);
// setExp.get(currentIndex).add(set);
// hwExp.get(currentIndex).add(hw);
// timeExp.get(currentIndex).add(now);
// if(posExp.get(currentIndex).size()>5000) {
// posExp.get(currentIndex).remove(0);
// setExp.get(currentIndex).remove(0);
// hwExp.get(currentIndex).remove(0);
// timeExp.get(currentIndex).remove(0);
// }
}
for (Series s : pidGraphSeries) {
while (s.getData().size() > 2000) {
Expand All @@ -84,35 +80,5 @@ public void clearGraph(int currentIndex) {
this.currentIndex=currentIndex;
}

public void export(String type) throws Exception {
File desktop = new File(System.getProperty("user.home")+"/Desktop/");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String timestamp =dateFormat.format(new Date());
System.out.println(timestamp);
for (int i = 0; i < numPid; i++) {
String filename = desktop.getAbsolutePath()+"/Motor-"+i+"-"+type+"_"+timestamp+".csv";
System.out.println(filename);
File exportFile = new File(filename);
if(!exportFile.exists())
exportFile.createNewFile();
String content = "Timestamp,Current Value,Target Value,Hardware Value\n";
ArrayList<Double> data = timeExp.get(i);
for(int j=0;j<data.size();j++) {
content+=timeExp.get(i).get(j)+","+
posExp.get(i).get(j)+","+
setExp.get(i).get(j)+","+
hwExp.get(i).get(j)+"\n";

}
posExp.get(i).clear();
setExp.get(i).clear();
hwExp.get(i).clear();
timeExp.get(i).clear();
PrintWriter out = new PrintWriter(filename);
out.println(content);
out.flush();
out.close();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ public class InterfaceController {
private static final int numPIDControllersOnDevice = 3;
private File lastSearchedName = new File(
System.getProperty("user.home") + "/" + "rbeFieldControllerLastSearchedRobot.txt");

private CSVManager csv;
private double Azimuth =0;
@FXML
private void initialize() {
me = this;
Expand Down Expand Up @@ -463,9 +464,10 @@ private void setFieldSim(ISimplePIDRobot r) {
gravy.setText(formatter.format(datas[base + 1]));
gravz.setText(formatter.format(datas[base + 2]));
base = 9;
Azimuth=datas[base + 2];
eulx.setText(formatter.format(datas[base + 0]));
euly.setText(formatter.format(datas[base + 1]));
eulz.setText(formatter.format(datas[base + 2]));
eulz.setText(formatter.format(Azimuth));

});
});
Expand Down Expand Up @@ -505,7 +507,13 @@ private void setFieldSim(ISimplePIDRobot r) {
;
Platform.runLater(() -> position.setText(positionVal));
Platform.runLater(() -> pidManager.updateGraph(pos, set, vel));

csv.addLine(System.currentTimeMillis(),
robot.getPidPosition(0), robot.getPidPosition(1), robot.getPidPosition(2),
robot.getVelocity(0), robot.getVelocity(1), robot.getVelocity(2),
robot.getHardwareOutput(0), robot.getHardwareOutput(1), robot.getHardwareOutput(2),
robot.getVelSetpoint(0), robot.getVelSetpoint(1), robot.getVelSetpoint(2),
robot.getPidSetpoint(0), robot.getPidSetpoint(1), robot.getPidSetpoint(2),
Azimuth);
} catch (Exception ex) {
ex.printStackTrace();
}
Expand All @@ -520,14 +528,15 @@ private void setFieldSim(ISimplePIDRobot r) {
double pos = robot.getVelocity(currentIndex);
double set = robot.getVelSetpoint(currentIndex);
double hw = robot.getHardwareOutput(currentIndex);

String positionVal = formatter.format(pos);
// System.out.println(positionVal+"");
;
Platform.runLater(() -> velocityVal.setText(positionVal));
Platform.runLater(() -> hardwareOut.setText(formatter.format(hw)));
Platform.runLater(() -> posHwValue.setText(formatter.format(hw)));
Platform.runLater(() -> velManager.updateGraph(pos, set, hw));

} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down Expand Up @@ -679,7 +688,7 @@ void stop() {
@FXML
void onPidExport() {
try {
pidManager.export("position");
csv.writeToFile();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand All @@ -689,7 +698,7 @@ void onPidExport() {
@FXML
void onVelExport() {
try {
velManager.export("velocity");
csv.writeToFile();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down

0 comments on commit 8cb66e1

Please sign in to comment.