Итак, я управляю биполярным шаговым двигателем NEMA34, и он приводится в движение свинцом DM860.Я создаю программу в Raspberry Pi, используя библиотеку pigpio, программу, которая делает изменение шагового двигателя движением вперед и назад в цикле потока.так что это пример моего кода, который я следую из примера (Джоан) (https://raspberrypi.stackexchange.com/questions/26216/how-to-generate-smooth-frequency-ramp):
import time
import pigpio
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QTime, QTimer
#this function I implement it on GUI PyQt
class step(QtCore.QThread):
tulis = write()
dataArray = []
dataInput = 0
startDelay=0
finalDelay=0
def __init__(self, parent=None):
QtCore.QThread.__init__(self,parent) #this is the function of thread from PyQt
self.GPIO=20
self.dirrection = 21
def triger(self,slider): #this function 'def triger(self,slider)' I use for update the value of variable 'stepdataInput','step.DataDelay',and 'step.dataStart' if the Gui gas update their input value
step.dataInput = slider #get value from slider UI and set it to 'step.dataInput' variable
if slider > 0:
step.startDelay = 600 - (2 * slider) #just the calculation of startDelay variable
step.finalDelay = 570 - (2 * slider) #just the calculation of startDelay variable
else:
step.startDelay = 0 #set variable 'step.startDelay' to zero
step.finalDelay = 0 #set variable 'step.finalDelay' to zero
def move(self):
pi = pigpio.pi() #inisialisasion of pigpio.pi() in pi variable
pi.set_mode(self.GPIO, pigpio.OUTPUT) #inisiation of gpio pulse pin
pi.set_mode(self.dirrection, pigpio.OUTPUT) #inisiation of gpio diretion pin
pi.set_mode(26,pigpio.INPUT) #left limit switch
pi.set_mode(16,pigpio.INPUT) #right limit switch
statee = 0 #the first inisialisation of motor direction
x = 0
while step.startDelay > 0 or step.finalDelay > 0: #this is the requitmen of loop
pi.wave_clear() #clear all previous loop wave
pi.write(self.dirrection,statee) #set the stepper motor rotation direction
#ramp up wave process from startDelay to finalDelay
wf=[] #inisialitation en set 'wf' array to empty
for delay in range(step.startDelay, step.finalDelay, -1): #looping for create ramp up wave from step.starDelay' to 'step.finalDelay' by try to generate the length between high and low pulse that create from the looping and save the chain wave to 'wf' array variable
wf.append(pigpio.pulse(1<<self.GPIO, 0, delay)) #'delay variable' create from the looping and it adjust the high and low pulse length
wf.append(pigpio.pulse(0, 1<<self.GPIO, delay))
pi.wave_add_generic(wf) #Adds a list of pulses to the current waveform
offset = pi.wave_get_micros() #Returns the length in microseconds of the current waveform.
wid1 = pi.wave_create() #Creates a waveform from the data provided by the prior calls to the wave_add_generic() function
#end ramp up code
wf=[] #clear the previous stored chain wave
#max speed wave from finalDelay, save the wave chain with the length of high and low pulse is from 'step.finalDelay' and save it to 'wf' array variable
wf.append(pigpio.pulse(1<<self.GPIO, 0, step.finalDelay))
wf.append(pigpio.pulse(0, 1<<self.GPIO, step.finalDelay))
pi.wave_add_generic(wf)
wid2 = pi.wave_create()
#end max speed code
#ramp down wave from finalDelay to startDelay
wf = [] #clear the previous stored chain wave
for delay in range(step.finalDelay,step.startDelay): #looping for create ramp down wave from step.finalDelay' to 'step.startDelay' by try to generate the length between high and low pulse that create from the looping and save the chain wave to 'wf' array variable, Iuse this for slow down the move ment when the maximum wave chain is finish the execution
wf.append(pigpio.pulse(1<<self.GPIO, 0, delay)) #'delay variable' create from the looping and it adjust the high and low pulse length
wf.append(pigpio.pulse(0, 1<<self.GPIO, delay))
pi.wave_add_generic(wf)
wid3 = pi.wave_create()
#end ramp down code
pi.wave_send_using_mode(wid1, pigpio.WAVE_MODE_ONE_SHOT_SYNC) #send ramp up wave chain to steper motor
time.sleep(float(offset)/1000000.0) #transition delay to another wave send
pi.wave_send_using_mode(wid2, pigpio.WAVE_MODE_REPEAT_SYNC) # the maximum wave chain send to stepper motor
if pi.read(26) == 0: #check if something hit the limmiter switch
pi.wave_tx_stop() #this function from gpiod to stop and terminate all wave
self.stop() #stop the loop
if pi.read(16) == 0: #check if something hit the limmiter switch
pi.wave_tx_stop() #this function from gpiod to stop and terminate all wave
self.stop() #stop the loop
time.sleep(0.35) #timer to set how long wave_send_using_mode(wid2, pigpio.WAVE_MODE_REPEAT_SYNC) is execute
pi.wave_send_using_mode(wid3, pigpio.WAVE_MODE_ONE_SHOT_SYNC) #send ramp down wave chain to slow down the stepper motor movement from the max speed to the startting speed ('step.starDelay')
if statee == 0: #if the value of 'statee' variable is 0
step.dataArray.append({'x':x,'y':0.25,'speed':step.dataInput}) #just save record
statee = 1 #set 'statee' varable to 1, for chaging the direction
elif statee == 1:
step.dataArray.append({'x':x,'y':0.25 * -1,'speed':step.dataInput})
statee = 0
x = x + 1
print("stop the process")
#this function from gpiod to stop and terminate all wave
pi.wave_tx_stop()
pi.stop()
def run(self):
#this function 'def run(self)' run by itself when you first time start the thread by calling 'def start() function'
self.move() #I call the 'def move(self)' function
def start(self):
#this function 'def start(self)' I use to start the thead looping if you don't undestand you can se the python documentation
super(step, self).start()
def stop(self):
#this function 'def stop(self)'I use to stop the thread loop
step.startDelay = 0
step.finalDelay = 0
проблема возникла, когда шаговый двигатель меняет направление, он не может изменить направление или замедлитьплавно слишком сильно стучит при изменении направления, я использую рампу, чтобы замедлить движущихся, когда pi.wave_send_using_mode(wid2, pigpio.WAVE_MODE_REPEAT_SYNC)
уже заканчивает выполнение и диапазон между step.startDelay
и step.finalDelay
не так далеко, потому что сделать движениешаговый двигатель не слишком далеко и увеличьте точность pi.read(26)
и pi.read(26)
до финиша pi.wave_send_using_mode(wid2, pigpio.WAVE_MODE_REPEAT_SYNC)
, когда я нажимаю на нормально замкнутый переключатель. Он работает так, как замедление не работает вообще, так что мне делать?