【发布时间】:2021-06-11 12:36:44
【问题描述】:
我的设置:一个 Arduino 从串行(步数)中读取数字,然后它将步进电机移动许多步。它使用 a4988 驱动器,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!
如果可能,请解释答案,我是一个长期的潜伏者,只是为了这个才注册了一个帐户。谢谢大家的帮助。 编辑: 问题是如何制作一个将步进电机移动到一定步数的功能。如果你命令它两次执行 10 步,它只会移动 10 步。 step(10) 步进器移动 10 步,再做一次电机什么也不做,因为它已经是 10 步了。 如果我执行第(15)步,它只会再移动 5 步,因为它是 10 步。 Edit2 说明: 我的意思是功能
def step(steps):
#logic that makes it work
【问题讨论】:
-
你有什么问题?请edit 澄清。顺便说一句,欢迎来到 SO!如果您还没有,请查看tour,如果您想了解更多提示,请查看How to Ask。
-
一个将步进电机移动一定步数的函数,你命令它走 10 两次它只会移动 10 步而不是 20.step(10) 执行 10 步 step(11) 移动 1 step 因为它已经移动到 10 步,还需要 1 步才能到达 11。如果这仍然没有意义,你能告诉我你是如何解释它的吗?以英语为母语的人,不知何故比平均水平差。我很抱歉。
-
这不是问题,这是作业。 SO 不是代码编写服务,但是如果您编写的代码有问题,可以在这里提问。请阅读How to Ask。
-
不是任何事情的任务,只是想为自己创造一些东西。不是要求你写所有东西。我只是在寻求帮助。我已经发布了我所拥有的一切,我想要它做。我相信这是 SO 的目的,如果我弄错了,请纠正我。
-
我的意思是这就像你给我们的任务。如果更有意义,请将“assignment”替换为“task”或“project”。无论如何,SO 本身也不是为了提供帮助,而是为了回答关于代码的具体问题。似乎您想问诸如“仅当新值大于以前的值时,我如何增加一个数字?”