Chr157i4n/TMC2209_Raspberry_Pi

Motor randomly spins in the wrong direction

EliaKr opened this issue · 12 comments

Description
When I run my code the motor randomly spins in the wrong direction sometimes.

To Reproduce
Steps to reproduce the behavior:

  1. Installed libraries
  2. Ran code
  3. Motor spins in wrong direction at random times

Expected behavior
Expected it to consistently run in the right direction

Versions

  • Raspberry Pi 3B+
  • Latest Version of Library
  • Bigtreetech TMC2209 v1.2

Code

from TMC_2209.TMC_2209_StepperDriver import *
import time

# initiate the TMC_2209 class
# use your pins for pin_step, pin_dir, pin_en here

tmc = TMC_2209(16, 20, 21)

# set whether the movement should be relative or absolute
#tmc.setMovementAbsRel(MovementAbsRel.relative)

# these functions change settings in the TMC register
tmc.setDirection_reg(True)
tmc.setVSense(True)
tmc.setCurrent(300)
tmc.setIScaleAnalog(True)
tmc.setInterpolation(True)
tmc.setSpreadCycle(False)
tmc.setMicrosteppingResolution(16)
tmc.setInternalRSense(False)

#print("---\n---")

# set the Accerleration and maximal Speed
tmc.setAcceleration(16000)
tmc.setMaxSpeed(4000)

#activate motor
tmc.setMotorEnabled(True)

tmc.runToPositionSteps(6000, MovementAbsRel.relative)

tmc.setMotorEnabled(False)
del tmc
tmc.setAcceleration(16000)
tmc.setMaxSpeed(4000)

your MaxSpeed is rather high. I could not run that speed on my setup with only 300mA (without load).
1500 steps / seconds is the hightest i can run my Nema17 without load and only 300mA current.

Does it work on your setup with lower speed (like 500 or 1000)?

That Speed is with 16 micro steps but I will try a lower speed to see if the problem happens again. It runs fine on my setup with load. How high do you think I could get the current to not have any issues?

ohh i did not see the 16 microstepping...
Then that speed should be fine.

To clarify. Do you mean:
When you run the script like 4 times, the motor moves 3 times in the one direction and at the 4 time it moves in the other direction or
does the motor change direction during one run?

Yes you are correct, when I run the script like 4 times, the motor moves 3 times in the one direction and at the 4 time it moves in the other. I am gonna try to replicate it and post the console output later.

I could not replicate the bug with my setup. For me in turned every time in the same direction.

The direction can be set with the direction pin tmc.setDirection_pin(True) and via UART tmc.setDirection_reg(False).
Currently for moving in different direction the DIR pin is used to change the direction while moving.
The UART interface can be used to invert the directions.

So in the tmc.runToPositionSteps(6000, MovementAbsRel.relative) the direction is determinend by the sign of the movement distance and then the DIR pin is switched accordingly to get the stepper driver to move in that direction.
You should check check your DIR pin wiring.
the DIR pin should have a HIGH level when the distance is positive and LOW level when the distance is negative.

tmc.setVActual(int(200/0.715))
time.sleep(1)
tmc.setVActual(0)

that looks a lot more like 1 revolution forward

The console output of a normal run with the code posted above.

pi@neo-robotics-pi:~/Desktop/test-code-elias $ sudo python3 test.py
TMC2209: Init
TMC2209: GPIO Init finished
TMC2209: Init finished
TMC2209: write inverse motor direction
TMC2209: activated High sensitivity, low sense resistor voltage
TMC2209: Vsense: 0
TMC2209: CS_IRun: 10
TMC2209: CS_IHold: 5
TMC2209: Delay: 10
TMC2209: ihold_irun
0b10100000101000000101
TMC2209: writing ihold_irun
TMC2209: writing not successful!
ifcnt: 1426063880 34102043
TMC2209: activated Vref for current scale
TMC2209: writing not successful!
ifcnt: 1767178242 22
TMC2209: writing microstep interpolation setting: True
TMC2209: activated Stealthchop
TMC2209: writing 16 microstep setting
TMC2209: writing MStep Reg Select: True
TMC2209: activated operation with external sense resistors
TMC2209: Motor output active: True
TMC2209: Motor output active: False
TMC2209: Deinit
TMC2209: Motor output active: False

Console output when bug happens

pi@neo-robotics-pi:~/Desktop/test-code-elias $ sudo python3 test.py
TMC2209: Init
TMC2209: GPIO Init finished
TMC2209: Init finished
TMC2209: write inverse motor direction
TMC2209: activated High sensitivity, low sense resistor voltage
TMC2209: Vsense: 1
TMC2209: CS_IRun: 19
TMC2209: CS_IHold: 10
TMC2209: Delay: 10
TMC2209: ihold_irun
0b10100001001100001010
TMC2209: writing ihold_irun
TMC2209: writing not successful!
ifcnt: 3 3
TMC2209: activated Vref for current scale
TMC2209: writing microstep interpolation setting: True
TMC2209: writing not successful!
ifcnt: 861208578 538975067
TMC2209: activated Stealthchop
TMC2209: writing 16 microstep setting
TMC2209: writing MStep Reg Select: True
TMC2209: writing not successful!
ifcnt: 1426063880 -497745918
TMC2209: activated operation with external sense resistors
TMC2209: writing not successful!
ifcnt: 1532297218 8
TMC2209: Motor output active: True
TMC2209: Motor output active: False
TMC2209: Deinit
TMC2209: Motor output active: False

So, your scripts tries to inverse the direction via UART. In your first log that was successful and in your second log every writing failed, so the direction was not inversed in that case. So your actual problem is the UART communication.

Do you have the console/login shell over serial disabled?
#7 (comment)

Serial connection through raspi-config is enabled. It seems to fail randomly, sometimes it works perfectly, others everything goes wrong. Do you have any tips on solving that?

yes, the serial port needs to be enabled, but you will also get asked whether you want the console/shell login be available over serial and that needs to be disabled:

sudo raspi-config
there go to 3 Interface Options
then P3 Serial Port
Would you like a login shell to be accessible over serial? NO
Would you like the serial port hardware to be enabled? Yes
Finish and then reboot

Had both of them enabled so turned off the login shell. It seems to not be having the problem after a reboot but I am gonna have to see how it goes over the next few days. Please don't close this issue just yet.

I have also been noticing that the speed fluctuates a bit between different times of running the code but I can fix that in the script.

Thanks for your help!

Disabling login shell over serial seems to have fixed all serial connection problems. Marking this as closed thanks for the help.