Skip to content

Commit c04238a

Browse files
committed
Updating to the start of C19
1 parent 8c6d0fa commit c04238a

File tree

7 files changed

+699
-152
lines changed

7 files changed

+699
-152
lines changed

src/.DS_Store

-8 KB
Binary file not shown.

src/main/.DS_Store

-6 KB
Binary file not shown.

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

Lines changed: 282 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,89 @@
11
package edu.wpi.rbe.rbe2001.fieldsimulator.gui;
22

3-
import edu.wpi.rbe.rbe2001.fieldsimulator.robot.WarehouseRobot;
3+
import java.text.DecimalFormat;
4+
import java.util.ArrayList;
5+
import edu.wpi.rbe.rbe2001.fieldsimulator.robot.RBE2001Robot;
46
import edu.wpi.rbe.rbe2001.fieldsimulator.robot.WarehouseRobotStatus;
57
import javafx.application.Platform;
68
import javafx.collections.FXCollections;
79
import javafx.collections.ObservableList;
810
import javafx.fxml.FXML;
11+
import javafx.scene.chart.LineChart;
912
import javafx.scene.control.Button;
1013
import javafx.scene.control.ChoiceBox;
14+
import javafx.scene.control.Label;
1115
import javafx.scene.control.RadioButton;
16+
import javafx.scene.control.Tab;
1217
import javafx.scene.control.TextArea;
1318
import javafx.scene.control.TextField;
19+
import javafx.scene.chart.XYChart;
20+
import javafx.scene.chart.XYChart.Series;
1421

