diff --git a/examples/servo.py b/examples/servo.py index 2a93538..9c220b4 100644 --- a/examples/servo.py +++ b/examples/servo.py @@ -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) diff --git a/examples/stepper_absolute.py b/examples/stepper_absolute.py index c8e15d7..8fea2b0 100644 --- a/examples/stepper_absolute.py +++ b/examples/stepper_absolute.py @@ -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 @@ -15,7 +15,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """ - import sys import time @@ -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): @@ -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) + diff --git a/examples/stepper_relative.py b/examples/stepper_relative.py index 42095c8..670e25b 100644 --- a/examples/stepper_relative.py +++ b/examples/stepper_relative.py @@ -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 @@ -15,7 +15,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """ - import sys import time @@ -23,39 +22,98 @@ """ 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) diff --git a/examples/wifi/stepper_absolute_wifi.py b/examples/wifi/stepper_absolute_wifi.py index c2cff03..c3973db 100644 --- a/examples/wifi/stepper_absolute_wifi.py +++ b/examples/wifi/stepper_absolute_wifi.py @@ -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) @@ -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...') diff --git a/examples/wifi/stepper_relative_wifi.py b/examples/wifi/stepper_relative_wifi.py index 29cb30e..9c5359a 100644 --- a/examples/wifi/stepper_relative_wifi.py +++ b/examples/wifi/stepper_relative_wifi.py @@ -27,6 +27,7 @@ # Create a Telemetrix instance. board = telemetrix.Telemetrix(ip_address='192.168.2.112') +time.sleep(.2) def the_callback(data): @@ -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. @@ -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) diff --git a/setup.py b/setup.py index 15a6145..64d5e86 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/telemetrix/private_constants.py b/telemetrix/private_constants.py index 45459b5..778761e 100644 --- a/telemetrix/private_constants.py +++ b/telemetrix/private_constants.py @@ -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 diff --git a/telemetrix/telemetrix.py b/telemetrix/telemetrix.py index 6169b88..11be2a5 100644 --- a/telemetrix/telemetrix.py +++ b/telemetrix/telemetrix.py @@ -767,8 +767,8 @@ def set_pin_mode_digital_input_pullup(self, pin_number, callback=None): The pin_type for digital input pins with pullups enabled = 11 """ - self._set_pin_mode(pin_number, PrivateConstants.AT_INPUT_PULLUP, callback= - callback) + self._set_pin_mode(pin_number, PrivateConstants.AT_INPUT_PULLUP, + callback=callback) def set_pin_mode_digital_output(self, pin_number): """ @@ -1063,7 +1063,11 @@ def stepper_move_to(self, motor_id, position): :param position: target position. Maximum value is 32 bits. """ - + if position < 0: + polarity = 1 + else: + polarity = 0 + position = abs(position) if not self.stepper_info_list[motor_id]['instance']: if self.shutdown_on_exception: self.shutdown() @@ -1074,6 +1078,7 @@ def stepper_move_to(self, motor_id, position): command = [PrivateConstants.STEPPER_MOVE_TO, motor_id] for value in position_bytes: command.append(value) + command.append(polarity) self._send_command(command) def stepper_move(self, motor_id, relative_position): @@ -1086,6 +1091,12 @@ def stepper_move(self, motor_id, relative_position): position. Negative is anticlockwise from the current position. Maximum value is 32 bits. """ + if relative_position < 0: + polarity = 1 + else: + polarity = 0 + + relative_position = abs(relative_position) if not self.stepper_info_list[motor_id]['instance']: if self.shutdown_on_exception: self.shutdown() @@ -1096,6 +1107,7 @@ def stepper_move(self, motor_id, relative_position): command = [PrivateConstants.STEPPER_MOVE, motor_id] for value in position_bytes: command.append(value) + command.append(polarity) self._send_command(command) def stepper_run(self, motor_id, completion_callback=None):