Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
trinamic-bp committed Dec 16, 2022
2 parents 99af1b2 + 5d433f0 commit 1f61f2d
Show file tree
Hide file tree
Showing 53 changed files with 2,390 additions and 796 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pip install pytrinamic

## Getting Started

Please have a look at the [code examples on GitHub](https://github.com/trinamic/PyTrinamic/tree/feature_feature_hierarchy_v2/examples).
Please have a look at the [code examples on GitHub](https://github.com/trinamic/PyTrinamic/tree/master/examples).

## Migration Guide<a id="migration-guide"></a>

Expand Down
15 changes: 11 additions & 4 deletions examples/evalboards/MAX22216/plunger_move.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@

import logging
import time

import pytrinamic
from pytrinamic.connections import ConnectionManager
from pytrinamic.ic import MAX22216
from pytrinamic.evalboards import MAX22216_eval
import time


logging.basicConfig(level=logging.DEBUG)

pytrinamic.show_info()

with ConnectionManager(debug=True).connect() as my_interface:
with ConnectionManager().connect() as my_interface:
print(my_interface)

eval = MAX22216_eval(my_interface)
ic = eval.ics[0]
solenoid = ic.motors[0]

solenoid.u_supply = 24.0 # V
solenoid.u_dc_h = 24.0 # V

solenoid.u_dc_h = 10.0 # V
solenoid.u_dc_l = 0.0 # V
solenoid.u_dc_l2h = 24.0 # V
solenoid.u_dc_l2h = 10.0 # V
solenoid.u_dc_h2l = 0.0 # V
solenoid.u_ac = 1.0 # V ampl
solenoid.f_ac = 50.0 # Hz
Expand Down
5 changes: 4 additions & 1 deletion examples/evalboards/MAX22216/ramdebug.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import logging

import pytrinamic
from pytrinamic.connections import ConnectionManager
from pytrinamic.ic import MAX22216
from pytrinamic.RAMDebug import Channel, RAMDebug, RAMDebug_Trigger

logging.basicConfig(level=logging.DEBUG)
pytrinamic.show_info()

with ConnectionManager(debug=True).connect() as my_interface:
with ConnectionManager().connect() as my_interface:
print(my_interface)

ch = Channel.field(0, MAX22216.FIELD.ADC_VM_RAW, signed=True, eval_channel=1)
Expand Down
66 changes: 66 additions & 0 deletions examples/evalboards/TMC2240/register_dump.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Dump all register values of the TMC2240 IC.
The connection to a Landungsbrücke is established over USB. TMCL commands are used for communicating with the IC.
"""
import time
import pytrinamic
from pytrinamic.connections import ConnectionManager
from pytrinamic.evalboards import TMC2240_eval

pytrinamic.show_info()

with ConnectionManager().connect() as my_interface:
print(my_interface)

eval_board = TMC2240_eval(my_interface)
mc = eval_board.ics[0]


print("GCONF: 0x{0:08X}".format(eval_board.read_register(mc.REG.GCONF)))
print("GSTAT: 0x{0:08X}".format(eval_board.read_register(mc.REG.GSTAT)))
print("IFCNT: 0x{0:08X}".format(eval_board.read_register(mc.REG.IFCNT)))
print("SLAVECONF: 0x{0:08X}".format(eval_board.read_register(mc.REG.SLAVECONF)))
print("IOIN: 0x{0:08X}".format(eval_board.read_register(mc.REG.IOIN)))
print("DRV_CONF: 0x{0:08X}".format(eval_board.read_register(mc.REG.DRV_CONF)))
print("GLOBAL_SCALE 0x{0:08X}".format(eval_board.read_register(mc.REG.GLOBAL_SCALER)))
print("IHOLD_IRUN: 0x{0:08X}".format(eval_board.read_register(mc.REG.IHOLD_IRUN)))
print("TPOWERDOWN: 0x{0:08X}".format(eval_board.read_register(mc.REG.TPOWERDOWN)))
print("TSTEP: 0x{0:08X}".format(eval_board.read_register(mc.REG.TSTEP)))
print("TPWMTHRS: 0x{0:08X}".format(eval_board.read_register(mc.REG.TPWMTHRS)))
print("TCOOLTHRS: 0x{0:08X}".format(eval_board.read_register(mc.REG.TCOOLTHRS)))
print("THIGH: 0x{0:08X}".format(eval_board.read_register(mc.REG.THIGH)))
print("DIRECT_MODE: 0x{0:08X}".format(eval_board.read_register(mc.REG.DIRECT_MODE)))
print("ENCMODE: 0x{0:08X}".format(eval_board.read_register(mc.REG.ENCMODE)))
print("X_ENC: 0x{0:08X}".format(eval_board.read_register(mc.REG.X_ENC)))
print("ENC_CONST: 0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_CONST)))
print("ENC_STATUS: 0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_STATUS)))
print("ENC_LATCH: 0x{0:08X}".format(eval_board.read_register(mc.REG.ENC_LATCH)))
print("ADC_VSUPPLY_AIN: 0x{0:08X}".format(eval_board.read_register(mc.REG.ADC_VSUPPLY_AIN)))
print("ADC_TEMP: 0x{0:08X}".format(eval_board.read_register(mc.REG.ADC_TEMP)))
print("OTW_OV_VTH: 0x{0:08X}".format(eval_board.read_register(mc.REG.OTW_OV_VTH)))
print("MSLUT_0: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT_0)))
print("MSLUT_1: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT_1)))
print("MSLUT_2: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT_2)))
print("MSLUT_3: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT_3)))
print("MSLUT_4: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT_4)))
print("MSLUT_5: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT_5)))
print("MSLUT_6: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT_6)))
print("MSLUT_7: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUT_7)))
print("MSLUTSEL: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUTSEL)))
print("MSLUTSTART: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSLUTSTART)))
print("MSCNT: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSCNT)))
print("MSCURACT: 0x{0:08X}".format(eval_board.read_register(mc.REG.MSCURACT)))
print("CHOPCONF: 0x{0:08X}".format(eval_board.read_register(mc.REG.CHOPCONF)))
print("COOLCONF: 0x{0:08X}".format(eval_board.read_register(mc.REG.COOLCONF)))
print("DCCTRL: 0x{0:08X}".format(eval_board.read_register(mc.REG.DCCTRL)))
print("DRV_STATUS: 0x{0:08X}".format(eval_board.read_register(mc.REG.DRV_STATUS)))
print("PWMCONF: 0x{0:08X}".format(eval_board.read_register(mc.REG.PWMCONF)))
print("PWM_SCALE: 0x{0:08X}".format(eval_board.read_register(mc.REG.PWM_SCALE)))
print("PWM_AUTO: 0x{0:08X}".format(eval_board.read_register(mc.REG.PWM_AUTO)))
print("SG4_THRS: 0x{0:08X}".format(eval_board.read_register(mc.REG.SG4_THRS)))
print("SG4_RESULT: 0x{0:08X}".format(eval_board.read_register(mc.REG.SG4_RESULT)))
print("SG4_IND: 0x{0:08X}".format(eval_board.read_register(mc.REG.SG4_IND)))


print("\nReady.")
45 changes: 45 additions & 0 deletions examples/evalboards/TMC2240/rotate_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Move a motor back and forth using velocity and position mode of the TMC2240
"""
import time
import pytrinamic
from pytrinamic.connections import ConnectionManager
from pytrinamic.evalboards import TMC2240_eval

pytrinamic.show_info()

with ConnectionManager().connect() as my_interface:
print(my_interface)

# Create TMC2240-EVAL class which communicates over the Landungsbrücke via TMCL
eval_board = TMC2240_eval(my_interface)
mc = eval_board.ics[0]
motor = eval_board.motors[0]

print("Preparing parameter...")
motor.set_axis_parameter(motor.AP.MaxAcceleration, 20000)
motor.set_axis_parameter(motor.AP.MaxVelocity, 100000)
motor.set_axis_parameter(motor.AP.MaxCurrent, 30)

# Clear actual positions
motor.actual_position = 0

print("Rotating...")
motor.rotate(7*25600)
time.sleep(5)

print("Stopping...")
motor.stop()
time.sleep(1)

print("Moving back to 0...")
motor.move_to(0, 100000)

# Wait until position 0 is reached
while motor.actual_position != 0:
print("Actual position: " + str(motor.actual_position))
time.sleep(0.2)

print("Reached position 0")

print("\nReady.")
1 change: 0 additions & 1 deletion examples/evalboards/TMC4671/TMC4671_eval_BLDC_open_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
pytrinamic.show_info()

with ConnectionManager().connect() as my_interface:
# my_interface.enable_debug(True)
print(my_interface)

if isinstance(my_interface, UartIcInterface):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
pytrinamic.show_info()

with ConnectionManager().connect() as my_interface:
# my_interface.enable_debug(True)
print(my_interface)

# Create a TMC4671-EVAL and TMC6100-EVAL which communicates over the Landungsbrücke via TMCL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
pytrinamic.show_info()

with ConnectionManager().connect() as my_interface:
# myInterface.enable_debug(True)
print(my_interface)

# Create a TMC4671-EVAL and TMC6200-EVAL which communicates over the Landungsbrücke via TMCL
Expand Down
15 changes: 12 additions & 3 deletions examples/evalboards/TMC5160/rotate_demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""
Move a motor back and forth using velocity and position mode of the TMC5160
Line 31, we set a lower run/standby current for the motor. Using NEMA17, this should result in a coil current around 800mA.
If the motor is stalling due to too low current, set motorCurrent higher.
If a lower value still is needed, set GLOBAL_SCALER register to 128 to half motor current.
"""
import time
import pytrinamic
Expand All @@ -16,7 +20,7 @@
mc = eval_board.ics[0]
motor = eval_board.motors[0]

print("Preparing parameter...")
print("Preparing parameters...")
eval_board.write_register(mc.REG.A1, 1000)
eval_board.write_register(mc.REG.V1, 50000)
eval_board.write_register(mc.REG.D1, 500)
Expand All @@ -25,19 +29,24 @@
eval_board.write_register(mc.REG.VSTOP, 10)
eval_board.write_register(mc.REG.AMAX, 1000)

# Set lower run/standby current
motorCurrent = 2
motor.set_axis_parameter(motor.AP.MaxCurrent, motorCurrent)
motor.set_axis_parameter(motor.AP.StandbyCurrent, motorCurrent)

# Clear actual positions
motor.actual_position = 0

print("Rotating...")
motor.rotate(7*25600)
motor.rotate(7 * 25600)
time.sleep(5)

print("Stopping...")
motor.stop()
time.sleep(1)

print("Moving back to 0...")
motor.move_to(0, 100000)
motor.move_to(0, 7 * 25600)

# Wait until position 0 is reached
while motor.actual_position != 0:
Expand Down
78 changes: 78 additions & 0 deletions examples/modules/TMCM1231/TMCL/StallGuard2_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Sets the StallGuard2 threshold such that the stall guard value (i.e SG value) is zero
when the motor comes close to stall and also sets the stop on stall velocity to a value
one less than the actual velocity of the motor
"""
import pytrinamic
from pytrinamic.connections import ConnectionManager
from pytrinamic.modules import TMCM1231
import time

def stallguard2_init(motor, init_velocity):
# Resetting SG2 threshold and stop on stall velocity to zero
motor.stallguard2.set_threshold(0)
motor.stallguard2.stop_velocity = 0
print("Initial StallGuard2 values:")
print(motor.stallguard2)
print("Rotating...")
motor.rotate(init_velocity)
sgthresh = 0
sgt = 0
load_samples = []
while (sgt == 0) and (sgthresh < 64):
load_samples = []
motor.stallguard2.set_threshold(sgthresh)
time.sleep(0.2)
sgthresh += 1
for i in range(50):
load_samples.append(motor.stallguard2.get_load_value())
if not any(load_samples):
sgt = 0
else:
sgt = max(load_samples)
while 1:
load_samples = []
for i in range(50):
load_samples.append(motor.stallguard2.get_load_value())
if 0 in load_samples:
motor.drive_settings.max_current = motor.drive_settings.max_current - 1
else:
break

motor.stallguard2.stop_velocity = motor.get_actual_velocity() - 1
print("Configured StallGuard2 parameters:")
print(motor.stallguard2)

def main():
pytrinamic.show_info()

# This example is using PCAN, if you want to use another connection please change the next line.
connection_manager = ConnectionManager("--interface pcan_tmcl")
with connection_manager.connect() as my_interface:
module = TMCM1231(my_interface)
motor = module.motors[0]

print("Preparing parameters")
# preparing drive settings
motor.drive_settings.max_current = 20
motor.drive_settings.standby_current = 8
motor.drive_settings.boost_current = 0
motor.drive_settings.microstep_resolution = motor.ENUM.microstep_resolution_256_microsteps
print(motor.drive_settings)
print(motor.linear_ramp)

time.sleep(1.0)

# clear position counter
motor.actual_position = 0

# set up StallGuard2
print("Configuring StallGuard2 parameters...")
stallguard2_init(motor, init_velocity = 10000)
print("Apply load and try to stall the motor...")
while not (motor.actual_velocity == 0):
pass
print("Motor stopped by StallGuard2!")

if __name__ == "__main__":
main()
68 changes: 68 additions & 0 deletions examples/modules/TMCM1231/TMCL/rotate_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import pytrinamic
from pytrinamic.connections import ConnectionManager
from pytrinamic.modules import TMCM1231
import time

pytrinamic.show_info()

# This example is using PCAN, if you want to use another connection please change the next line.
connectionManager = ConnectionManager("--interface pcan_tmcl")
with connectionManager.connect() as myInterface:
module = TMCM1231(myInterface)
motor = module.motors[0]

# Please be sure not to use a too high current setting for your motor.

print("Preparing parameters")
# preparing drive settings
motor.drive_settings.max_current = 128
motor.drive_settings.standby_current = 0
motor.drive_settings.boost_current = 0
motor.drive_settings.microstep_resolution = motor.ENUM.microstep_resolution_256_microsteps
print(motor.drive_settings)


# preparing linear ramp settings
motor.max_acceleration = 51200
motor.max_velocity = 51200

# reset actual position
motor.actual_position = 0

print(motor.linear_ramp)

# start rotating motor in different directions
print("Rotating")
motor.rotate(51200)
time.sleep(5)

# stop rotating motor
print("Stopping")
motor.stop()

# read actual position
print("ActualPostion = {}".format(motor.actual_position))
time.sleep(2)

# doubling moved distance
print("Doubling moved distance")
motor.move_by(motor.actual_position)

# wait till position_reached
while not(motor.get_position_reached()):
print("target position motor: " + str(motor.target_position) + " actual position motor: " + str(motor.actual_position))

time.sleep(0.2)
print("Furthest point reached")
print("ActualPostion motor = {}".format(motor.actual_position))

# short delay and move back to start
time.sleep(2)
print("Moving back to 0")
motor.move_to(0)

# wait until position 0 is reached
while not(motor.get_position_reached()):
print("target position motor: " + str(motor.target_position) + " actual position motor: " + str(motor.actual_position))

print("Reached Position 0")
Loading

0 comments on commit 1f61f2d

Please sign in to comment.