Skip to content

Commit

Permalink
Merge pull request #72 from Chr157i4n/dev
Browse files Browse the repository at this point in the history
version 0.5.1
  • Loading branch information
Chr157i4n authored Jul 23, 2024
2 parents 468acb1 + 37acb3b commit 062e205
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
121 changes: 121 additions & 0 deletions demo/demo_script_09_uartless.py
Original file line number Diff line number Diff line change
@@ -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("---")
151 changes: 151 additions & 0 deletions demo/demo_script_10_only_step_pin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#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.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)
tmc.set_toff(0)

print("---\n---")





#-----------------------------------------------------------------------
# deinitiate the TMC_2209 class
#-----------------------------------------------------------------------
del tmc

print("---")
print("SCRIPT FINISHED")
print("---")
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
60 changes: 47 additions & 13 deletions src/TMC_2209/TMC_2209_StepperDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -134,8 +144,9 @@ def __init__(self, pin_en, pin_step=-1, pin_dir=-1, baudrate=115200, serialport=
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:
Expand Down Expand Up @@ -200,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)



Expand Down Expand Up @@ -314,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)



Expand All @@ -325,8 +342,25 @@ 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)



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



Expand Down
Loading

0 comments on commit 062e205

Please sign in to comment.