Created
May 15, 2018 16:24
-
-
Save tomeaton17/100dc7a6ef3ea9f522e195ddda21e7b6 to your computer and use it in GitHub Desktop.
Current stepper for EDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, serial | |
from datetime import datetime | |
ser = serial.Serial('COM17') | |
time.sleep(10) | |
path = datetime.now().strftime('%Y-%m-%d-%H%M%S') + ".log" | |
file = open(path, 'w') | |
ser.write(b'S') | |
counter = 5 | |
forwards = True | |
# `attrs` is the distribution package name... | |
try: | |
import attrs # learn more: https://python.org/pypi/attrs | |
except ModuleNotFoundError: | |
pass | |
# but the python package name you import is `attr` | |
import attr | |
@attr.s | |
class PeriodicTime: | |
period = attr.ib() | |
clock = attr.ib(default=time.monotonic) | |
now = attr.ib() | |
last = attr.ib() | |
avoid_drift = attr.ib(default=True) | |
@now.default | |
def _(self): | |
return self.clock() | |
@last.default | |
def _(self): | |
return self.now | |
def due(self): | |
self.now = self.clock() | |
elapsed = self.now - self.last | |
due = elapsed >= self.period | |
if due: | |
if self.avoid_drift: | |
self.last += self.period | |
else: | |
self.last = self.now | |
return due | |
def set_last(self, last=None): | |
if last is None: | |
self.last = self.clock() | |
else: | |
self.last = last | |
periodic_time = PeriodicTime(period=5) | |
while 1: | |
data = ser.readline().decode('utf-8').strip() | |
time_string = str(time.time()) | |
print(data + ',' + time_string) | |
file.write(data + ',' + time_string + "\n") | |
if periodic_time.due(): | |
if(forwards): | |
if(counter == 50): | |
forwards = False | |
ser.write(b'-') | |
counter -= 5 | |
elif(counter < 50): | |
ser.write(b'+') | |
counter += 5 | |
elif(forwards == 0): | |
if(counter == 0): | |
ser.write(b's') | |
break | |
ser.write(b'-') | |
counter -= 5 | |
ser.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment