documents reformatted

This commit is contained in:
cs-powell
2024-11-25 13:49:01 -05:00
parent cc17797319
commit 88967419c6
12 changed files with 490 additions and 542 deletions

View File

@@ -1,41 +1,36 @@
package ModelFiles; package ModelFiles;
public abstract class Action { public abstract class Action {
Integer priority; // Priority of the Task
Integer delay; // Potential Delay before starting the task
String targetDREF;
Integer taskTime; // How long should the task take
boolean taskComplete; // Is the task completed?
ActionType actionType;
public Action(ActionType actionType, int delay) {
this.actionType = actionType;
this.delay = delay;
this.targetDREF = null;
}
Integer priority; //Priority of the Task public Action(ActionType actionType, int delay, String target) {
Integer delay; //Potential Delay before starting the task this.actionType = actionType;
String targetDREF; this.delay = delay;
Integer taskTime; //How long should the task take targetDREF = target;
boolean taskComplete; //Is the task completed? }
ActionType actionType;
public ActionType getType() {
return actionType;
}
public Integer getDelay() {
return delay;
}
public String getTarget() {
return targetDREF;
}
public Action(ActionType actionType, int delay){
this.actionType = actionType;
this.delay = delay;
this.targetDREF = null;
}
public Action(ActionType actionType, int delay, String target){
this.actionType = actionType;
this.delay = delay;
targetDREF = target;
}
public ActionType getType(){
return actionType;
}
public Integer getDelay(){
return delay;
}
public String getTarget(){
return targetDREF;
}
} }

View File

@@ -1,9 +1,7 @@
package ModelFiles; package ModelFiles;
public enum ActionType {
public enum ActionType{
VISION, VISION,
MOTOR, MOTOR,
DELAY, DELAY,
} }

View File

@@ -1,15 +1,14 @@
package ModelFiles; package ModelFiles;
// package ProjectModels.CognitiveModel; // package ProjectModels.CognitiveModel;
public class Delay extends Action { public class Delay extends Action {
public Delay(){ public Delay() {
super(ActionType.DELAY,1000); super(ActionType.DELAY, 1000);
} }
public Delay(int delay){ public Delay(int delay) {
super(ActionType.DELAY,delay); super(ActionType.DELAY, delay);
} }
} }

View File

