python - 将步进电机移动到准确位置
问题描述
我的设置:一个 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
解决方案
对于遇到此问题且人们似乎无能为力的其他人,这是我的新代码来驱动它,
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 没有被编程为接受负数。相反,它删除了 - 符号,将负数乘以负数 = 正数。它发送相反方向的代码/号码,然后发送到达那里所需的步骤。祝大家好运。这段代码对我有用,不保证它会为你工作。如果没有,我很抱歉。
推荐阅读
- python - 对pytorch中张量行的所有组合应用一个函数
- popup - 我可以在单元格中输入特定单词时创建弹出消息吗
- scheme - 在 Scheme/Racket 中实现 A* 搜索算法
- python - 如何删除文本文件中的特定行而不留下空行?
- python - Django & Tagulous - 在我的查询集中显示来自两个过滤器的帖子
- python - 熊猫:使用日期列计算天数
- flutter - 如何确保在新小部件的 initState() 之前调用 dispose()?
- google-cloud-dataflow - Google Cloud Monitoring - 捕获数据流运行时间
- azure - 由于 Git 子模块上的 $(SolutionDir) 宏,Azure DevOps Pipelines 找不到项目文件路径
- javascript - 如何在javascript中从2个数组创建1个数组