Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify code to support up to 4 TMC2209 motor drivers on a single UART pin #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
# TMC_2209_ESP32 and Micropython
This is a libary to drive a stepper motor with a TMC2209 stepper driver and a LOLIN32 V1.0.0 ESP32 Board

This is a clone off the great work from Chr157i4n, thank you for the time you spend !
https://github.com/Chr157i4n/TMC2209_Raspberry_Pi
This is a clone off the great work from Chr157i4n, thank you for the time you spent https://github.com/Chr157i4n/TMC2209_Raspberry_Pi, and a fork of https://github.com/kjk25/TMC2209_ESP32.

This code is still experimental, so use it on your own risk.
i have not tested all functions, hope its a start for others.

This fork supports multiple TMC2209 motor drivers by using the address pins on the motor driver.

The datasheet is here: https://www.analog.com/media/en/technical-documentation/data-sheets/TMC2209_datasheet_rev1.08.pdf

![](images/tmc2209_datasheet_UART.png)

I had trouble initially understanding this, but eventually I got it. This code helped https://github.com/anonymousaga/TMC2209_RPI_PICO

If you wire your UART TX to both UART pins on the two TMC2209 controllers, then you can change the address of each by wiring MS1 and/or MS2 to Vccio (your board power pin, NOT THE MOTOR PIN). The two pins form a binary address.


![](images/photo_2021-09-18_11-49-04.jpg)
| MS2 | MS1 | Address |
|-----|-----|--------|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 2 |
| 1 | 1 | 3 |


This code is still experimental, so use it on your own risk.
i have not tested all functions, hope its a start for others.

![](images/IMG_2425.png)
Binary file added images/IMG_2425.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/photo_2021-09-18_11-49-04.jpg
Binary file not shown.
Binary file added images/tmc2209_datasheet_UART.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions test_script_01_uart_connection motor 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import sys
from tmc.TMC_2209_StepperDriver import *
import time


print("---")
print("SCRIPT START")
print("---")





#-----------------------------------------------------------------------
# initiate the TMC_2209 class
# use your pins for pin_step, pin_dir, pin_en here
#-----------------------------------------------------------------------
print("INIT START")
tmc = TMC_2209(22, 23, 0, 2)
print("INIT COMPLETE")





#-----------------------------------------------------------------------
# set the loglevel of the libary (currently only printed)
# set whether the movement should be relative or absolute
# both optional
#-----------------------------------------------------------------------
tmc.setLoglevel(Loglevel.info)
tmc.setMovementAbsRel(MovementAbsRel.absolute)





#-----------------------------------------------------------------------
# these functions change settings in the TMC register
#-----------------------------------------------------------------------
tmc.setDirection_reg(False)
tmc.setVSense(True)
tmc.setCurrent(300)
tmc.setIScaleAnalog(True)
tmc.setInterpolation(True)
tmc.setSpreadCycle(False)
tmc.setMicrosteppingResolution(2)
tmc.setInternalRSense(False)


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





#-----------------------------------------------------------------------
# these functions read and print the current settings in the TMC register
#-----------------------------------------------------------------------
tmc.readIOIN()
tmc.readCHOPCONF()
tmc.readDRVSTATUS()
tmc.readGCONF()

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



#-----------------------------------------------------------------------
# deactivate the motor current output
#-----------------------------------------------------------------------
tmc.setMotorEnabled(False)

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

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

print("---")
print("SCRIPT FINISHED")
print("---")
2 changes: 1 addition & 1 deletion test_script_01_uart_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# initiate the TMC_2209 class
# use your pins for pin_step, pin_dir, pin_en here
#-----------------------------------------------------------------------
tmc = TMC_2209(27, 14, 26)
tmc = TMC_2209(18, 19, 0) # you can populate pin_en, but I just tied it to ground



Expand Down
84 changes: 84 additions & 0 deletions test_script_02_pin_connection motor 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from tmc.TMC_2209_StepperDriver import *
import time


print("---")
print("SCRIPT START")
print("---")





#-----------------------------------------------------------------------
# initiate the TMC_2209 class
# use your pins for pin_step, pin_dir, pin_en here
#-----------------------------------------------------------------------
tmc = TMC_2209(22, 23, 0, 2)





