-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdemo_script_07_threads.py
More file actions
109 lines (80 loc) · 2.88 KB
/
demo_script_07_threads.py
File metadata and controls
109 lines (80 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Demo file for movement of motors with threads
"""
import time
from tmc_driver import (
Tmc2209,
Loglevel,
Board,
tmc_gpio,
MovementPhase,
MovementAbsRel,
TmcEnableControlPin,
TmcMotionControlStepDir,
)
from tmc_driver.com import TmcComUart
print("---")
print("SCRIPT START")
print("---")
# -----------------------------------------------------------------------
# initiate the Tmc2209 class
# use your pins for pin_en, pin_step, pin_dir here
# -----------------------------------------------------------------------
UART_PORT = {
Board.RASPBERRY_PI: "/dev/serial0",
Board.RASPBERRY_PI5: "/dev/ttyAMA0",
Board.NVIDIA_JETSON: "/dev/ttyTHS1",
}
tmc1 = Tmc2209(
TmcEnableControlPin(21),
TmcMotionControlStepDir(16, 20),
TmcComUart(UART_PORT.get(tmc_gpio.BOARD, "/dev/serial0")),
loglevel=Loglevel.DEBUG,
)
tmc_driverlist = [tmc1]
# -----------------------------------------------------------------------
# set the loglevel of the libary (currently only printed)
# set whether the movement should be relative or absolute
# both optional
# -----------------------------------------------------------------------
tmc1.tmc_logger.loglevel = Loglevel.DEBUG
tmc1.movement_abs_rel = MovementAbsRel.ABSOLUTE
# -----------------------------------------------------------------------
# these functions change settings in the TMC register
# -----------------------------------------------------------------------
for tmc in tmc_driverlist:
tmc.set_direction_reg(False)
tmc.set_current_rms(300)
tmc.set_interpolation(True)
tmc.set_spreadcycle(False)
tmc.set_microstepping_resolution(2)
tmc.set_internal_rsense(False)
tmc.set_motor_enabled(True)
tmc.acceleration_fullstep = 1000
tmc.max_speed_fullstep = 250
print("---\n---")
# -----------------------------------------------------------------------
# run part
# -----------------------------------------------------------------------
# move 4000 steps forward
tmc1.tmc_mc.run_to_position_steps_threaded(4000, MovementAbsRel.RELATIVE)
time.sleep(1)
tmc1.tmc_mc.stop() # stop the movement after 1 second
tmc1.tmc_mc.wait_for_movement_finished_threaded()
# move 4000 steps backward
tmc1.tmc_mc.run_to_position_steps_threaded(-4000, MovementAbsRel.RELATIVE)
# while the motor is still moving
while tmc1.movement_phase != MovementPhase.STANDSTILL:
# print the current movement phase
print(tmc1.movement_phase)
time.sleep(0.02)
tmc1.tmc_mc.wait_for_movement_finished_threaded()
print("---\n---")
# -----------------------------------------------------------------------
# deinitiate the Tmc2209 class
# -----------------------------------------------------------------------
tmc1.set_motor_enabled(False)
del tmc1
print("---")
print("SCRIPT FINISHED")
print("---")