New visualization for model queue and delay events between looking at compass and looking at airspeed

This commit is contained in:
cs-powell
2024-12-07 23:19:42 -05:00
parent 2297854c28
commit 33a4b8eb7e
6 changed files with 118 additions and 46 deletions

View File

@@ -33,7 +33,7 @@ public class MindQueue {
return q.isEmpty(); return q.isEmpty();
} }
public String printQueue() { public String queueToString() {
String queueTrace = "Next to Execute ==> "; String queueTrace = "Next to Execute ==> ";
for (Action action : q) { for (Action action : q) {
@@ -47,6 +47,7 @@ public class MindQueue {
queueTrace += "[D]"; queueTrace += "[D]";
} }
} }
queueTrace += "\n";
return queueTrace; return queueTrace;
} }

View File

@@ -13,7 +13,8 @@ public class Model {
private MindQueue q; // Queue for actions private MindQueue q; // Queue for actions
boolean modelActive; // On/Off Switch for the model execution boolean modelActive; // On/Off Switch for the model execution
XPlaneConnect xpc; // Allows connection to the simulator XPlaneConnect xpc; // Allows connection to the simulator
float[] storedVision; String storedVisionTarget;
float[] storedVisionResult;
boolean logCreated; boolean logCreated;
boolean logAllowed; boolean logAllowed;
File dataLogFile = null; File dataLogFile = null;
@@ -68,7 +69,7 @@ public class Model {
} }
public void printModelQueue() { public void printModelQueue() {
System.out.println(q.printQueue()); System.out.println(q.queueToString());
} }
public int getModelQueueLength() { public int getModelQueueLength() {
@@ -124,7 +125,8 @@ public class Model {
String dref = tempV.getTarget(); String dref = tempV.getTarget();
try { try {
returnArray = xpc.getDREF(dref); returnArray = xpc.getDREF(dref);
storedVision = returnArray.clone(); storedVisionTarget = dref;
storedVisionResult = returnArray.clone();
// System.out.println(returnArray[0]); // System.out.println(returnArray[0]);
} catch (IOException e) { } catch (IOException e) {
@@ -146,7 +148,7 @@ public class Model {
switch (motorType) { switch (motorType) {
case PITCHUP: case PITCHUP:
float[] pitchUp = { currentControls[0] + 0.01f }; float[] pitchUp = { currentControls[0] + 0.01f };
if (storedVision[0] > 80) { if (storedVisionResult[0] > 80) {
if (currentControls[0] < 0.2f) { if (currentControls[0] < 0.2f) {
System.out.println("Pitching Up"); System.out.println("Pitching Up");
xpc.sendCTRL(pitchUp); xpc.sendCTRL(pitchUp);
@@ -155,7 +157,7 @@ public class Model {
break; break;
case PITCHDOWN: case PITCHDOWN:
float[] pitchDown = { currentControls[0] - 0.01f }; float[] pitchDown = { currentControls[0] - 0.01f };
if (storedVision[0] < 80) { if (storedVisionResult[0] < 80) {
if (currentControls[0] > -0.2f) { if (currentControls[0] > -0.2f) {
// System.out.println("Sending Pitch Down"); // System.out.println("Sending Pitch Down");
System.out.println("Pitching Down"); System.out.println("Pitching Down");
@@ -179,6 +181,7 @@ public class Model {
private void handleDelayAction(Action temp) { private void handleDelayAction(Action temp) {
Delay tempD = (Delay) temp; Delay tempD = (Delay) temp;
initiateDelay(tempD.getDelay()); initiateDelay(tempD.getDelay());
storedVisionTarget = "INTERRUPTION: DELAY";
} }
public static void initiateDelay(int delay) { public static void initiateDelay(int delay) {
@@ -197,26 +200,27 @@ public class Model {
returnArray = xpc.getCTRL(0); returnArray = xpc.getCTRL(0);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
throw e; while(returnArray == null) {
System.out.println("Waiting for reconnect");
returnArray = xpc.getCTRL(0);
}
} }
return returnArray; return returnArray;
} }
public void startLog() {
public void startLog(){
logAllowed = true; logAllowed = true;
} }
public void stopLog(){ public void stopLog() {
logAllowed = false; logAllowed = false;
logCreated = false;
} }
public boolean logPermission(){ public boolean logPermission() {
return logAllowed; return logAllowed;
} }
public void logData() { public void logData() {
if(logAllowed) { if(logAllowed) {
logProcess(); logProcess();
@@ -226,15 +230,31 @@ public class Model {
private void logProcess(){ private void logProcess(){
if(!logCreated) { if(!logCreated) {
dataLogFile = new File("./dataLog/" + java.time.LocalDateTime.now()); dataLogFile = new File("./dataLog/" + java.time.LocalDateTime.now() + ".csv");
logCreated = true; logCreated = true;
} try {
float[] ctrl1 = this.getCurrentControls();
String setup = "Elevator, Roll, Yaw, Throttle, Flaps\n";
FileWriter fw = new FileWriter(dataLogFile, true);
BufferedWriter br = new BufferedWriter(fw);
br.write(setup);
br.flush();
} catch (IOException e) {
}
}
String log = null; String log = null;
try { try {
float[] ctrl1 = this.getCurrentControls(); float[] ctrl1 = this.getCurrentControls();
//Labled Format
log = String.format("[Elevator: %2f] [Roll: %2f] [Yaw: %2f] [Throttle:%2f] [Flaps: %2f] \n", log = String.format("[Elevator: %2f] [Roll: %2f] [Yaw: %2f] [Throttle:%2f] [Flaps: %2f] \n",
ctrl1[0], ctrl1[1], ctrl1[2], ctrl1[3], ctrl1[5]); ctrl1[0], ctrl1[1], ctrl1[2], ctrl1[3], ctrl1[5]);
//CSV Format
log = String.format("%2f, %2f, %2f, %2f, %2f\n",
ctrl1[0], ctrl1[1], ctrl1[2], ctrl1[3], ctrl1[5]);
FileWriter fw = new FileWriter(dataLogFile, true); FileWriter fw = new FileWriter(dataLogFile, true);
BufferedWriter br = new BufferedWriter(fw); BufferedWriter br = new BufferedWriter(fw);
br.write(log); br.write(log);
@@ -245,4 +265,13 @@ public class Model {
System.out.println("No data to log"); System.out.println("No data to log");
} }
} }
public float[] getStoredVisionResult() {
return storedVisionResult;
}
public String getStoredVisionTarget() {
return storedVisionTarget;
}
} }

View File

@@ -1,26 +1,26 @@
import javax.swing.UIManager; import javax.swing.UIManager;
//import CognitiveModel.ModelFiles.*; import CognitiveModel.ModelFiles.*;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// Encapsulation (or lack thereof) Test // Encapsulation (or lack thereof) Test
// Model m = new Model(); Model m = new Model();
// testprocess1 tp1 = new testprocess1(m, null); testprocess1 tp1 = new testprocess1(m, null);
// tp1.runProcess(); tp1.runProcess();
// testprocess2 tp2 = new testprocess2(m, null); // testprocess2 tp2 = new testprocess2(m, null);
// tp2.runProcess(); // tp2.runProcess();
for (int i = 1; i<10; i++) { // for (int i = 1; i<10; i++) {
if (i%2 == 0) { // if (i%2 == 0) {
int t = 0; // int t = 0;
} else if(i == 9) { // } else if(i == 9) {
System.out.println("Breaking now"); // System.out.println("Breaking now");
break; // break;
} // }
} // }
} }
} }

View File

@@ -5,6 +5,7 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.IOException;
import CognitiveModel.ModelFiles.*; import CognitiveModel.ModelFiles.*;
@@ -12,7 +13,11 @@ public class visualizer {
JFrame frame = new JFrame(); JFrame frame = new JFrame();
JPanel frameBottom = new JPanel(); JPanel frameBottom = new JPanel();
JPanel display = new JPanel(); JPanel controlDisplay = new JPanel();
JPanel modelDisplay = new JPanel();
JTextArea modelQueue = new JTextArea();
JTextArea modelVision = new JTextArea();
JPanel mainDisplay = new JPanel();
JPanel visualizer = new JPanel(); JPanel visualizer = new JPanel();
JPanel yokeGrid = new JPanel(); JPanel yokeGrid = new JPanel();
axis axis = new axis(0,0); axis axis = new axis(0,0);
@@ -42,8 +47,9 @@ public class visualizer {
frame.setVisible(true); frame.setVisible(true);
display.setPreferredSize(new Dimension(100,100)); controlDisplay.setPreferredSize(new Dimension(100,100));
display.setLayout(new GridLayout(2,4));
controlDisplay.setLayout(new GridLayout(2,4));
Font fontTitles = new Font("Impact", 1,40); Font fontTitles = new Font("Impact", 1,40);
Font fontData = new Font("Monospaced", 1,30); Font fontData = new Font("Monospaced", 1,30);
@@ -84,14 +90,14 @@ public class visualizer {
two.setText("Test"); two.setText("Test");
three.setText("Test"); three.setText("Test");
four.setText("Test"); four.setText("Test");
display.add(titleOne); controlDisplay.add(titleOne);
display.add(titleTwo); controlDisplay.add(titleTwo);
display.add(titleThree); controlDisplay.add(titleThree);
display.add(titleFour); controlDisplay.add(titleFour);
display.add(one); controlDisplay.add(one);
display.add(two); controlDisplay.add(two);
display.add(three); controlDisplay.add(three);
display.add(four); controlDisplay.add(four);
visualizer.setLayout(new GridLayout(2,1)); visualizer.setLayout(new GridLayout(2,1));
@@ -170,10 +176,28 @@ public class visualizer {
tool3.add(b6); tool3.add(b6);
buttonBar.add(tool2); buttonBar.add(tool2);
buttonBar.add(tool3); buttonBar.add(tool3);
modelQueue.setRows(3);
modelQueue.setPreferredSize(new Dimension(250, mainDisplay.getWidth()));
modelVision.setRows(1);
Font font = new Font("Verdana", Font.BOLD, 12);
modelVision.setFont(font);
modelVision.setBackground(Color.PINK);
modelVision.setPreferredSize(new Dimension(250, mainDisplay.getWidth()));
modelDisplay.setLayout(new GridLayout(2,1));
modelDisplay.add(modelQueue);
modelDisplay.add(modelVision);
mainDisplay.setLayout(new BorderLayout());
mainDisplay.add(controlDisplay,BorderLayout.CENTER);
mainDisplay.add(modelDisplay,BorderLayout.SOUTH);
frameBottom.add(display); // Left Side frameBottom.add(mainDisplay); // Left Side
frameBottom.add(visualizer); // Right Side frameBottom.add(visualizer); // Right Side
frame.add(frameBottom,BorderLayout.CENTER); frame.add(frameBottom,BorderLayout.CENTER);
frame.add(buttonBar, BorderLayout.SOUTH); frame.add(buttonBar, BorderLayout.SOUTH);
@@ -181,7 +205,9 @@ public class visualizer {
} }
public void updateVisualizer(float[] ctrl1){ public void updateVisualizer(Model m) throws IOException {
float[] ctrl1 = m.getCurrentControls();
one.setText(String.valueOf(ctrl1[0])); one.setText(String.valueOf(ctrl1[0]));
if(ctrl1[0] >= 0 ) { if(ctrl1[0] >= 0 ) {
one.setForeground(Color.green); one.setForeground(Color.green);
@@ -218,7 +244,15 @@ public class visualizer {
rudderGrid.setX(ctrl1[2]); rudderGrid.setX(ctrl1[2]);
rudderGrid.repaint(); rudderGrid.repaint();
modelQueue.setText(m.getQueue().queueToString());
modelQueue.setPreferredSize(new Dimension(mainDisplay.getWidth(),100));
modelVision.setPreferredSize(new Dimension(mainDisplay.getWidth(),100));
modelVision.setText("We just looked at: " + m.getStoredVisionTarget() +"\n" +
"Result: " + m.getStoredVisionResult()[0]);
// yaw.setYBound(grid.getHeight()); // yaw.setYBound(grid.getHeight());
// yaw.setYBound(grid.getHeight()); // yaw.setYBound(grid.getHeight());

View File

@@ -38,7 +38,8 @@ class yokePosition extends JComponent {
g2.setColor(Color.red); g2.setColor(Color.red);
int currX = (int)(x*xBound) + xBound/2 - width/2; int currX = (int)(x*xBound) + xBound/2 - width/2;
int currY = (int)(y*yBound) + yBound/2 - height/2; int currY = (int)(y*yBound) + yBound/2 - height/2;
System.out.println("CurrX, CurrY: " + currX +", " + currY);
// System.out.println("CurrX, CurrY: " + currX +", " + currY);
// g2.drawOval(currX, currY, width, height); // g2.drawOval(currX, currY, width, height);
g2.fillOval(currX, currY, width, height); g2.fillOval(currX, currY, width, height);

View File

@@ -12,15 +12,22 @@ public class testprocess1 extends testprocess {
} }
@Override @Override
public void innerProcess(visualizer vis1){ public void innerProcess(visualizer vis1) {
float[] array = m.next(); float[] array = m.next();
double r = Math.random();
if (m.getModelQueueLength() < 5) { if (m.getModelQueueLength() < 5) {
m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); if (r > 0.5) {
// m.createAction(ActionType.DELAY, null, 200, null);
m.createAction(ActionType.DELAY, null, 200, null);
m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/compass_heading_deg_mag");
} else {
m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot");
}
} }
// m.printModelQueue(); // m.printModelQueue();
m.logData(); m.logData();
try { try {
vis1.updateVisualizer(m.getCurrentControls()); vis1.updateVisualizer(m);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();