Skip to content

Commit 559d42b

Browse files
author
Rik Starmans
committed
fixed bug in tmc 2209 driver
1 parent dc401f3 commit 559d42b

File tree

4 files changed

+22
-44
lines changed

4 files changed

+22
-44
lines changed

src/tests/steppertest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from machine import Pin
44

5-
from tmc.TMC_2209_StepperDriver import *
5+
from tmc.stepperdriver import *
66

77

88
pin_en = 38 # enable pin
File renamed without changes.

src/tmc/TMC_2209_StepperDriver.py renamed to src/tmc/stepperdriver.py

+10-37
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import machine
55
from machine import Pin as GPIO
66

7-
from .TMC_2209_uart import TMC_UART
8-
from . import TMC_2209_reg as reg
7+
from .uart import TMC_UART
8+
from . import reg as reg
99

1010

1111
logger = logging.getLogger(__name__)
@@ -275,8 +275,7 @@ def setMotorEnabled(self, en):
275275
if en:
276276
self.p_pin_en.off()
277277
else:
278-
self.p_pin_en.on()
279-
logging.info("TMC2209: Motor output active: {}".format(en))
278+
self.p_pin_en.on()
280279

281280
#-----------------------------------------------------------------------
282281
# homes the motor in the given direction using stallguard
@@ -374,10 +373,8 @@ def getIScaleAnalog(self):
374373
def setIScaleAnalog(self,en):
375374
gconf = self.tmc_uart.read_int(reg.GCONF)
376375
if(en):
377-
logging.info("TMC2209: activated Vref for current scale")
378376
gconf = self.tmc_uart.set_bit(gconf, reg.i_scale_analog)
379377
else:
380-
logging.info("TMC2209: activated 5V-out for current scale")
381378
gconf = self.tmc_uart.clear_bit(gconf, reg.i_scale_analog)
382379
self.tmc_uart.write_reg_check(reg.GCONF, gconf)
383380

@@ -420,10 +417,8 @@ def getInternalRSense(self):
420417
def setInternalRSense(self,en):
421418
gconf = self.tmc_uart.read_int(reg.GCONF)
422419
if(en):
423-
logging.info("TMC2209: activated internal sense resistors.")
424420
gconf = self.tmc_uart.set_bit(gconf, reg.internal_rsense)
425421
else:
426-
logging.info("TMC2209: activated operation with external sense resistors")
427422
gconf = self.tmc_uart.clear_bit(gconf, reg.internal_rsense)
428423
self.tmc_uart.write_reg_check(reg.GCONF, gconf)
429424

@@ -438,9 +433,6 @@ def setIRun_Ihold(self, IHold, IRun, IHoldDelay):
438433
ihold_irun = ihold_irun | IHold << 0
439434
ihold_irun = ihold_irun | IRun << 8
440435
ihold_irun = ihold_irun | IHoldDelay << 16
441-
logging.info("TMC2209: ihold_irun: ", bin(ihold_irun))
442-
#logging.info(bin(ihold_irun))
443-
logging.info("TMC2209: writing ihold_irun")
444436
self.tmc_uart.write_reg_check(reg.IHOLD_IRUN, ihold_irun)
445437

