Skip to content

Commit

Permalink
Merge pull request #65
Browse files Browse the repository at this point in the history
Fix stepper continuous reverse direction
  • Loading branch information
MrYsLab committed Sep 24, 2023
2 parents 2a4e658 + 6d21b5b commit 9df4ad4
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/stepper_absolute.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def step_absolute(the_board):
# 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,
motor = the_board.set_pin_mode_stepper(interface=1, pin1=PULSE_PIN,
pin2=DIRECTION_PIN)

# if you are using a 28BYJ-48 Stepper Motor with ULN2003
Expand Down
42 changes: 26 additions & 16 deletions examples/stepper_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"""
Run a motor continuously without acceleration
"""
# GPIO Pins
PULSE_PIN = 8
DIRECTION_PIN = 9

# Create a Telemetrix instance.
board = telemetrix.Telemetrix()
Expand All @@ -39,24 +42,31 @@ def the_callback(data):


# create an accelstepper instance for a TB6600 motor driver
# motor = board.set_pin_mode_stepper(interface=1, pin1=7, pin2=8)
motor = board.set_pin_mode_stepper(interface=1, 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 = board.set_pin_mode_stepper(interface=8, pin1=8, pin2=10, pin3=9, pin4=11)
try:
while True:
# set the max speed and speed
board.stepper_set_max_speed(motor, 999)
board.stepper_set_speed(motor, 500)

# run the motor
board.stepper_run_speed(motor)

# set the max speed and speed
board.stepper_set_max_speed(motor, 900)
board.stepper_set_speed(motor, 500)
time.sleep(5)

# reverse direction
board.stepper_stop(motor)
time.sleep(2)

board.stepper_set_max_speed(motor, 999)
board.stepper_set_speed(motor, -500)
board.stepper_run_speed(motor)

time.sleep(5)

except KeyboardInterrupt:
board.shutdown()
sys.exit(0)

# run the motor
board.stepper_run_speed(motor)

# keep application running
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
board.shutdown()
sys.exit(0)
2 changes: 1 addition & 1 deletion examples/stepper_relative.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def step_relative(the_board):
# 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,
motor = the_board.set_pin_mode_stepper(interface=1, pin1=PULSE_PIN,
pin2=DIRECTION_PIN)

# if you are using a 28BYJ-48 Stepper Motor with ULN2003
Expand Down
5 changes: 4 additions & 1 deletion examples/stepper_run_speed_to_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"""
Run a motor using runSpeedToPosition position
"""
# GPIO Pins
PULSE_PIN = 8
DIRECTION_PIN = 9

# Create a Telemetrix instance.
board = telemetrix.Telemetrix()
Expand All @@ -39,7 +42,7 @@ def the_callback(data):

# 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)
motor = board.set_pin_mode_stepper(interface=1, pin1=PULSE_PIN, pin2=DIRECTION_PIN)


# set the max speed and target position
Expand Down
4 changes: 2 additions & 2 deletions examples/wifi/blink_wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"""

# some globals
DIGITAL_PIN = 5 # the board LED
DIGITAL_PIN = 2 # the board LED

# Create a Telemetrix instance.
board = telemetrix.Telemetrix(ip_address='192.168.2.112')
board = telemetrix.Telemetrix(ip_address='192.168.2.118', ip_port=31336)

# Set the DIGITAL_PIN as an output pin
board.set_pin_mode_digital_output(DIGITAL_PIN)
Expand Down
2 changes: 1 addition & 1 deletion examples/wifi/servo_wifi.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
SERVO_PIN = 4

# Create a Telemetrix instance.
board = telemetrix.Telemetrix(ip_address='192.168.2.220')
board = telemetrix.Telemetrix(ip_address='192.168.2.168')
board.set_pin_mode_servo(SERVO_PIN)
time.sleep(.2)
board.servo_write(SERVO_PIN, 90)
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.20',
version='1.30',
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 @@ -103,7 +103,7 @@ class PrivateConstants:
FEATURES = 20
DEBUG_PRINT = 99

TELEMETRIX_VERSION = "1.20"
TELEMETRIX_VERSION = "1.30"

# reporting control
REPORTING_DISABLE_ALL = 0
Expand Down
32 changes: 22 additions & 10 deletions telemetrix/telemetrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ def __init__(self, com_port=None, arduino_instance_id=1,
raise RuntimeError(f'Telemetrix4Arduino firmware version')

else:
if self.firmware_version[0] < 5:
raise RuntimeError('Please upgrade the server firmware to version 5.0.0 or greater')
# if self.firmware_version[0] < 5:
# raise RuntimeError('Please upgrade the server firmware to version '
# '5.0.0 or greater')
print(f'Telemetrix4Arduino firmware version: {self.firmware_version[0]}.'
f'{self.firmware_version[1]}.{self.firmware_version[2]}')
command = [PrivateConstants.ENABLE_ALL_REPORTS]
Expand Down Expand Up @@ -1280,31 +1281,42 @@ def stepper_set_speed(self, motor_id, speed):
:param motor_id: 0 - 3
:param speed: 0 - 1000 The desired constant speed in steps per
:param speed: -1000 - 1000 The desired constant speed in steps per
second. Positive is clockwise. Speeds of more than 1000 steps per
second are unreliable. Speed accuracy depends on the Arduino
crystal. Jitter depends on how frequently you call the
stepper_run_speed() method.
The speed will be limited by the current value of
stepper_set_max_speed().
"""

if not self.stepper_info_list[motor_id]['instance']:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('stepper_set_speed: Invalid motor_id.')

if not 0 < speed <= 1000:
if not -1000 <= speed <= 1000:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('stepper_set_speed: Speed range is 0 - '
'1000.')
raise RuntimeError('stepper_set_speed: speed range is -1000 to 1000')

self.stepper_info_list[motor_id]['speed'] = speed
if speed < 0:
polarity = 1
else:
polarity = 0

speed_msb = speed >> 8
speed_lsb = speed & 0xff
speed = abs(speed)
if not self.stepper_info_list[motor_id]['instance']:
if self.shutdown_on_exception:
self.shutdown()
raise RuntimeError('stepper_move: Invalid motor_id.')

position_bytes = list(speed.to_bytes(2, 'big', signed=True))

command = [PrivateConstants.STEPPER_SET_SPEED, motor_id, speed_msb, speed_lsb]
command = [PrivateConstants.STEPPER_SET_SPEED, motor_id]
for value in position_bytes:
command.append(value)
command.append(polarity)
self._send_command(command)

def stepper_get_speed(self, motor_id):
Expand Down

0 comments on commit 9df4ad4

Please sign in to comment.