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;
public class MindQueue {
/*
Recommended Changes/Improvements
See Google Doc Notes 12/11/24
*/
LinkedList<Action> q;

View File

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

View File

@@ -1,15 +1,30 @@
import javax.swing.UIManager;
import javax.swing.*;
import CognitiveModel.ModelFiles.*;
import Visualizer.Screen;
import Visualizer.ScreenFrame;
import Visualizer.ScreenManager;
import Visualizer.visualizer;
import java.awt.*;
public class Main {
public static void main(String[] args) {
// Encapsulation (or lack thereof) Test
Model m = new Model();
testprocess1 tp1 = new testprocess1(m, null);
testprocess1 tp1 = new testprocess1();
tp1.runProcess();
// Screen s2 = new Screen(new GridLayout(1,2));
// s2.add(t3);
// s2.add(t4);
// testprocess2 tp2 = new testprocess2(m, null);
// 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;
}
public void paint(Graphics g)
{
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.green);
g2.drawLine(xBound/2, 0, xBound/2, yBound);

View File

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

View File

@@ -9,9 +9,8 @@ import java.io.IOException;
import CognitiveModel.ModelFiles.*;
public class visualizer {
public class visualizer extends Screen {
JFrame frame = new JFrame();
JPanel frameBottom = new JPanel();
JPanel controlDisplay = new JPanel();
JPanel modelDisplay = new JPanel();
@@ -27,25 +26,42 @@ public class visualizer {
JLabel two = new JLabel();
JLabel three = new JLabel();
JLabel four = new JLabel();
Timer t = new Timer(1, null);
Model m;
public visualizer(Model m){
public visualizer(Model m) {
super(new BorderLayout());
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() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception ignored){}
// try {
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
// } 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));
frame.setVisible(true);
controlDisplay.setPreferredSize(new Dimension(100,100));
@@ -136,14 +152,14 @@ public class visualizer {
ActionListener press = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(m.logPermission() == false){
if(m.logPermission() == false) {
m.startLog();
JOptionPane.showMessageDialog(frame,"Data Logging Enabled");
JOptionPane.showMessageDialog(visualizer,"Data Logging Enabled");
tool.setBackground(Color.green);
b1.setText("Data Log Enabled");
} else {
m.stopLog();
JOptionPane.showMessageDialog(frame,"Data Logging Disabled");
JOptionPane.showMessageDialog(visualizer,"Data Logging Disabled");
tool.setBackground(Color.white);
b1.setText("Data Log Disabled");
}
@@ -157,7 +173,7 @@ public class visualizer {
tool.setOrientation(1);
tool.add(b1);
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(visualizer); // Right Side
frame.add(frameBottom,BorderLayout.CENTER);
frame.add(buttonBar, BorderLayout.SOUTH);
frame.pack();
this.add(frameBottom,BorderLayout.CENTER);
this.add(buttonBar, BorderLayout.SOUTH);
// frame.pack();
}
public void updateVisualizer(Model m) throws IOException {
float[] ctrl1 = m.getCurrentControls();
one.setText(String.valueOf(ctrl1[0]));
if(ctrl1[0] >= 0 ) {
if (ctrl1[0] >= 0 ) {
one.setForeground(Color.green);
} else {
one.setForeground(Color.red);
}
two.setText(String.valueOf(ctrl1[1]));
if(ctrl1[1] >= 0 ) {
if (ctrl1[1] >= 0 ) {
two.setForeground(Color.green);
} else {
two.setForeground(Color.red);
}
three.setText(String.valueOf(ctrl1[2]));
if(ctrl1[2] >= 0 ) {
if (ctrl1[2] >= 0 ) {
three.setForeground(Color.green);
} else {
three.setForeground(Color.red);
}
four.setText(String.valueOf(ctrl1[3]));
if(ctrl1[3] >= 0 ) {
if (ctrl1[3] >= 0 ) {
four.setForeground(Color.green);
} else {
four.setForeground(Color.red);
@@ -244,7 +261,6 @@ public class visualizer {
rudderGrid.setX(ctrl1[2]);
rudderGrid.repaint();
modelQueue.setText(m.getQueue().queueToString());
modelQueue.setPreferredSize(new Dimension(mainDisplay.getWidth(),100));
@@ -253,7 +269,6 @@ public class visualizer {
modelVision.setText("We just looked at: " + m.getStoredVisionTarget() +"\n" +
"Result: " + m.getStoredVisionResult()[0]);
// yaw.setYBound(grid.getHeight());
// yaw.setYBound(grid.getHeight());
// yaw.setX(ctrl1[2]);
@@ -262,8 +277,17 @@ public class visualizer {
axis.setXBound(yokeGrid.getWidth());
axis.setYBound(yokeGrid.getHeight());
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.net.SocketException;
import CognitiveModel.ModelFiles.*;
import Visualizer.Screen;
import Visualizer.ScreenFrame;
import Visualizer.ScreenManager;
import Visualizer.visualizer;
import javax.swing.*;
public abstract class testprocess {
Model m = new Model();
public testprocess(Model inputM, XPlaneConnect xpc) {
m = inputM;
m.establishConnection(xpc);
public testprocess() {
m = new Model();
}
public void runProcess() {
try (XPlaneConnect xpc = new XPlaneConnect()) {
// Ensure connection established.
m = new Model(xpc);
m.establishConnection(xpc);
m.activateModel();
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();
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()) {
innerProcess(vis1);
innerProcess();
}
} catch (SocketException ex) {
System.out.println("Unable to set up the connection. (Error message was '" + ex.getMessage() + "'.)");
} catch (IOException ex) {
System.out.println(
"Something went wrong with one of the commands. (Error message was '" + ex.getMessage() + "'.)");
System.out.println("Something went wrong with one of the commands. (Error message was '" + ex.getMessage() + "'.)");
}
System.out.println("Exiting");
}
public void innerProcess(visualizer vis1){
public void innerProcess(){
System.out.println("Using default innerProcess");
}

View File

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

View File

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