Somehow fixed the problem with the timer activating too soon by changing the timer start to when it is in the "inner process" of the main method

This commit is contained in:
cs-powell
2024-12-17 14:18:53 -05:00
parent 33a4b8eb7e
commit 0eafd83c5b
15 changed files with 267 additions and 75 deletions

View File

@@ -4,6 +4,10 @@ package CognitiveModel.ModelFiles;
import java.util.LinkedList; import java.util.LinkedList;
public class MindQueue { public class MindQueue {
/*
Recommended Changes/Improvements
See Google Doc Notes 12/11/24
*/
LinkedList<Action> q; LinkedList<Action> q;

View File

@@ -134,6 +134,11 @@ public class Model {
} }
private void handleMotorAction(Action temp) { private void handleMotorAction(Action temp) {
/*
TODO: GoogleDoc Notes 12/11/24
*/
Motor tempM = (Motor) temp; Motor tempM = (Motor) temp;
MotorType motorType = tempM.getMotorType(); MotorType motorType = tempM.getMotorType();
float[] currentControls = null; float[] currentControls = null;
@@ -228,7 +233,7 @@ public class Model {
} }
private void logProcess(){ private void logProcess() {
if(!logCreated) { if(!logCreated) {
dataLogFile = new File("./dataLog/" + java.time.LocalDateTime.now() + ".csv"); dataLogFile = new File("./dataLog/" + java.time.LocalDateTime.now() + ".csv");
logCreated = true; logCreated = true;
@@ -246,15 +251,12 @@ public class Model {
String log = null; String log = null;
try { try {
float[] ctrl1 = this.getCurrentControls(); float[] ctrl1 = this.getCurrentControls();
//Labeled Format
//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 //CSV Format
log = String.format("%2f, %2f, %2f, %2f, %2f\n", log = String.format("%2f, %2f, %2f, %2f, %2f\n",
ctrl1[0], ctrl1[1], ctrl1[2], ctrl1[3], ctrl1[5]); 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);
@@ -266,7 +268,6 @@ public class Model {
} }
} }
public float[] getStoredVisionResult() { public float[] getStoredVisionResult() {
return storedVisionResult; return storedVisionResult;
} }
@@ -274,4 +275,9 @@ public class Model {
public String getStoredVisionTarget() { public String getStoredVisionTarget() {
return storedVisionTarget; return storedVisionTarget;
} }
public XPlaneConnect exportXPC() {
return xpc;
}
} }

View File

@@ -1,15 +1,30 @@
import javax.swing.UIManager; import javax.swing.*;
import CognitiveModel.ModelFiles.*; import CognitiveModel.ModelFiles.*;
import Visualizer.Screen;
import Visualizer.ScreenFrame;
import Visualizer.ScreenManager;
import Visualizer.visualizer;
import java.awt.*;
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(); testprocess1 tp1 = new testprocess1();
testprocess1 tp1 = new testprocess1(m, null);
tp1.runProcess(); tp1.runProcess();
// Screen s2 = new Screen(new GridLayout(1,2));
// s2.add(t3);
// s2.add(t4);
// testprocess2 tp2 = new testprocess2(m, null); // testprocess2 tp2 = new testprocess2(m, null);
// tp2.runProcess(); // tp2.runProcess();

View File

@@ -0,0 +1,7 @@
Project Log:
TODO: Backfill from Google doc project log; create automated prompt.
2024-12-17: {
}

View File

@@ -0,0 +1,11 @@
package Visualizer;
import javax.swing.*;
import java.awt.*;
public class Screen extends JPanel {
public Screen(LayoutManager l) {
this.setLayout(l);
this.setVisible(true);
}
}

View File

@@ -0,0 +1,20 @@
package Visualizer;
import javax.script.ScriptEngine;
import javax.swing.*;
import java.awt.*;
public class ScreenFrame extends JFrame {
ScreenManager sm;
public ScreenFrame(ScreenManager sm, Dimension d) {
this.sm = sm;
this.setPreferredSize(d);
}
public void initialize() {
this.setVisible(true);
this.setFocusable(true);
this.add(sm);
this.pack();
}
}

View File

@@ -0,0 +1,80 @@
package Visualizer;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ScreenManager extends JPanel {
JPanel display = new JPanel(new BorderLayout());
Screen[] screens = new Screen[2];
ScreenType currentScreen;
JButton toggle = new JButton("Cycle Screens");
public ScreenManager(Screen start, Screen data) {
super(new BorderLayout());
this.setVisible(true);
screens[0] = start;
screens[1] = data;
toggle.setVisible(true);
toggle.addActionListener(e -> {
if (currentScreen == ScreenType.START) {
switchScreens(ScreenType.DATA);
} else {
switchScreens(ScreenType.DATA2);
}
// if (currentScreen == ScreenType.DATA2) {
// switchScreens(ScreenType.DATA);
// }
});
this.add(toggle, BorderLayout.WEST);
currentScreen = ScreenType.START;
display.add(screens[0]);
this.add(display, BorderLayout.CENTER);
display.setBackground(Color.black);
}
public void switchScreens(ScreenType st) {
display.removeAll();
switch(st) {
case START:
System.out.println("Switch to start");
display.add(screens[0],BorderLayout.CENTER);
currentScreen = ScreenType.START;
display.repaint();
revalidate();
break;
case DATA:
System.out.println("Switch to data");
display.add(screens[1],BorderLayout.CENTER);
currentScreen = ScreenType.DATA;
display.repaint();
revalidate();
break;
case DATA2:
System.out.println("Switch to data 2");
display.add(((visualizer)screens[1]).exportDisplay(),BorderLayout.CENTER);
currentScreen = ScreenType.DATA2;
display.repaint();
revalidate();
break;
}
}
}

View File

@@ -0,0 +1,7 @@
package Visualizer;
public enum ScreenType {
START,
DATA,
DATA2
}

View File

@@ -0,0 +1,15 @@
package Visualizer;
import javax.swing.*;
public class StartScreen extends JPanel {
public StartScreen() {
JLabel l = new JLabel();
l.setText("Hello");
l.setVisible(true);
this.add(l);
}
}

View File

@@ -16,8 +16,7 @@ public class axis extends JComponent {
yBound = currentY; yBound = currentY;
} }
public void paint(Graphics g) public void paint(Graphics g) {
{
Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.green); g2.setColor(Color.green);
g2.drawLine(xBound/2, 0, xBound/2, yBound); g2.drawLine(xBound/2, 0, xBound/2, yBound);

View File

@@ -5,17 +5,13 @@ import java.awt.Graphics;
import java.awt.*; import java.awt.*;
public class rudderPosition extends JComponent { public class rudderPosition extends JComponent {
int xBound = 0; int xBound = 0;
int yBound = 0; int yBound = 0;
float rudderTravel = 0; float rudderTravel = 0;
rudderPosition(int currentXBound, int currentYBound) { rudderPosition(int currentXBound, int currentYBound) {
xBound = currentXBound; xBound = currentXBound;
yBound = currentYBound; yBound = currentYBound;
} }
public void paint(Graphics g) { public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g;
int spacer = 10; int spacer = 10;
@@ -23,9 +19,9 @@ public class rudderPosition extends JComponent {
g2.fillRect(0, spacer, xBound, yBound - spacer); g2.fillRect(0, spacer, xBound, yBound - spacer);
g2.setColor(Color.red); g2.setColor(Color.red);
int width = (int) (xBound/2 * rudderTravel); int width = (int) (xBound / 2 * rudderTravel);
//g2.drawRect(xBound/2, 0 +spacer, width, yBound); //g2.drawRect(xBound/2, 0 +spacer, width, yBound);
g2.fillRect(xBound/2, 0 + spacer, width, yBound); g2.fillRect(xBound / 2, 0 + spacer, width, yBound);
g.drawString("Yaw Left",0, yBound/2); g.drawString("Yaw Left",0, yBound/2);
int textWidth = g.getFontMetrics().stringWidth("Roll Right"); int textWidth = g.getFontMetrics().stringWidth("Roll Right");

View File

@@ -9,9 +9,8 @@ import java.io.IOException;
import CognitiveModel.ModelFiles.*; import CognitiveModel.ModelFiles.*;
public class visualizer { public class visualizer extends Screen {
JFrame frame = new JFrame();
JPanel frameBottom = new JPanel(); JPanel frameBottom = new JPanel();
JPanel controlDisplay = new JPanel(); JPanel controlDisplay = new JPanel();
JPanel modelDisplay = new JPanel(); JPanel modelDisplay = new JPanel();
@@ -27,25 +26,42 @@ public class visualizer {
JLabel two = new JLabel(); JLabel two = new JLabel();
JLabel three = new JLabel(); JLabel three = new JLabel();
JLabel four = new JLabel(); JLabel four = new JLabel();
Timer t = new Timer(1, null);
Model m; Model m;
public visualizer(Model m) {
super(new BorderLayout());
public visualizer(Model m){
this.m = m; this.m = m;
t.addActionListener(e -> {
try {
if (m.exportXPC().getCTRL(0) != null) { // Check to Make sure the XPC is actually receiving data
m.next();
updateVisualizer(m);
} else {
System.out.print("XPC not returning data yet");
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
initializeDisplay();
} }
public void initializeDisplay() { public void initializeDisplay() {
try { // try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); // UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception ignored){} // } catch(Exception ignored){
//
// }
this.setPreferredSize(new Dimension(1700,270));
this.setVisible(true);
// frame.setPreferredSize(new Dimension(1700,270));
// frame.setLayout(new BorderLayout());
// frame.setVisible(true);
frame.setPreferredSize(new Dimension(1700,270));
frame.setLayout(new BorderLayout());
frameBottom.setLayout(new GridLayout(1,2)); frameBottom.setLayout(new GridLayout(1,2));
frame.setVisible(true);
controlDisplay.setPreferredSize(new Dimension(100,100)); controlDisplay.setPreferredSize(new Dimension(100,100));
@@ -136,14 +152,14 @@ public class visualizer {
ActionListener press = new ActionListener() { ActionListener press = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if(m.logPermission() == false){ if(m.logPermission() == false) {
m.startLog(); m.startLog();
JOptionPane.showMessageDialog(frame,"Data Logging Enabled"); JOptionPane.showMessageDialog(visualizer,"Data Logging Enabled");
tool.setBackground(Color.green); tool.setBackground(Color.green);
b1.setText("Data Log Enabled"); b1.setText("Data Log Enabled");
} else { } else {
m.stopLog(); m.stopLog();
JOptionPane.showMessageDialog(frame,"Data Logging Disabled"); JOptionPane.showMessageDialog(visualizer,"Data Logging Disabled");
tool.setBackground(Color.white); tool.setBackground(Color.white);
b1.setText("Data Log Disabled"); b1.setText("Data Log Disabled");
} }
@@ -157,7 +173,7 @@ public class visualizer {
tool.setOrientation(1); tool.setOrientation(1);
tool.add(b1); tool.add(b1);
tool.add(b2); tool.add(b2);
frame.add(tool,BorderLayout.WEST); this.add(tool,BorderLayout.WEST);
@@ -199,35 +215,36 @@ public class visualizer {
frameBottom.add(mainDisplay); // Left Side frameBottom.add(mainDisplay); // Left Side
frameBottom.add(visualizer); // Right Side frameBottom.add(visualizer); // Right Side
frame.add(frameBottom,BorderLayout.CENTER); this.add(frameBottom,BorderLayout.CENTER);
frame.add(buttonBar, BorderLayout.SOUTH); this.add(buttonBar, BorderLayout.SOUTH);
frame.pack(); // frame.pack();
} }
public void updateVisualizer(Model m) throws IOException { public void updateVisualizer(Model m) throws IOException {
float[] ctrl1 = m.getCurrentControls(); 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);
} else { } else {
one.setForeground(Color.red); one.setForeground(Color.red);
} }
two.setText(String.valueOf(ctrl1[1])); two.setText(String.valueOf(ctrl1[1]));
if(ctrl1[1] >= 0 ) { if (ctrl1[1] >= 0 ) {
two.setForeground(Color.green); two.setForeground(Color.green);
} else { } else {
two.setForeground(Color.red); two.setForeground(Color.red);
} }
three.setText(String.valueOf(ctrl1[2])); three.setText(String.valueOf(ctrl1[2]));
if(ctrl1[2] >= 0 ) { if (ctrl1[2] >= 0 ) {
three.setForeground(Color.green); three.setForeground(Color.green);
} else { } else {
three.setForeground(Color.red); three.setForeground(Color.red);
} }
four.setText(String.valueOf(ctrl1[3])); four.setText(String.valueOf(ctrl1[3]));
if(ctrl1[3] >= 0 ) { if (ctrl1[3] >= 0 ) {
four.setForeground(Color.green); four.setForeground(Color.green);
} else { } else {
four.setForeground(Color.red); four.setForeground(Color.red);
@@ -244,7 +261,6 @@ public class visualizer {
rudderGrid.setX(ctrl1[2]); rudderGrid.setX(ctrl1[2]);
rudderGrid.repaint(); rudderGrid.repaint();
modelQueue.setText(m.getQueue().queueToString()); modelQueue.setText(m.getQueue().queueToString());
modelQueue.setPreferredSize(new Dimension(mainDisplay.getWidth(),100)); modelQueue.setPreferredSize(new Dimension(mainDisplay.getWidth(),100));
@@ -253,7 +269,6 @@ public class visualizer {
modelVision.setText("We just looked at: " + m.getStoredVisionTarget() +"\n" + modelVision.setText("We just looked at: " + m.getStoredVisionTarget() +"\n" +
"Result: " + m.getStoredVisionResult()[0]); "Result: " + m.getStoredVisionResult()[0]);
// yaw.setYBound(grid.getHeight()); // yaw.setYBound(grid.getHeight());
// yaw.setYBound(grid.getHeight()); // yaw.setYBound(grid.getHeight());
// yaw.setX(ctrl1[2]); // yaw.setX(ctrl1[2]);
@@ -262,8 +277,17 @@ public class visualizer {
axis.setXBound(yokeGrid.getWidth()); axis.setXBound(yokeGrid.getWidth());
axis.setYBound(yokeGrid.getHeight()); axis.setYBound(yokeGrid.getHeight());
axis.repaint(); axis.repaint();
JButton j = new JButton();
}
public JPanel exportDisplay() {
return visualizer;
} }
public Timer exportTimer() {
return t;
}
} }

View File

@@ -1,48 +1,58 @@
import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.net.SocketException; import java.net.SocketException;
import CognitiveModel.ModelFiles.*; import CognitiveModel.ModelFiles.*;
import Visualizer.Screen;
import Visualizer.ScreenFrame;
import Visualizer.ScreenManager;
import Visualizer.visualizer; import Visualizer.visualizer;
import javax.swing.*;
public abstract class testprocess { public abstract class testprocess {
Model m = new Model(); Model m = new Model();
public testprocess(Model inputM, XPlaneConnect xpc) { public testprocess() {
m = inputM; m = new Model();
m.establishConnection(xpc);
} }
public void runProcess() { public void runProcess() {
try (XPlaneConnect xpc = new XPlaneConnect()) { try (XPlaneConnect xpc = new XPlaneConnect()) {
// Ensure connection established. // Ensure connection established.
m = new Model(xpc); m.establishConnection(xpc);
m.activateModel(); m.activateModel();
m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot");
m.createAction(ActionType.MOTOR, MotorType.PITCHUP, 0, null); //m.createAction(ActionType.MOTOR, MotorType.PITCHUP, 0, null);
m.printModelQueue(); m.printModelQueue();
visualizer vis1 = new visualizer(m); visualizer vis1 = new visualizer(m);
vis1.initializeDisplay(); Timer timer1 = vis1.exportTimer();
JLabel t1 = new JLabel("t1");
JLabel t2 = new JLabel("t2");
Screen s1 = new Screen(new GridLayout(1,2));
s1.add(t1);
s1.add(t2);
ScreenManager sm = new ScreenManager(s1,vis1);
Dimension d = new Dimension(500,500);
ScreenFrame f = new ScreenFrame(sm,d);
f.initialize();
timer1.start();
while (m.isActive() && !m.isEmpty()) { while (m.isActive() && !m.isEmpty()) {
innerProcess(vis1); innerProcess();
} }
} catch (SocketException ex) { } catch (SocketException ex) {
System.out.println("Unable to set up the connection. (Error message was '" + ex.getMessage() + "'.)"); System.out.println("Unable to set up the connection. (Error message was '" + ex.getMessage() + "'.)");
} catch (IOException ex) { } catch (IOException ex) {
System.out.println( System.out.println("Something went wrong with one of the commands. (Error message was '" + ex.getMessage() + "'.)");
"Something went wrong with one of the commands. (Error message was '" + ex.getMessage() + "'.)");
} }
System.out.println("Exiting"); System.out.println("Exiting");
} }
public void innerProcess(visualizer vis1){ public void innerProcess(){
System.out.println("Using default innerProcess"); System.out.println("Using default innerProcess");
} }

View File

@@ -6,31 +6,29 @@ import Visualizer.visualizer;
public class testprocess1 extends testprocess { public class testprocess1 extends testprocess {
public testprocess1(Model inputM, XPlaneConnect xpc) { public testprocess1() {
super(inputM, xpc); super();
} }
@Override @Override
public void innerProcess(visualizer vis1) { public void innerProcess() {
float[] array = m.next();
double r = Math.random(); double r = Math.random();
if (m.getModelQueueLength() < 5) { if (m.getModelQueueLength() < 5) {
if (r > 0.5) { if (r > 0.5) {
// m.createAction(ActionType.DELAY, null, 200, null); // m.createAction(ActionType.DELAY, null, 200, null);
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"); m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot");
} else { } else {
m.createAction(ActionType.VISION, null, 0, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); 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); // vis1.updateVisualizer(m);
} catch (IOException e) { // } catch (IOException e) {
// TODO Auto-generated catch block // // TODO Auto-generated catch block
e.printStackTrace(); // e.printStackTrace();
} // }
} }
} }

View File

@@ -5,7 +5,7 @@ import CognitiveModel.ModelFiles.*;
public class testprocess2 extends testprocess { public class testprocess2 extends testprocess {
public testprocess2(Model m, XPlaneConnect xpc){ public testprocess2(Model m, XPlaneConnect xpc){
super(m, xpc); super();
} }