From 08297801238a90fd489aa08e84c270c13cf52049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6hlke?= Date: Wed, 3 Jul 2024 18:05:39 +0100 Subject: [PATCH 1/6] added uartless demo script --- demo/demo_script_09_uartless.py | 121 ++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 demo/demo_script_09_uartless.py diff --git a/demo/demo_script_09_uartless.py b/demo/demo_script_09_uartless.py new file mode 100644 index 0000000..36c6a0d --- /dev/null +++ b/demo/demo_script_09_uartless.py @@ -0,0 +1,121 @@ +#pylint: disable=wildcard-import +#pylint: disable=unused-wildcard-import +#pylint: disable=unused-import +#pylint: disable=duplicate-code +""" +test file for testing basic movement +""" + +import time +try: + from src.TMC_2209.TMC_2209_StepperDriver import * + from src.TMC_2209._TMC_2209_GPIO_board import Board +except ModuleNotFoundError: + from TMC_2209.TMC_2209_StepperDriver import * + from TMC_2209._TMC_2209_GPIO_board import Board + + +print("---") +print("SCRIPT START") +print("---") + + + + + +#----------------------------------------------------------------------- +# initiate the TMC_2209 class +# use your pins for pin_en, pin_step, pin_dir here +#----------------------------------------------------------------------- +if BOARD == Board.RASPBERRY_PI: + tmc = TMC_2209(21, 16, 20, loglevel=Loglevel.DEBUG, skip_uart_init=True) +elif BOARD == Board.RASPBERRY_PI5: + tmc = TMC_2209(21, 16, 20, loglevel=Loglevel.DEBUG, skip_uart_init=True) +elif BOARD == Board.NVIDIA_JETSON: + tmc = TMC_2209(13, 6, 5, loglevel=Loglevel.DEBUG, skip_uart_init=True) +else: + # just in case + tmc = TMC_2209(21, 16, 20, loglevel=Loglevel.DEBUG, skip_uart_init=True) + + + + + + +#----------------------------------------------------------------------- +# set the loglevel of the libary (currently only printed) +# set whether the movement should be relative or absolute +# both optional +#----------------------------------------------------------------------- +tmc.tmc_logger.set_loglevel(Loglevel.DEBUG) +tmc.set_movement_abs_rel(MovementAbsRel.ABSOLUTE) + + + + + + +#----------------------------------------------------------------------- +# set the Acceleration and maximal Speed +#----------------------------------------------------------------------- +# tmc.set_acceleration(2000) +# tmc.set_max_speed(500) + +#----------------------------------------------------------------------- +# set the Acceleration and maximal Speed in fullsteps +#----------------------------------------------------------------------- +tmc.set_acceleration_fullstep(1000) +tmc.set_max_speed_fullstep(250) + + + + + + + +#----------------------------------------------------------------------- +# activate the motor current output +#----------------------------------------------------------------------- +tmc.set_motor_enabled(True) + + + + + +#----------------------------------------------------------------------- +# move the motor 1 revolution +#----------------------------------------------------------------------- +tmc.run_to_position_steps(400) #move to position 400 +tmc.run_to_position_steps(0) #move to position 0 + + +tmc.run_to_position_steps(400, MovementAbsRel.RELATIVE) #move 400 steps forward +tmc.run_to_position_steps(-400, MovementAbsRel.RELATIVE) #move 400 steps backward + + +tmc.run_to_position_steps(400) #move to position 400 +tmc.run_to_position_steps(0) #move to position 0 + + + + + +#----------------------------------------------------------------------- +# deactivate the motor current output +#----------------------------------------------------------------------- +tmc.set_motor_enabled(False) + +print("---\n---") + + + + + +#----------------------------------------------------------------------- +# deinitiate the TMC_2209 class +#----------------------------------------------------------------------- +del tmc + +print("---") +print("SCRIPT FINISHED") +print("---") From e003275af99780ee30c1c37b85e4fd538ce30c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6hlke?= Date: Wed, 3 Jul 2024 18:06:45 +0100 Subject: [PATCH 2/6] split params of constructor in separate lines --- src/TMC_2209/TMC_2209_StepperDriver.py | 18 ++++++++++++++---- src/TMC_2209/_TMC_2209_logger.py | 7 +++++-- src/TMC_2209/_TMC_2209_uart.py | 6 +++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/TMC_2209/TMC_2209_StepperDriver.py b/src/TMC_2209/TMC_2209_StepperDriver.py index af42ea0..bb46509 100644 --- a/src/TMC_2209/TMC_2209_StepperDriver.py +++ b/src/TMC_2209/TMC_2209_StepperDriver.py @@ -100,10 +100,20 @@ class TMC_2209: - def __init__(self, pin_en, pin_step=-1, pin_dir=-1, baudrate=115200, serialport="/dev/serial0", - driver_address=0, gpio_mode=None, loglevel=None, logprefix=None, - log_handlers: list = None, log_formatter : logging.Formatter = None, - skip_uart_init: bool = False): + def __init__(self, + pin_en=-1, + pin_step=-1, + pin_dir=-1, + baudrate=115200, + serialport="/dev/serial0", + driver_address=0, + gpio_mode=None, + loglevel=None, + logprefix=None, + log_handlers: list = None, + log_formatter : logging.Formatter = None, + skip_uart_init: bool = False + ): """constructor Args: diff --git a/src/TMC_2209/_TMC_2209_logger.py b/src/TMC_2209/_TMC_2209_logger.py index 2de29b3..05c142f 100644 --- a/src/TMC_2209/_TMC_2209_logger.py +++ b/src/TMC_2209/_TMC_2209_logger.py @@ -28,8 +28,11 @@ class TMC_logger: log messages from the TMC_2209 lib """ - def __init__(self, loglevel: Loglevel = Loglevel.INFO, logprefix: str = "TMC2209", - handlers: list = None, formatter: logging.Formatter = None): + def __init__(self, + loglevel: Loglevel = Loglevel.INFO, + logprefix: str = "TMC2209", + handlers: list = None, + formatter: logging.Formatter = None): """constructor Args: diff --git a/src/TMC_2209/_TMC_2209_uart.py b/src/TMC_2209/_TMC_2209_uart.py index c9c8da7..06fce16 100644 --- a/src/TMC_2209/_TMC_2209_uart.py +++ b/src/TMC_2209/_TMC_2209_uart.py @@ -31,7 +31,11 @@ class TMC_UART: - def __init__(self, tmc_logger, serialport, baudrate, mtr_id = 0): + def __init__(self, + tmc_logger, + serialport, + baudrate, mtr_id = 0 + ): """constructor Args: From 9cee743199c9002a2e75b53e0b3d1919f677af29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6hlke?= Date: Wed, 3 Jul 2024 18:46:04 +0100 Subject: [PATCH 3/6] added demo_script only_step_pin - added get_toff function - set_direction_reg now sets the direction variable in order to get the run_to_position function to work without the DIR gpio pin --- demo/demo_script_10_only_step_pin.py | 156 +++++++++++++++++++++++++ src/TMC_2209/TMC_2209_StepperDriver.py | 28 +++-- src/TMC_2209/_TMC_2209_comm.py | 19 +++ src/TMC_2209/_TMC_2209_reg.py | 4 + 4 files changed, 198 insertions(+), 9 deletions(-) create mode 100644 demo/demo_script_10_only_step_pin.py diff --git a/demo/demo_script_10_only_step_pin.py b/demo/demo_script_10_only_step_pin.py new file mode 100644 index 0000000..32a7b0e --- /dev/null +++ b/demo/demo_script_10_only_step_pin.py @@ -0,0 +1,156 @@ +#pylint: disable=wildcard-import +#pylint: disable=unused-wildcard-import +#pylint: disable=unused-import +#pylint: disable=duplicate-code +""" +test file for testing basic movement +""" + +import time +try: + from src.TMC_2209.TMC_2209_StepperDriver import * + from src.TMC_2209._TMC_2209_GPIO_board import Board +except ModuleNotFoundError: + from TMC_2209.TMC_2209_StepperDriver import * + from TMC_2209._TMC_2209_GPIO_board import Board + + +print("---") +print("SCRIPT START") +print("---") + + + + + +#----------------------------------------------------------------------- +# initiate the TMC_2209 class +# use your pins for pin_en, pin_step, pin_dir here +#----------------------------------------------------------------------- +if BOARD == Board.RASPBERRY_PI: + tmc = TMC_2209(pin_step=16, loglevel=Loglevel.DEBUG) +elif BOARD == Board.RASPBERRY_PI5: + tmc = TMC_2209(pin_step=16, serialport="/dev/ttyAMA0", loglevel=Loglevel.DEBUG) +elif BOARD == Board.NVIDIA_JETSON: + tmc = TMC_2209(pin_step=13, serialport="/dev/ttyTHS1", loglevel=Loglevel.DEBUG) +else: + # just in case + tmc = TMC_2209(pin_step=21, loglevel=Loglevel.DEBUG) + + + + + + +#----------------------------------------------------------------------- +# set the loglevel of the libary (currently only printed) +# set whether the movement should be relative or absolute +# both optional +#----------------------------------------------------------------------- +tmc.tmc_logger.set_loglevel(Loglevel.DEBUG) +tmc.set_movement_abs_rel(MovementAbsRel.ABSOLUTE) + + + + + +#----------------------------------------------------------------------- +# these functions change settings in the TMC register +#----------------------------------------------------------------------- +tmc.set_toff(0) +tmc.set_direction_reg(False) +tmc.set_current(300) +tmc.set_interpolation(True) +tmc.set_spreadcycle(False) +tmc.set_microstepping_resolution(2) +tmc.set_internal_rsense(False) + + +print("---\n---") + + + + + +#----------------------------------------------------------------------- +# these functions read and print the current settings in the TMC register +#----------------------------------------------------------------------- +tmc.read_ioin() +tmc.read_chopconf() +tmc.read_drv_status() +tmc.read_gconf() + +print("---\n---") + + + + + +#----------------------------------------------------------------------- +# set the Acceleration and maximal Speed +#----------------------------------------------------------------------- +# tmc.set_acceleration(2000) +# tmc.set_max_speed(500) + +#----------------------------------------------------------------------- +# set the Acceleration and maximal Speed in fullsteps +#----------------------------------------------------------------------- +tmc.set_acceleration_fullstep(1000) +tmc.set_max_speed_fullstep(250) + + + + + + + +#----------------------------------------------------------------------- +# activate the motor current output +#----------------------------------------------------------------------- +#tmc.set_motor_enabled(True) +tmc.set_toff(3) + + + + +#----------------------------------------------------------------------- +# move the motor 1 revolution +#----------------------------------------------------------------------- +tmc.run_to_position_steps(400) #move to position 400 +tmc.set_direction_reg(True) +tmc.run_to_position_steps(0) #move to position 0 + +tmc.set_direction_reg(False) +tmc.run_to_position_steps(400, MovementAbsRel.RELATIVE) #move 400 steps forward +tmc.set_direction_reg(True) +tmc.run_to_position_steps(-400, MovementAbsRel.RELATIVE) #move 400 steps backward + +tmc.set_direction_reg(False) +tmc.run_to_position_steps(400) #move to position 400 +tmc.set_direction_reg(True) +tmc.run_to_position_steps(0) #move to position 0 + + + + + +#----------------------------------------------------------------------- +# deactivate the motor current output +#----------------------------------------------------------------------- +#tmc.set_motor_enabled(False) +tmc.set_toff(0) + +print("---\n---") + + + + + +#----------------------------------------------------------------------- +# deinitiate the TMC_2209 class +#----------------------------------------------------------------------- +del tmc + +print("---") +print("SCRIPT FINISHED") +print("---") diff --git a/src/TMC_2209/TMC_2209_StepperDriver.py b/src/TMC_2209/TMC_2209_StepperDriver.py index bb46509..9efe9c8 100644 --- a/src/TMC_2209/TMC_2209_StepperDriver.py +++ b/src/TMC_2209/TMC_2209_StepperDriver.py @@ -41,7 +41,7 @@ class TMC_2209: read_microstepping_resolution, get_microstepping_resolution, set_microstepping_resolution, set_mstep_resolution_reg_select, get_interface_transmission_counter, get_tstep, set_vactual, get_stallguard_result, set_stallguard_threshold, set_coolstep_threshold, - get_microstep_counter, get_microstep_counter_in_steps, set_toff + get_microstep_counter, get_microstep_counter_in_steps, get_toff, set_toff ) from ._TMC_2209_move import ( @@ -144,8 +144,9 @@ def __init__(self, TMC_gpio.init(gpio_mode) self.tmc_logger.log(f"EN Pin: {pin_en}", Loglevel.DEBUG) - self._pin_en = pin_en - TMC_gpio.gpio_setup(self._pin_en, GpioMode.OUT, initial=Gpio.HIGH) + if pin_en != -1: + self._pin_en = pin_en + TMC_gpio.gpio_setup(self._pin_en, GpioMode.OUT, initial=Gpio.HIGH) self.tmc_logger.log(f"STEP Pin: {pin_step}", Loglevel.DEBUG) if pin_step != -1: @@ -210,8 +211,11 @@ def set_motor_enabled(self, en): Args: en (bool): whether the motor current output should be enabled """ - TMC_gpio.gpio_output(self._pin_en, not en) - self.tmc_logger.log(f"Motor output active: {en}", Loglevel.INFO) + if self._pin_en != -1: + TMC_gpio.gpio_output(self._pin_en, not en) + self.tmc_logger.log(f"Motor output active: {en}", Loglevel.INFO) + else: + self.tmc_logger.log(f"Motor pin is: {self._pin_en}", Loglevel.INFO) @@ -324,8 +328,11 @@ def do_homing2(self, revolutions, threshold=None): def reverse_direction_pin(self): """reverses the motor shaft direction""" - self._direction = not self._direction - TMC_gpio.gpio_output(self._pin_dir, self._direction) + if self._pin_dir != -1: + self._direction = not self._direction + TMC_gpio.gpio_output(self._pin_dir, self._direction) + else: + self.tmc_logger.log(f"Direction pin is: {self._pin_dir}", Loglevel.INFO) @@ -335,8 +342,11 @@ def set_direction_pin(self, direction): Args: direction (bool): motor shaft direction: False = CCW; True = CW """ - self._direction = direction - TMC_gpio.gpio_output(self._pin_dir, direction) + if self._pin_dir != -1: + self._direction = direction + TMC_gpio.gpio_output(self._pin_dir, direction) + else: + self.tmc_logger.log(f"Direction pin is: {self._pin_dir}", Loglevel.INFO) diff --git a/src/TMC_2209/_TMC_2209_comm.py b/src/TMC_2209/_TMC_2209_comm.py index de437d8..55963ad 100644 --- a/src/TMC_2209/_TMC_2209_comm.py +++ b/src/TMC_2209/_TMC_2209_comm.py @@ -242,6 +242,7 @@ def set_direction_reg(self, direction): self.tmc_logger.log("write normal motor direction", Loglevel.INFO) gconf = self.tmc_uart.clear_bit(gconf, tmc_reg.shaft) self.tmc_uart.write_reg_check(tmc_reg.GCONF, gconf) + self._direction = not direction @@ -505,6 +506,23 @@ def set_interpolation(self, en): +def get_toff(self): + """returns the TOFF register value + + Returns: + int: TOFF register value + """ + chopconf = self.tmc_uart.read_int(tmc_reg.CHOPCONF) + + toff = chopconf & (tmc_reg.toff0 | tmc_reg.toff1 | + tmc_reg.toff2 | tmc_reg.toff3) + + toff = toff >> 0 + + return toff + + + def set_toff(self, toff): """Sets TOFF register to value @@ -530,6 +548,7 @@ def set_toff(self, toff): self.tmc_logger.log(f"writing toff setting: {str(toff)}", Loglevel.INFO) + def read_microstepping_resolution(self): """returns the current native microstep resolution (1-256) this reads the value from the driver register diff --git a/src/TMC_2209/_TMC_2209_reg.py b/src/TMC_2209/_TMC_2209_reg.py index 785be38..1cb7c77 100644 --- a/src/TMC_2209/_TMC_2209_reg.py +++ b/src/TMC_2209/_TMC_2209_reg.py @@ -40,6 +40,10 @@ uv_cp = 1<<2 #CHOPCONF +toff0 = 1<<0 +toff1 = 1<<1 +toff2 = 1<<2 +toff3 = 1<<3 vsense = 1<<17 msres0 = 1<<24 msres1 = 1<<25 From 03e6f12e626d4befd2fa3787d11f670d1cc7a7a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6hlke?= Date: Wed, 3 Jul 2024 18:49:25 +0100 Subject: [PATCH 4/6] pylint fix: removed trailing whitespaces --- src/TMC_2209/_TMC_2209_reg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TMC_2209/_TMC_2209_reg.py b/src/TMC_2209/_TMC_2209_reg.py index 1cb7c77..dd5ac88 100644 --- a/src/TMC_2209/_TMC_2209_reg.py +++ b/src/TMC_2209/_TMC_2209_reg.py @@ -41,7 +41,7 @@ #CHOPCONF toff0 = 1<<0 -toff1 = 1<<1 +toff1 = 1<<1 toff2 = 1<<2 toff3 = 1<<3 vsense = 1<<17 From 23e7bdaa1a16aa877f42063d78a6ea6028704458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6hlke?= Date: Wed, 3 Jul 2024 19:05:10 +0100 Subject: [PATCH 5/6] added function set_direction_pin_or_reg - run_to_position now internally decides whether to use pins or reg to change direction --- demo/demo_script_10_only_step_pin.py | 5 ----- src/TMC_2209/TMC_2209_StepperDriver.py | 14 ++++++++++++++ src/TMC_2209/_TMC_2209_move.py | 4 ++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/demo/demo_script_10_only_step_pin.py b/demo/demo_script_10_only_step_pin.py index 32a7b0e..45900cd 100644 --- a/demo/demo_script_10_only_step_pin.py +++ b/demo/demo_script_10_only_step_pin.py @@ -117,17 +117,12 @@ # move the motor 1 revolution #----------------------------------------------------------------------- tmc.run_to_position_steps(400) #move to position 400 -tmc.set_direction_reg(True) tmc.run_to_position_steps(0) #move to position 0 -tmc.set_direction_reg(False) tmc.run_to_position_steps(400, MovementAbsRel.RELATIVE) #move 400 steps forward -tmc.set_direction_reg(True) tmc.run_to_position_steps(-400, MovementAbsRel.RELATIVE) #move 400 steps backward -tmc.set_direction_reg(False) tmc.run_to_position_steps(400) #move to position 400 -tmc.set_direction_reg(True) tmc.run_to_position_steps(0) #move to position 0 diff --git a/src/TMC_2209/TMC_2209_StepperDriver.py b/src/TMC_2209/TMC_2209_StepperDriver.py index 9efe9c8..d156649 100644 --- a/src/TMC_2209/TMC_2209_StepperDriver.py +++ b/src/TMC_2209/TMC_2209_StepperDriver.py @@ -350,6 +350,20 @@ def set_direction_pin(self, direction): + def set_direction_pin_or_reg(self, direction): + """sets the motor shaft direction to the given value: 0 = CCW; 1 = CW + will use the reg, if pin==-1, otherwise use the pin + + Args: + direction (bool): motor shaft direction: False = CCW; True = CW + """ + if self._pin_dir != -1: + self.set_direction_pin(direction) + else: + self.set_direction_reg(not direction) #no clue, why this has to be inverted + + + def read_steps_per_rev(self): """returns how many steps are needed for one revolution. this reads the value from the tmc driver. diff --git a/src/TMC_2209/_TMC_2209_move.py b/src/TMC_2209/_TMC_2209_move.py index 82e5140..81ad89b 100644 --- a/src/TMC_2209/_TMC_2209_move.py +++ b/src/TMC_2209/_TMC_2209_move.py @@ -352,10 +352,10 @@ def compute_new_speed(self): self._cn = self._c0 TMC_gpio.gpio_output(self._pin_step, Gpio.LOW) if distance_to > 0: - self.set_direction_pin(1) + self.set_direction_pin_or_reg(1) self.tmc_logger.log("going CW", Loglevel.MOVEMENT) else: - self.set_direction_pin(0) + self.set_direction_pin_or_reg(0) self.tmc_logger.log("going CCW", Loglevel.MOVEMENT) self._movement_phase = MovementPhase.ACCELERATING else: From 37acb3b5d4c8c65d4711b659f97b37f8ff21e70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6hlke?= Date: Tue, 23 Jul 2024 18:26:51 +0200 Subject: [PATCH 6/6] changed version to 0.5.1 --- CHANGELOG.md | 6 ++++++ setup.cfg | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85940e6..406a394 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## version 0.5.1 + +- added toff setting +- added support for controlling direction during movement over UART +- added demo script for motor movement using only the STEP pin + ## version 0.5 - decoupled gpio access from gpio library diff --git a/setup.cfg b/setup.cfg index 833213a..f377ba7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = TMC_2209_Raspberry_Pi -version = 0.5 +version = 0.5.1 author = Christian Köhlke author_email = christian@koehlke.de description = this is a Python libary to drive a stepper motor with a Trinamic TMC2209 stepper driver and a Raspberry Pi