@@ -7,16 +7,15 @@ public class MindQueue {
LinkedList<Action> q; LinkedList<Action> q;
public MindQueue () { public MindQueue() {
q = new LinkedList<Action>(); q = new LinkedList<Action>();
} }
public void push(Action e){ public void push(Action e) {
q.add(e); q.add(e);
} }
public Action removeEvent(Action e) {
public Action removeEvent(Action e){
Action temp = e; Action temp = e;
q.remove(e); q.remove(e);
return temp; return temp;
@@ -24,36 +23,35 @@ public class MindQueue {
public Action pop() { public Action pop() {
Vision v = new Vision(); Vision v = new Vision();
if(q.isEmpty()) { if (q.isEmpty()) {
return v; return v;
} }
return q.remove(); return q.remove();
} }
public boolean isEmpty(){ public boolean isEmpty() {
return q.isEmpty(); return q.isEmpty();
} }
public String printQueue(){ public String printQueue() {
String queueTrace = "Next to Execute ==> "; String queueTrace = "Next to Execute ==> ";
for (Action action : q) { for (Action action : q) {
if(action.getType() == ActionType.VISION){ if (action.getType() == ActionType.VISION) {
queueTrace += "[V]"; queueTrace += "[V]";
} }
if(action.getType() == ActionType.MOTOR){ if (action.getType() == ActionType.MOTOR) {
queueTrace += "[M]"; queueTrace += "[M]";
} }
if(action.getType() == ActionType.DELAY){ if (action.getType() == ActionType.DELAY) {
queueTrace += "[D]"; queueTrace += "[D]";
} }
} }
return queueTrace; return queueTrace;
} }
public int queueLength(){ public int queueLength() {
return q.size(); return q.size();
} }
} }

View File

@@ -1,4 +1,5 @@
package ModelFiles; package ModelFiles;
import java.io.IOException; import java.io.IOException;
// import ModelFiles.ActionType; // import ModelFiles.ActionType;
@@ -6,12 +7,12 @@ import java.io.IOException;
public class Model { 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; float[] storedVision;
//Constructors // Constructors
public Model() { public Model() {
q = new MindQueue(); q = new MindQueue();
modelActive = false; modelActive = false;
@@ -20,62 +21,61 @@ public class Model {
public Model(XPlaneConnect xpc) { public Model(XPlaneConnect xpc) {
q = new MindQueue(); q = new MindQueue();
modelActive = false; modelActive = false;
this.xpc = xpc; this.xpc = xpc;
} }
/* Getters */
/*Getters*/
public MindQueue getQueue() { public MindQueue getQueue() {
return q; return q;
} }
/*Setters*/ /* Setters */
/* /*
* Setup Methods * Setup Methods
* Printing, Empty check, Activation/Deactivation of Model, etc. * Printing, Empty check, Activation/Deactivation of Model, etc.
*/ */
public void activateModel() { public void activateModel() {
modelActive = true; modelActive = true;
} }
public void establishConnection(XPlaneConnect newXPC){ public void establishConnection(XPlaneConnect newXPC) {
this.xpc = newXPC; this.xpc = newXPC;
} }
public void deactivateModel(){ public void deactivateModel() {
modelActive = false; modelActive = false;
} }
public boolean isEmpty(){ public boolean isEmpty() {
return q.isEmpty(); return q.isEmpty();
} }
public void push(Action a){ public void push(Action a) {
q.push(a); q.push(a);
} }
public boolean isActive(){ public boolean isActive() {
return modelActive; return modelActive;
} }
public void printModelQueue(){ public void printModelQueue() {
System.out.println(q.printQueue()); System.out.println(q.printQueue());
} }
public int getModelQueueLength(){ public int getModelQueueLength() {
return q.queueLength(); return q.queueLength();
} }
public void createAction(ActionType actionType, MotorType motorType, int delay,String target) { public void createAction(ActionType actionType, MotorType motorType, int delay, String target) {
Action newAction = null; Action newAction = null;
switch(actionType) { switch (actionType) {
case VISION: case VISION:
newAction = new Vision(delay,target); newAction = new Vision(delay, target);
this.push(newAction); this.push(newAction);
break; break;
case MOTOR: case MOTOR:
newAction = new Motor(motorType,delay,target); newAction = new Motor(motorType, delay, target);
this.push(newAction); this.push(newAction);
break; break;
@@ -83,13 +83,13 @@ public class Model {
newAction = new Delay(delay); newAction = new Delay(delay);
this.push(newAction); this.push(newAction);
break; break;
} }
} }
/* /*
* Processing Methods * Processing Methods
* *
*/ */
/* /*
* Process the next event in the model queue * Process the next event in the model queue
@@ -99,30 +99,30 @@ public class Model {
Action temp = q.pop(); Action temp = q.pop();
// System.out.println("Type of Action: " + temp.getType()); // System.out.println("Type of Action: " + temp.getType());
if(temp.getType() == ActionType.VISION){ // Vision Action (Get Data) if (temp.getType() == ActionType.VISION) { // Vision Action (Get Data)
handelVisionAction(temp, returnArray); handelVisionAction(temp, returnArray);
} else if (temp.getType() == ActionType.MOTOR){ //Motor Action (Act Upon Data) } else if (temp.getType() == ActionType.MOTOR) { // Motor Action (Act Upon Data)
handleMotorAction(temp); handleMotorAction(temp);
} else if (temp.getType() == ActionType.DELAY){//Pure Delays (Do nothing) } else if (temp.getType() == ActionType.DELAY) {// Pure Delays (Do nothing)
handleDelayAction(temp); handleDelayAction(temp);
} }
// q.push(temp); // q.push(temp);
return returnArray; return returnArray;
} }
/* Next Helpers */ /* Next Helpers */
private void handelVisionAction(Action temp, float[] returnArray) { private void handelVisionAction(Action temp, float[] returnArray) {
Vision tempV = (Vision) temp; Vision tempV = (Vision) temp;
initiateDelay(tempV.getDelay()); initiateDelay(tempV.getDelay());
String dref = tempV.getTarget(); String dref = tempV.getTarget();
try { try {
returnArray = xpc.getDREF(dref); returnArray = xpc.getDREF(dref);
storedVision = returnArray.clone(); storedVision = returnArray.clone();
// System.out.println(returnArray[0]); // System.out.println(returnArray[0]);
} catch (IOException e) { } catch (IOException e) {
} }
} }
private void handleMotorAction(Action temp) { private void handleMotorAction(Action temp) {
@@ -135,50 +135,51 @@ public class Model {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
try { try {
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 (storedVision[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);
} }
} }
break; break;
case PITCHDOWN: case PITCHDOWN:
float[] pitchDown = {currentControls[0] - 0.01f}; float[] pitchDown = { currentControls[0] - 0.01f };
if(storedVision[0] < 80) { if (storedVision[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");
xpc.sendCTRL(pitchDown); xpc.sendCTRL(pitchDown);
} }
} }
break; break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
private void handleDelayAction(Action temp){ private void handleDelayAction(Action temp) {
Delay tempD = (Delay) temp; Delay tempD = (Delay) temp;
initiateDelay(tempD.getDelay()); initiateDelay(tempD.getDelay());
} }
public static void initiateDelay(int delay){ public static void initiateDelay(int delay) {
try { try {
Thread.sleep(delay); // Using the action's Built in Delay Thread.sleep(delay); // Using the action's Built in Delay
} catch (InterruptedException e) { } catch (InterruptedException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@@ -1,6 +1,6 @@
package ModelFiles; package ModelFiles;
import java.io.IOException;
import java.io.IOException;
public class Motor extends Action { public class Motor extends Action {
@@ -8,41 +8,37 @@ public class Motor extends Action {
MotorType motorType; MotorType motorType;
public Motor(MotorType motorType) { public Motor(MotorType motorType) {
super(ActionType.MOTOR,1000); super(ActionType.MOTOR, 1000);
this.motorType = motorType; this.motorType = motorType;
} }
// public Motor(int delay) { // public Motor(int delay) {
// super(ActionType.MOTOR,delay); // super(ActionType.MOTOR,delay);
// } // }
public Motor(MotorType motorType, int delay, String target) {
super(ActionType.MOTOR, delay, target);
this.motorType = motorType;
}
public Motor(MotorType motorType,int delay, String target) { public void createMotorControl() { // TODO: Maybe takes in a formatted set of vision inputs?
super(ActionType.MOTOR,delay,target); control = null;
this.motorType = motorType; }
}
public MotorType getMotorType() {
return motorType;
}
public void createMotorControl(){ // TODO: Maybe takes in a formatted set of vision inputs? public void sendMotorControl(XPlaneConnect xpc) {
control = null;
}
if (control != null) {
public MotorType getMotorType(){ try {
return motorType; xpc.sendCTRL(control);
} } catch (IOException e) {
// TODO Auto-generated catch block
public void sendMotorControl(XPlaneConnect xpc) { System.out.println("Control failed to send");
}
if(control != null) {
try {
xpc.sendCTRL(control);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Control failed to send");
} }
} }
}
} }

View File

@@ -1,6 +1,6 @@
package ModelFiles; package ModelFiles;
public enum MotorType{ public enum MotorType {
PITCHUP, PITCHUP,
PITCHDOWN PITCHDOWN
} }

View File

@@ -1,10 +1,9 @@
package ModelFiles; package ModelFiles;
public class Process { public class Process {
public Process() {
public Process(){
} }
} }

View File

@@ -1,4 +1,5 @@
package ModelFiles; package ModelFiles;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -14,5 +15,5 @@ public class Tests {
m.push(a); m.push(a);
assertEquals(i, m.pop()); assertEquals(i, m.pop());
} }
} }

View File

@@ -2,21 +2,20 @@ package ModelFiles;
public class Vision extends Action { public class Vision extends Action {
public Vision() {
public Vision(){ super(ActionType.VISION, 1000);
super(ActionType.VISION,1000);
} }
public Vision(int delay){ public Vision(int delay) {
super(ActionType.VISION,delay); super(ActionType.VISION, delay);
} }
public Vision(String dref){ public Vision(String dref) {
super(ActionType.VISION,1000,dref); super(ActionType.VISION, 1000, dref);
} }
public Vision(int delay, String dref){ public Vision(int delay, String dref) {
super(ActionType.VISION,delay, dref); super(ActionType.VISION, delay, dref);
} }
} }

View File

@@ -25,7 +25,7 @@ import ModelFiles.XPlaneConnect;
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();
MindQueue q = m.getQueue(); MindQueue q = m.getQueue();
Action a = new Vision(); Action a = new Vision();
@@ -34,46 +34,43 @@ public class Main {
MindQueue q2 = m.getQueue(); MindQueue q2 = m.getQueue();
System.out.println(q2.pop()); System.out.println(q2.pop());
//Using actions // Using actions
try(XPlaneConnect xpc = new XPlaneConnect()) { try (XPlaneConnect xpc = new XPlaneConnect()) {
Action b = new Vision(10,"sim/cockpit2/gauges/indicators/airspeed_kts_pilot"); Action b = new Vision(10, "sim/cockpit2/gauges/indicators/airspeed_kts_pilot");
Action d = new Delay(20); Action d = new Delay(20);
// Ensure connection established. // Ensure connection established.
m = new Model(xpc); m = new Model(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.push(b); // m.push(b);
// m.push(d); // m.push(d);
m.printModelQueue(); m.printModelQueue();
// m.deactivateModel(); // m.deactivateModel();
while(m.isActive() && !m.isEmpty()) { while (m.isActive() && !m.isEmpty()) {
float[] array = m.next(); float[] array = m.next();
if(array != null) { if (array != null) {
// System.out.println(array[0]); // System.out.println(array[0]);
// if(array[0] > 80.0f) { // if(array[0] > 80.0f) {
// m.push(d); // m.push(d);
// } // }
} else { } else {
// System.out.println("no dice"); // System.out.println("no dice");
// System.out.println(xpc.getDREF(null)"); // System.out.println(xpc.getDREF(null)");
} }
if(m.getModelQueueLength() < 5){ if (m.getModelQueueLength() < 5) {
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.createAction(ActionType.MOTOR, MotorType.PITCHDOWN, 0, null); m.createAction(ActionType.MOTOR, MotorType.PITCHDOWN, 0, null);
} }
// m.printModelQueue(); // m.printModelQueue();
} }
} } 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(
{ "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"); System.out.println("Exiting");
} }