import serial
import math
import time
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox
import cv2
import numpy as np
import threading
import inspect
import ctypes
import math
# opencv部分可以改进 简化轮廓
# 解决tkinter卡死问题
'''
通信格式说明:
波特率 9600
PC发送:
第0字节 xDir
第1字节 yDir
第2字节 zState
第3&4字节 xPS
第5&6字节 yPS
单片机发送: 0xff 表示打印完成
'''
# build serial
global s
try:
s = serial.Serial('com3', 9600, timeout=0.5)
except serial.serialutil.SerialException:
print('串口未打开')
# 参数设置
m = 5.9 # 臂长1(cm)
n =5.8 # 臂长2(cm)
MaxW = (2**0.5)*(m+n) # weight底cm
MaxH = MaxW/2-0.1 # high 避开0 cm
print('MAXW:', MaxW, ',', 'MAXH:', MaxH)
global Q
Q = 4096 # 默认值
# globals
global w, new_thread, isb, osb, input, output, cv, menu, is_print_button, set_threshold, isCanny, CannyMin, CannyMax, iterations, fanse
global mpx, npx
fanse = False
# iterations迭代次数
isCanny = True
global contours
global lst # list
global isfinish, pos
isfinish = False
cishu = -1 # 发送开始标志时 也会有接收
global is_justshow
is_justshow = False
global buffer
buffer = []
global motorA, motorB
motorA, motorB = 0, 0 # 电机反馈角度
def readdata():
global isfinish, cishu, buffer
while True:
inputdata = s.read(1)
if inputdata!=b'':
print(inputdata[0])
# buffer.append(inputdata)
# if len(buffer)==4:#接收4个数据完成,解析数据
# motorA= int.from_bytes(buffer[0],'big')<<8 + int.from_bytes(buffer[1],'big')
# motorA=motorA/4096*(math.pi*2)#等价于motorA=motorA/4096*360*pi/180 已经转为弧度制角度
# motorB= int.from_bytes(buffer[2],'big')<<8 + int.from_bytes(buffer[3],'big')
# motorB=motorB/4096*(math.pi*2)
# print('电机反馈角度(360角度制):',motorA*180/math.pi,motorB*180/math.pi)
# buffer=[]#buffer清零归位
# isfinish = True# 打印完成
# s.flushInput()
if inputdata == b'\xff': # 打印完成
isfinish = True # 若发送0xff且坐标正确
# 清空缓存 防止次数误加
s.flushInput()
cishu += 1
#print(inputdata)
def stop_thread(thread):
tid = thread.ident
exctype = SystemExit
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
# define image
img = []
# size setting
canvasw = 256
canvash = 256
listboxw = 150
listboxh = 300
scrollbarw = 10
scrollbarh = listboxh
# class pen
class pen:
x = 0
y = 0
alpha = 0 # rad
beta =int(20*math.pi/180)
Xangle = 0
Yangle =int(20/360*4096)
# 可增加xanglenow...
z = 0 # 状态 0:up 1:down
deltaX, deltaY = 50, 50 # 原点偏移量
def __init__(self):
global cv
self.id1 = cv.create_line(0, 0, 0, 0)
self.id2 = cv.create_line(0, 0, 0, 0)
self.id3 = cv.create_line(0, 0, 0, 0)
self.id4 = cv.create_line(0, 0, 0, 0)
def up(self):
self.z = 0
def drawline(self, x1, y1, x2, y2):
return cv.create_line(x1+self.deltaX, -y1+self.deltaY, x2+self.deltaX, -y2+self.deltaY)
def drawline2(self, x1, y1, x2, y2):
#return cv.create_line(x1, -y1, x2, -y2, dash=True, fill='red')
return cv.create_line(x1+self.deltaX, -y1+self.deltaY, x2+self.deltaX, -y2+self.deltaY, dash=True, fill='red')
# return cv.create_line(x1,-y1,x2,-y2)
def down(self):
self.z = 180 # 180du
def goto(self, pos): # pos是经典坐标系下!!
global MaxH, MaxW, mpx, npx, cv, MaxH_px, motorA, motorB, is_justshow, isfinish, Q
coe = 350/(mpx+npx)
if self.z == 180:
self.drawline(self.x*coe, self.y*coe, pos[0]*coe, pos[1]*coe)
# self.drawline(self.x*coe, self.y*coe,pos[0]*coe,pos[1]*coe)#显示所有轨迹
cv.delete(self.id1)
cv.delete(self.id2)
cv.delete(self.id3)
cv.delete(self.id4)
a, b = pos[0], pos[1]
S = -((npx**2-a**2-b**2-mpx**2)/(2*b))
x = (2*S*a/b+math.sqrt((2*S*a/b)**2-4*(1+a**2/b**2)
* (S**2-mpx**2)))/(2*(1+a**2/b**2))
y = S-a*x/b
al = math.acos(x/mpx) if y >= 0 else -math.acos(x/mpx)
# ((a-x)*(0-x)+(b-y)*(0-y))/( (a-x)**2+(b-y)**2 + (0-x)**2+(0-y)**2 )**0.5
be = math.acos((-(a-x)*x-(b-y)*y) /
((((a-x)**2+(b-y)**2) * (x**2+y**2))**0.5))
# virtual理论模拟 验证alpha&beta可行
self.id1 = self.drawline(
0, 0, mpx*math.cos(al)*coe, mpx*math.sin(al)*coe)
self.id2 = self.drawline(
mpx*math.cos(al)*coe,
mpx*math.sin(al)*coe,
(mpx*math.cos(al)+npx*math.cos(al-math.pi+be))*coe,
(mpx*math.sin(al)+npx*math.sin(al-math.pi+be))*coe)
drawpad.update()
self.alpha, self.beta, self.x, self.y = al, be, a, b
angle1 = (al/(2*math.pi)*Q) % Q # 取余操作 转为绝对坐标
angle1 = int(angle1)
angle2 = (be/(2*math.pi)*Q) % Q
angle2 = int(angle2)#杜绝眼花,严查改正
#angle模拟
self.id3 = self.drawline2(
0, 0, mpx*math.cos(angle1/Q*(2*math.pi))*coe, mpx*math.sin(angle1/Q*(2*math.pi))*coe)
self.id4 = self.drawline2(
mpx*math.cos(angle1/Q*2*math.pi)*coe,
mpx*math.sin(angle1/Q*2*math.pi)*coe,
(mpx*math.cos(angle1/Q*(2*math.pi))+npx*math.cos(angle1/Q*(2*math.pi)-math.pi+angle2/Q*(2*math.pi)))*coe,
(mpx*math.sin(angle1/Q*(2*math.pi))+npx*math.sin(angle1/Q*(2*math.pi)-math.pi+angle2/Q*(2*math.pi)))*coe)
drawpad.update()
# 计算移动距离-x //注意!这里因与模拟不和,所以方向手动反向 注:未改
if Q < 2*abs(angle1-self.Xangle): # 劣弧
xdirection_tosend = 1 if self.Xangle >= angle1 else 0
xps_tosend = Q-abs(angle1-self.Xangle)
else:
xdirection_tosend = 0 if self.Xangle >= angle1 else 1
xps_tosend = abs(angle1-self.Xangle)
# 计算移动距离-y
if Q < 2*abs(angle2-self.Yangle): # 劣弧
ydirection_tosend = 1 if self.Yangle >= angle2 else 0
yps_tosend = Q-abs(angle2-self.Yangle)
else:
ydirection_tosend = 0 if self.Yangle >= angle2 else 1
yps_tosend = abs(angle2-self.Yangle)
# update angle
self.Xangle = angle1
self.Yangle = angle2
# send
if is_justshow:
pass
else:
print(xdirection_tosend, ydirection_tosend, self.z,
'xps_tosend(10)', xps_tosend,
'yps_tosend(10)', yps_tosend, '\n',
'xps_tosend(bin2)', bin(xps_tosend),
'yps_tosend(bin2)', bin(yps_tosend), '\n',
'xps_tosend(bytes)', bytes(
[xps_tosend >> 8]), bytes([xps_tosend & 255]),
'yps_tosend(bytes)', bytes([yps_tosend >> 8]), bytes(
[yps_tosend & 255]), '\n',
)
if input('test(run 1 step_&send data)?') == '': # 逐步调试
s.write(bytes([xdirection_tosend]))
s.write(bytes([ydirection_tosend]))
PRINTER-CODE.rar INCLUDE-MCUMAIN&-PRINTER-HELPER
需积分: 0 90 浏览量
2024-04-26
18:09:21
上传
评论
收藏 8KB RAR 举报
includestdiohintmain
- 粉丝: 19
- 资源: 13
最新资源
- 基于Javascript的影视动画设计源码 - cad
- 基于Java和深度学习的瓦斯浓度预测系统后端设计源码 - 瓦斯浓度预测后端
- Screenshot_20240528_103010.jpg
- 基于Python的新能源承载力计算及界面设计源码 - HAINING-DG
- 基于Java的本科探索学习项目设计源码 - 本科探索
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