将步进电机移动到精确位置

Move a stepper motor to an exact position

我的设置:一个 Arduino 从串行(步数)读取数字,然后它移动步进电机许多步。它使用 4988 驱动器、12 伏电源、1.5 安培 Nema 17。一切都很好,我的问题在 Python 方面。

在Python中,我尝试过以多种方式创建函数。 我使用 tkinter 获取屏幕上的鼠标位置,它在旋转步进器的 x 轴上移动。步进器有一个手电筒,可以照亮我用鼠标指向的任何地方。

假设步进器下方有一个摄像头,如果我将鼠标移到某个对象上,它会移动到那里,无论哪种方式都在 5 步的公差范围内。 +5, -5 我尝试创建的函数应该像这样工作。

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!

请尽可能解释答案,我潜伏了很长时间,现在才为this.Thank注册了一个帐户,以求大家的帮助。 编辑: 问题是如何制作一个将步进电机移动到一定数量的功能 steps.If 你命令它做两次 10 步,它只移动 10 步。 step(10) 步进器移动 10 步,再做一次电机什么都不做,因为它已经在 10 步了。 如果我执行步骤(15),它只会再移动 5 个,因为它是 10 个步骤。 编辑2澄清: 按功能我的意思是

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 没有被编程为接受负数。相反,它删除了 - 符号,将负数乘以负数 = 正数。它发送相反的方向 code/number 然后发送到达那里所需的步骤。祝大家好运。此代码适用于我,不保证它适用于 you.If 它不,我很抱歉。