|
| 1 | +""" |
| 2 | +Demo file for basic movement |
| 3 | +""" |
| 4 | + |
| 5 | +import board |
| 6 | +from tmc_driver import ( |
| 7 | + Tmc2209, |
| 8 | + Loglevel, |
| 9 | + TmcEnableControlPin, |
| 10 | + TmcMotionControlStepDir, |
| 11 | + MovementAbsRel, |
| 12 | +) |
| 13 | +from tmc_driver.com import TmcComUartCircuitPython |
| 14 | + |
| 15 | + |
| 16 | +print("---") |
| 17 | +print("SCRIPT START") |
| 18 | +print("---") |
| 19 | + |
| 20 | +# ----------------------------------------------------------------------- |
| 21 | +# initiate the Tmc2240 class |
| 22 | +# use your pins for pin_en, pin_step, pin_dir here |
| 23 | +# ----------------------------------------------------------------------- |
| 24 | +tmc = Tmc2209( |
| 25 | + TmcEnableControlPin(board.GP18), |
| 26 | + TmcMotionControlStepDir(board.GP17, board.GP16), |
| 27 | + TmcComUartCircuitPython(tx=board.GP4, rx=board.GP5), |
| 28 | + loglevel=Loglevel.DEBUG, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +# ----------------------------------------------------------------------- |
| 33 | +# set the loglevel of the libary (currently only printed) |
| 34 | +# set whether the movement should be relative or absolute |
| 35 | +# both optional |
| 36 | +# ----------------------------------------------------------------------- |
| 37 | +tmc.tmc_logger.loglevel = Loglevel.DEBUG |
| 38 | +tmc.movement_abs_rel = MovementAbsRel.ABSOLUTE |
| 39 | + |
| 40 | + |
| 41 | +# ----------------------------------------------------------------------- |
| 42 | +# these functions change settings in the TMC register |
| 43 | +# ----------------------------------------------------------------------- |
| 44 | +tmc.set_direction_reg(False) |
| 45 | +tmc.set_current_rms(300) |
| 46 | +tmc.set_interpolation(True) |
| 47 | +tmc.set_spreadcycle(False) |
| 48 | +tmc.set_microstepping_resolution(2) |
| 49 | +tmc.set_internal_rsense(False) |
| 50 | + |
| 51 | + |
| 52 | +print("---\n---") |
| 53 | + |
| 54 | + |
| 55 | +# ----------------------------------------------------------------------- |
| 56 | +# these functions read and print the current settings in the TMC register |
| 57 | +# ----------------------------------------------------------------------- |
| 58 | +tmc.read_register("ioin") |
| 59 | +tmc.read_register("chopconf") |
| 60 | +tmc.read_register("drvstatus") |
| 61 | +tmc.read_register("gconf") |
| 62 | + |
| 63 | +print("---\n---") |
| 64 | + |
| 65 | + |
| 66 | +# ----------------------------------------------------------------------- |
| 67 | +# set the Acceleration and maximal Speed in fullsteps |
| 68 | +# ----------------------------------------------------------------------- |
| 69 | +tmc.acceleration_fullstep = 1000 |
| 70 | +tmc.max_speed_fullstep = 250 |
| 71 | + |
| 72 | + |
| 73 | +# ----------------------------------------------------------------------- |
| 74 | +# activate the motor current output |
| 75 | +# ----------------------------------------------------------------------- |
| 76 | +tmc.set_motor_enabled(True) |
| 77 | + |
| 78 | + |
| 79 | +# ----------------------------------------------------------------------- |
| 80 | +# move the motor 1 revolution |
| 81 | +# ----------------------------------------------------------------------- |
| 82 | +tmc.run_to_position_fullsteps(200) # move to position 200 (fullsteps) |
| 83 | +tmc.run_to_position_fullsteps(0) # move to position 0 |
| 84 | + |
| 85 | +tmc.run_to_position_fullsteps( |
| 86 | + 200, MovementAbsRel.RELATIVE |
| 87 | +) # move 200 fullsteps forward |
| 88 | +tmc.run_to_position_fullsteps( |
| 89 | + -200, MovementAbsRel.RELATIVE |
| 90 | +) # move 200 fullsteps backward |
| 91 | + |
| 92 | +tmc.run_to_position_steps(400) # move to position 400 (µsteps) |
| 93 | +tmc.run_to_position_steps(0) # move to position 0 |
| 94 | + |
| 95 | +tmc.run_to_position_revolutions(1) # move 1 revolution forward |
| 96 | +tmc.run_to_position_revolutions(0) # move 1 revolution backward |
| 97 | + |
| 98 | + |
| 99 | +# ----------------------------------------------------------------------- |
| 100 | +# deactivate the motor current output |
| 101 | +# ----------------------------------------------------------------------- |
| 102 | +tmc.set_motor_enabled(False) |
| 103 | +print("---\n---") |
| 104 | + |
| 105 | + |
| 106 | +# ----------------------------------------------------------------------- |
| 107 | +# deinitiate the Tmc2209 class |
| 108 | +# ----------------------------------------------------------------------- |
| 109 | +del tmc |
| 110 | + |
| 111 | +print("---") |
| 112 | +print("SCRIPT FINISHED") |
| 113 | +print("---") |
0 commit comments