Skip to main content

this is a Python libary to drive a stepper motor with a Trinamic TMC2209 stepper driver and a Raspberry Pi

Project description

TMC_2209_Raspberry_Pi

This is a library to drive a stepper motor with a TMC2209 stepper driver and a Raspberry Pi

This code is still experimental, so use it on your own risk.

This library is programmed in pure Python. The performance of Python is not good enough to drive the motor with high speed. So if you move the motor with high speed and this library the motor will lose steps.

My TMC2209 is a Bigtreetech TMC 2209 V1.2: https://github.com/bigtreetech/BIGTREETECH-TMC2209-V1.2

It has a rSense of 110 mOhm and it uses one Pin (PDN_UART) for UART RX and TX. So the PD_UART-Pin needs to be connected to the Raspberrry Pis RX-Pin directly and to the TX-Pin with an 1kOhm resistor. You can read more about this in the datasheet from Trinamic.

Because the TMC2209 use one shared pin for transmit and receive in the UART communication line, the Raspberry Pi also receives what it sends, Well, the Pi receives 8 bits from itself and 4 bit from the driver. So the Pi receives a total of 12 bits and only the last 4 needs to be used.

the code to run the stepper motor is based on the code of the AccelStepper Library from Mike McCauley:
https://github.com/adafruit/AccelStepper
http://www.airspayce.com/mikem/arduino/AccelStepper/

the code for the uart communication is based on this code from troxel:
https://github.com/troxel/TMC_UART

the Documentation of the TMC2209 can be found here:
https://www.trinamic.com/fileadmin/assets/Products/ICs_Documents/TMC2209_Datasheet_rev1.06.pdf

the code is also available on PyPI: https://pypi.org/project/TMC-2209-Raspberry-Pi

Installation

Installation with PIP

pip3 install TMC-2209-Raspberry-Pi

Installation with GIT

  • clone this repo to your Raspberry Pi using
git clone https://github.com/Chr157i4n/TMC2209_Raspberry_Pi
  • install the python module bitstring with
pip3 install bitstring
  • enable the serial port in
sudo raspi-config

Wiring

Pin TMC2209 connect to Function
TX or PDN_UART with 1kOhm TX of Raspberry Pi send data to TMC via UART
RX or PDN_UART directly RX of Raspberry Pi receive data from TMC via UART
VM 12V or 24V of power supply power for the motor
GND GND of power supply power for the motor
VDD 5V of Raspberry Pi does not need to be connected
GND2 GND of Raspberry Pi GND for VDD and Signals
EN GPIO21 of Raspberry Pi enable the motor output
STEP GPIO16 of Raspberry Pi moves the motor one step per pulse
DIR GPIO20 of Raspberry Pi set the direction of the motor
DIAG GPIO26 of Raspberry Pi optional, for StallGuard

The GPIO pins can be specific when initiating the class

Tests

You can run the test files from the main directory with

python3 -m tests.test_script_01_uart_connection

test_script_01_uart_connection.py

this only communicates with the TMC driver over UART. It should set some settings in the driver and then outputs the settings. When it outputs TMC2209: after 10 tries not valid answer. exiting, you need to check the UART-connection.

test_script_02_pin_connection.py

this scripts enables the raspberry GPIO output for the dir, en and step pin and then checks the tmc driver register, whether the driver sees them as HIGH or LOW. Because then enable pin is activated for a short time, the motor current ouput will be also activated in this script for a short time. This script should output: Pin DIR: OK Pin STEP: OK Pin EN: OK if not, check the connection of the pin

test_script_03_basic_movement.py

this script should move the motor 6 times one revolution back and forth.

test_script_04_stallguard.py

in this script the stallguard feature of the TMC2209 is beeing setup. a funtion will be called, if the driver detects a stall. the function stops the current movement. The motor will be moved 10 revolutions. If the movement is unhindered finished, the script outputs Movement finished successfully. If you block the motor with pliers or so, the the motor will stop and the script outputs StallGuard! and Movement was not completed

