Skip to content

Commit

Permalink
Merge pull request #61 from Chr157i4n/dev
Browse files Browse the repository at this point in the history
version 0.4.5
  • Loading branch information
Chr157i4n committed Apr 17, 2024
2 parents b3aaf7d + 985675c commit a1d59a5
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## version 0.4.5

- enhancement of logging module
- small bugfix

## version 0.4.4

- change logger to use logging module
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This is a library to drive a stepper motor with a TMC2209 stepper driver and a R
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.
So if you move the motor with high speed using this library the motor will lose steps.

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

Expand All @@ -23,7 +23,7 @@ You can read more about this in the datasheet from Trinamic.
Because the TMC2209 uses one shared pin for transmit and receive in the UART communication line, the Raspberry Pi also receives what it sends.
Well, the Pi receives 4 bytes from itself and 8 bytes from the driver. So the Pi receives a total of 12 bytes and only the last 8 are the reply, of which only 4 are data bytes.

the Documentation of the TMC2209 can be found here:
The Documentation of the TMC2209 can be found here:
[TMC2209 - Datsheet](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).
Expand Down Expand Up @@ -124,6 +124,10 @@ Simultaneous movement of multiple motors can be done with threaded movement.

In this script, the movement of a stepper with threads is shown. This can be used to do other task while moving a motor, or to move several motors simultaneous.

### [demo_script_08_log_to_file.py](demo/demo_script_08_log_to_file.py)

This script shows how you can alter the formatting of the TMC2209 log messages and redirect the log output to a file called `tmc2209_log_file.log` that will be created in the current directory.

\
\
For me these baudrates worked fine: 19200, 38400, 57600, 115200, 230400, 460800, 576000.
Expand Down Expand Up @@ -166,7 +170,7 @@ I reserve the right not to answer E-Mails.
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 <br /> run `sudo raspi-config` in your terminal. <br /> there go to '3 Interface Options' -> 'P3 Serial Port' <br /> Would you like a login shell to be accessible over serial? No <br /> Would you like the serial port hardware to be enabled? Yes <br /> Finish and then reboot
PermissionError: [Errno 13] <br /> Permission denied: '/dev/serial0' | you need to give the permission to acces the Serial Port to your current user <br /> You may need to add your user (pi) to the dialout group with `sudo usermod -a -G dialout pi`
PermissionError: [Errno 13] <br /> Permission denied: '/dev/serial0' | you need to give the permission to acces the Serial Port to your current user <br /> You may need to add your user (pi) to the dialout group with `sudo usermod -a -G dialout pi` and then relog. <br /> If that does not work, make sure that your user has read/write permissions on the dev file `/dev/serial0` by calling `sudo chmod 660 /dev/serial0`.
"TMC2209: UART Communication Error" | You can use the 'debug_script_01_uart_connection' script to get a better reading on the received bytes and troubleshoot your problem
"TMC2209: UART Communication Error: 0 data bytes \| 4 total bytes" | only 4 total bytes received indicates, that the Raspberry Pi receives its own data, but nothing from the TMC driver. This happens if RX and TX are connected properly, but the TMC driver has no power
"TMC2209: UART Communication Error: 0 data bytes \| 0 total bytes" | 0 total bytes received indicates, a problem with your wiring or your Raspberry Pi. This happens if TX is not connected
Expand Down
74 changes: 74 additions & 0 deletions demo/demo_script_08_log_to_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pylint: disable=wildcard-import
#pylint: disable=unused-wildcard-import
#pylint: disable=unused-import
#pylint: disable=duplicate-code
"""
test file for testing writing the log messages to a file
"""

import logging
from src.TMC_2209.TMC_2209_StepperDriver import *



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


#-----------------------------------------------------------------------
# initiate the log level, handler, and formatter
#-----------------------------------------------------------------------
loglevel = Loglevel.ALL
logging_handler = logging.FileHandler("tmc2209_log_file.log")
logformatter = logging.Formatter('%(name)s %(asctime)s - %(levelname)s - %(message)s',
'%Y%m%d %H:%M:%S')






#-----------------------------------------------------------------------
# initiate the TMC_2209 class
# use your pins for pin_en, pin_step, pin_dir here
#-----------------------------------------------------------------------
if BOARD == "RASPBERRY_PI":
tmc = TMC_2209(21, 16, 20, skip_uart_init=True,
loglevel=loglevel, log_handlers=[logging_handler], log_formatter=logformatter)
elif BOARD == "NVIDIA_JETSON":
tmc = TMC_2209(13, 6, 5, serialport="/dev/ttyTHS1", skip_uart_init=True,
loglevel=loglevel, log_handlers=[logging_handler], log_formatter=logformatter)
else:
# just in case
tmc = TMC_2209(21, 16, 20, skip_uart_init=True,
loglevel=loglevel, log_handlers=[logging_handler], log_formatter=logformatter)






