From a8ca22f392214650185c089516204661a0b5d01d Mon Sep 17 00:00:00 2001 From: tpollina Date: Tue, 10 Dec 2019 21:51:01 -0800 Subject: [PATCH] Add files via upload --- pipeline/focus.php | 8 +++++++ pipeline/focus.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ pipeline/index.html | 22 +++++++++++++----- pipeline/pipeline.js | 30 ++++++++++++++++-------- pipeline/pump.php | 8 +++++++ pipeline/pump.py | 41 +++++++++++++++++++++++++++++++++ pipeline/thread2.py | 45 ++++++++++++++++++++++++++++++++++++ 7 files changed, 192 insertions(+), 16 deletions(-) create mode 100644 pipeline/focus.php create mode 100644 pipeline/focus.py create mode 100644 pipeline/pump.php create mode 100644 pipeline/pump.py create mode 100644 pipeline/thread2.py diff --git a/pipeline/focus.php b/pipeline/focus.php new file mode 100644 index 0000000..70d768a --- /dev/null +++ b/pipeline/focus.php @@ -0,0 +1,8 @@ + diff --git a/pipeline/focus.py b/pipeline/focus.py new file mode 100644 index 0000000..837ec79 --- /dev/null +++ b/pipeline/focus.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +from adafruit_motor import stepper +from adafruit_motorkit import MotorKit +from time import sleep + +from thread2 import Thread +import sys + +nb_step = int(sys.argv[1]) +orientation = str(sys.argv[2]) +toogler = str(sys.argv[3]) + + +#Execute a python cmd with the previous defined variables from php + + +kit = MotorKit() + +stage = kit.stepper1 + +stage.release() + +def focus(steps,orientation): + #0.25mm/step + #31um/microsteps + + stage.release() + + if orientation == 'up': + for i in range(steps): + stage.onestep(direction=stepper.FORWARD, style=stepper.MICROSTEP) + sleep(0.001) + + if orientation == 'down': + for i in range(steps): + stage.onestep(direction=stepper.BACKWARD, style=stepper.MICROSTEP) + sleep(0.001) + + stage.release() + +focus_thread = Thread(target = focus, name = 'focus_thread', args =(nb_step, orientation) ) + +if toogler == "ON": + + focus_thread.start() + focus_thread.isAlive() + +if toogler == "OFF": + + focus_thread.terminate() + focus_thread.join() + focus_thread.isAlive() + stage.release() + diff --git a/pipeline/index.html b/pipeline/index.html index 515c2a1..a57b19b 100644 --- a/pipeline/index.html +++ b/pipeline/index.html @@ -14,13 +14,23 @@ - -nb_step - -orientation - - +
+ nb_step + + orientation + + +
+ volume(ml) + + flowrate (ml/min) (from 0 to 20) + + direction (foward or backward) + + +
+
diff --git a/pipeline/pipeline.js b/pipeline/pipeline.js index 52503fa..d880a15 100644 --- a/pipeline/pipeline.js +++ b/pipeline/pipeline.js @@ -1,21 +1,31 @@ $(document).ready(function(){ - $( "#send_nb_step" ).click(function() { - pipeline() + $( "#focus" ).click(function() { + focus("ON") }); + + $( "#pump" ).click(function() { + pump() + + }); + -function pipeline() { - var value = $("#nb_step").val(); - var orientation = $("#orientation").val(); - - console.log("pipeline.php?nb_step="+value+"&orientation="+orientation); - +function focus(toggler) { + var value = $("#nb_step").val(); + var orientation = $("#orientation").val(); var xmlhttp = new XMLHttpRequest(); - xmlhttp.open("GET", "pipeline.php?nb_step="+value+"&orientation="+orientation, true); + xmlhttp.open("GET", "focus.php?nb_step="+value+"&orientation="+orientation+"&toggler="+toggler, true); xmlhttp.send(); } - +function pump() { + var volume = $("#volume").val(); + var flowrate = $("#flowrate").val(); + var direction = $("#direction").val(); + var xmlhttp = new XMLHttpRequest(); + xmlhttp.open("GET", "pump.php?volume="+volume+"&flowrate="+flowrate+"&direction="+direction, true); + xmlhttp.send(); + } }); diff --git a/pipeline/pump.php b/pipeline/pump.php new file mode 100644 index 0000000..01d1244 --- /dev/null +++ b/pipeline/pump.php @@ -0,0 +1,8 @@ + diff --git a/pipeline/pump.py b/pipeline/pump.py new file mode 100644 index 0000000..44c8898 --- /dev/null +++ b/pipeline/pump.py @@ -0,0 +1,41 @@ +from adafruit_motor import stepper +from adafruit_motorkit import MotorKit +from time import sleep + +import sys + +vol = int(sys.argv[1]) +flowrate = int(sys.argv[2]) +dir = str(sys.argv[3]) + +kit = MotorKit() + +pump_stepper = kit.stepper2 + +pump_stepper.release() + +def pump(vol, flowrate, dir): + + if dir == "foward": + dir=stepper.FORWARD + if dir == "backward": + dir=stepper.BACKWARD + + nb_step=vol*507 #if sleep(0.05) in between 2 steps + #35000steps for 69g + + #nb_step=vol*460 if sleep(0) in between 2 steps + duration=(vol*60)/flowrate + + delay=(duration/nb_step)-0.005 + + for i in range(nb_step): + pump_stepper.onestep(direction=dir, style=stepper.DOUBLE) + sleep(delay) + + sleep(1) + pump_stepper.release() + +#volume, flowrate (from 0 to 20), direction (foward or backward) +pump(vol, flowrate, dir) + diff --git a/pipeline/thread2.py b/pipeline/thread2.py new file mode 100644 index 0000000..789f79c --- /dev/null +++ b/pipeline/thread2.py @@ -0,0 +1,45 @@ +import threading +import inspect +import ctypes + + +def _async_raise(tid, exctype): + """raises the exception, performs cleanup if needed""" + if not inspect.isclass(exctype): + raise TypeError("Only types can be raised (not instances)") + res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) + if res == 0: + raise ValueError("invalid thread id") + elif res != 1: + # """if it returns a number greater than one, you're in trouble, + # and you should call it again with exc=NULL to revert the effect""" + ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0) + raise SystemError("PyThreadState_SetAsyncExc failed") + + +class Thread(threading.Thread): + def _get_my_tid(self): + """determines this (self's) thread id""" + if not self.isAlive(): + raise threading.ThreadError("the thread is not active") + + # do we have it cached? + if hasattr(self, "_thread_id"): + return self._thread_id + + # no, look for it in the _active dict + for tid, tobj in threading._active.items(): + if tobj is self: + self._thread_id = tid + return tid + + raise AssertionError("could not determine the thread's id") + + def raise_exc(self, exctype): + """raises the given exception type in the context of this thread""" + _async_raise(self._get_my_tid(), exctype) + + def terminate(self): + """raises SystemExit in the context of the given thread, which should + cause the thread to exit silently (unless caught)""" + self.raise_exc(SystemExit)