Skip to content

Commit

Permalink
AccelStepper
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYsLab committed Feb 11, 2022
1 parent 31cca28 commit 326d170
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 62 deletions.
2 changes: 1 addition & 1 deletion examples/servo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

# Create a Telemetrix instance.
board = telemetrix.Telemetrix()
board.set_pin_mode_servo(SERVO_PIN)
board.set_pin_mode_servo(SERVO_PIN, 100, 3000)
time.sleep(.2)
board.servo_write(SERVO_PIN, 90)
time.sleep(1)
Expand Down
114 changes: 86 additions & 28 deletions examples/stepper_absolute.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Copyright (c) 2021 Alan Yorinks All rights reserved.
Copyright (c) 2022 Alan Yorinks All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Expand All @@ -15,7 +15,6 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""

import sys
import time

Expand All @@ -24,15 +23,38 @@
"""
Run a motor to an absolute position. Server will send a callback notification
when motion is complete.
Motor used to test is a NEMA-17 size - 200 steps/rev, 12V 350mA.
And the driver is a TB6600 4A 9-42V Nema 17 Stepper Motor Driver.
The driver was connected as follows:
VCC 12 VDC
GND Power supply ground
ENA- Not connected
ENA+ Not connected
DIR- GND
DIR+ GPIO Pin 23 ESP32
PUL- ESP32 GND
PUL+ GPIO Pin 22 ESP32
A-, A+ Coil 1 stepper motor
B-, B+ Coil 2 stepper motor
"""

# Create a Telemetrix instance.
board = telemetrix.Telemetrix()
# GPIO Pins
PULSE_PIN = 8
DIRECTION_PIN = 9

# flag to keep track of the number of times the callback
# was called. When == 2, exit program
exit_flag = 0


def the_callback(data):
global exit_flag
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[2]))
print(f'Motor {data[1]} absolute motion completed at: {date}.')
exit_flag += 1


def running_callback(data):
Expand All @@ -42,34 +64,70 @@ def running_callback(data):
print('The motor IS NOT running.')


# create an accelstepper instance for a TB6600 motor driver
# motor = board.set_pin_mode_stepper(interface=1, pin1=7, pin2=8)
def step_absolute(the_board):

# if you are using a 28BYJ-48 Stepper Motor with ULN2003
# comment out the line above and uncomment out the line below.
motor = board.set_pin_mode_stepper(interface=8, pin1=8, pin2=10, pin3=9, pin4=11)
global exit_flag
# create an accelstepper instance for a TB6600 motor drive
# if you are using a micro stepper controller board:
# pin1 = pulse pin, pin2 = direction
motor = the_board.set_pin_mode_stepper(interface=2, pin1=PULSE_PIN,
pin2=DIRECTION_PIN)

# if you are using a 28BYJ-48 Stepper Motor with ULN2003
# comment out the line above and uncomment out the line below.
# motor = the_board.set_pin_mode_stepper(interface=4, pin1=5, pin2=4, pin3=14,
# pin4=12)

board.stepper_is_running(motor, callback=running_callback)
time.sleep(.2)
# set the max speed and acceleration
board.stepper_set_max_speed(motor, 400)
board.stepper_set_acceleration(motor, 800)
# the_board.stepper_is_running(motor, callback=running_callback)
time.sleep(.5)

# set the absolute position in steps
board.stepper_move_to(motor, 2000)
# set the max speed and acceleration
the_board.stepper_set_current_position(0, 0)
the_board.stepper_set_max_speed(motor, 400)
the_board.stepper_set_acceleration(motor, 800)

# run the motor
print('Starting motor...')
board.stepper_run(motor, completion_callback=the_callback)
time.sleep(.2)
board.stepper_is_running(motor, callback=running_callback)
time.sleep(.2)
# set the absolute position in steps
the_board.stepper_move_to(motor, 2000)

# keep application running
while True:
try:
# run the motor
print('Starting motor...')
the_board.stepper_run(motor, completion_callback=the_callback)
time.sleep(.2)
the_board.stepper_is_running(motor, callback=running_callback)
time.sleep(.2)
while exit_flag == 0:
time.sleep(.2)
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)

the_board.stepper_set_current_position(0, 0)
the_board.stepper_set_max_speed(motor, 400)
the_board.stepper_set_acceleration(motor, 800)
# set the absolute position in steps
print('Running motor in opposite direction')
the_board.stepper_move_to(motor, -2000)

the_board.stepper_run(motor, completion_callback=the_callback)
time.sleep(.2)
the_board.stepper_is_running(motor, callback=running_callback)
time.sleep(.2)

# keep application running
while exit_flag < 2:
try:
time.sleep(.2)
except KeyboardInterrupt:
the_board.shutdown()
sys.exit(0)
the_board.shutdown()
sys.exit(0)


# instantiate telemetrix
board = telemetrix.Telemetrix()
try:
# start the main function
step_absolute(board)
board.shutdown()
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)

104 changes: 81 additions & 23 deletions examples/stepper_relative.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Copyright (c) 2021 Alan Yorinks All rights reserved.
Copyright (c) 2022 Alan Yorinks All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Expand All @@ -15,47 +15,105 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""

import sys
import time

from telemetrix import telemetrix

