Skip to content

Commit 8cb66e1

Browse files
committed
move CSV to a central class
1 parent e57bc0a commit 8cb66e1

4 files changed

Lines changed: 103 additions & 61 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ shadowJar {
5454
mainClassName = 'edu.wpi.rbe.rbe2001.fieldsimulator.gui.Main'
5555
baseName = 'FieldControl'
5656
classifier = null
57-
version = '0.1.7'
57+
version = '0.1.8'
5858
mergeServiceFiles()
5959
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package edu.wpi.rbe.rbe2001.fieldsimulator.gui;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.io.PrintWriter;
7+
import java.text.DateFormat;
8+
import java.text.SimpleDateFormat;
9+
import java.util.HashMap;
10+
import java.util.ArrayList;
11+
import java.util.Date;
12+
public class CSVManager {
13+
long timestampLast;
14+
ArrayList<double[]> hashMap= new ArrayList<double[]>();
15+
16+
public void addLine(long timestamp, double pos0, double pos1, double pos2, double vel0, double vel1, double vel2,
17+
double hw0, double hw1, double hw2, double velsetpoint0, double velsetpoint1, double velsetpoint2,
18+
double setpoint0, double setpoint1, double setpoint2, double Azimuth) {
19+
if(timestampLast+50>timestamp)
20+
return;
21+
double[] line = new double[] { timestamp,pos0, pos1, pos2, vel0, vel1, vel2, hw0, hw1, hw2, velsetpoint0, velsetpoint1,
22+
velsetpoint2, setpoint0, setpoint1, setpoint2,Azimuth };
23+
hashMap.add( line);
24+
if(hashMap.size()>100000)
25+
writeToFile();
26+
27+
}
28+
29+
public void writeToFile() {
30+
new Thread(()->{
31+
File desktop = new File(System.getProperty("user.home")+"/Desktop/");
32+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
33+
String timestamp =dateFormat.format(new Date());
34+
System.out.println(timestamp);
35+
String filename = desktop.getAbsolutePath()+"/Motor-Data_"+timestamp+".csv";
36+
System.out.println(filename);
37+
File exportFile = new File(filename);
38+
if(!exportFile.exists())
39+
try {
40+
exportFile.createNewFile();
41+
} catch (IOException e) {
42+
// TODO Auto-generated catch block
43+
e.printStackTrace();
44+
}
45+
String content = "Timestamp,Current Value 1,Current Value 1,Current Value 1,Target Value,Hardware Value\n";
46+
for(int j=0;j<hashMap.size();j++) {
47+
double[] line=hashMap.get(j);
48+
for(int i=0;i<line.length;i++)
49+
content+=line[i]+",";
50+
51+
}
52+
PrintWriter out;
53+
try {
54+
out = new PrintWriter(filename);
55+
out.println(content);
56+
out.flush();
57+
out.close();
58+
} catch (FileNotFoundException e) {
59+
// TODO Auto-generated catch block
60+
e.printStackTrace();
61+
}
62+
63+
hashMap.clear();
64+
}).start();
65+
}
66+
67+
}
Lines changed: 20 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
package edu.wpi.rbe.rbe2001.fieldsimulator.gui;
22

3-
import java.io.File;
4-
import java.io.PrintWriter;
5-
import java.text.DateFormat;
6-
import java.text.SimpleDateFormat;
73
import java.util.ArrayList;
8-
import java.util.Date;
94
import java.util.HashMap;
105

116
import javafx.scene.chart.LineChart;
127
import javafx.scene.chart.XYChart;
138
import javafx.scene.chart.XYChart.Series;
9+
@SuppressWarnings("restriction")
1410
public class GraphManager {
1511
private ArrayList<XYChart.Series> pidGraphSeries=new ArrayList<>();
1612
private LineChart<Double, Double> pidGraph;
1713
private double start = ((double) System.currentTimeMillis()) / 1000.0;
1814
private long lastPos;
1915
private long lastSet;
2016
private long lastHw;
21-
private HashMap<Integer,ArrayList<Double>> posExp = new HashMap<>();
22-
private HashMap<Integer,ArrayList<Double>> setExp = new HashMap<>();
23-
private HashMap<Integer,ArrayList<Double>> hwExp = new HashMap<>();
24-
private HashMap<Integer,ArrayList<Double>> timeExp = new HashMap<>();
17+
// private HashMap<Integer,ArrayList<Double>> posExp = new HashMap<>();
18+
// private HashMap<Integer,ArrayList<Double>> setExp = new HashMap<>();
19+
// private HashMap<Integer,ArrayList<Double>> hwExp = new HashMap<>();
20+
// private HashMap<Integer,ArrayList<Double>> timeExp = new HashMap<>();
2521
private int currentIndex=0;
2622
private int numPid=0;
2723
public GraphManager(LineChart<Double, Double> g, int num ) {
@@ -32,10 +28,10 @@ public GraphManager(LineChart<Double, Double> g, int num ) {
3228

3329
pidGraphSeries.add(i, e);
3430
pidGraph.getData().add(e);
35-
posExp.put(i,new ArrayList<>());
36-
setExp.put(i,new ArrayList<>());
37-
hwExp.put(i,new ArrayList<>());
38-
timeExp.put(i,new ArrayList<>());
31+
// posExp.put(i,new ArrayList<>());
32+
// setExp.put(i,new ArrayList<>());
33+
// hwExp.put(i,new ArrayList<>());
34+
// timeExp.put(i,new ArrayList<>());
3935
}
4036
pidGraph.getXAxis().autoRangingProperty().set(true);
4137

@@ -59,16 +55,16 @@ public void updateGraph(double pos, double set, double hw) {
5955
pidGraphSeries.get(0).getData().add(new XYChart.Data(now, pos));
6056
pidGraphSeries.get(1).getData().add(new XYChart.Data(now, set));
6157
pidGraphSeries.get(2).getData().add(new XYChart.Data(now , hw));
62-
posExp.get(currentIndex).add(pos);
63-
setExp.get(currentIndex).add(set);
64-
hwExp.get(currentIndex).add(hw);
65-
timeExp.get(currentIndex).add(now);
66-
if(posExp.get(currentIndex).size()>5000) {
67-
posExp.get(currentIndex).remove(0);
68-
setExp.get(currentIndex).remove(0);
69-
hwExp.get(currentIndex).remove(0);
70-
timeExp.get(currentIndex).remove(0);
71-
}
58+
// posExp.get(currentIndex).add(pos);
59+
// setExp.get(currentIndex).add(set);
60+
// hwExp.get(currentIndex).add(hw);
61+
// timeExp.get(currentIndex).add(now);
62+
// if(posExp.get(currentIndex).size()>5000) {
63+
// posExp.get(currentIndex).remove(0);
64+
// setExp.get(currentIndex).remove(0);
65+
// hwExp.get(currentIndex).remove(0);
66+
// timeExp.get(currentIndex).remove(0);
67+
// }
7268
}
7369
for (Series s : pidGraphSeries) {
7470
while (s.getData().size() > 2000) {
@@ -84,35 +80,5 @@ public void clearGraph(int currentIndex) {
8480
this.currentIndex=currentIndex;
8581
}
8682

87-
public void export(String type) throws Exception {
88-
File desktop = new File(System.getProperty("user.home")+"/Desktop/");
89-
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
90-
String timestamp =dateFormat.format(new Date());
91-
System.out.println(timestamp);
92-
for (int i = 0; i < numPid; i++) {
93-
String filename = desktop.getAbsolutePath()+"/Motor-"+i+"-"+type+"_"+timestamp+".csv";
94-
System.out.println(filename);
95-
File exportFile = new File(filename);
96-
if(!exportFile.exists())
97-
exportFile.createNewFile();
98-
String content = "Timestamp,Current Value,Target Value,Hardware Value\n";
99-
ArrayList<Double> data = timeExp.get(i);
100-
for(int j=0;j<data.size();j++) {
101-
content+=timeExp.get(i).get(j)+","+
102-
posExp.get(i).get(j)+","+
103-
setExp.get(i).get(j)+","+
104-
hwExp.get(i).get(j)+"\n";
105-
106-
}
107-
posExp.get(i).clear();
108-
setExp.get(i).clear();
109-
hwExp.get(i).clear();
110-
timeExp.get(i).clear();
111-
PrintWriter out = new PrintWriter(filename);
112-
out.println(content);
113-
out.flush();
114-
out.close();
115-
}
116-
117-
}
83+
11884
}

src/main/java/edu/wpi/rbe/rbe2001/fieldsimulator/gui/InterfaceController.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ public class InterfaceController {
218218
private static final int numPIDControllersOnDevice = 3;
219219
private File lastSearchedName = new File(
220220
System.getProperty("user.home") + "/" + "rbeFieldControllerLastSearchedRobot.txt");
221-
221+
private CSVManager csv;
222+
private double Azimuth =0;
222223
@FXML
223224
private void initialize() {
224225
me = this;
@@ -463,9 +464,10 @@ private void setFieldSim(ISimplePIDRobot r) {
463464
gravy.setText(formatter.format(datas[base + 1]));
464465
gravz.setText(formatter.format(datas[base + 2]));
465466
base = 9;
467+
Azimuth=datas[base + 2];
466468
eulx.setText(formatter.format(datas[base + 0]));
467469
euly.setText(formatter.format(datas[base + 1]));
468-
eulz.setText(formatter.format(datas[base + 2]));
470+
eulz.setText(formatter.format(Azimuth));
469471

470472
});
471473
});
@@ -505,7 +507,13 @@ private void setFieldSim(ISimplePIDRobot r) {
505507
;
506508
Platform.runLater(() -> position.setText(positionVal));
507509
Platform.runLater(() -> pidManager.updateGraph(pos, set, vel));
508-
510+
csv.addLine(System.currentTimeMillis(),
511+
robot.getPidPosition(0), robot.getPidPosition(1), robot.getPidPosition(2),
512+
robot.getVelocity(0), robot.getVelocity(1), robot.getVelocity(2),
513+
robot.getHardwareOutput(0), robot.getHardwareOutput(1), robot.getHardwareOutput(2),
514+
robot.getVelSetpoint(0), robot.getVelSetpoint(1), robot.getVelSetpoint(2),
515+
robot.getPidSetpoint(0), robot.getPidSetpoint(1), robot.getPidSetpoint(2),
516+
Azimuth);
509517
} catch (Exception ex) {
510518
ex.printStackTrace();
511519
}
@@ -520,14 +528,15 @@ private void setFieldSim(ISimplePIDRobot r) {
520528
double pos = robot.getVelocity(currentIndex);
521529
double set = robot.getVelSetpoint(currentIndex);
522530
double hw = robot.getHardwareOutput(currentIndex);
531+
523532
String positionVal = formatter.format(pos);
524533
// System.out.println(positionVal+"");
525534
;
526535
Platform.runLater(() -> velocityVal.setText(positionVal));
527536
Platform.runLater(() -> hardwareOut.setText(formatter.format(hw)));
528537
Platform.runLater(() -> posHwValue.setText(formatter.format(hw)));
529538
Platform.runLater(() -> velManager.updateGraph(pos, set, hw));
530-
539+
531540
} catch (Exception ex) {
532541
ex.printStackTrace();
533542
}
@@ -679,7 +688,7 @@ void stop() {
679688
@FXML
680689
void onPidExport() {
681690
try {
682-
pidManager.export("position");
691+
csv.writeToFile();
683692
} catch (Exception e) {
684693
// TODO Auto-generated catch block
685694
e.printStackTrace();
@@ -689,7 +698,7 @@ void onPidExport() {
689698
@FXML
690699
void onVelExport() {
691700
try {
692-
velManager.export("velocity");
701+
csv.writeToFile();
693702
} catch (Exception e) {
694703
// TODO Auto-generated catch block
695704
e.printStackTrace();

0 commit comments

Comments
 (0)