1522
public class InterfaceController {
1623

17-
private static WarehouseRobot fieldSim;
24+
@FXML
25+
private Tab connectTab;
26+
27+
@FXML
28+
private TextField teamName;
1829

19-
private WarehouseRobotStatus status = WarehouseRobotStatus.Fault_E_Stop_pressed;
20-
private ObservableList<String> weights = FXCollections.observableArrayList("Aluminum", "Plastic");
21-
private ObservableList<String> sides = FXCollections.observableArrayList("25", "45");
22-
private ObservableList<String> pos = FXCollections.observableArrayList("1", "2");
2330
@FXML
2431
private Button connectToDevice;
2532

2633
@FXML
27-
private Button start;
34+
private Label robotName;
35+
36+
@FXML
37+
private Tab pidVelTab;
38+
@FXML
39+
private Tab tab2001Field;
40+
@FXML
41+
private TextField kpVel;
42+
43+
@FXML
44+
private TextField kdVel;
45+
46+
@FXML
47+
private Button pidConstUpdateVelocity;
48+
49+
@FXML
50+
private ChoiceBox<?> pidChannelVelocity;
51+
52+
@FXML
53+
private TextField setpointVelocity;
54+
55+
@FXML
56+
private Button setSetpointVelocity;
57+
58+
@FXML
59+
private Label velocityVal;
60+
61+
@FXML
62+
private Tab pidTab;
63+
64+
@FXML
65+
private TextField kp;
66+
67+
@FXML
68+
private TextField ki;
69+
70+
@FXML
71+
private TextField kd;
72+
73+
@FXML
74+
private Button pidConstUpdate;
75+
76+
@FXML
77+
private TextField setpoint;
78+
79+
@FXML
80+
private Button setSetpoint;
81+
82+
@FXML
83+
private Label position;
84+
85+
@FXML
86+
private ChoiceBox<Integer> pidChannel;
2887

2988
@FXML
3089
private Button stop;
@@ -46,28 +105,73 @@ public class InterfaceController {
46105

47106
@FXML
48107
private ChoiceBox<String> choiceBoxPos;
49-
@FXML
50-
private TextField teamName;
108+
51109
@FXML
52110
private Button approveButton;
53111

112+
@FXML // fx:id="setDuration"
113+
private TextField setDuration; // Value injected by FXMLLoader
114+
115+
@FXML // fx:id="setType"
116+
private ChoiceBox<String> setType; // Value injected by FXMLLoader
117+
118+
@FXML
119+
private LineChart<Double, Double> pidGraph;
120+
@FXML
121+
private LineChart<Double, Double> pidGraphVel;
122+
private ArrayList<XYChart.Series> pidGraphSeriesVel = new ArrayList<>();
123+
private ArrayList<XYChart.Series> pidGraphSeries = new ArrayList<>();
124+
private WarehouseRobotStatus status = WarehouseRobotStatus.Fault_E_Stop_pressed;
125+
private ObservableList<String> weights = FXCollections.observableArrayList("Aluminum", "Plastic");
126+
private ObservableList<String> sides = FXCollections.observableArrayList("25", "45");
127+
private ObservableList<String> pos = FXCollections.observableArrayList("1", "2");
128+
private double pidConfig[] = null;
129+
130+
private DecimalFormat formatter = new DecimalFormat();
131+
private double start = ((double) System.currentTimeMillis()) / 1000.0;
132+
private long lastPos;
133+
private long lastSet;
134+
static InterfaceController me;
135+
private static RBE2001Robot fieldSim;
136+
private int numPIDControllers = -1;
137+
private int currentIndex = 0;
138+
private static final int numPIDControllersOnDevice = 3;
139+
54140
@FXML
55141
private void initialize() {
56-
assert connectToDevice != null : "fx:id=\"connectToDevice\" was not injected: check your FXML file 'MainScreen"
57-
+ ".fxml'.";
58-
assert start != null : "fx:id=\"start\" was not injected: check your FXML file 'MainScreen.fxml'.";
59-
assert stop != null : "fx:id=\"stop\" was not injected: check your FXML file 'MainScreen.fxml'.";
142+
me = this;
143+
formatter.setMaximumFractionDigits(6);
144+
145+
assert connectTab != null : "fx:id=\"connectTab\" was not injected: check your FXML file 'MainScreen.fxml'.";
60146
assert teamName != null : "fx:id=\"teamName\" was not injected: check your FXML file 'MainScreen.fxml'.";
61-
assert response != null : "fx:id=\"response\" was not injected: check your FXML file 'MainScreen.fxml'.";
62-
assert send != null : "fx:id=\"send\" was not injected: check your FXML file 'MainScreen.fxml'.";
63-
assert heartBeat != null : "fx:id=\"heartBeat\" was not injected: check your FXML file 'MainScreen.fxml'.";
64-
assert choiceBoxWeight != null : "fx:id=\"choiceBoxWeight\" was not injected: check your FXML file 'MainScreen"
65-
+ ".fxml'.";
66-
assert choiceBoxSide != null : "fx:id=\"choiceBoxSide\" was not injected: check your FXML file 'MainScreen"
67-
+ ".fxml'.";
68-
assert choiceBoxPos != null : "fx:id=\"choiceBoxPos\" was not injected: check your FXML file 'MainScreen"
69-
+ ".fxml'.";
147+
assert connectToDevice != null : "fx:id=\"connectToDevice\" was not injected: check your FXML file 'MainScreen.fxml'.";
148+
149+
assert pidTab != null : "fx:id=\"pidTab\" was not injected: check your FXML file 'MainScreen.fxml'.";
150+
assert pidGraph != null : "fx:id=\"pidGraph\" was not injected: check your FXML file 'MainScreen.fxml'.";
151+
assert kp != null : "fx:id=\"kp\" was not injected: check your FXML file 'MainScreen.fxml'.";
152+
assert ki != null : "fx:id=\"ki\" was not injected: check your FXML file 'MainScreen.fxml'.";
153+
assert kd != null : "fx:id=\"kd\" was not injected: check your FXML file 'MainScreen.fxml'.";
154+
assert pidConstUpdate != null : "fx:id=\"pidConstUpdate\" was not injected: check your FXML file 'MainScreen.fxml'.";
155+
assert pidChannel != null : "fx:id=\"pidChannel\" was not injected: check your FXML file 'MainScreen.fxml'.";
156+
assert setpoint != null : "fx:id=\"setpoint\" was not injected: check your FXML file 'MainScreen.fxml'.";
157+
assert setSetpoint != null : "fx:id=\"setSetpoint\" was not injected: check your FXML file 'MainScreen.fxml'.";
158+
assert position != null : "fx:id=\"position\" was not injected: check your FXML file 'MainScreen.fxml'.";
159+
teamName.setText("IMU-Team21");
70160

161+
for (int i = 0; i < numPIDControllersOnDevice; i++) {
162+
Series e = new XYChart.Series();
163+
164+
pidGraphSeries.add(i, e);
165+
pidGraph.getData().add(e);
166+
}
167+
pidGraphVel.getXAxis().autoRangingProperty().set(true);
168+
for (int i = 0; i < numPIDControllersOnDevice; i++) {
169+
Series e = new XYChart.Series();
170+
171+
pidGraphSeriesVel.add(i, e);
172+
pidGraphVel.getData().add(e);
173+
}
174+
pidGraphVel.getXAxis().autoRangingProperty().set(true);
71175
choiceBoxWeight.setValue(weights.get(0));
72176
choiceBoxWeight.setItems(weights);
73177
choiceBoxSide.setValue("25");
@@ -77,7 +181,6 @@ private void initialize() {
77181

78182
choiceBoxWeight.getSelectionModel().select(weights.get(0));
79183

80-
start.setDisable(true);
81184
stop.setDisable(true);
82185
// PLE.setDisable(true);
83186
// RHE.setDisable(true);
@@ -90,7 +193,26 @@ private void connectToDevice() {
90193
connectToDevice.setDisable(true);
91194
new Thread(() -> {
92195
try {
93-
setFieldSim(WarehouseRobot.get(teamName.getText()).get(0));
196+
setFieldSim(RBE2001Robot.get(teamName.getText()).get(0));
197+
Thread.sleep(1000);
198+
// getFieldSim().setReadTimeout(1000);
199+
if (getRobot() != null) {
200+
Platform.runLater(() -> {
201+
robotName.setText(getRobot().getName());
202+
pidTab.setDisable(false);
203+
//pidVelTab.setDisable(false);
204+
tab2001Field.setDisable(false);
205+
});
206+
}
207+
} catch (Exception ex) {
208+
// ex.printStackTrace();
209+
Platform.runLater(() -> robotName.setText(teamName.getText() + " Not Found!"));
210+
}
211+
if (getRobot() == null) {
212+
Platform.runLater(() -> connectToDevice.setDisable(false));
213+
}
214+
215+
try {
94216
// getFieldSim().setReadTimeout(1000);
95217
if (getRobot() != null) {
96218
getRobot().addEvent(2012, () -> {
@@ -114,7 +236,6 @@ private void connectToDevice() {
114236
});
115237

116238
Platform.runLater(() -> {
117-
start.setDisable(false);
118239
stop.setDisable(false);
119240
// PLE.setDisable(false);
120241
// RHE.setDisable(false);
@@ -132,6 +253,142 @@ private void connectToDevice() {
132253
}
133254
}
134255

256+
@FXML
257+
void onConnect() {
258+
System.out.println("onConnect");
259+
connectToDevice();
260+
}
261+
262+
@FXML
263+
void onSetGains() {
264+
double kpv = Double.parseDouble(kp.getText());
265+
double kiv = Double.parseDouble(ki.getText());
266+
double kdv = Double.parseDouble(kd.getText());
267+
for (int i = 0; i < numPIDControllers; i++)
268+
fieldSim.setPidGains(i, kpv, kiv, kdv);
269+
}
270+
271+
@FXML
272+
void onSetSetpoint() {
273+
fieldSim.setPidSetpoint(Integer.parseInt(setDuration.getText()),
274+
setType.getSelectionModel().getSelectedItem().equals("LIN") ? 0 : 1,
275+
currentIndex,
276+
Double.parseDouble(setpoint.getText()));
277+
278+
}
279+
280+
public RBE2001Robot getRobot() {
281+
return fieldSim;
282+
}
283+
284+
private void setFieldSim(RBE2001Robot fieldSim) {
285+
fieldSim.setReadTimeout(1000);
286+
InterfaceController.fieldSim = fieldSim;
287+
288+
fieldSim.addEvent(1910, () -> {
289+
try {
290+
291+
if (numPIDControllers != fieldSim.getMyNumPid()) {
292+
numPIDControllers = fieldSim.getMyNumPid();
293+
setUpPid();
294+
}
295+
double pos = fieldSim.getPidPosition(currentIndex);
296+
double set = fieldSim.getPidSetpoint(currentIndex);
297+
298+
String positionVal = formatter.format(pos);
299+
// System.out.println(positionVal+"
300+
// "+DoubleStream.of(piddata).boxed().collect(Collectors.toCollection(ArrayList::new)));
301+
;
302+
Platform.runLater(() -> position.setText(positionVal));
303+
Platform.runLater(() -> updateGraph(pos, set));
304+
305+
} catch (Exception ex) {
306+
ex.printStackTrace();
307+
}
308+
});
309+
310+
fieldSim.addEvent(1857, () -> {
311+
try {
312+
if (pidConfig == null)
313+
pidConfig = new double[3 * numPIDControllersOnDevice];
314+
fieldSim.readFloats(1857, pidConfig);
315+
316+
// System.out.println("
317+
// "+DoubleStream.of(pidConfig).boxed().collect(Collectors.toCollection(ArrayList::new)));
318+
319+
Platform.runLater(() -> kp.setText(formatter.format(pidConfig[currentIndex * 3 + 0])));
320+
321+
Platform.runLater(() -> ki.setText(formatter.format(pidConfig[currentIndex * 3 + 1])));
322+
323+
Platform.runLater(() -> kd.setText(formatter.format(pidConfig[currentIndex * 3 + 2])));
324+
325+
} catch (Exception ex) {
326+
ex.printStackTrace();
327+
}
328+
});
329+
fieldSim.updatConfig();
330+
331+
}
332+
333+
@SuppressWarnings("unchecked")
334+
private void updateGraph(double pos, double set) {
335+
if (pidGraphSeries.size() == 0)
336+
return;
337+
double now = ((double) System.currentTimeMillis()) / 1000.0 - start;
338+
long thispos = (long) pos;
339+
long thisSet = (long) set;
340+
if (thispos != lastPos || thisSet != lastSet) {
341+
pidGraphSeries.get(0).getData().add(new XYChart.Data(now - 0.0001, lastPos));
342+
pidGraphSeries.get(1).getData().add(new XYChart.Data(now - 0.0001, lastSet));
343+
lastSet = thisSet;
344+
lastPos = thispos;
345+
pidGraphSeries.get(0).getData().add(new XYChart.Data(now, pos));
346+
pidGraphSeries.get(1).getData().add(new XYChart.Data(now, set));
347+
}
348+
for (Series s : pidGraphSeries) {
349+
while (s.getData().size() > 200) {
350+
s.getData().remove(0);
351+
}
352+
}
353+
}
354+
355+
private void setUpPid() {
356+
System.out.println("PID controller has " + fieldSim.getNumPid() + " controllers");
357+
if (fieldSim.getNumPid() > 0) {
358+
for (int i = 0; i < fieldSim.getNumPid(); i++) {
359+
int index = i;
360+
Platform.runLater(() -> pidChannel.getItems().add(index));
361+
}
362+
pidChannel.getSelectionModel().selectedIndexProperty().addListener((obs, old, newVal) -> {
363+
System.out.println("Set to channel " + newVal);
364+
currentIndex = newVal.intValue();
365+
fieldSim.updatConfig();
366+
for (Series s : pidGraphSeries) {
367+
s.getData().clear();
368+
369+
}
370+
});
371+
Platform.runLater(() -> pidChannel.setValue(0));
372+
Platform.runLater(() -> setType.getItems().add("LIN"));
373+
Platform.runLater(() -> setType.getItems().add("SIN"));
374+
Platform.runLater(() -> setType.setValue("LIN"));
375+
}
376+
}
377+
378+
public static void disconnect() {
379+
if (me.getRobot() != null)
380+
me.getRobot().disconnect();
381+
}
382+
383+
@FXML
384+
void onSetVelocity() {
385+
386+
}
387+
388+
@FXML
389+
void onSetGainsVelocity() {
390+
391+
}
135392
@FXML
136393
void onApprove() {
137394
System.out.println("approve");
@@ -172,18 +429,6 @@ void stop() {
172429
}
173430
}
174431

175-
@FXML
176-
void onConnect() {
177-
System.out.println("onConnect");
178-
connectToDevice();
179-
}
180432

181-
public static WarehouseRobot getRobot() {
182-
return fieldSim;
183-
}
184433

185-
private static void setFieldSim(WarehouseRobot fieldSim) {
186-
fieldSim.setReadTimeout(1000);
187-
InterfaceController.fieldSim = fieldSim;
188-
}
189434
}

0 commit comments

Comments
 (0)