446438
#-----------------------------------------------------------------------
@@ -454,10 +446,10 @@ def setCurrent(self, run_current, hold_current_multiplier = 0.5, hold_current_de
454446
Vfs = 0
455447

456448
if(self.getVSense()):
457-
logging.info("TMC2209: Vsense: 1")
449+
logging.debug("TMC2209: Vsense: 1")
458450
Vfs = 0.180 * Vref / 2.5
459451
else:
460-
logging.info("TMC2209: Vsense: 0")
452+
logging.debug("TMC2209: Vsense: 0")
461453
Vfs = 0.325 * Vref / 2.5
462454

463455
CS_IRun = 32.0*1.41421*run_current/1000.0*(Rsense+0.02)/Vfs - 1
@@ -471,10 +463,6 @@ def setCurrent(self, run_current, hold_current_multiplier = 0.5, hold_current_de
471463
CS_IHold = round(CS_IHold)
472464
hold_current_delay = round(hold_current_delay)
473465

474-
logging.debug("TMC2209: CS_IRun: " + str(CS_IRun))
475-
logging.debug("TMC2209: CS_IHold: " + str(CS_IHold))
476-
logging.debug("TMC2209: Delay: " + str(hold_current_delay))
477-
478466
self.setIRun_Ihold(CS_IHold, CS_IRun, hold_current_delay)
479467

480468
#-----------------------------------------------------------------------
@@ -490,10 +478,8 @@ def getSpreadCycle(self):
490478
def setSpreadCycle(self,en_spread):
491479
gconf = self.tmc_uart.read_int(reg.GCONF)
492480
if(en_spread):
493-
logging.info("TMC2209: activated Spreadcycle")
494481
gconf = self.tmc_uart.set_bit(gconf, reg.en_spreadcycle)
495482
else:
496-
logging.info("TMC2209: activated Stealthchop")
497483
gconf = self.tmc_uart.clear_bit(gconf, reg.en_spreadcycle)
498484
self.tmc_uart.write_reg_check(reg.GCONF, gconf)
499485

@@ -518,7 +504,7 @@ def setInterpolation(self, en):
518504
else:
519505
chopconf = self.tmc_uart.clear_bit(chopconf, reg.intpol)
520506

521-
logging.info("TMC2209: writing microstep interpolation setting: "+str(en))
507+
logging.debug("TMC2209: writing microstep interpolation setting: "+str(en))
522508
self.tmc_uart.write_reg_check(reg.CHOPCONF, chopconf)
523509

524510
#-----------------------------------------------------------------------
@@ -543,7 +529,7 @@ def setMicrosteppingResolution(self, msres):
543529
chopconf = int(chopconf) & int(4043309055)
544530
chopconf = chopconf | msresdezimal <<24
545531

546-
logging.info("TMC2209: writing "+str(msres)+" microstep setting")
532+
logging.debug("TMC2209: writing "+str(msres)+" microstep setting")
547533
self.tmc_uart.write_reg_check(reg.CHOPCONF, chopconf)
548534
self.setMStepResolutionRegSelect(True)
549535
self.readStepsPerRevolution()
@@ -562,7 +548,7 @@ def setMStepResolutionRegSelect(self, en):
562548
else:
563549
gconf = self.tmc_uart.clear_bit(gconf, reg.mstep_reg_select)
564550

565-
logging.info("TMC2209: writing MStep Reg Select: "+str(en))
551+
logging.debug("TMC2209: writing MStep Reg Select: "+str(en))
566552
self.tmc_uart.write_reg_check(reg.GCONF, gconf)
567553

568554
#-----------------------------------------------------------------------
@@ -585,7 +571,6 @@ def getStepsPerRevolution(self):
585571
#-----------------------------------------------------------------------
586572
def getInterfaceTransmissionCounter(self):
587573
ifcnt = self.tmc_uart.read_int(reg.IFCNT)
588-
logging.info("TMC2209: Interface Transmission Counter: "+str(ifcnt))
589574
return ifcnt
590575

591576
#-----------------------------------------------------------------------
@@ -613,23 +598,13 @@ def getStallguard_Result(self):
613598
# SG_RESULT ≤ SGTHRS*2
614599
#-----------------------------------------------------------------------
615600
def setStallguard_Threshold(self, threshold):
616-
617-
logging.info("TMC2209: sgthrs")
618-
logging.info(bin(threshold))
619-
620-
logging.info("TMC2209: writing sgthrs")
621601
self.tmc_uart.write_reg_check(reg.SGTHRS, threshold)
622602

623603
#-----------------------------------------------------------------------
624604
# This is the lower threshold velocity for switching
625605
# on smart energy CoolStep and StallGuard to DIAG output. (unsigned)
626606
#-----------------------------------------------------------------------
627607
def setCoolStep_Threshold(self, threshold):
628-
629-
logging.info("TMC2209: tcoolthrs")
630-
logging.info(bin(threshold))
631-
632-
logging.info("TMC2209: writing tcoolthrs")
633608
self.tmc_uart.write_reg_check(reg.TCOOLTHRS, threshold)
634609

635610
#-----------------------------------------------------------------------
@@ -640,10 +615,7 @@ def setCoolStep_Threshold(self, threshold):
640615
def setStallguard_Callback(self, pin_stallguard, threshold, my_callback, min_speed = 2000):
641616

642617
self.setStallguard_Threshold(threshold)
643-
self.setCoolStep_Threshold(min_speed)
644-
645-
logging.info("TMC2209: setup stallguard callback")
646-
618+
self.setCoolStep_Threshold(min_speed)
647619
#GPIO.setup(pin_stallguard, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
648620
#GPIO.add_event_detect(pin_stallguard, GPIO.RISING, callback=my_callback, bouncetime=300)
649621
p25 = machine.Pin(pin_stallguard, machine.Pin.IN, machine.Pin.PULL_DOWN)
@@ -714,3 +686,4 @@ def getAcceleration(self):
714686
def stop(self):
715687
self._stop = True
716688

689+

src/tmc/TMC_2209_uart.py renamed to src/tmc/uart.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
logger = logging.getLogger(__name__)
99

10+
class ConnectionFail(Exception):
11+
pass
12+
13+
1014
#-----------------------------------------------------------------------
1115
# TMC_UART
1216
#
@@ -74,14 +78,14 @@ def read_reg(self, reg):
7478

7579
rt = self.ser.write(bytes(self.rFrame))
7680
if rt != len(self.rFrame):
77-
logging.error("TMC2209: Err in write ", file=sys.stderr)
81+
logging.info("TMC2209: Err in write ", file=sys.stderr)
7882
return False
7983
time.sleep(self.communication_pause) # adjust per baud and hardware. Sequential reads without some delay fail.
8084
if self.ser.any():
8185
rtn = self.ser.read()#read what it self
8286
time.sleep(self.communication_pause) # adjust per baud and hardware. Sequential reads without some delay fail.
8387
if rtn is None:
84-
logging.error("TMC2209: Err in read")
88+
logging.info("TMC2209: Err in read")
8589
return ""
8690
# print("received "+str(len(rtn))+" bytes; "+str(len(rtn)*8)+" bits")
8791
return(rtn[7:11])
@@ -95,13 +99,13 @@ def read_int(self, reg):
9599
rtn = self.read_reg(reg)
96100
tries += 1
97101
if((tries>=10) | (rtn is False)):
98-
logging.info("TMC2209: after 10 tries not valid answer. exiting")
99-
logging.info("TMC2209: is Stepper Powersupply switched on ?")
100-
raise Exception("UART connection to stepper motors fails")
102+
logging.debug("TMC2209: after 10 tries not valid answer. exiting")
103+
logging.debug("TMC2209: is Stepper Powersupply switched on ?")
104+
raise ConnectionFail()
101105
if(len(rtn)>=4):
102106
break
103107
else:
104-
logging.info("TMC2209: did not get the expected 4 data bytes. Instead got "+str(len(rtn))+" Bytes")
108+
logging.debug("TMC2209: did not get the expected 4 data bytes. Instead got "+str(len(rtn))+" Bytes")
105109

106110
val = struct.unpack(">i",rtn)[0]
107111
return(val)
@@ -176,3 +180,4 @@ def set_bit(self, value, bit):
176180
def clear_bit(self, value, bit):
177181
return value & ~(bit)
178182

183+

0 commit comments

Comments
 (0)