|
1 |
| -#pylint: disable=too-many-public-methods |
2 |
| -#pylint: disable=too-many-branches |
3 |
| -#pylint: disable=protected-access |
4 |
| -#pylint: disable=no-member |
5 |
| -#pylint: disable=bare-except |
6 |
| -#pylint: disable=duplicate-code |
7 |
| -""" |
8 |
| -Tmc2209 stepper driver test module |
9 |
| -""" |
10 | 1 |
|
11 |
| -import time |
12 |
| -from ._tmc_gpio_board import tmc_gpio, Gpio |
13 |
| -from ._tmc_logger import Loglevel |
14 |
| -from .motion_control._tmc_mc import MovementAbsRel, MovementPhase |
15 |
| -from .reg.bitfields import _tmc_220x_ioin as tmc_ioin_reg |
16 |
| -from .reg._tmc_220x_reg_addr import TmcRegAddr |
17 |
| - |
18 |
| - |
19 |
| -def test_step(self): |
20 |
| - """test method""" |
21 |
| - self.set_direction_pin(1) |
22 |
| - |
23 |
| - for _ in range(100): |
24 |
| - self._current_pos += 1 |
25 |
| - tmc_gpio.gpio_output(self._pin_step, Gpio.HIGH) |
26 |
| - time.sleep(0.001) |
27 |
| - tmc_gpio.gpio_output(self._pin_step, Gpio.LOW) |
28 |
| - time.sleep(0.01) |
29 |
| - |
30 |
| - |
31 |
| -def test_pin(self, pin, ioin_reg_bp): |
32 |
| - """tests one pin |
33 |
| -
|
34 |
| - this function checks the connection to a pin |
35 |
| - by toggling it and reading the IOIN register |
36 |
| - """ |
37 |
| - pin_ok = True |
38 |
| - |
39 |
| - tmc_gpio.gpio_output(self.tmc_mc._pin_dir, Gpio.HIGH) |
40 |
| - tmc_gpio.gpio_output(self.tmc_mc._pin_step, Gpio.HIGH) |
41 |
| - tmc_gpio.gpio_output(self._pin_en, Gpio.HIGH) |
42 |
| - |
43 |
| - ioin = self.read_ioin() |
44 |
| - if not ioin.data >> ioin_reg_bp & 0x1: |
45 |
| - pin_ok = False |
46 |
| - |
47 |
| - tmc_gpio.gpio_output(pin, Gpio.LOW) |
48 |
| - time.sleep(0.1) |
49 |
| - |
50 |
| - ioin = self.read_ioin() |
51 |
| - if ioin.data >> ioin_reg_bp & 0x1: |
52 |
| - pin_ok = False |
53 |
| - |
54 |
| - return pin_ok |
55 |
| - |
56 |
| - |
57 |
| -def test_dir_step_en(self): |
58 |
| - """tests the EN, DIR and STEP pin |
59 |
| -
|
60 |
| - this sets the EN, DIR and STEP pin to HIGH, LOW and HIGH |
61 |
| - and checks the IOIN Register of the TMC meanwhile |
62 |
| - """ |
63 |
| - pin_dir_ok = self.test_pin(self.tmc_mc._pin_dir, tmc_ioin_reg.dir_bp) |
64 |
| - pin_step_ok = self.test_pin(self.tmc_mc._pin_step, tmc_ioin_reg.step_bp) |
65 |
| - pin_en_ok = self.test_pin(self._pin_en, tmc_ioin_reg.enn_bp) |
66 |
| - |
67 |
| - self.set_motor_enabled(False) |
68 |
| - |
69 |
| - self.tmc_logger.log("---") |
70 |
| - if pin_dir_ok: |
71 |
| - self.tmc_logger.log("Pin DIR: \tOK") |
72 |
| - else: |
73 |
| - self.tmc_logger.log("Pin DIR: \tnot OK") |
74 |
| - if pin_step_ok: |
75 |
| - self.tmc_logger.log("Pin STEP: \tOK") |
76 |
| - else: |
77 |
| - self.tmc_logger.log("Pin STEP: \tnot OK") |
78 |
| - if pin_en_ok: |
79 |
| - self.tmc_logger.log("Pin EN: \tOK") |
80 |
| - else: |
81 |
| - self.tmc_logger.log("Pin EN: \tnot OK") |
82 |
| - self.tmc_logger.log("---") |
83 |
| - |
84 |
| - |
85 |
| -def test_com(self): |
86 |
| - """test method""" |
87 |
| - self.tmc_logger.log("---") |
88 |
| - self.tmc_logger.log("TEST COM") |
89 |
| - result = self.tmc_com.test_com(TmcRegAddr.IOIN) |
90 |
| - |
91 |
| - snd = result[0] |
92 |
| - rtn = result[1] |
93 |
| - |
94 |
| - status = True |
95 |
| - |
96 |
| - self.tmc_logger.log(f"length snd: {len(snd)}", Loglevel.DEBUG) |
97 |
| - self.tmc_logger.log(f"length rtn: {len(rtn)}", Loglevel.DEBUG) |
98 |
| - |
99 |
| - |
100 |
| - self.tmc_logger.log("complete messages:", Loglevel.DEBUG) |
101 |
| - self.tmc_logger.log(str(snd.hex()), Loglevel.DEBUG) |
102 |
| - self.tmc_logger.log(str(rtn.hex()), Loglevel.DEBUG) |
103 |
| - |
104 |
| - self.tmc_logger.log("just the first 4 bytes:", Loglevel.DEBUG) |
105 |
| - self.tmc_logger.log(str(snd[0:4].hex()), Loglevel.DEBUG) |
106 |
| - self.tmc_logger.log(str(rtn[0:4].hex()), Loglevel.DEBUG) |
107 |
| - |
108 |
| - if len(rtn)==12: |
109 |
| - self.tmc_logger.log("""the Raspberry Pi received the sent |
110 |
| - bytes and the answer from the TMC""", Loglevel.DEBUG) |
111 |
| - elif len(rtn)==4: |
112 |
| - self.tmc_logger.log("the Raspberry Pi received only the sent bytes", |
113 |
| - Loglevel.ERROR) |
114 |
| - status = False |
115 |
| - elif len(rtn)==0: |
116 |
| - self.tmc_logger.log("the Raspberry Pi did not receive anything", |
117 |
| - Loglevel.ERROR) |
118 |
| - status = False |
119 |
| - else: |
120 |
| - self.tmc_logger.log(f"the Raspberry Pi received an unexpected amount of bytes: {len(rtn)}", |
121 |
| - Loglevel.ERROR) |
122 |
| - status = False |
123 |
| - |
124 |
| - if snd[0:4] == rtn[0:4]: |
125 |
| - self.tmc_logger.log("""the Raspberry Pi received exactly the bytes it has send. |
126 |
| - the first 4 bytes are the same""", Loglevel.DEBUG) |
127 |
| - else: |
128 |
| - self.tmc_logger.log("""the Raspberry Pi did not received the bytes it has send. |
129 |
| - the first 4 bytes are different""", Loglevel.DEBUG) |
130 |
| - status = False |
131 |
| - |
132 |
| - self.tmc_logger.log("---") |
133 |
| - if status: |
134 |
| - self.tmc_logger.log("UART connection: OK", Loglevel.INFO) |
135 |
| - else: |
136 |
| - self.tmc_logger.log("UART connection: not OK", Loglevel.ERROR) |
137 |
| - |
138 |
| - self.tmc_logger.log("---") |
139 |
| - return status |
140 |
| - |
141 |
| - |
142 |
| -def test_stallguard_threshold(self, steps): |
143 |
| - """test method for tuning stallguard threshold |
144 |
| -
|
145 |
| - run this function with your motor settings and your motor load |
146 |
| - the function will determine the minimum stallguard results for each movement phase |
147 |
| -
|
148 |
| - Args: |
149 |
| - steps (int): |
150 |
| - """ |
151 |
| - |
152 |
| - self.tmc_logger.log("---", Loglevel.INFO) |
153 |
| - self.tmc_logger.log("test_stallguard_threshold", Loglevel.INFO) |
154 |
| - |
155 |
| - self.set_spreadcycle(0) |
156 |
| - |
157 |
| - min_stallguard_result_accel = 511 |
158 |
| - min_stallguard_result_maxspeed = 511 |
159 |
| - min_stallguard_result_decel = 511 |
160 |
| - |
161 |
| - self.tmc_mc.run_to_position_steps_threaded(steps, MovementAbsRel.RELATIVE) |
162 |
| - |
163 |
| - |
164 |
| - while self.tmc_mc.movement_phase != MovementPhase.STANDSTILL: |
165 |
| - stallguard_result = self.get_stallguard_result() |
166 |
| - |
167 |
| - self.tmc_logger.log(f"{self.tmc_mc.movement_phase} | {stallguard_result}", |
168 |
| - Loglevel.INFO) |
169 |
| - |
170 |
| - if (self.tmc_mc.movement_phase == MovementPhase.ACCELERATING and |
171 |
| - stallguard_result < min_stallguard_result_accel): |
172 |
| - min_stallguard_result_accel = stallguard_result |
173 |
| - if (self.tmc_mc.movement_phase == MovementPhase.MAXSPEED and |
174 |
| - stallguard_result < min_stallguard_result_maxspeed): |
175 |
| - min_stallguard_result_maxspeed = stallguard_result |
176 |
| - if (self.tmc_mc.movement_phase == MovementPhase.DECELERATING and |
177 |
| - stallguard_result < min_stallguard_result_decel): |
178 |
| - min_stallguard_result_decel = stallguard_result |
179 |
| - |
180 |
| - self.tmc_mc.wait_for_movement_finished_threaded() |
181 |
| - |
182 |
| - self.tmc_logger.log("---", Loglevel.INFO) |
183 |
| - self.tmc_logger.log(f"min StallGuard result during accel: {min_stallguard_result_accel}", |
184 |
| - Loglevel.INFO) |
185 |
| - self.tmc_logger.log(f"min StallGuard result during maxspeed: {min_stallguard_result_maxspeed}", |
186 |
| - Loglevel.INFO) |
187 |
| - self.tmc_logger.log(f"min StallGuard result during decel: {min_stallguard_result_decel}", |
188 |
| - Loglevel.INFO) |
189 |
| - self.tmc_logger.log("---", Loglevel.INFO) |
0 commit comments