Skip to content

Commit

Permalink
Merge pull request #2 from pacoito123/untested
Browse files Browse the repository at this point in the history
2019-08-19 Merge
  • Loading branch information
pacoito123 authored Aug 19, 2019
2 parents d8757f9 + 3b0ab24 commit ec2bd97
Show file tree
Hide file tree
Showing 18 changed files with 549 additions and 681 deletions.
11 changes: 8 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ dependencies {

// Use JUnit test framework
testImplementation 'junit:junit:4.12'

// https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.0.pr1'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.0.pr1'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.10.0.pr1'


//Phoenix dependencies.
compile group: 'com.ctre.phoenix', name: 'api-java', version: '5.14.1'
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/org/usfirst/lib6647/oi/ControllerProfiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.usfirst.lib6647.oi;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.usfirst.lib6647.subsystem.RobotMap;

/**
* Class holding instances of objects required to read values from a JSON file,
* for {@link JController} usage.
*/
public class ControllerProfiles {

private static ControllerProfiles m_instance = null;

/**
* Creates static {@link ControllerProfiles} instance.
*/
public static void createInstance(String filePath) {
m_instance = new ControllerProfiles(filePath);
}

/**
* Gets static {@link ControllerProfiles} instance.
*
* @return static {@link ControllerProfiles} instance
*/
public static ControllerProfiles getInstance() {
return m_instance;
}

/** JSON {@link ObjectMapper} for {@link JController JControllers}. */
private final ObjectMapper mapper;
/** Path to JSON file. */
private final String filePath;

/**
* Constructor for {@link RobotMap}. Initializes {@link #filePath} and
* {@link #mapper}.
*
* @param filePath
*/
public ControllerProfiles(String filePath) {
this.filePath = filePath;
mapper = new ObjectMapper();
}

/**
* Get {@link ObjectMapper} instance.
*
* @return mapper
*/
public ObjectMapper getMapper() {
return mapper;
}

/**
* Get {@link #filePath} for JSON file.
*
* @return filePath
*/
public String getFilePath() {
return filePath;
}
}
60 changes: 17 additions & 43 deletions src/main/java/org/usfirst/lib6647/oi/JController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.usfirst.lib6647.oi;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Optional;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.fasterxml.jackson.databind.JsonNode;

import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.Joystick;
Expand All @@ -32,23 +28,28 @@ public class JController extends Joystick {
private int leftAxis = 1, rightAxis = 5;

/**
* Location of the JSON file for {@link Button} nicknames.
* {@link JSONNode} for usage of friendly button names with JSON.
*/
private String filePath;
private JsonNode profile;

/**
* Constructor for {@link JController}.
*
* Initializes each and every {@link Button} from the {@link Joystick} found at
* the given port. Also initializes {@link Button Buttons} for each of the axes
* and POVs.
* and POVs. Also initializes {@link #profile} {@link JsonNode} if possible.
*
* @param port
*/
public JController(int port, String filePath) {
public JController(int port) {
super(port);

this.filePath = filePath;
try (Reader file = new FileReader(ControllerProfiles.getInstance().getFilePath())) {
profile = ControllerProfiles.getInstance().getMapper().readTree(file).get(getName());
} catch (Exception e) {
System.out.println(
"[!] COULD NOT INITIALIZE CONTROLLER PROFILE FOR CONTROLLER '" + getName().toUpperCase() + "'.");
}

// Button initialization. Starting at 1.
for (int i = 1; i <= this.getButtonCount(); i++) {
Expand Down Expand Up @@ -102,40 +103,13 @@ public double getRightAxis() {

/**
* Method for getting a {@link Button} with a friendly name (declared in the
* JSON configuration) from this {@link JController}. Returns an empty Optional
* if no {@link Button} is found at that specific key, or if any exception is
* thrown.
* JSON configuration) from this {@link JController}.
*
* @param joystickName
* @param buttonName
* @return {@link Button}
*/
public Optional<Button> get(String buttonName) {
try {
// Create a new JSONParser and JSONObject with the given key.
JSONParser parser = new JSONParser();
Reader file = new FileReader(filePath);
JSONObject jsonJoystick = (JSONObject) ((JSONObject) parser.parse(file)).get(getName());

// Create Button object and initialize it with values from the JSONObject.
Button button = buttons.get(jsonJoystick.get(buttonName).toString());

// Clear JSONObject and JSONParser after use, and close Reader. Not sure if it
// does anything, but it might free some unused memory.
jsonJoystick.clear();
file.close();
parser.reset();

// Finally, return Optional of Button object from the friendly name.
return Optional.of(button);
} catch (IOException e) {
System.out.println("[!] OIBUTTON " + buttonName + " IO ERROR: " + e.getMessage());
} catch (ParseException e) {
System.out.println("[!] OIBUTTON " + buttonName + " PARSE ERROR: " + e.getMessage());
} catch (Exception e) {
System.out.println("[!] OIBUTTON " + buttonName + " ERROR: " + e.getMessage());
}
return Optional.empty();
public Button get(String buttonName) {
return buttons.get(profile.get(buttonName).asText());
}

/**
Expand Down Expand Up @@ -209,7 +183,7 @@ public boolean get() {
}

/**
* Method for getting an axisButton input for any value, with 0.15 as tolerance
* Method for getting an axisButton input for any value, with 0.30 as tolerance
* (so as to avoid accidental input due to improper {@link JController}
* calibration).
*
Expand All @@ -227,7 +201,7 @@ public boolean get() {
}

/**
* Method for getting a negative axisButton input, with 0.15 as tolerance (to
* Method for getting a negative axisButton input, with -0.30 as tolerance (to
* avoid accidental input due to improper {@link JController} calibration).
*
* @param controller
Expand All @@ -244,7 +218,7 @@ public boolean get() {
}

/**
* Method for getting a positive axisButton input, with 0.15 as tolerance (to
* Method for getting a positive axisButton input, with 0.30 as tolerance (to
* avoid accidental input due to improper {@link JController} calibration).
*
* @param controller
Expand Down
72 changes: 23 additions & 49 deletions src/main/java/org/usfirst/lib6647/subsystem/PIDSuperSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.io.FileReader;
import java.io.Reader;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.fasterxml.jackson.databind.JsonNode;

import edu.wpi.first.wpilibj.command.PIDSubsystem;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
Expand All @@ -18,8 +17,8 @@ public abstract class PIDSuperSubsystem extends PIDSubsystem {
/**
* Bread and butter of {@link PIDSuperSubsystem}.
*/
protected JSONObject robotMap;
protected float p = 0.0f, i = 0.0f, d = 0.0f;
protected JsonNode robotMap;
protected double p = 0.0, i = 0.0, d = 0.0;
protected double pidOutput;

/**
Expand All @@ -30,29 +29,18 @@ public abstract class PIDSuperSubsystem extends PIDSubsystem {
* @param name (of the {@link PIDSubsystem})
* @param fileName (to {@link #robotMap JSON file})
*/
public PIDSuperSubsystem(String name, String fileName) {
super(name, 0.0f, 0.0f, 0.0f);
public PIDSuperSubsystem(String name) {
super(name, 0.0, 0.0, 0.0);

initJSON(fileName);
initPID();
outputPIDValues(getName(), p, i, d);
}

/**
* Method to initialize {@link #robotMap} at the given path.
*
* @param fileName (to {@link #robotMap JSON file})
*/
private void initJSON(String fileName) {
try {
JSONParser parser = new JSONParser();
Reader file = new FileReader(fileName);
robotMap = (JSONObject) parser.parse(file);
file.close();
try (Reader file = new FileReader(RobotMap.getInstance().getFilePath())) {
robotMap = RobotMap.getInstance().getMapper().readTree(file).get(getName());
} catch (Exception e) {
System.out.println("[!] SUBSYSTEM '" + getName().toUpperCase() + "' JSON INIT ERROR: " + e.getMessage());
System.exit(1);
}

initPID();
outputPIDValues(getName(), p, i, d);
}

/**
Expand All @@ -61,40 +49,26 @@ private void initJSON(String fileName) {
*/
private void initPID() {
try {
// Create a JSONObject out of the 'pid' key.
JSONObject pid = (JSONObject) ((JSONObject) ((JSONObject) robotMap.get("subsystems")).get(getName()))
.get("pid");
// Get JsonNode out of the 'pid' key.
JsonNode pid = robotMap.get("pid");

// Update current PID values.
p = Float.parseFloat(pid.get("p").toString());
i = Float.parseFloat(pid.get("i").toString());
d = Float.parseFloat(pid.get("d").toString());
p = pid.get("p").asDouble();
i = pid.get("i").asDouble();
d = pid.get("d").asDouble();

// Update PIDSubsystem PID values and configuration.
getPIDController().setPID(p, i, d);
setInputRange(Double.parseDouble(pid.get("inputMin").toString()),
Double.parseDouble(pid.get("inputMax").toString()));
setOutputRange(Double.parseDouble(pid.get("outputMin").toString()),
Double.parseDouble(pid.get("outputMax").toString()));
setAbsoluteTolerance(Double.parseDouble(pid.get("absoluteTolerance").toString()));
getPIDController().setContinuous(Boolean.parseBoolean(pid.get("continuous").toString()));

// Clear JSONObject after use, not sure if it does anything, but it might free
// some unused memory.
pid.clear();
setInputRange(pid.get("inputMin").asDouble(), pid.get("inputMax").asDouble());
setOutputRange(pid.get("outputMin").asDouble(), pid.get("outputMax").asDouble());
setAbsoluteTolerance(pid.get("absoluteTolerance").asDouble());
getPIDController().setContinuous(pid.get("continuous").asBoolean());
} catch (Exception e) {
System.out.println("[!] SUBSYSTEM '" + getName().toUpperCase() + "' PID INIT ERROR: " + e.getMessage());
System.exit(1);
}
}

/**
* Method to clear {@link #robotMap}.
*/
public void finishedJSONInit() {
robotMap.clear();
}

/**
* Method to output {@link #p}, {@link #i}, and {@link #d} values from the
* {@link PIDSuperSubsystem} to the {@link SmartDashboard}.
Expand All @@ -104,7 +78,7 @@ public void finishedJSONInit() {
* @param {@link #i}
* @param {@link #d}
*/
private void outputPIDValues(String subsystemName, float p, float i, float d) {
private void outputPIDValues(String subsystemName, double p, double i, double d) {
SmartDashboard.putString(subsystemName + "P", p + "");
SmartDashboard.putString(subsystemName + "I", i + "");
SmartDashboard.putString(subsystemName + "D", d + "");
Expand All @@ -115,9 +89,9 @@ private void outputPIDValues(String subsystemName, float p, float i, float d) {
* the {@link SmartDashboard}.
*/
public void updatePIDValues() {
p = Float.parseFloat(SmartDashboard.getString(getName() + "P", p + ""));
i = Float.parseFloat(SmartDashboard.getString(getName() + "I", i + ""));
d = Float.parseFloat(SmartDashboard.getString(getName() + "D", d + ""));
p = Double.parseDouble(SmartDashboard.getString(getName() + "P", p + ""));
i = Double.parseDouble(SmartDashboard.getString(getName() + "I", i + ""));
d = Double.parseDouble(SmartDashboard.getString(getName() + "D", d + ""));

getPIDController().setPID(p, i, d);
}
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/org/usfirst/lib6647/subsystem/RobotMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.usfirst.lib6647.subsystem;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Class holding instances of objects required to read values from a JSON file,
* for {@link SuperSubsystem} usage.
*/
public class RobotMap {

private static RobotMap m_instance = null;

/**
* Creates static {@link RobotMap} instance.
*/
public static void createInstance(String filePath) {
m_instance = new RobotMap(filePath);
}

/**
* Gets static {@link RobotMap} instance.
*
* @return static {@link RobotMap} instance
*/
public static RobotMap getInstance() {
return m_instance;
}

/** JSON {@link ObjectMapper} for {@link SuperSubsystem SuperSubsystems}. */
private final ObjectMapper mapper;
/** Path to JSON file. */
private final String filePath;

/**
* Constructor for {@link RobotMap}. Initializes {@link #filePath} and
* {@link #mapper}.
*
* @param filePath
*/
public RobotMap(String filePath) {
this.filePath = filePath;
mapper = new ObjectMapper();
}

/**
* Get {@link ObjectMapper} instance.
*
* @return mapper
*/
public ObjectMapper getMapper() {
return mapper;
}

/**
* Get {@link #filePath} for JSON file.
*
* @return filePath
*/
public String getFilePath() {
return filePath;
}
}
Loading

0 comments on commit ec2bd97

Please sign in to comment.