test_script_05_vactual.py

VACTUAL allows moving the motor by UART control. It gives the motor velocity in +-(2^23)-1 [μsteps / t]

test_script_06_multiple_drivers.py

Multiple drivers can be addressed via UART by setting different addresses with the MS1 and MS2 pins. Simultaneous movement of multiple motors is currently not supported.



For me this baudrates worked fine: 19200, 38400, 57600, 115200, 230400, 460800, 576000

If the TMC2209 driver is connected to Vmotor, the internal voltage regulator will create the Vio for the chip. So you dont need to connect anything to the Vio pin of the driver.

Usage

from TMC_2209.TMC_2209_StepperDriver import *
tmc = TMC_2209(16, 20, 21)

tmc.setDirection_reg(False)
tmc.setCurrent(300)
tmc.setInterpolation(True)
tmc.setSpreadCycle(False)
tmc.setMicrosteppingResolution(2)
tmc.setInternalRSense(False)

tmc.setAcceleration(2000)
tmc.setMaxSpeed(500)

tmc.setMotorEnabled(True)

tmc.runToPositionSteps(400)
tmc.runToPositionSteps(0)

tmc.setMotorEnabled(False)

Troubleshoot

if you encounter any problem, feel free to open an issue

Problem Solution
FileNotFoundError: [Errno 2]
No such file or directory: '/dev/serial0'
depending on your Raspberry Pi version, you need to enable the Serial Port
run sudo raspi-config in your terminal.
there go to '3 Interface Options' -> 'P3 Serial Port'
Would you like a login shell to be accessible over serial? No
Would you like the serial port hardware to be enabled? Yes
Finish and then reboot
PermissionError: [Errno 13]
Permission denied: '/dev/serial0'
you need to give the permission to acces the Serial Port to your current user
You may need to add your user (pi) to the dialout group with sudo usermod -a -G dialout pi
"did not get the expected 4 data bytes. Instead got 0 Bytes" You can use the 'debug_script_01_uart_connection' script to get a better reading on the received bytes and troubleshoot your problem
"the Raspberry Pi received only the sended bits" or
inconsistent received bits
Make sure the UART ist properly connected to the TMC driver and the driver is powered and working.
Make sure login shell (console) over serial is disabled

photo wiring diagram

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

TMC_2209_Raspberry_Pi-0.1.6.tar.gz (30.3 kB view details)

Uploaded Source

Built Distribution

TMC_2209_Raspberry_Pi-0.1.6-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

Details for the file TMC_2209_Raspberry_Pi-0.1.6.tar.gz.

File metadata

  • Download URL: TMC_2209_Raspberry_Pi-0.1.6.tar.gz
  • Upload date:
  • Size: 30.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.21.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/17.1.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.3

File hashes

Hashes for TMC_2209_Raspberry_Pi-0.1.6.tar.gz
Algorithm Hash digest
SHA256 b54ba0331ecbe6cdf0f9d568b5e30ad23249c9c1c852e9cb2ab8853342cf0ecb
MD5 362a0e96dac5187d337521bd58cbb6af
BLAKE2b-256 cad1336288066e77f34141791c7dae869adffe5e7c92f104c9bb4ea181460b48

See more details on using hashes here.

Provenance

File details

Details for the file TMC_2209_Raspberry_Pi-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: TMC_2209_Raspberry_Pi-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.21.0 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/17.1.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.3

File hashes

Hashes for TMC_2209_Raspberry_Pi-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5f8ffc5385956fbcf47c0d4014d56ff1b28e5df8411973e18d8ae436011bbf0e
MD5 6c4bcd13c3b3a0a10ada2ed936c598dc
BLAKE2b-256 74dbbafc636ec14330add1ecbbef89633dda7840d92bd5c32291fcb9362d576a

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page