Skip to content
Snippets Groups Projects
Commit ae5427db authored by Jake Read's avatar Jake Read
Browse files

apres dex testing

parent 9c14eb1e
Branches
No related tags found
No related merge requests found
/*
hunks/data/exFilter.js
removes old lovers from data streams
Jake Read at the Center for Bits and Atoms
(c) Massachusetts Institute of Technology 2019
This work may be reproduced, modified, distributed, performed, and
displayed for any purpose, but must acknowledge the squidworks and cuttlefish projects.
Copyright is retained and must be preserved. The work is provided as is;
no warranty is provided, and users accept all liability.
*/
import { Hunkify, Input, Output, State } from '../hunks.js'
export default function AverageStream(){
Hunkify(this)
let inNum = this.input('number', 'vals')
let outNum = this.output('number', 'average')
let reset = this.state('boolean', 'reset', false)
let count = this.state('number', 'count', 10)
let counter = this.state('number', 'counter', 0)
counter.onChange = (val) => {
counter.set(counter.val) // no,
}
let sum = 0
reset.onChange = (value) => {
counter.set(0)
sum = 0
}
this.loop = () => {
if(inNum.io() && (counter.value < count.value)){
sum += inNum.get()
let tally = counter.value + 1
counter.set(tally)
}
if(!outNum.io() && (counter.value >= count.value)){
outNum.put(sum / count.value)
sum = 0
counter.set(0)
}
}
}
/*
hunks/data/accumulator.js
stashes data stream to an array
Jake Read at the Center for Bits and Atoms
(c) Massachusetts Institute of Technology 2019
This work may be reproduced, modified, distributed, performed, and
displayed for any purpose, but must acknowledge the squidworks and cuttlefish projects.
Copyright is retained and must be preserved. The work is provided as is;
no warranty is provided, and users accept all liability.
*/
import {
Hunkify,
Input,
Output,
State
} from '../hunks.js'
export default function BuildTable(){
Hunkify(this)
// todo: make this polymorphic
let ipr = this.input("boolean", "reset")
let ivx = this.input("number", "x")
let ivy = this.input("number", "y")
// push a reference out, for now
let outref = this.output("reference", "accumulated")
// reset via button as well,
let rstrig = this.state("boolean", "reset", false)
let reset = () => {
arr = []
if(!outref.io()){
outref.put(arr)
}
}
rstrig.onChange = (value) => {
reset()
rstrig.set(false)
}
this.init = () => {
//
}
let arr = []
this.loop = () => {
if(ipr.io()){
ipr.get()
reset()
}
if(ivx.io() && ivy.io()){
arr.push([ivx.get(), ivy.get()])
// todo: for ref, pass always OK ? breaks rules anyways ...
if(!outref.io()){
outref.put(arr)
}
}
}
}
......@@ -22,32 +22,24 @@ import {
} from '../hunks.js'
function LineChart() {
export default function LineChart() {
Hunkify(this)
//
let dtRef = new Input('reference', 'array', this)
this.inputs.push(dtRef)
let dtRef = this.input('reference', 'array')
// how many to keep
let displayNum = new State('number', 'displayCount', 50)
this.states.push(displayNum)
let displayNum = this.state('number', 'displayCount', 50)
// some global items,
var datas = [
[0, 0]
]
let uniqueDivId = ''
// to claim some space in the DOM, we make a handle for ourselv
this.dom = {}
// hack, likely there is a better way
let dom = this.document()
this.init = () => {
console.log('INIT linechart')
// make a unique id, we use a flat d3 search to find and update this ...
uniqueDivId = 'linechart_div_' + Math.round(Math.random() * 1000)
this.dom = $('<div>').attr('id', uniqueDivId).get(0)
}
this.onload = () => {
let uid = `lineChart_${this.ind}_uid`
$(dom).attr('id', uid)
// our vars,
var margin = {
top: 20,
......@@ -81,9 +73,9 @@ function LineChart() {
return d[1];
})])
if (thesvg) {
d3.select(`#${uniqueDivId}`).selectAll("*").remove()
d3.select(`#${uid}`).selectAll("*").remove()
}
thesvg = d3.select(`#${uniqueDivId}`).append("svg")
thesvg = d3.select(`#${uid}`).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
......@@ -101,16 +93,10 @@ function LineChart() {
thesvg.append("g")
.call(d3.axisLeft(y))
}
// so then
this.requestResize(1000, 520)
// startup
this.reloadDatas()
}
this.onresize = () => {
console.log(this.dom.clientHeight, this.dom.clientWidth)
}
// hmmm
this.loop = () => {
// pull and append
......@@ -122,5 +108,3 @@ function LineChart() {
}
}
}
export default LineChart
......@@ -38,23 +38,17 @@ export default function OpenJSON() {
let reader = new FileReader()
reader.onload = (evt) => {
theRef = JSON.parse(evt.target.result)
// that's it,
refUpdated = true
}
reader.readAsText(file)
}
this.init = () => {
this.dom = $('<div>').get(0)
}
this.onload = () => {
let dom = this.document()
let btn = $('<input type="file" accept=".json">').get(0)
$(btn).on('change', (evt) => {
readJSON(evt.target.files[0])
})
$(this.dom).append(btn)
}
dom.append(btn)
this.loop = () => {
if(refUpdated && !outRef.io()){
......@@ -62,5 +56,4 @@ export default function OpenJSON() {
refUpdated = false
}
}
}
......@@ -51,6 +51,7 @@ export default function Save(){
svbutton.onChange = (val) => {
dishit()
svbutton.set(false)
}
this.loop = () => {
......
/*
hunks/x_adhoc/center.js
find subpixel hotspot in grayscale image
Jake Read at the Center for Bits and Atoms with Neil Gershenfeld and Leo McElroy
(c) Massachusetts Institute of Technology 2019
This work may be reproduced, modified, distributed, performed, and
displayed for any purpose, but must acknowledge the squidworks and cuttlefish projects.
Copyright is retained and must be preserved. The work is provided as is;
no warranty is provided, and users accept all liability.
*/
import {
Hunkify,
Input,
Output,
State
} from '../hunks.js'
import REG from '../../libs/regression.js'
export default function PolynomialFit() {
Hunkify(this)
let dataIn = this.input('reference', 'data')
let predict = this.input('number', 'predict')
let fitDisplay = this.output('reference', 'fit')
let pout = this.output('number', 'predict') // thruput,
let prediction = this.output('number', 'prediction')
let expression = this.state('string', 'result', '-')
this.init = () => {
expression.set('-')
}
let fit = null
this.loop = () => {
if (dataIn.io()) {
let datas = JSON.parse(JSON.stringify(dataIn.get()))
fit = REG.polynomial(datas, { order: 3, precision: 6 })
console.log(fit)
expression.set(fit.string)
// go-by to check, for x bounds of data, by .. 0.5 (arbitrary)
let min = Infinity
let max = -Infinity
for (let i in datas) {
if (datas[i][0] < min) min = datas[i][0]
if (datas[i][0] > max) max = datas[i][0]
}
let fdata = []
let count = 0
for (let i = min; i < max; i += 0.2) {
fdata.push(fit.predict(i))
}
if (!fitDisplay.io()) fitDisplay.put(JSON.parse(JSON.stringify(fdata)))
}
if (!fit) {
if(predict.io()) predict.get()
} else if (predict.io() && !prediction.io() && !pout.io()) {
let res = fit.predict(predict.get())
prediction.put(res[1])
pout.put(res[0])
}
}
}
/*
hunks/math/absolutevalue.js
Jake Read at the Center for Bits and Atoms
(c) Massachusetts Institute of Technology 2019
This work may be reproduced, modified, distributed, performed, and
displayed for any purpose, but must acknowledge the squidworks and cuttlefish projects.
Copyright is retained and must be preserved. The work is provided as is;
no warranty is provided, and users accept all liability.
*/
import {
Hunkify,
Input,
Output,
State
} from '../hunks.js'
export default function Subtraction(){
Hunkify(this)
let a = this.input('number', 'a')
let b = this.input('number', 'b')
let c = this.output('number', 'c')
this.loop = () => {
if(a.io() && b.io() && !c.io()){
c.put(a.get() - b.get())
}
}
}
......@@ -39,61 +39,45 @@ import {
export default function DEX() {
Hunkify(this)
//let motorReturn = new Input('int32', 'motor return', this)
let loadcellReturn = new Input('int32', 'loadcell return', this)
this.inputs.push(loadcellReturn)
// to operate,
let motorOut = new Output('int32', 'motor output', this)
let loadcellTrigger = new Output('boolean', 'loadcell trigger', this)
this.outputs.push(motorOut, loadcellTrigger)
// to run,
let stressOut = new Output('number', 'current stress', this)
let strainOut = new Output('number', 'current strain', this)
this.outputs.push(stressOut, strainOut)
// to operate
let motorOut = this.output('int32', 'motor output', this)
// request a reading,
let loadcellTrigger = this.output('boolean', 'loadcell trigger', this)
// open loop displacement
let cdOut = this.output('number', 'displacement (um)')
// to config,
let runState = this.state('boolean', 'running', false)
let resetState = this.state('boolean', 'reset', false)
let byIncrement = (count) => {
return Math.round(count * umPerStep.value * 100) / 100
let stepsToIncrement = (steps) => {
// for this many steps,
return steps / stepsPerMM.value
}
let shipIt = () => {
motorOut.put(Math.round(incrementSize.value / umPerStep.value))
loadcellTrigger.put(true)
let incrementToSteps = (increment) => {
return Math.ceil(increment * stepsPerMM.value)
}
// to config,
let runState = new State('boolean', 'running', false)
let resetState = new State('boolean', 'reset', false)
// movement sizes info,
let umPerStep = new State('number', 'displacement per step (um)', 4.23387)
let incrementSize = new State('number', 'increment (um)', byIncrement(5))
// loadcell info,
let newtonsPerTick = new State('number', 'newtons per tick', )
this.states.push(runState, umPerStep, incrementSize)
let stepsPerMM = this.state('number', 'steps per mm', 4960)
let incrementSize = this.state('number', 'increment (mm)', stepsToIncrement(100))
// to run,
let currentDisplacement = new State('number', 'current stress', 0)
this.states.push(currentDisplacement)
let currentDisplacement = this.state('number', 'current displacement (mm)', 0)
runState.onChange = (value) => {
if (runState.value) {
runState.set(false)
} else {
if(!loadcellTrigger.io()){
shipIt()
runState.set(true)
} else {
// nope, donot set
}
}
}
// delay,
let stepDelay = this.state('number', 'delay(ms)', 500)
resetState.onChange = (value) => {
currentDisplacement.set(0)
runState.set(false)
resetState.set(false)
}
incrementSize.onChange = (value) => {
let count = Math.ceil(value / umPerStep.value)
incrementSize.set(byIncrement(count))
// from <um> in fixed steps, do stepsPerUm / value
incrementSize.set(incrementToSteps(value) / stepsPerMM.value)
}
this.init = () => {
......@@ -101,19 +85,23 @@ export default function DEX() {
runState.set(false)
}
this.loop = () => {
// clear in pairs,
if(loadcellReturn.io()){
let strain = loadcellReturn.get()
let waiting = false
let shipIt = () => {
waiting = true
motorOut.put(Math.round(stepsPerMM.value * incrementSize.value))
setTimeout(() => {
loadcellTrigger.put(true)
currentDisplacement.set(currentDisplacement.value + incrementSize.value)
if(!stressOut.io() && !strainOut.io()){
stressOut.put(currentDisplacement.value)
strainOut.put(strain)
cdOut.put(currentDisplacement.value)
waiting = false
}, stepDelay.value)
}
this.loop = () => {
// if motor and loadcell trigger empty, release another round:
if(runState.value){
if(!loadcellTrigger.io() && !motorOut.io() && !cdOut.io() && !waiting){
shipIt()
// and
}
}
}
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment