Last active
September 7, 2020 18:35
-
-
Save devtrillo/5fbb2d37db3a29459de57d20ce4ef4a3 to your computer and use it in GitHub Desktop.
This is a controller for two linear actuators to a desk
This file contains hidden or 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
from gpiozero import LED | |
from time import sleep | |
import sys | |
relay_a = LED("GPIO4") | |
relay_b = LED("GPIO17") | |
ht_file = "height.txt" | |
max_height = 400 | |
def extend(): | |
print('Extending') | |
relay_a.on() | |
relay_b.off() | |
def retract(): | |
print('Retracting') | |
relay_a.off() | |
relay_b.on() | |
def stop(): | |
print('Stopping') | |
relay_a.off() | |
relay_b.off() | |
def write_height(height): | |
if float(height) < 0: | |
print('WARNING, height {0} is less than zero, reset recomended') | |
height = 0 | |
if float(height) >= max_height: | |
print(('WARNING: height' + str(height) + ' greater than max height (' + | |
str(max_height) + '), reset recommended.')) | |
height = max_height | |
height_file = open(ht_file, 'w') | |
height_file.write(str(height)) | |
height_file.close() | |
def read_height(): | |
height = '0' | |
try: | |
height_file = open(ht_file, 'r') | |
height = height_file.read() | |
except IOError: | |
print('WARNING: height not known resetting') | |
write_height(0) | |
try: | |
float_height = float(height) | |
except: | |
print('WARNING: height file corrupted. Reset recomended') | |
float_height = 0 | |
return float_height | |
print('Argument height:', ) | |
desired_height = float(sys.argv[1]) | |
if desired_height > max_height: | |
print('That is to high') | |
exit() | |
current = read_height() | |
distance = desired_height - current | |
print(("distance to move:" + str(distance))) | |
if distance > 0: | |
extend() | |
sleep(abs(distance/10)) | |
write_height(read_height() + distance) | |
else: | |
retract() | |
sleep(abs(distance/10)) | |
write_height(read_height() + distance) | |
stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment