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

Python 将步进电机移动到精确位置,python,stepper,Python,Stepper,我的设置:一个Arduino可以读取序列号(步数),然后将步进电机移动很多步。它使用4988驱动器,12伏电源,1.5安培Nema 17。所有这些都很好,我的问题在Python方面 在Python中,我尝试了多种方法创建函数。 我使用tkinter在屏幕上获得鼠标位置,它在x轴上移动,旋转步进器。踏步机上有一个手电筒,只要我用鼠标指向任何东西,它都会发光 假设在步进机下面有一个摄像头,如果我把鼠标移到一个物体上,它会移到那里,在5步的公差范围内+5, -5 我尝试创建的函数应该是这样工作的 wh

我的设置:一个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:

对于任何其他有这个问题的人,人们似乎没有帮助,这里是我的新代码来驱动它

pass=0
发送方=序列编码('utf_8')
dir1=str(400).encode('utf_8')
dir2=str(401).encode('utf_8')
def发送(步骤):
全球过去
全球电流
全局发送方
全局dir1
全局dir2
全球stepstaken
sere='\r\n'
电流=步数
neg=过去的-5#公差都是未使用的atm
pos=过去+5#公差
需要=当前-过去
打印(需要,步骤)
如果是当前的!=过去的:
如果需要>0:
serialsteps=str(需要).encode('utf_8')
序列写入(dir1+发送方)
ser.write(串行步骤+发送方)
如果需要<0:
需要=需要*-1
serialstepss=str(需要).encode('utf_8')
序列写入(dir2+发送方)
ser.write(串行步骤+发送器)
过去=步骤
这是我的鼠标到步进电机的完整代码

将tkinter作为tk导入
root=tk.tk()
导入序列号
导入结构
导入时间
stepstaken=0
ardunioport=“COM”+str(输入(“Ardunio端口号:)#仅端口号
ser=串行。串行(ardunioport,9600)
wi=root.winfo_screenwidth()/2
width=root.winfo_screenwidth()
hi=root.winfo_屏幕高度()/2
高度=root.winfo_屏幕高度()
sere='\r\n'#仍然不理解为什么,但如果没有这个,它就无法工作。
发送方=序列编码('utf_8')
pps=宽度/200#一步中有多少像素
过去=0
发送方=序列编码('utf_8')
dir1=str(400).encode('utf_8')
dir2=str(401).encode('utf_8')
def发送(步骤):
全球过去
全球电流
全局发送方
全局dir1
全局dir2
全球stepstaken
#需要克服超越
sere='\r\n'
电流=步数
neg=过去的-5#公差
pos=过去+5#公差
需要=当前-过去
打印(需要,步骤)
如果是当前的!=过去的:
如果需要>0:
serialsteps=str(需要).encode('utf_8')
序列写入(dir1+发送方)
ser.write(串行步骤+发送方)
如果需要<0:
需要=需要*-1
serialstepss=str(需要).encode('utf_8')
序列写入(dir2+发送方)
ser.write(串行步骤+发送器)
过去=步骤
尽管如此:
pos=root.winfo_pointerx()-wi
步骤=圆形(pos/pps)
#打印(“鼠标位置”+str(pos)+“需要的步骤”+str(步骤))#显示光标位置
发送(步骤)

对我来说,这是一个Arduino,Arduino并没有接受负数。相反,它会删除-号,将负数乘以负数=正数。它发送相反方向的代码/编号,然后发送到达目的地所需的步骤。祝大家好运。此代码适用于我,不能保证它适用于您。如果不适用,我很抱歉。

您有什么问题?请澄清。顺便说一句,欢迎来到SO!如果你还没有,如果你想要更多的提示,请查看。一个将步进电机移动一定步数的功能,你命令它移动10次,它只移动10步,而不是20步。步骤(10)执行10步步骤(11)移动1步,因为它已经移动到10步,并且需要再移动1步才能到达11步。如果这仍然没有意义,你能告诉我你是怎么翻译的吗。以英语为母语,比一般人差。对不起,这不是问题,这是任务。所以它不是一个代码编写服务,但是如果您在编写代码时遇到问题,可以在这里询问。请阅读。不是任何作业,只是尝试为自己创造一些东西。不是要求你写所有的东西。我只是在这方面寻求帮助。我已经发布了我所有的东西,我希望它做什么。我相信这就是这样做的目的,如果我错了,请纠正我。我的意思是这就像你给我们的任务一样。如果更有意义,用“任务”或“项目”替换“任务”。无论如何,SO本身也不是为了提供帮助,而是为了回答有关代码的特定问题。您似乎想问这样的问题:“只有当新值大于以前的值时,我如何增加一个数字?”
#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!
def step(steps):
    #logic that makes it work