add stepper 2, shutdown control

This commit is contained in:
brentru 2019-02-12 15:47:43 -05:00
parent 256135bd13
commit 7d212aa27a

View file

@ -39,10 +39,19 @@ ADAFRUIT_IO_USERNAME = ''
# Create an instance of the REST client.
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Stepper 1 Speed Feed
# Delay between checking for new stepper slider value on Adafruit IO, in seconds
ADAFRUIT_IO_DELAY = 15
# Stepper 1 Adafruit IO Feeds
feed_step_1_steps = aio.feeds('stepper1steps')
feed_step_1_direction = aio.feeds('stepper1direction')
feed_step_1_step_size = aio.feeds('stepper1stepsize')
# Stepper 2 Adafruit Feeds
feed_step_2_steps = aio.feeds('stepper2steps')
feed_step_2_direction = aio.feeds('stepper2direction')
feed_step_2_step_size = aio.feeds('stepper2stepsize')
# Shutdown Feed
feed_stepper_shutdown = aio.feeds('steppershutdown')
# create a default object, no changes to I2C address or frequency
kit = MotorKit()
@ -59,20 +68,6 @@ def turnOffMotors():
atexit.register(turnOffMotors)
stepstyles = [STEPPER.SINGLE, STEPPER.DOUBLE, STEPPER.INTERLEAVE, STEPPER.MICROSTEP]
#TODO: Save previous state of stepper1/stepper2 IO RX's
# TODO: Fetch this data every 30sec
# Get stepper 1 configuration from IO
stepper1_steps = aio.receive(feed_step_1_steps.key)
stepper1_steps = int(stepper1_steps.value)
stepper_1_direction = aio.receive(feed_step_1_direction.key)
stepper1_step_size = aio.receive(feed_step_1_step_size.key)
print('Stepper 1 Configuration (from IO)')
print("%d steps" % stepper1_steps)
print('Step Size: ', stepper1_step_size.value)
print('Stepper Direction: ', stepper_1_direction.value)
def stepper_worker(stepper, numsteps, direction, stepper_name, style, show_steps=False):
print("Steppin!")
stepper_steps = numsteps
@ -85,19 +80,31 @@ def stepper_worker(stepper, numsteps, direction, stepper_name, style, show_steps
aio.send(feed_step_1_steps.key, stepper_steps)
time.sleep(0.30)
print("Done using ", stepper_name)
if stepper_name == "Stepper 1":
aio.send(feed_step_1_steps.key, 0)
elif stepper_name == "Stepper 2":
aio.send(feed_step_2_steps.key, 0)
while True:
# Stepper 1 Adafruit IO Configuration
# Retrieve Data from Adafruit IO
# Stepper Shutdown
is_shutdown = aio.receive(feed_stepper_shutdown.key)
if is_shutdown.value == 1:
print('* SHUTTING OFF BOTH STEPPER MOTORS *')
turnOffMotors()
# Stepper 1
if not st1.isAlive(): # if thread is killed
print('Retrieving Stepper 1 Config from IO...')
stepper1_steps = aio.receive(feed_step_1_steps.key)
stepper1_steps = int(stepper1_steps.value)
if stepper1_steps is not 0: # check against if the feed value is 0
stepper_1_steps = aio.receive(feed_step_1_steps.key)
stepper_1_steps = int(stepper_1_steps.value)
if stepper_1_steps is not 0: # check against if the feed value is 0
stepper_1_direction = aio.receive(feed_step_1_direction.key)
stepper1_step_size = aio.receive(feed_step_1_step_size.key)
stepper_1_step_size = aio.receive(feed_step_1_step_size.key)
print('Stepper 1 Configuration (from IO)')
print("%d steps" % stepper1_steps)
print('Step Size: ', stepper1_step_size.value)
print("%d steps" % stepper_1_steps)
print('Step Size: ', stepper_1_step_size.value)
print('Stepper Direction: ', stepper_1_direction.value)
stepper_name = "Stepper 1"
# Set Stepper Direction
@ -109,12 +116,40 @@ while True:
print("backward")
# Stepper 1 Thread
st1 = threading.Thread(target=stepper_worker, args=(kit.stepper1,
stepper1_steps,
stepper_1_steps,
move_dir,
stepper_name,
stepstyles[STEPPER.SINGLE],))
st1.start()
# poll for new data every 30sec to avoid Adafruit IO Timeouts
print('delaying...')
time.sleep(30)
# Stepper 2
if not st2.isAlive(): # if thread is killed
print('Retrieving Stepper 2 Config from IO...')
stepper_2_steps = aio.receive(feed_step_2_steps.key)
stepper_2_steps = int(stepper_2_steps.value)
if stepper_2_steps is not 0: # check against if the feed value is 0
stepper_2_direction = aio.receive(feed_step_2_direction.key)
stepper_2_step_size = aio.receive(feed_step_2_step_size.key)
print('Stepper 2 Configuration (from IO)')
print("%d steps" % stepper_2_steps)
print('Step Size: ', stepper_2_step_size.value)
print('Stepper Direction: ', stepper_2_direction.value)
stepper_name = "Stepper 2"
# Set Stepper Direction
if stepper_2_direction.value == 'Forward':
move_dir = STEPPER.FORWARD
print("forward")
elif stepper_2_direction.value == 'Backward':
move_dir = STEPPER.BACKWARD
print("backward")
# Stepper 2 Thread
st2 = threading.Thread(target=stepper_worker, args=(kit.stepper2,
stepper_2_steps,
move_dir,
stepper_name,
stepstyles[STEPPER.SINGLE],))
st2.start()
# poll for new data every 30sec to avoid Adafruit IO Timeouts
print('delaying checking Adafruit IO for %d seconds...' % ADAFRUIT_IO_DELAY)
time.sleep(ADAFRUIT_IO_DELAY)