#-----------------------------------------------------------------------
# Log custom messages
#-----------------------------------------------------------------------
tmc.tmc_logger.log("========================", Loglevel.ALL)
tmc.tmc_logger.log("Hello World!", Loglevel.DEBUG)
tmc.tmc_logger.log("Wow, you can even log your own messages!", Loglevel.ERROR)
tmc.tmc_logger.log("If you like this library, please give us a star on GitHub!", Loglevel.INFO)
tmc.tmc_logger.log("The cake is a lie", Loglevel.WARNING)
tmc.tmc_logger.log("I like to move it, move it", Loglevel.MOVEMENT)
tmc.tmc_logger.log("========================", Loglevel.ALL)





#-----------------------------------------------------------------------
# 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.4.4
version = 0.4.5
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
15 changes: 11 additions & 4 deletions src/TMC_2209/TMC_2209_StepperDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import time
import statistics
import logging
from ._TMC_2209_GPIO_board import GPIO, BOARD
from ._TMC_2209_uart import TMC_UART as tmc_uart
from ._TMC_2209_logger import TMC_logger, Loglevel
Expand Down Expand Up @@ -101,7 +102,8 @@ class TMC_2209:

def __init__(self, pin_en, pin_step=-1, pin_dir=-1, baudrate=115200, serialport="/dev/serial0",
driver_address=0, gpio_mode=GPIO.BCM, loglevel=None, logprefix=None,
log_handlers=None, skip_uart_init=False):
log_handlers: list = None, log_formatter : logging.Formatter = None,
skip_uart_init: bool = False):
"""constructor
Args:
Expand All @@ -113,14 +115,18 @@ def __init__(self, pin_en, pin_step=-1, pin_dir=-1, baudrate=115200, serialport=
driver_address (int, optional): driver adress [0-3]. Defaults to 0.
gpio_mode (enum, optional): gpio mode. Defaults to GPIO.BCM.
loglevel (enum, optional): loglevel. Defaults to None.
logprefix (str, optional): log prefix. Defaults to None (standard TMC prefix).
logprefix (str, optional): log prefix (name of the logger).
Defaults to None (standard TMC prefix).
log_handlers (list, optional): list of logging handlers.
Defaults to None (log to console).
log_formatter (logging.Formatter, optional): formatter for the log messages.
Defaults to None (messages are logged in the format
'%(asctime)s - %(name)s - %(levelname)s - %(message)s').
skip_uart_init (bool, optional): skip UART init. Defaults to False.
"""
if logprefix is None:
logprefix = f"TMC2209 {driver_address}"
self.tmc_logger = TMC_logger(loglevel, logprefix, log_handlers)
self.tmc_logger = TMC_logger(loglevel, logprefix, log_handlers, log_formatter)
self.tmc_uart = tmc_uart(self.tmc_logger, serialport, baudrate, driver_address)


Expand Down Expand Up @@ -482,7 +488,8 @@ def set_stallguard_callback(self, pin_stallguard, threshold, callback,
self._pin_stallguard = pin_stallguard

GPIO.setup(self._pin_stallguard, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# first remove existing events
GPIO.remove_event_detect(self._pin_stallguard)
GPIO.add_event_detect(self._pin_stallguard, GPIO.RISING, callback=self.stallguard_callback,
bouncetime=300)

Expand Down
11 changes: 6 additions & 5 deletions src/TMC_2209/_TMC_2209_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ class TMC_logger:
"""

def __init__(self, loglevel: Loglevel = Loglevel.INFO, logprefix: str = "TMC2209",
handlers=None):
handlers: list = None, formatter: logging.Formatter = None):
"""constructor
Args:
logprefix (string): new logprefix
logprefix (string): new logprefix (name of the logger) (default: "TMC2209")
loglevel (enum): level for which to log
handlers (list): list of logging handlers, see logging.handlers (default: None)
formatter (logging.Formatter): formatter for the log messages (default: None)
"""
if logprefix is None:
logprefix = "TMC2209"

# Add our custom log levels to the logger
for level in [Loglevel.ALL, Loglevel.MOVEMENT, Loglevel.NONE]:
print(level)
self._add_logging_level(level.name, level.value)

self.logger = logging.getLogger(logprefix)

self.loglevel = loglevel
self.set_loglevel(loglevel)
self.formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
if formatter is None:
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
self.formatter = formatter

if handlers is None:
# Default handler: StreamHandler (logs to console)
Expand Down Expand Up @@ -136,7 +138,6 @@ def _add_logging_level(level_name: str, level_num: int, method_name: str = None)

def logForLevel(self, message, *args, **kwargs):
if self.isEnabledFor(level_num):
print("test")
self._log(level_num, message, args, **kwargs)

def logToRoot(message, *args, **kwargs):
Expand Down

0 comments on commit a1d59a5

Please sign in to comment.