工作中常采用伺服电机+减速机+齿轮+齿条的结构,借助通义千问用python写了一段代码。
界面如下:
首先打开命令提示符,输入以下命令
pip install Pillow
将以下代码复制进记事本 ,将后缀改成.py或.pyw(后缀为.pyw时可不打开命令提示符运行)。
import tkinter as tk
from tkinter import filedialog
from PIL import ImageGrab
#import time
# 假设的功率计算函数(这需要根据实际情况进行调整)
def calculate_power(torque, speed):
# 假设的功率计算(P = Torque * Speed / 9550,这里9550是常数,用于将rpm和Nm转换为kW)
return torque * speed / 9550
def on_radio_button_group1():
global fuxuan1
fuxuan1=var1.get()
def on_radio_button_group2():
global fuxuan2
fuxuan2=var2.get()
def capture_and_save():
# 获取屏幕坐标
x = root.winfo_rootx()
y = root.winfo_rooty()
width = root.winfo_width()
height = root.winfo_height()
# 捕获指定区域的屏幕截图
screenshot = ImageGrab.grab(bbox=(x, y, x+width, y+height))
# 保存截图
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png"), ("All files", "*.*")])
if file_path:
# 保存截图到用户选择的路径
screenshot.save(file_path)
print(f"Screenshot saved to {file_path}")
#timestamp = time.strftime("_%Y%m%d_%H%M%S")
#image.save(f"window_screenshot{timestamp}.png")
def on_calculate():
try:
on_radio_button_group1();
on_radio_button_group2();
# 从输入框中获取值
modulus = float(modulus_entry.get())
teeth_number = int(teeth_entry.get())
#rpm = float(rpm_entry.get())
reduction_ratio = float(reduction_entry.get())
load_mass = float(load_entry.get())
linear_speed = float(speed_entry.get())
acceleration = float(accel_entry.get())
uk = float(uk_entry.get()) # 动摩擦系数
dzhuansu = float(dzhuansu_entry.get())
gonglv=float(gonglv_entry.get())
if fuxuan1=="1":
radius = modulus*teeth_number/1000/2/0.9424 # 分度圆半径
else:
if fuxuan1=="2":
radius = modulus*teeth_number/1000/2 # 分度圆半径
if fuxuan2=="3":
torque = (load_mass * acceleration + uk * load_mass * 9.8 ) * radius # 计算扭矩
else:
if fuxuan2=="4":
torque = (load_mass * acceleration + uk * load_mass * 9.8 +load_mass * 9.8) * radius # 计算扭矩
#original_speed = rpm # 电机转速
fenduyuan= 2 * radius
speed= linear_speed *60 / 3.14/fenduyuan# 计算齿轮转速
czhuansu = dzhuansu/reduction_ratio
# 计算功率
power = calculate_power(torque, speed)
# 计算负载惯量
guanliang =load_mass * radius * radius
# 计算实际扭矩
storque =9550 * gonglv / dzhuansu * reduction_ratio
# 显示结果
result_label.config(text=f"电机所需功率: {power:.3f} kW")
sgonglv_label.config(text=f"电机实际功率: {gonglv:.3f} kW")
torque_label.config(text=f"齿轮所需扭矩: {torque:.3f} Nm")
storque_label.config(text=f"齿轮实际扭矩: {storque:.3f} Nm")
teethspeed_label.config(text=f"齿轮所需转速: {speed:.3f} r/min")
czhuansu_label.config(text=f"齿轮实际转速: {czhuansu:.3f} r/min")
fenduyuan_label.config(text=f"齿轮分度圆直径: {fenduyuan:.3f} m")
guanliang_label.config(text=f"负载惯量JL: {guanliang:.3f} kgm^2")
except ValueError:
messagebox.showerror("Error", "Please enter valid numbers for all fields.")
except Exception as e:
messagebox.showerror("Error", str(e))
# 创建主窗口
root = tk.Tk()
root.title("电机功率计算")
root.geometry("400x800")
var1=tk.StringVar(value=1)
fuxuan1=var1.get()
var2=tk.StringVar(value=3)
fuxuan2=var2.get()
# 创建输入框和标签
modulus_label = tk.Label(root, text="齿轮模数:")
modulus_entry = tk.Entry(root)
modulus_label.pack()
modulus_entry.pack()
teeth_label = tk.Label(root, text="齿轮齿数:")
teeth_entry = tk.Entry(root)
teeth_label.pack()
teeth_entry.pack()
#rpm_label = tk.Label(root, text="电机转速 (r/min):")
#rpm_entry = tk.Entry(root)
#rpm_label.pack()
#rpm_entry.pack()
reduction_label = tk.Label(root, text="减速比:")
reduction_entry = tk.Entry(root)
reduction_label.pack()
reduction_entry.pack()
load_label = tk.Label(root, text="负载质量 (kg):")
load_entry = tk.Entry(root)
load_label.pack()
load_entry.pack()
speed_label = tk.Label(root, text="线速度 (m/s):")
speed_entry = tk.Entry(root)
speed_label.pack()
speed_entry.pack()
accel_label = tk.Label(root, text="加速度 (m/s^2):")
accel_entry = tk.Entry(root)
accel_label.pack()
accel_entry.pack()
uk_label = tk.Label(root, text="动摩擦系数 :")
uk_entry = tk.Entry(root)
uk_label.pack()
uk_entry.pack()
gonglv_label = tk.Label(root, text="电机实际功率 (kw):")
gonglv_entry = tk.Entry(root)
gonglv_label.pack()
gonglv_entry.pack()
dzhuansu_label = tk.Label(root, text="电机实际转速 (r/min):")
dzhuansu_entry = tk.Entry(root)
dzhuansu_label.pack()
dzhuansu_entry.pack()
#创建单选框组
tk.Radiobutton(root,text="斜齿轮",variable=var1,value=1,command=on_radio_button_group1).pack(anchor=tk.W)
tk.Radiobutton(root,text="直齿轮",variable=var1,value=2,command=on_radio_button_group1).pack(anchor=tk.W)
tk.Radiobutton(root,text="水平运动",variable=var2,value=3,command=on_radio_button_group2).pack(anchor=tk.W)
tk.Radiobutton(root,text="垂直运动",variable=var2,value=4,command=on_radio_button_group2).pack(anchor=tk.W)
# 创建计算按钮
calculate_button = tk.Button(root, text="计算", command=on_calculate)
calculate_button.pack()
# 创建结果标签
result_label = tk.Label(root, text="")
result_label.pack()
sgonglv_label = tk.Label(root, text="")
sgonglv_label.pack()
torque_label = tk.Label(root, text="")
torque_label.pack()
storque_label = tk.Label(root, text="")
storque_label.pack()
teethspeed_label = tk.Label(root, text="")
teethspeed_label.pack()
czhuansu_label = tk.Label(root, text="")
czhuansu_label.pack()
fenduyuan_label = tk.Label(root, text="")
fenduyuan_label.pack()
guanliang_label = tk.Label(root, text="")
guanliang_label.pack()
guanliangbi_label = tk.Label(root, text="惯量比:I总=(负载惯量/i^2)+减速机转动惯量)/电机转子惯量")
guanliangbi_label.pack()
# 添加一个按钮来触发截图保存操作
capture_button = tk.Button(root, text="截图保存", command=capture_and_save)
capture_button.pack()
# 创建一个按钮,点击时执行截图保存操作
#capture_button = tk.Button(root, text="截图保存", command=capture_and_save)
#capture_button.pack(padx=20, pady=20)
# 运行主循环
root.mainloop()