analogdevicesinc / PyTrinamic

TRINAMIC's Python Technology Access Package.
Other
61 stars 36 forks source link

Stall guard not working #33

Closed MrGloriousFast closed 3 years ago

MrGloriousFast commented 3 years ago

Hello Pytrinamic,

I want to use the stallguard function with my motor module 1240 (and 1260 later on) I build a test setup: the motor can rotate freely for some degrees. then the motor hits a weight and has to lift it to keep rotating. i want to use the stallguard feature so the motor stops at the weight and not lift it with full force.

My idea is that the motor should rotate up until the weight freely but then not be able to lift the weight. The weight is representing the maximal force i want the motor to be able to apply.

Later i want to use this feature to run the motor against a wall and set that wall as the zero position.

I think i am using the functions completely wrong, because it does not do what i would expect it to do. It just Lifts the weight with full power and then puts it down again. No numbers i put into it seem to change that.

"""
standalone test for the stallguard feature
"""

import PyTrinamic, time
from PyTrinamic.connections.ConnectionManager import ConnectionManager
from PyTrinamic.modules.TMCM1240.TMCM_1240 import TMCM_1240

#get the motor connected
connectionManager = ConnectionManager("--port /dev/ttyACM0",debug = True)
interface = connectionManager.connect(debug_interface = True)
motor = TMCM_1240(interface)

try:

  """
   the function definitions i found in my module:
    def setStallguard2Filter(self, enableFilter):
        self.setAxisParameter(self.APs.SG2FilterEnable, enableFilter)

    def setStallguard2Threshold(self, threshold):
        self.setAxisParameter(self.APs.SG2Threshold, threshold)

    def setStopOnStallVelocity(self, velocity):
        self.setAxisParameter(self.APs.SmartEnergyStallVelocity, velocity)
  """

  #activate Stall guard
  motor.setStallguard2Filter(1)

  #set to sensitive settings
  motor.setStallguard2Threshold( -60) #tried everything between 100 and -100

  #cut off speed
  #motor.setStopOnStallVelocity(100) # error in this line: TMCM_1240.py", line 77, in setStopOnStallVelocity
    self.setAxisParameter(self.APs.SmartEnergyStallVelocity, velocity)
AttributeError: type object '_APs' has no attribute 'SmartEnergyStallVelocity'

  #stop the motor
  motor.stop()
  time.sleep(1.0)

  #init motor position as 0
  motor.setActualPosition(0)
  print('pos', motor.getActualPosition())
  time.sleep(1)

  #set max speed etc
  motor.setMaxAcceleration(20000000)
  speed = 10000

  #lift the weight
  motor.moveTo(20000)
  time.sleep(3.0)
  print('pos', motor.getActualPosition())

  """
  stall guard should prevent the motor from lifting the weight
  """

  #put the weight back down
  motor.moveTo(0)
  time.sleep(3.0)
  print('pos', motor.getActualPosition())

finally:

  # disconnect the motor again
  print('S T O P P I N G       M O T O R')
  motor.stop()
  interface.close()

i put everything into try/finally blocks so i can just use CTRL+C to stop the program whenever i want including a motor stop.

trinamic-LH commented 3 years ago

Hello,

#motor.setStopOnStallVelocity(100)

# error in this line: TMCM_1240.py", line 77, in setStopOnStallVelocity
self.setAxisParameter(self.APs.SmartEnergyStallVelocity, velocity)
AttributeError: type object '_APs' has no attribute 'SmartEnergyStallVelocity'

This error has been fixed in d567d61da4e433f41749f2c46ee732421d03dc40 This should probably fix your script issue.

Regards, Lenard