### Install PyTrinamicMicro (Python) Source: https://github.com/analogdevicesinc/pytrinamicmicro/blob/master/guides/getting_started.md Script to install PyTrinamicMicro packages on the target MicroPython system. It requires specifying the installation directory. An optional '-c' flag can be used for incremental installations. ```Python python install.py D:\ python install.py D:\ -c ``` -------------------------------- ### Execute Example Scripts (Python) Source: https://github.com/analogdevicesinc/pytrinamicmicro/blob/master/guides/getting_started.md Demonstrates two ways to execute example scripts: directly from a file path or using a shortcut defined in the MP configuration class. The latter uses a script identifier mapped internally. ```Python exec(open("PyTrinamicMicro/platforms/motionpy/examples/io/blinky.py").read()) exec(MP.script("blinky")) ``` -------------------------------- ### Build MicroPython Firmware (Bash) Source: https://github.com/analogdevicesinc/pytrinamicmicro/blob/master/guides/getting_started.md Command to build the MicroPython firmware for the PYBv11 board. It requires navigating to the micropython directory and using make commands. Ensure 'python' is linked to your Python binary. ```Bash cd micropython && make -C mpy-cross PYTHON=python && cd ports/stm32 && make submodules PYTHON=python && make BOARD=PYBV11 PYTHON=python ``` -------------------------------- ### Execute Example Scripts via MotionPy Configuration Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Demonstrates how to execute pre-configured example scripts and tests using the MotionPy platform's shortcut system. This includes running LED blinky, TMCL bridges, logger scripts, and interface version tests. It also shows how to configure the REPL and initialize the Real-Time Clock (RTC). ```python from PyTrinamicMicro.platforms.motionpy1.MotionPy import MotionPy # Execute blinky example exec(MotionPy.script("blinky")) # Execute TMCL bridge from USB to CAN exec(MotionPy.script("tmcl_bridge_usb_can")) # Execute CAN logger exec(MotionPy.script("can_logger")) # Run version test for RS232 interface exec(MotionPy.test("rs232_version")) # Run CAN interface version test exec(MotionPy.test("can_version")) # Configure REPL on UART3 at 9600 baud MotionPy.repl_uart() # Initialize RTC with datetime tuple (year, month, day, weekday, hours, minutes, seconds, subseconds) MotionPy.init_time((2023, 11, 26, 6, 14, 30, 0, 0)) ``` -------------------------------- ### Install PyTrinamicMicro Library to MicroPython Device Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Provides commands to install the PyTrinamicMicro library and its dependencies onto a MicroPython-compatible storage device, such as an SD card. Options include full installation, API-only, platform-specific builds, and cleaning existing files. ```bash # Full installation with all dependencies python install.py /path/to/sdcard --selection full # Install only PyTrinamicMicro API without examples python install.py /path/to/sdcard --selection pytrinamicmicro-api # Install for MotionPy v1 platform with compiled bytecode python install.py /path/to/sdcard --selection motionpy1 --compile # Clean install (remove existing files first) python install.py D:\ --selection full --clean --compile # Install specific components python install.py /path/to/sdcard --selection pytrinamic motionpy1 lib ``` -------------------------------- ### Control Onboard LEDs: PyTrinamicMicro Example Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Demonstrates control of onboard LEDs using GPIO pins for visual feedback. This snippet defines multiple LEDs with their respective pin assignments and initial states (on/off). It utilizes the `pyb.Pin` class for GPIO control and `time` for delays. The example logs the LED configuration. ```python from pyb import Pin import time import logging # Configure logger logger = logging.getLogger(__name__) # Define all onboard LEDs with pin assignments and initial states leds = [ ("LEDG1", Pin(Pin.cpu.A13, Pin.OUT_PP), True), # Green LED 1 ("LEDR1", Pin(Pin.cpu.A14, Pin.OUT_PP), False), # Red LED 1 ("LEDG2", Pin(Pin.cpu.A15, Pin.OUT_PP), True), # Green LED 2 ("LEDR2", Pin(Pin.cpu.B4, Pin.OUT_PP), False) # Red LED 2 ] ``` -------------------------------- ### Clone PyTrinamicMicro Repository with Submodules (Git) Source: https://github.com/analogdevicesinc/pytrinamicmicro/blob/master/README.MD This code snippet demonstrates the Git command to clone the PyTrinamicMicro repository, ensuring that all its submodules are also downloaded. This is essential for setting up the project with all its dependencies. ```bash git clone https://github.com/trinamic/PyTrinamicMicro.git --recurse-submodules ``` -------------------------------- ### Control LEDs and Log Status Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Initializes LEDs to predefined states and then toggles them periodically, logging the status of each LED. This script demonstrates basic LED control and status logging within the PyTrinamicMicro environment. ```python from PyTrinamicMicro import PyTrinamicMicro import logging import time # Assume leds is a list of tuples: (name, pin_object, initial_value) # Example: leds = [("LED1", pyb.Pin.board.LED1, 1), ...] # Assume logger is configured logger = logging.getLogger(__name__) # Set initial states for led in leds: led[1].value(led[2]) logger.info(f"{led[0]} initialized to {led[2]}") # Toggle all LEDs every 0.5 seconds while True: for led in leds: led[1].value(not led[1].value()) logger.debug(f"{led[0]} state: {led[1].value()}") time.sleep(0.5) ``` -------------------------------- ### Initialize and Run TMCL-Slave Bridge via USB (Python) Source: https://github.com/analogdevicesinc/pytrinamicmicro/blob/master/guides/motionpy_operation_modes.md This snippet shows the initialization of a USB VCP interface and a TMCL-Slave bridge. It then enters a loop to continuously check for incoming TMCL requests, process them using the slave instance, and send back replies. Dependencies include `usb_vcp_tmcl_interface` and `TMCL_Slave_Bridge`. The input is a TMCL request, and the output is a TMCL reply. ```python con = usb_vcp_tmcl_interface() slave = TMCL_Slave_Bridge(MODULE_ADDRESS, HOST_ADDRESS, VERSION_STRING, BUILD_VERSION) while(not(slave.status.stop)): if(con.request_available()): request = con.receive_request() if(not(slave.filter(request))): continue reply = slave.handle_request(request) con.send_reply(reply) ``` -------------------------------- ### Implement TMCL Slave Functionality Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Implements TMCL slave functionality, enabling a device to respond to commands from a host system. This snippet shows how to initialize the slave, set axis and global parameters, and process incoming TMCL requests. It requires the TMCL_Slave class and TMCL constants. ```python from PyTrinamicMicro.TMCL_Slave import TMCL_Slave from PyTrinamic.TMCL import TMCL_Request, TMCL_Reply, TMCL_Status, TMCL # Create TMCL slave with module address 1, host address 2 slave = TMCL_Slave(module_address=1, host_address=2, version_string="0960V100", build_version=1) # Initialize axis parameters storage for 1 motor slave.ap = [{}] # Set axis parameter example slave.ap[0][4] = 1000 # Max velocity for motor 0 # Initialize global parameters slave.gp = {100: 42} # Custom global parameter # Process incoming TMCL request request = TMCL_Request( reply_address=2, module_address=1, command=TMCL.COMMANDS["GAP"], # Get Axis Parameter commandType=4, # Parameter number motorBank=0, # Motor/Bank number value=0 ) # Handle request and generate reply if slave.filter(request): reply = slave.handle_request(request) # reply.status == TMCL_Status.SUCCESS # reply.value == 1000 ``` -------------------------------- ### Configure Logging for PyTrinamicMicro Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Configures the logging system for the PyTrinamicMicro library. This includes enabling or disabling console and file logging, as well as creating module-specific loggers for detailed debugging and error reporting. The default log format includes timestamp, logger name, level, and message. ```python from PyTrinamicMicro import PyTrinamicMicro import logging # Initialize root logger PyTrinamicMicro.init() # Enable/disable console logging PyTrinamicMicro.set_logging_console_enabled(True) # Enable/disable file logging PyTrinamicMicro.set_logging_file_enabled(True) # Disable all logging (both console and file) PyTrinamicMicro.set_logging_enabled(False) # Create module-specific logger logger = logging.getLogger(__name__) logger.info("Module initialized") logger.debug("Detailed debug information") logger.error("Error occurred") # Logging is automatically configured with format: # [timestamp] [logger_name] [level] message ``` -------------------------------- ### RS485 Motor Control with PyTrinamicMicro in Python Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt This Python code snippet demonstrates how to establish communication with a TRINAMIC motor module via RS485. It initializes the serial interface, creates a TMCM_1240 module object, and performs basic motor operations including setting parameters, rotating, and stopping. Dependencies include PyTrinamic and PyTrinamicMicro libraries. The input is the RS485 port configuration and module parameters, while the output includes firmware version and motor position. ```python from PyTrinamic.modules.TMCM1240.TMCM_1240 import TMCM_1240 from PyTrinamicMicro.platforms.motionpy1.connections.rs485_tmcl_interface import rs485_tmcl_interface import time # Initialize RS485 interface (port=4, 115200 baud) con = rs485_tmcl_interface(port=4, data_rate=115200, host_id=2, module_id=1) # Create module instance module = TMCM_1240(con) # Get firmware version version = module.get_firmware_version() print(f"Firmware version: {version}") # Set maximum velocity for motor 0 module.set_axis_parameter(module.APs.MaxVelocity, 0, 2000) # Rotate motor at specified velocity module.rotate(0, 1500) time.sleep(3) # Stop motor module.stop(0) # Get actual position position = module.get_axis_parameter(module.APs.ActualPosition, 0) print(f"Final position: {position}") con.close() ``` -------------------------------- ### Initialize TMCL Bridge: USB to RS232 Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Establishes a TMCL bridge between a USB host and an RS232 module interface. This allows routing motor control commands from the host to the module. It requires PyTrinamicMicro and specific connection interfaces. The function processes commands until a stop signal is received and cleans up connections afterwards. ```python from PyTrinamicMicro import PyTrinamicMicro from PyTrinamicMicro.platforms.motionpy1.connections.rs232_tmcl_interface import rs232_tmcl_interface from PyTrinamicMicro.platforms.motionpy1.connections.usb_vcp_tmcl_interface import usb_vcp_tmcl_interface from PyTrinamicMicro.TMCL_Bridge import TMCL_Bridge import logging # Disable console logging to prevent interference with VCP PyTrinamicMicro.set_logging_console_enabled(False) logger = logging.getLogger(__name__) # Initialize host connection (USB) and module connection (RS232) host = usb_vcp_tmcl_interface() module = rs232_tmcl_interface() # Create bridge with host and module connections bridge = TMCL_Bridge(host, [{"module": module}]) # Process commands until stop signal received while not bridge.process(): pass # Clean up connections host.close() module.close() ``` -------------------------------- ### Implement Custom TMCL Host Interface Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Implements a custom host interface for receiving TMCL requests and sending replies. It extends the base `tmcl_host_interface` and requires hardware-specific send and receive methods to be implemented. ```python from PyTrinamicMicro.connections.tmcl_host_interface import tmcl_host_interface from PyTrinamic.TMCL import TMCL_Request, TMCL_Reply class CustomHostInterface(tmcl_host_interface): def __init__(self): super().__init__(host_id=2, module_id=1, debug=True) self.buffer = [] def data_available(self, host_id, module_id): return len(self.buffer) > 0 def _send(self, hostID, moduleID, data): # Implement hardware-specific send print(f"Sending to module {moduleID}: {data.hex()}") def _recv(self, hostID, moduleID): # Implement hardware-specific receive return self.buffer.pop(0) # Usage host_if = CustomHostInterface() # Check for incoming requests if host_if.request_available(): request = host_if.receive_request() # Process request... reply = TMCL_Reply(reply_address=2, module_address=1, status=100, value=0, command=request.command) reply.calculate_checksum() host_if.send_reply(reply) ``` -------------------------------- ### Configure Multi-Interface TMCL Bridge Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Sets up a TMCL bridge that can manage multiple module connections simultaneously. It allows for custom request and reply callbacks to modify communication data before forwarding or sending back to the host. Supported interfaces include USB VCP, RS232, and CAN. ```python from PyTrinamicMicro.TMCL_Bridge import TMCL_Bridge from PyTrinamicMicro.platforms.motionpy1.connections.usb_vcp_tmcl_interface import usb_vcp_tmcl_interface from PyTrinamicMicro.platforms.motionpy1.connections.rs232_tmcl_interface import rs232_tmcl_interface from PyTrinamicMicro.platforms.motionpy1.connections.can_tmcl_interface import can_tmcl_interface # Callback to modify request before forwarding def modify_request(request): request.moduleAddress = 3 # Change target module address return request # Callback to modify reply before sending to host def modify_reply(reply): reply.status = 100 # Ensure success status return reply # Initialize interfaces host = usb_vcp_tmcl_interface() rs232_module = rs232_tmcl_interface() can_module = can_tmcl_interface() # Create bridge with multiple modules and callbacks bridge = TMCL_Bridge( host_connection=host, module_connections=[ {"module": rs232_module, "request_callback": modify_request, "reply_callback": modify_reply}, {"module": can_module} ], module_id=3, host_id=2 ) # Process commands while not bridge.process(): pass host.close() rs232_module.close() can_module.close() ``` -------------------------------- ### TMCL-Bridge Forwarding and Analysis with PyTrinamic Source: https://github.com/analogdevicesinc/pytrinamicmicro/blob/master/guides/motionpy_operation_modes.md This Python snippet illustrates setting up the MotionPy as a TMCL-Bridge to forward TMCL datagrams between interfaces, with the ability to analyze commands via callback functions. It shows the initialization of host and module interfaces and the periodic processing of bridge operations. Dependencies include TMCL library constants and interface classes. ```Python from PyTrinamic.modules import TMCL def request_callback(request): global request_command request_command = request.command return request def reply_callback(reply): if(request_command != TMCL.COMMANDS["GET_FIRMWARE_VERSION"]): reply.calculate_checksum() return reply host = usb_vcp_tmcl_interface() # Assuming this function exists module = can_tmcl_interface() # Assuming this function exists bridge = TMCL_Bridge(host, [{"module":module, "request_callback":request_callback, "reply_callback":reply_callback}]) while(not(bridge.process(request_callback=request_callback, reply_callback=reply_callback))): pass ``` -------------------------------- ### Configure CAN Interface for TMCL Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Initializes CAN bus communication for TMCL motor control modules. It supports both normal (bidirectional) and silent (listen-only) modes. Configuration includes port, bitrate, host ID, and module ID. The snippet demonstrates checking for data availability and sending/receiving TMCL commands. ```python from PyTrinamicMicro.platforms.motionpy1.connections.can_tmcl_interface import can_tmcl_interface, CanModeNormal, CanModeSilent # Normal mode - full bidirectional communication # port=2, 1000 kBit/s bitrate, host_id=2, module_id=1 can_interface = can_tmcl_interface(port=2, host_id=2, module_id=1, can_mode=CanModeNormal()) # Check if data available from module if can_interface.data_available(): # Receive TMCL request/reply data = can_interface._recv(host_id=2, module_id=1) # Send TMCL command can_interface._send(host_id=2, module_id=1, data=b'\x01\x02\x03\x04\x05\x06\x07\x08\x09') # Silent mode for monitoring (listen-only) can_monitor = can_tmcl_interface(port=2, can_mode=CanModeSilent()) can_interface.close() can_monitor.close() ``` -------------------------------- ### TMCL-Master Control with PyTrinamic Source: https://github.com/analogdevicesinc/pytrinamicmicro/blob/master/guides/motionpy_operation_modes.md This Python snippet demonstrates controlling a TMCL-1270 module as a TMCL-Master using the PyTrinamic library. It initializes a CAN TMCL interface, instantiates the module wrapper, and performs basic motor control operations (rotate, stop). ```Python from PyTrinamic.modules.TMCM1270.TMCM_1270 import TMCM_1270 from PyTrinamicMicro.platforms.motionpy.connections.can_tmcl_interface import can_tmcl_interface import time con = can_tmcl_interface() module = TMCM_1270(con) module.rotate(0, 1000) time.sleep(5) module.stop(0) con.close() ``` -------------------------------- ### Control Motor via RS232: TMCM1161 Source: https://context7.com/analogdevicesinc/pytrinamicmicro/llms.txt Controls a TMCM1161 motor module using an RS232 interface. This snippet rotates a motor at a specified velocity for a duration and then stops it. It requires the PyTrinamic library and the rs232_tmcl_interface. The function takes motor index, velocity, and sleep duration as parameters. ```python from PyTrinamic.modules.TMCM1161.TMCM_1161 import TMCM_1161 from PyTrinamicMicro.platforms.motionpy1.connections.rs232_tmcl_interface import rs232_tmcl_interface import time # Establish RS232 connection with default parameters # port=2, data_rate=115200, host_id=2, module_id=1 con = rs232_tmcl_interface() # Initialize motor module module = TMCM_1161(con) # Rotate motor 0 at velocity 1000 module.rotate(0, 1000) time.sleep(5) # Stop motor module.stop(0) # Close connection con.close() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.