Skip to content

Commit

Permalink
added demo_script only_step_pin
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Chr157i4n committed Jul 3, 2024
1 parent e003275 commit 9cee743
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 9 deletions.
156 changes: 156 additions & 0 deletions demo/demo_script_10_only_step_pin.py
Original file line number Diff line number Diff line change
@@ -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("---")
28 changes: 19 additions & 9 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 @@ -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:
Expand Down Expand Up @@ -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)



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



Expand All @@ -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)



Expand Down
19 changes: 19 additions & 0 deletions src/TMC_2209/_TMC_2209_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/TMC_2209/_TMC_2209_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9cee743

Please sign in to comment.