"""
Run a motor to a relative position.
Motor used to test is a NEMA-17 size - 200 steps/rev, 12V 350mA.
And the driver is a TB6600 4A 9-42V Nema 17 Stepper Motor Driver.
The driver was connected as follows:
VCC 12 VDC
GND Power supply ground
ENA- Not connected
ENA+ Not connected
DIR- ESP32 GND
DIR+ GPIO Pin 23 ESP32
PUL- ESP32 GND
PUL+ GPIO Pin 22 ESP32
A-, A+ Coil 1 stepper motor
B-, B+ Coil 2 stepper motor
"""

# Create a Telemetrix instance.
board = telemetrix.Telemetrix()
# GPIO Pins
PULSE_PIN = 8
DIRECTION_PIN = 9

# flag to keep track of the number of times the callback
# was called. When == 2, exit program
exit_flag = 0


def the_callback(data):
global exit_flag

date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[2]))
print(f'Motor {data[1]} relative motion completed at: {date}.')
exit_flag += 1


def step_relative(the_board):

global exit_flag

# create an accelstepper instance for a TB6600 motor driver
# if you are using a micro stepper controller board:
# pin1 = pulse pin, pin2 = direction
motor = the_board.set_pin_mode_stepper(interface=2, pin1=PULSE_PIN,
pin2=DIRECTION_PIN)

# create an accelstepper instance for a TB6600 motor driver
# motor = board.set_pin_mode_stepper(interface=1, pin1=7, pin2=8)
# if you are using a 28BYJ-48 Stepper Motor with ULN2003
# comment out the line above and uncomment out the line below.
# motor = the_board.set_pin_mode_stepper(interface=4, pin1=5, pin2=4, pin3=14,
# pin4=12)

# if you are using a 28BYJ-48 Stepper Motor with ULN2003
# comment out the line above and uncomment out the line below.
motor = board.set_pin_mode_stepper(interface=8, pin1=8, pin2=10, pin3=9, pin4=11)
# set the max speed and acceleration
the_board.stepper_set_max_speed(motor, 400)
the_board.stepper_set_acceleration(motor, 800)

# set the relative position in steps
the_board.stepper_move(motor, 2000)

# set the max speed and acceleration
board.stepper_set_max_speed(motor, 400)
board.stepper_set_acceleration(motor, 800)
print('Running Motor')
# run the motor
the_board.stepper_run(motor, completion_callback=the_callback)

# keep application running
while exit_flag == 0:
try:
time.sleep(.2)
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)

print('Reversing Direction')
the_board.stepper_move(motor, -2000)
the_board.stepper_run(motor, completion_callback=the_callback)

while exit_flag < 2:
try:
time.sleep(.2)
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)

the_board.shutdown()
sys.exit(0)


# instantiate telemetrix
board = telemetrix.Telemetrix()

# set the relative position in steps
board.stepper_move(motor, 2000)

# run the motor
board.stepper_run(motor, completion_callback=the_callback)
try:
# start the main function
step_relative(board)
board.shutdown()
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)

# keep application running
while True:
try:
time.sleep(.2)
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)
7 changes: 4 additions & 3 deletions examples/wifi/stepper_absolute_wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ def running_callback(data):


# create an accelstepper instance for a TB6600 motor driver
# motor = board.set_pin_mode_stepper(interface=1, pin1=4, pin2=5)
# motor = board.set_pin_mode_stepper(interface=2, pin1=4, pin2=5)
motor = board.set_pin_mode_stepper(interface=2, pin1=5, pin2=4)


# if you are using a 28BYJ-48 Stepper Motor with ULN2003
# comment out the line above and uncomment out the line below.
# motor = board.set_pin_mode_stepper(interface=4, pin1=5, pin2=4, pin3=14, pin4=12)
motor = board.set_pin_mode_stepper(interface=1, pin1=5, pin2=4)


board.stepper_is_running(motor, callback=running_callback)
Expand All @@ -59,7 +60,7 @@ def running_callback(data):
board.stepper_set_acceleration(motor, 800)

# set the absolute position in steps
board.stepper_move_to(motor, 2000)
board.stepper_move_to(motor, -2000)

# run the motor
print('Starting motor...')
Expand Down
5 changes: 3 additions & 2 deletions examples/wifi/stepper_relative_wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

# Create a Telemetrix instance.
board = telemetrix.Telemetrix(ip_address='192.168.2.112')
time.sleep(.2)


def the_callback(data):
Expand All @@ -35,7 +36,7 @@ def the_callback(data):


# create an accelstepper instance for a TB6600 motor driver
motor = board.set_pin_mode_stepper(interface=1, pin1=5, pin2=4)
motor = board.set_pin_mode_stepper(interface=2, pin1=5, pin2=4)

# if you are using a 28BYJ-48 Stepper Motor with ULN2003
# comment out the line above and uncomment out the line below.
Expand All @@ -47,7 +48,7 @@ def the_callback(data):
board.stepper_set_acceleration(motor, 800)

# set the relative position in steps
board.stepper_move(motor, 2000)
board.stepper_move(motor, -2000)

# run the motor
board.stepper_run(motor, completion_callback=the_callback)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
packages=['telemetrix'],
install_requires=['pyserial'],

version='1.8',
version='1.9',
description="Remotely Control And Monitor Arduino-Core devices",
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
2 changes: 1 addition & 1 deletion telemetrix/private_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class PrivateConstants:
FEATURES = 20
DEBUG_PRINT = 99

TELEMETRIX_VERSION = "1.8"
TELEMETRIX_VERSION = "1.9"

# reporting control
REPORTING_DISABLE_ALL = 0
Expand Down
Loading

0 comments on commit 326d170

Please sign in to comment.