Refactoring getVal, putVal into odrive_utils.js

This commit is contained in:
PAJohnson
2020-09-23 19:00:10 -04:00
parent bcd627fae0
commit c140afc7aa
15 changed files with 133 additions and 424 deletions
+2 -17
View File
@@ -10,7 +10,7 @@
</template>
<script>
const axios = require("axios");
import { putVal } from "../../odrive_utils.js";
export default {
name: "Action",
@@ -40,24 +40,9 @@ export default {
this.$store.commit("setActionVal", {dashID: this.dashID, actionID: this.id, val: this.value});
},
putVal: function () {
var params = new URLSearchParams();
let keys = this.path.split(".");
keys.shift();
for (const key of keys) {
params.append("key", key);
}
params.append("val", this.value);
params.append("type", "number");
console.log(params.toString());
let request = {
params: params,
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
putVal(keys.join('.'), this.value);
},
deleteAction: function() {
// commit a mutation to remove this action from the dashboard
+3 -18
View File
@@ -5,7 +5,8 @@
</template>
<script>
const axios = require("axios");
import { putVal } from "../odrive_utils.js";
export default {
name: "clearErrors.vue",
props: {
@@ -23,23 +24,7 @@ export default {
"odrive0." + this.data.axis + ".controller.error",
];
for (const path of paths) {
var params = new URLSearchParams();
let keys = path.split(".");
for (const key of keys) {
params.append("key", key);
}
params.append("val", 0);
params.append("type", "number");
console.log(params.toString());
let request = {
params: params,
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
putVal(path, 0);
}
},
},
+5 -28
View File
@@ -16,7 +16,7 @@
</template>
<script>
const axios = require("axios");
import { getVal, getReadonly, putVal } from "../../odrive_utils.js";
export default {
name: "CtrlBoolean",
@@ -30,11 +30,7 @@ export default {
value: function () {
let keys = this.path.split(".");
keys.shift(); // don't need first key here
let odriveObj = this.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return odriveObj["val"];
return getVal(keys.join('.'));
},
name: function () {
let keys = this.path.split(".");
@@ -44,33 +40,14 @@ export default {
writeAccess: function () {
let keys = this.path.split(".");
keys.shift(); // don't need first key here
let odriveObj = this.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return odriveObj["readonly"] == false;
return getReadonly(keys.join('.')) == false;
},
},
methods: {
putVal: function (e) {
console.log(e.target.checked);
var params = new URLSearchParams();
let keys = this.path.split(".");
let keys = this.path.split('.');
keys.shift();
for (const key of keys) {
params.append("key", key);
}
params.append("val", e.target.checked);
params.append("type", "boolean");
console.log(params.toString());
let request = {
params: params,
};
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
putVal(keys.join('.'), e.target.checked);
},
deleteCtrl: function() {
// commit a mutation in the store with the relevant information
+3 -35
View File
@@ -6,7 +6,7 @@
</template>
<script>
const axios = require("axios");
import { callFcn } from "../../odrive_utils.js";
export default {
name: "CtrlFunction",
@@ -24,43 +24,11 @@ export default {
}
},
methods: {
putVal: function(e) {
var params = new URLSearchParams();
let keys = this.path.split(".");
keys.shift();
for (const key of keys) {
params.append("key", key);
}
params.append("val", e.target.value);
console.log(params.toString());
let request = {
params: params
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
},
executeFunction: function(e) {
executeFunction: function() {
//execute this function on the odrive
console.log(e);
var params = new URLSearchParams();
let keys = this.path.split(".");
keys.shift();
for (const key of keys) {
params.append("key", key);
}
console.log(params.toString());
let request = {
params: params
};
axios.put(
this.$store.state.odriveServerAddress + "/api/function",
null,
request
);
callFcn(keys.join('.'));
},
deleteCtrl: function() {
// commit a mutation in the store with the relevant information
+7 -30
View File
@@ -10,7 +10,7 @@
</template>
<script>
const axios = require("axios");
import { getVal, getReadonly, putVal } from "../../odrive_utils.js";
export default {
name: "CtrlNumeric",
@@ -22,13 +22,9 @@ export default {
},
computed: {
value: function () {
let keys = this.path.split(".");
keys.shift(); // don't need first key here
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return parseFloat(odriveObj["val"]).toFixed(3);
let keys = this.path.split('.');
keys.shift();
return parseFloat(getVal(keys.join('.'))).toFixed(3);
},
name: function () {
let keys = this.path.split(".");
@@ -38,33 +34,14 @@ export default {
writeAccess: function () {
let keys = this.path.split(".");
keys.shift(); // don't need first key here
let odriveObj = this.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return odriveObj["readonly"] === false;
return getReadonly(keys.join('.')) === false;
},
},
methods: {
putVal: function (e) {
var params = new URLSearchParams();
let keys = this.path.split(".");
let keys = this.path.split('.');
keys.shift();
for (const key of keys) {
params.append("key", key);
}
params.append("val", e.target.value);
params.append("type", "number");
console.log(params.toString());
let request = {
params: params,
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
putVal(keys.join('.'), parseFloat(e.target.value));
},
deleteCtrl: function() {
// commit a mutation in the store with the relevant information
+4 -33
View File
@@ -16,7 +16,7 @@
<script>
import VueSlider from "vue-slider-component";
import "vue-slider-component/theme/default.css";
const axios = require("axios");
import { getVal, putVal } from "../../odrive_utils.js";
export default {
name: "CtrlSlider",
@@ -43,15 +43,6 @@ export default {
keys.shift();
return keys.join(".");
},
writeAccess: function () {
let keys = this.path.split(".");
keys.shift(); // don't need first key here
let odriveObj = this.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return odriveObj["readonly"] === false;
},
interval: function () {
return (this.max - this.min) / 100;
},
@@ -61,27 +52,11 @@ export default {
}
},
methods: {
putVal: function (value, index) {
putVal: function (value) {
console.log(value);
console.log(index);
var params = new URLSearchParams();
let keys = this.path.split(".");
keys.shift();
for (const key of keys) {
params.append("key", key);
}
params.append("val", value);
params.append("type", "number");
console.log(params.toString());
let request = {
params: params,
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
putVal(keys.join('.'), value);
},
setMin: function (e) {
this.min = parseFloat(e.target.value);
@@ -100,11 +75,7 @@ export default {
let initVal = () => {
let keys = this.path.split(".");
keys.shift(); // don't need first key here
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return parseFloat(odriveObj["val"]);
return parseFloat(getVal(keys.join('.'))).toFixed(3);
};
this.value = initVal();
this.max = this.value * 4;
@@ -12,7 +12,7 @@
<script>
import { enumVars } from "../../../assets/wizard/wizard.js";
const axios = require("axios");
import { putVal } from "../../../odrive_utils.js"
export default {
name: "wizardEnd",
@@ -82,31 +82,10 @@ export default {
applyConfig(){
// flatpaths will already be populated at this point
for (const diff of this.configDiffs) {
this.putVal(diff.path, diff.newVal);
putVal("odrive0." + diff.path, diff.newVal);
console.log("applying " + diff.newVal + " to " + diff.path);
}
},
putVal(path, val) {
var params = new URLSearchParams();
params.append("key", "odrive0");
let keys = path.split(".");
for (const key of keys) {
params.append("key", key);
}
params.append("val", val);
params.append("type", typeof val);
console.log(path + " = " + " type = " + typeof val);
//console.log(params.toString());
let request = {
params: params,
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
},
}
}
</script>
@@ -11,6 +11,7 @@
<script>
import odriveEnums from "../../../assets/odriveEnums.json";
import { getVal } from "../../../odrive_utils.js"
export default {
name: "wizardInputFiltered",
@@ -24,18 +25,8 @@ export default {
};
},
created() {
let keys = [
"odrive0",
this.data.axis,
"controller",
"config",
"input_filter_bandwidth",
];
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
this.bandwidth = parseFloat(odriveObj["val"]);
let path = "odrive0." + this.data.axis + ".controller.config.input_filter_bandwidth";
this.bandwidth = parseFloat(getVal(path));
},
methods: {
sendConfig() {
@@ -24,6 +24,8 @@
</template>
<script>
import { getVal } from "../../../odrive_utils.js"
export default {
name: "wizardInputTrajectory",
props: {
@@ -40,43 +42,12 @@ export default {
},
created() {
// get pre-existing values for trap_traj vel limit, accel, devel, and config.inertia
if (this.data.axis == "axis0") {
this.vel_limit = this.getODriveVal(
"odrive0.axis0.trap_traj.config.vel_limit"
);
this.accel_limit = this.getODriveVal(
"odrive0.axis0.trap_traj.config.accel_limit"
);
this.decel_limit = this.getODriveVal(
"odrive0.axis0.trap_traj.config.decel_limit"
);
this.inertia = this.getODriveVal(
"odrive0.axis0.controller.config.inertia"
);
} else if (this.data.axis == "axis1") {
this.vel_limit = this.getODriveVal(
"odrive0.axis1.trap_traj.config.vel_limit"
);
this.accel_limit = this.getODriveVal(
"odrive0.axis1.trap_traj.config.accel_limit"
);
this.decel_limit = this.getODriveVal(
"odrive0.axis1.trap_traj.config.decel_limit"
);
this.inertia = this.getODriveVal(
"odrive0.axis1.controller.config.inertia"
);
}
this.vel_limit = getVal("odrive0." + this.data.axis + ".trap_traj.config.vel_limit");
this.accel_limit = getVal("odrive0." + this.data.axis + ".trap_traj.config.accel_limit");
this.decel_limit = getVal("odrive0." + this.data.axis + ".trap_traj.config.decel_limit");
this.inertia = getVal("odrive0." + this.data.axis + ".controller.config.inertia");
},
methods: {
getODriveVal(path) {
let odriveObj = this.$store.state.odrives;
console.log("get val " + path);
for (const key of path.split(".")) {
odriveObj = odriveObj[key];
}
return parseFloat(odriveObj["val"]);
},
sendConfig() {
console.log("emitting choice event from limits page");
let configStub = undefined;
@@ -11,6 +11,7 @@
<script>
import odriveEnums from "../../../assets/odriveEnums.json";
import { getVal } from "../../../odrive_utils.js"
export default {
name: "wizardInputVelRamp",
@@ -24,18 +25,8 @@ export default {
};
},
created() {
let keys = [
"odrive0",
this.data.axis,
"controller",
"config",
"vel_ramp_rate",
];
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
this.vel_ramp_rate = parseFloat(odriveObj["val"]);
let path = "odrive0." + this.data.axis + ".controller.config.vel_ramp_rate";
this.vel_ramp_rate = parseFloat(getVal(path));
},
methods: {
sendConfig() {
@@ -12,6 +12,8 @@
</template>
<script>
import { getVal } from "../../../odrive_utils.js"
export default {
name: "wizardMisc",
props: {
@@ -28,26 +30,12 @@ export default {
},
computed: {
velocityLimit: function () {
let keys = [
"odrive0",
this.data.axis,
"controller",
"config",
"vel_limit",
];
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return parseFloat(odriveObj["val"]);
let path = "odrive0." + this.data.axis + ".controller.config.vel_limit";
return parseFloat(getVal(path));
},
currentLimit: function () {
let keys = ["odrive0", this.data.axis, "motor", "config", "current_lim"];
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return parseFloat(odriveObj["val"]);
let path = "odrive0." + this.data.axis + ".motor.config.current_lim";
return parseFloat(getVal(path));
},
},
methods: {
@@ -21,8 +21,8 @@
</template>
<script>
const axios = require("axios");
import odriveEnums from "../../../assets/odriveEnums.json";
import { getVal } from "../../../odrive_utils.js"
export default {
name: "wizardMotor",
@@ -45,64 +45,24 @@ export default {
},
computed: {
resistance: function () {
let keys = [
"odrive0",
this.data.axis,
"motor",
"config",
"phase_resistance",
];
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return parseFloat(odriveObj["val"]).toExponential(3);
let path = "odrive0." + this.data.axis + ".motor.config.phase_resistance";
return parseFloat(getVal(path)).toExponential(3);
},
inductance: function () {
let keys = [
"odrive0",
this.data.axis,
"motor",
"config",
"phase_inductance",
];
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return parseFloat(odriveObj["val"]).toExponential(3);
let path = "odrive0." + this.data.axis + ".motor.config.phase_inductance";
return parseFloat(getVal(path)).toExponential(3);
},
},
watch: {
resistance: function (newVal) {
console.log("from resistance watcher: " + newVal);
let keys = [
"odrive0",
this.data.axis,
"motor",
"config",
"phase_resistance",
];
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
this.phase_resistance = parseFloat(odriveObj["val"]);
let path = "odrive0." + this.data.axis + ".motor.config.phase_resistance";
this.phase_resistance = parseFloat(getVal(path));
},
inductance: function (newVal) {
console.log("from inductance watcher: " + newVal);
let keys = [
"odrive0",
this.data.axis,
"motor",
"config",
"phase_inductance",
];
let odriveObj = this.$store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
this.phase_inductance = parseFloat(odriveObj["val"]);
let path = "odrive0." + this.data.axis + ".motor.config.phase_inductance";
this.phase_inductance = parseFloat(getVal(path));
},
pp_set: function () {
console.log("pp_set: " + this.pp_set);
@@ -178,26 +138,6 @@ export default {
});
}
},
measure() {
// ask ODrive to measure resistance and inductance
var params = new URLSearchParams();
let keys = ["odrive0", this.data.axis, "requested_state"];
for (const key of keys) {
params.append("key", key);
}
params.append("val", odriveEnums.AXIS_STATE_MOTOR_CALIBRATION);
params.append("type", "number");
console.log(params.toString());
let request = {
params: params,
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
},
setKV(e) {
this.torque_constant = 8.27 / parseFloat(e.target.value);
this.kv_set = true;
@@ -246,14 +186,6 @@ input[type="number"] {
-moz-appearance: textfield; /* Firefox */
}
.measure-button {
margin-top: 2rem;
}
.measure-button:active {
background-color: var(--bg-color);
}
.name {
text-align: center;
}
@@ -5,8 +5,8 @@
</template>
<script>
const axios = require("axios");
import odriveEnums from "../../../assets/odriveEnums.json";
import { putVal } from "../../../odrive_utils.js"
export default {
name: "wizardMotorMeasure",
@@ -22,23 +22,8 @@ export default {
methods: {
calibrate() {
// ask ODrive to measure resistance and inductance
var params = new URLSearchParams();
let keys = ["odrive0", this.data.axis, "requested_state"];
for (const key of keys) {
params.append("key", key);
}
params.append("val", odriveEnums.AXIS_STATE_MOTOR_CALIBRATION);
params.append("type", "number");
console.log(params.toString());
let request = {
params: params,
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
let path = "odrive0." + this.data.axis + ".requested_state";
putVal(path, odriveEnums.AXIS_STATE_MOTOR_CALIBRATION);
this.$emit('page-comp-event', {data: "motor calibration", axis: this.data.axis});
},
},
+67
View File
@@ -0,0 +1,67 @@
import store from "./store.js";
const axios = require("axios");
// helper functions and utilities for getting ODrive values
// given a path like "odrive0.axis0.config.blah", return the value
export function getParam(path) {
let keys = path.split('.');
let odriveObj = store.state.odrives;
for (const key of keys) {
odriveObj = odriveObj[key];
}
return odriveObj;
}
// wrapper for val field
export function getVal(path) {
return getParam(path + ".val");
}
// wrapper for readonly field
export function getReadonly(path) {
return getParam(path + '.readonly');
}
// using axios, send an http request to change an ODrive value
export function putVal(path, value) {
var params = new URLSearchParams();
let keys = path.split(".");
for (const key of keys) {
params.append("key", key);
}
params.append("val", value);
params.append("type", typeof value);
let request = {
params: params,
};
console.log(request);
axios.put(
store.state.odriveServerAddress + "/api/property",
null,
request
);
}
// path is path to function, args is list of parameters
export function callFcn(path, args) {
var params = new URLSearchParams();
let keys = path.split(".");
for (const key of keys) {
params.append("key", key);
}
if (args) {
for (const arg of args) {
params.append("arg", arg);
}
}
console.log(params.toString());
let request = {
params: params
};
axios.put(
store.state.odriveServerAddress + "/api/function",
null,
request
);
}
+10 -68
View File
@@ -50,7 +50,7 @@ import configTemplate from "../assets/wizard/configTemplate.json";
import wizardPage from "../components/wizard/wizardPage.vue";
import odriveEnums from "../assets/odriveEnums.json";
import { pages } from "../assets/wizard/wizard.js";
const axios = require("axios");
import { getVal, putVal } from "../odrive_utils.js"
// see wizard.js for what wizard pages exist and what they contain
@@ -87,33 +87,10 @@ export default {
this.$store.state.odrives.odrive0.axis1.error.val == "0"
) {
let configStub = undefined;
let inductance;
let resistance;
let keys_L = [
"odrive0",
e.axis,
"motor",
"config",
"phase_inductance",
];
let keys_R = [
"odrive0",
e.axis,
"motor",
"config",
"phase_resistance",
];
let odriveObj = this.$store.state.odrives;
for (const key of keys_L) {
odriveObj = odriveObj[key];
}
inductance = parseFloat(odriveObj["val"]);
odriveObj = this.$store.state.odrives;
for (const key of keys_R) {
odriveObj = odriveObj[key];
}
resistance = parseFloat(odriveObj["val"]);
let path_inductance = "odrive0." + e.axis + ".motor.config.phase_inductance";
let path_resistance = "odrive0." + e.axis + ".motor.config.phase_resistance";
let inductance = parseFloat(getVal(path_inductance));
let resistance = parseFloat(getVal(path_resistance));
if (e.axis == "axis0") {
configStub = {
axis0: {
@@ -153,21 +130,7 @@ export default {
"odrive0." + e.axis + ".controller.error",
];
for (const path of paths) {
var params = new URLSearchParams();
let keys = path.split(".");
for (const key of keys) {
params.append("key", key);
}
params.append("val", 0);
params.append("type", "number");
let request = {
params: params,
};
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
putVal(path, 0);
}
console.log("clearing error for " + e.axis);
};
@@ -219,11 +182,11 @@ export default {
);
console.log("oldCPR = " + oldCPR);
console.log("axis = " + e.axis);
this.putVal(
putVal(
"odrive0." + e.axis + ".encoder.config.cpr",
this.wizardConfig[e.axis].encoder.config.cpr
);
this.putVal(
putVal(
"odrive0." + e.axis + ".requested_state",
odriveEnums.AXIS_STATE_ENCODER_OFFSET_CALIBRATION
);
@@ -240,12 +203,12 @@ export default {
} else if (
this.$store.state.odrives.odrive0[e.axis].error.val != "0"
) {
this.putVal("odrive0." + e.axis + ".encoder.config.cpr", oldCPR);
putVal("odrive0." + e.axis + ".encoder.config.cpr", oldCPR);
console.log("applying old CPR, error detected");
this.calibrating = false;
this.calStatus = false;
} else {
this.putVal("odrive0." + e.axis + ".encoder.config.cpr", oldCPR);
putVal("odrive0." + e.axis + ".encoder.config.cpr", oldCPR);
console.log("applying old CPR");
this.choiceMade = true;
this.calibrating = false;
@@ -327,27 +290,6 @@ export default {
this.currentStep = pages[this.currentStep.back];
this.choiceMade = false;
},
putVal(path, value) {
// path to value in odrive parameter tree for server
// for example, odrive0.axis0.config.requested_state
var params = new URLSearchParams();
// params.append("key", "odrive0");
let keys = path.split(".");
for (const key of keys) {
params.append("key", key);
}
params.append("val", value);
params.append("type", typeof value);
let request = {
params: params,
};
console.log(request);
axios.put(
this.$store.state.odriveServerAddress + "/api/property",
null,
request
);
},
},
};
</script>