簡體   English   中英

將步進電機移動到精確的 position

[英]Move a stepper motor to an exact position

我的設置:一個 Arduino 從串行(步數)讀取數字,然后它將步進電機移動許多步。 它使用 a4988 驅動器,12 伏電源,1.5 安培 Nema 17。一切都很好,我的問題是在 Python 方面。

在 Python 中,我嘗試以多種方式創建 function。 我使用 tkinter 將鼠標 position 放在屏幕上,它在 x 軸上移動,旋轉步進器。 步進器有一個手電筒,可以照亮我用鼠標指向的任何東西。

假設步進器下方有一個攝像頭,如果我將鼠標移到 object 上,它會移動到那里,誤差在 5 步以內。 +5, -5 我嘗試創建的 function 應該像這樣工作。

while True:
#I change direction by writing 400, clockwise, or 401, anti clockwise to serial.
    step(10)#move only 10 steps
    step(10)#it should do nothing as it has already moved 10 steps.
    step(15)#it should move 5 steps as it has moved 10 already
    step(5)#change direction and move back 10 steps
I am somewhat decent at python(or so I think)
here is my most promising code so far:
#I have direction changes done by writing a 400 for clockwise or 401 for anti clockwise to serial.
import tkinter as tk
import serial
ser = serial.Serial("COM8", 9600)
root = tk.Tk()
wi = root.winfo_screenwidth() / 2
width = root.winfo_screenwidth()
hi = root.winfo_screenheight() / 2
height = root.winfo_screenheight()
sere = '\r\n'
sender = sere.encode('utf_8')
pps = height / 200 
# p.p.s pixels per step divide that by the same thing to get correct.
#how many pixels are for 1 step
print(pps)
#number of pixels difference divided by pps to get steps, then round for ease of use
past = 0
print(height, width, wi) # height divided by pps should always = max steps in my case 200
def send(steps):
        global past
        global current
        sere = '\r\n'
        current = steps
        neg = past - 5
        pos = past + 5
        if current != past:
                if current > pos or current < neg:
                        if steps > 1:
                                sender = sere.encode('utf_8')
                                e = str(steps).encode('utf_8')
                                ser.write(e + sender)
        past = steps
while True:
        pos = root.winfo_pointerx() - wi
        steps = round(pos / pps)
        print(pos, steps) # shows cursor position
#direction code is not in here as I have no need for it atm.For one simple reason, IT DOESN'T WORK!

如果可能的話請解釋答案,我是一個長期的潛伏者,只是為了這個才注冊了一個帳戶。謝謝大家的幫助。 編輯:問題是如何制作一個 function 將步進電機移動到一定數量的步數。如果你命令它兩次執行 10 步,它只移動 10 步。 step(10) 步進器移動 10 步,再做一次電機什么也不做,因為它已經是 10 步了。 如果我執行第(15)步,它只會再移動 5 步,因為它是 10 步。 Edit2 澄清:function 我的意思是

def step(steps):
    #logic that makes it work

對於遇到此問題且人們似乎無能為力的其他人,這是我的新代碼來驅動它,

past = 0
sender = sere.encode('utf_8')
dir1 = str(400).encode('utf_8')
dir2 = str(401).encode('utf_8')
def send(steps):
        global past
        global current
        global sender
        global dir1
        global dir2
        global stepstaken
        sere = '\r\n'
        current = steps
        neg = past - 5 # tolerance both are unused atm
        pos = past + 5 # tolerance
        needed = current - past
        print(needed, steps)
        if current != past:
                if needed > 0:
                        serialsteps = str(needed).encode('utf_8')
                        ser.write(dir1 + sender)
                        ser.write(serialsteps + sender)
                if needed < 0:
                        needed = needed * -1
                        serialstepss = str(needed).encode('utf_8')
                        ser.write(dir2 + sender)
                        ser.write(serialstepss + sender)
        past = steps

這是我的鼠標到步進電機的完整代碼,

import tkinter as tk
root = tk.Tk()
import serial
import struct
import time
stepstaken = 0
ardunioport = "COM" + str(input("Ardunio Port Number:")) # port number only
ser = serial.Serial(ardunioport, 9600)
wi = root.winfo_screenwidth() / 2
width = root.winfo_screenwidth()
hi = root.winfo_screenheight() / 2
height = root.winfo_screenheight()
sere = '\r\n' #still don't understand why but it does not work without this.
sender = sere.encode('utf_8')
pps = width / 200 #how many pixels are in 1 step
past = 0
sender = sere.encode('utf_8')
dir1 = str(400).encode('utf_8')
dir2 = str(401).encode('utf_8')
def send(steps):
        global past
        global current
        global sender
        global dir1
        global dir2
        global stepstaken
        #need overcome overstepping
        sere = '\r\n'
        current = steps
        neg = past - 5 # tolerance 
        pos = past + 5 # tolerance
        needed = current - past
        print(needed, steps)
        if current != past:
                if needed > 0:
                        serialsteps = str(needed).encode('utf_8')
                        ser.write(dir1 + sender)
                        ser.write(serialsteps + sender)
                if needed < 0:
                        needed = needed * -1
                        serialstepss = str(needed).encode('utf_8')
                        ser.write(dir2 + sender)
                        ser.write(serialstepss + sender)
        past = steps

while True:
        pos = root.winfo_pointerx() - wi
        steps = round(pos / pps)
        #print("Mouse Position " + str(pos) + " Steps Needed" + str(steps)) # shows cursor position
        send(steps)        

對我來說,它轉到 Arduino,Arduino 沒有被編程為接受負數。 相反,它刪除了 - 符號,將負數乘以負數 = 正數。 它發送相反方向的代碼/號碼,然后發送到達那里所需的步驟。 大家好運。 這段代碼對我有用,沒有 promise 它會為你工作。如果沒有,我很抱歉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM