mirror of
https://github.com/Team4388/2022NoWayHome.git
synced 2026-06-09 08:48:07 -06:00
Added table support
This commit is contained in:
@@ -17,7 +17,7 @@ public class TestMotor extends SubsystemBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testDesmos() {
|
public void testDesmos() {
|
||||||
DesmosServer.putDecimal("Position", m_testEncoder.getPosition());
|
DesmosServer.putDouble("Position", m_testEncoder.getPosition());
|
||||||
m_testMotor.set(DesmosServer.readDouble("Speed"));
|
// m_testMotor.set(DesmosServer.readDouble("Speed"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,17 @@
|
|||||||
<body>
|
<body>
|
||||||
<div id="calculator" style="width: 100wh; height: 100vh; position: absolute; top: 0; left: 0; right: 0; bottom: 0; overflow: hidden;"></div>
|
<div id="calculator" style="width: 100wh; height: 100vh; position: absolute; top: 0; left: 0; right: 0; bottom: 0; overflow: hidden;"></div>
|
||||||
<script>
|
<script>
|
||||||
|
const EXPRESSION_TYPES = {
|
||||||
|
'expression': ['integer', 'double', 'point', 'array'],
|
||||||
|
'note': ['note'],
|
||||||
|
'table': ['table']
|
||||||
|
};
|
||||||
|
|
||||||
let elt = document.getElementById('calculator');
|
let elt = document.getElementById('calculator');
|
||||||
let calculator = Desmos.GraphingCalculator(elt);
|
let calculator = Desmos.GraphingCalculator(elt);
|
||||||
// calculator.setExpression({ id: 'graph1', latex: 'y=x^2' });
|
// calculator.setExpression({ id: 'graph1', latex: 'y=x^2' });
|
||||||
|
|
||||||
let variables = [{'name': 'bob', 'value': '27'}];
|
let variables = [{'name': 'bob', 'type': 'integer', 'value': '27'}];
|
||||||
|
|
||||||
let url = prompt("Robot URL", "localhost");
|
let url = prompt("Robot URL", "localhost");
|
||||||
let port = 8000;
|
let port = 8000;
|
||||||
@@ -36,8 +42,22 @@
|
|||||||
variables = JSON.parse(this.responseText);
|
variables = JSON.parse(this.responseText);
|
||||||
|
|
||||||
for(let variable of variables) {
|
for(let variable of variables) {
|
||||||
variable['lname'] = latexName(variable['name']);
|
switch(variable['type']) {
|
||||||
calculator.setExpression({ id: variable['name'], latex: variable['lname'] + '=' + variable['value']});
|
case 'expression':
|
||||||
|
variable['lname'] = latexName(variable['name']);
|
||||||
|
calculator.setExpression({ id: variable['name'], latex: variable['lname'] + '=' + variable['value']});
|
||||||
|
break;
|
||||||
|
case 'table':
|
||||||
|
let cols = [];
|
||||||
|
for(let col of variable['value'].split('\t')) {
|
||||||
|
let latex = latexName(col[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
calculator.setExpression({ type: 'table', id: variable['name'], columns: cols});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
console.log('Invalid type : ' + variable['type']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(requestVariables, 0);
|
setTimeout(requestVariables, 0);
|
||||||
@@ -71,10 +91,14 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function regularValue(lvalue) {
|
function regularValue(lvalue) {
|
||||||
let value = lvalue.replace('\\left(', '');
|
let value = lvalue.replace('\\left(', '')
|
||||||
value = lvalue.replace('\\right)', '');
|
.replace('\\right)', '')
|
||||||
value = lvalue.replace('\\left[', '');
|
.replace('\\left[', '')
|
||||||
value = lvalue.replace('\\right]', '');
|
.replace('\\right]', '')
|
||||||
|
.replace('(', '')
|
||||||
|
.replace(')', '')
|
||||||
|
.replace('[', '')
|
||||||
|
.replace(']', '');
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ import org.opencv.core.Point;
|
|||||||
* @author Daniel McGrath
|
* @author Daniel McGrath
|
||||||
* */
|
* */
|
||||||
public class DesmosServer extends Thread {
|
public class DesmosServer extends Thread {
|
||||||
private static HashMap<String, String> desmosVariables = new HashMap<>();
|
private static HashMap<String, String[]> desmosVariables = new HashMap<>();
|
||||||
private static HashMap<String, String> readVariables = new HashMap<>();
|
private static HashMap<String, String[]> readVariables = new HashMap<>();
|
||||||
|
|
||||||
private static boolean running = false;
|
private static boolean running = false;
|
||||||
|
|
||||||
@@ -101,7 +101,6 @@ public class DesmosServer extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
readVariables(body);
|
readVariables(body);
|
||||||
|
|
||||||
sendResponse(client);
|
sendResponse(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +144,8 @@ public class DesmosServer extends Thread {
|
|||||||
for(String key : desmosVariables.keySet()) {
|
for(String key : desmosVariables.keySet()) {
|
||||||
json += "\n\t{"
|
json += "\n\t{"
|
||||||
+ "\"name\":\"" + key + "\","
|
+ "\"name\":\"" + key + "\","
|
||||||
+ "\"value\":\"" + desmosVariables.get(key) + "\""
|
+ "\"type\":\"" + desmosVariables.get(key)[0] + "\","
|
||||||
|
+ "\"value\":\"" + desmosVariables.get(key)[1] + "\""
|
||||||
+ "},";
|
+ "},";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,16 +157,26 @@ public class DesmosServer extends Thread {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interpret client request and update variables
|
||||||
|
*
|
||||||
|
* @param requestBody Client request
|
||||||
|
*/
|
||||||
public static void readVariables(String requestBody) {
|
public static void readVariables(String requestBody) {
|
||||||
for(String variable : requestBody.split("\n")) {
|
for(String variable : requestBody.split("\n")) {
|
||||||
if(variable.equals(""))
|
if(variable.equals(""))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
String[] readVar = variable.split("\t");
|
String[] readVar = variable.split("\t");
|
||||||
readVariables.put(readVar[0], readVar[1]);
|
readVariables.put(readVar[0], new String[] {readVar[1], readVar[2]});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the server is running
|
||||||
|
*
|
||||||
|
* @return The server status
|
||||||
|
*/
|
||||||
public static boolean isRunning() {
|
public static boolean isRunning() {
|
||||||
return running;
|
return running;
|
||||||
}
|
}
|
||||||
@@ -174,44 +184,44 @@ public class DesmosServer extends Thread {
|
|||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
public static void putInteger(String name, Integer value) {
|
public static void putInteger(String name, Integer value) {
|
||||||
desmosVariables.put(name, value.toString());
|
desmosVariables.put(name, new String[] {"integer", value.toString()});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putDecimal(String name, Double value) {
|
public static void putDouble(String name, Double value) {
|
||||||
desmosVariables.put(name, value.toString());
|
desmosVariables.put(name, new String[] {"double", value.toString()});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putPoint(String name, Point point) {
|
public static void putPoint(String name, Point point) {
|
||||||
desmosVariables.put(name, "(" + point.x + "," + point.y + ")");
|
desmosVariables.put(name, new String[] {"point", "(" + point.x + "," + point.y + ")"});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void putArray(String name, double... arr) {
|
public static void putArray(String name, double... arr) {
|
||||||
desmosVariables.put(name, Arrays.toString(arr).replace(" ", ""));
|
desmosVariables.put(name, new String[] {"array", Arrays.toString(arr).replace(" ", "")});
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
public static int readInteger(String name) {
|
public static int readInteger(String name) {
|
||||||
if(!readVariables.containsKey(name))
|
if(!readVariables.containsKey(name) && !readVariables.get(name)[0].equals("integer"))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return Integer.parseInt(readVariables.get(name));
|
return Integer.parseInt(readVariables.get(name)[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double readDouble(String name) {
|
public static double readDouble(String name) {
|
||||||
if(!readVariables.containsKey(name))
|
if(!readVariables.containsKey(name) && !readVariables.get(name)[0].equals("double"))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return Double.parseDouble(readVariables.get(name));
|
return Double.parseDouble(readVariables.get(name)[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Point readPoint(String name) {
|
public static Point readPoint(String name) {
|
||||||
Point point = new Point();
|
Point point = new Point();
|
||||||
|
|
||||||
if(!readVariables.containsKey(name))
|
if(!readVariables.containsKey(name) && !readVariables.get(name)[0].equals("point"))
|
||||||
return point;
|
return point;
|
||||||
|
|
||||||
String pointStr = readVariables.get(name);
|
String pointStr = readVariables.get(name)[1];
|
||||||
point.x = Double.parseDouble(pointStr.split(",")[0]);
|
point.x = Double.parseDouble(pointStr.split(",")[0]);
|
||||||
point.x = Double.parseDouble(pointStr.split(",")[1]);
|
point.x = Double.parseDouble(pointStr.split(",")[1]);
|
||||||
|
|
||||||
@@ -219,10 +229,10 @@ public class DesmosServer extends Thread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static double[] readArray(String name) {
|
public static double[] readArray(String name) {
|
||||||
if(!readVariables.containsKey(name))
|
if(!readVariables.containsKey(name) && !readVariables.get(name)[0].equals("array"))
|
||||||
return new double[0];
|
return new double[0];
|
||||||
|
|
||||||
String[] unparsed = readVariables.get(name).split(",");
|
String[] unparsed = readVariables.get(name)[1].split(",");
|
||||||
double[] arr = new double[unparsed.length];
|
double[] arr = new double[unparsed.length];
|
||||||
|
|
||||||
for(int i = 0; i < arr.length; i++)
|
for(int i = 0; i < arr.length; i++)
|
||||||
|
|||||||
Reference in New Issue
Block a user