#-----------------------------------------------------------------------
# set the loglevel of the libary (currently only printed)
# set whether the movement should be relative or absolute
# both optional
#-----------------------------------------------------------------------
tmc.setLoglevel(Loglevel.debug)
tmc.setMovementAbsRel(MovementAbsRel.absolute)





#-----------------------------------------------------------------------
# these functions change settings in the TMC register
#-----------------------------------------------------------------------
tmc.setDirection_reg(False)
tmc.setVSense(True)
tmc.setCurrent(300)
tmc.setIScaleAnalog(True)
tmc.setInterpolation(True)
tmc.setSpreadCycle(False)
tmc.setMicrosteppingResolution(2)
tmc.setInternalRSense(False)


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





#-----------------------------------------------------------------------
# this function test whether the connection of the DIR, STEP and EN pin
# between Raspberry Pi and TMC driver is working
#-----------------------------------------------------------------------
tmc.testDirStepEn()

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





#-----------------------------------------------------------------------
# deactivate the motor current output
#-----------------------------------------------------------------------
tmc.setMotorEnabled(False)

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





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

print("---")
print("SCRIPT FINISHED")
print("---")
2 changes: 1 addition & 1 deletion test_script_02_pin_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# initiate the TMC_2209 class
# use your pins for pin_step, pin_dir, pin_en here
#-----------------------------------------------------------------------
tmc = TMC_2209(27, 14, 26)
tmc = TMC_2209(18, 19, 0) # you can populate pin_en, but I just tied it to ground



Expand Down
123 changes: 123 additions & 0 deletions test_script_03_basic_movement motor 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from tmc.TMC_2209_StepperDriver import *
import time


print("---")
print("SCRIPT START")
print("---")





#-----------------------------------------------------------------------
# initiate the TMC_2209 class
# use your pins for pin_step, pin_dir, pin_en here
#-----------------------------------------------------------------------
tmc = TMC_2209(22, 23, 0, 2)





#-----------------------------------------------------------------------
# set the loglevel of the libary (currently only printed)
# set whether the movement should be relative or absolute
# both optional
#-----------------------------------------------------------------------
tmc.setLoglevel(Loglevel.debug)
tmc.setMovementAbsRel(MovementAbsRel.relative)





#-----------------------------------------------------------------------
# these functions change settings in the TMC register
#-----------------------------------------------------------------------
tmc.setDirection_reg(False)
tmc.setVSense(True)
tmc.setCurrent(200)
tmc.setIScaleAnalog(True)
tmc.setInterpolation(True)
tmc.setSpreadCycle(False)
tmc.setMicrosteppingResolution(1)
tmc.setInternalRSense(False)


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





#-----------------------------------------------------------------------
# these functions read and print the current settings in the TMC register
#-----------------------------------------------------------------------
#tmc.readIOIN()
#tmc.readCHOPCONF()
#tmc.readDRVSTATUS()
#tmc.readGCONF()

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





#-----------------------------------------------------------------------
# set the Accerleration and maximal Speed
#-----------------------------------------------------------------------
tmc.setAcceleration(2000)
tmc.setMaxSpeed(500)





#-----------------------------------------------------------------------
# activate the motor current output
#-----------------------------------------------------------------------
tmc.setMotorEnabled(True)





#-----------------------------------------------------------------------
# move the motor 1 revolution
#-----------------------------------------------------------------------
tmc.runToPositionSteps(800) #move to position 400
tmc.runToPositionSteps(0) #move to position 0


tmc.runToPositionSteps(1000, MovementAbsRel.relative) #move 400 steps forward
tmc.runToPositionSteps(-600, MovementAbsRel.relative) #move 400 steps backward


tmc.runToPositionSteps(400) #move to position 400
tmc.runToPositionSteps(0) #move to position 0





#-----------------------------------------------------------------------
# deactivate the motor current output
#-----------------------------------------------------------------------
tmc.setMotorEnabled(False)

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





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

print("---")
print("SCRIPT FINISHED")
print("---")
2 changes: 1 addition & 1 deletion test_script_03_basic_movement.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# initiate the TMC_2209 class
# use your pins for pin_step, pin_dir, pin_en here
#-----------------------------------------------------------------------
tmc = TMC_2209(27, 14, 26)
tmc = TMC_2209(18, 19, 0) # you can populate pin_en, but I just tied it to ground



Expand Down
Loading