From 9f7816ca15ef5ca8403b132f3167f8ff9ccb7b9b Mon Sep 17 00:00:00 2001 From: Neil Gershenfeld <gersh@cba.mit.edu> Date: Sat, 28 Aug 2021 17:37:11 -0400 Subject: [PATCH] wip --- Urumbot/Urumbot.py | 211 +++++++++++++++++++++------------------------ 1 file changed, 98 insertions(+), 113 deletions(-) diff --git a/Urumbot/Urumbot.py b/Urumbot/Urumbot.py index 49a26a5..8d91e36 100644 --- a/Urumbot/Urumbot.py +++ b/Urumbot/Urumbot.py @@ -18,10 +18,8 @@ import serial,os,time,multiprocessing,sys # # start event process # -def StartProcess(arg): - while (True): - print(arg['key']) -def StartProcesses(vars): +def StartProcess(vars): +def StartProcesse(vars): # # process variables # @@ -34,22 +32,22 @@ def StartProcesses(vars): port0 = port1 = portz = 0 # try: - port0 = serial.Serial(vars.port0,baudrate=int(vars.baud),timeout=0) # non-blocking + port0 = serial.Serial(port0,baudrate=int(baud),timeout=0) # non-blocking ConnectButton.config(background='green') except: - print('can not open '+vars.port0) + print('can not open '+port0) # try: - port1 = serial.Serial(vars.port1,baudrate=int(vars.baud),timeout=0) # non-blocking + port1 = serial.Serial(port1,baudrate=int(baud),timeout=0) # non-blocking ConnectButton.config(background='green') except: - print('can not open '+vars.port1) + print('can not open '+port1) # try: - portz = serial.Serial(vars.portz,baudrate=int(vars.baud),timeout=0) # non-blocking + portz = serial.Serial(portz,baudrate=int(baud),timeout=0) # non-blocking ConnectButton.config(background='green') except: - print('can not open '+vars.portz) + print('can not open '+portz) # # start event loop # @@ -64,29 +62,29 @@ def StartProcesses(vars): # if ((portz != 0) and ((t-tservo) >= tpwm)): tservo = t - portz.write(vars.Servo.value.to_bytes(2,byteorder='little')) + portz.write(Servo.value.to_bytes(2,byteorder='little')) # # check for motor 0 event # - if ((port0 != 0) and (vars.Count0.value != 0) and ((t-t0) >= vars.Step0.value)): + if ((port0 != 0) and (Count0.value != 0) and ((t-t0) >= Step0.value)): t0 = t - if (vars.Count0.value > 0): + if (Count0.value > 0): port0.write(forward) - vars.Count0.value -= 1 - elif (vars.Count0.value < 0): + Count0.value -= 1 + elif (Count0.value < 0): port0.write(reverse) - vars.Count0.value += 1 + Count0.value += 1 # # check for motor 1 event # - if ((port1 != 0) and (vars.Count1.value != 0) and ((t-t1) >= vars.Step1.value)): + if ((port1 != 0) and (Count1.value != 0) and ((t-t1) >= Step1.value)): t1 = t - if (vars.Count1.value > 0): + if (Count1.value > 0): port1.write(forward) - vars.Count1.value -= 1 - elif (vars.Count1.value < 0): + Count1.value -= 1 + elif (Count1.value < 0): port1.write(reverse) - vars.Count1.value += 1 + Count1.value += 1 # # main process # @@ -96,75 +94,62 @@ if __name__ == '__main__': # multiprocessing.set_start_method('spawn') # - # variables class + # local variables # - class VarsClass: - # - # local variables - # - points = [] - plotsize = 250 - plotscale = 0 - xmin = ymin = xmax = ymax = 0 - # - # shared memory variables - # - Servo = multiprocessing.Value('H',0,lock=False) # unsigned short, servo duration - Count0 = multiprocessing.Value('i',0,lock=False) # signed int, motor 0 step count - Count1 = multiprocessing.Value('i',0,lock=False) # signed int, motor 1 step count - Step0 = multiprocessing.Value('f',0,lock=False) # float, motor 0 step time - Step1 = multiprocessing.Value('f',0,lock=False) # float, motor 1 step time - # - # UI variables - # - port0 = '' - port1 = '' - portz = '' - baud = '' - vars = VarsClass() - S = multiprocessing.Value('f',0,lock=False) # float, motor 1 step time - v = {'key':S} + points = [] + plotsize = 250 + plotscale = 0 + xmin = ymin = xmax = ymax = 0 + # + # shared memory variables + # + Servo = multiprocessing.Value('H',0,lock=False) # unsigned short, servo duration + Count0 = multiprocessing.Value('i',0,lock=False) # signed int, motor 0 step count + Count1 = multiprocessing.Value('i',0,lock=False) # signed int, motor 1 step count + Step0 = multiprocessing.Value('f',0,lock=False) # float, motor 0 step time + Step1 = multiprocessing.Value('f',0,lock=False) # float, motor 1 step time + vars = {'Servo':Servo,'Count0':Count0,'Count1':Count1,'Step0':Step0,'Step1':Step1} # # UI routines # def Quit(): - try: vars.process.terminate() + try: process.terminate() except: pass root.destroy() sys.exit() # def Connect(): try: - EventProcess = multiprocessing.Process(target=StartProcess,args=(v,)) + EventProcess = multiprocessing.Process(target=StartProcess,args=(vars,)) EventProcess.start() - vars.process = EventProcess + process = EventProcess except: print('can not start event loop') # def Jog0CW(): - vars.Step0.value = int(float(StepTimeVar.get())*1000000) - vars.Step1.value = 0 - vars.Count0.value = int(JogStepsVar.get()) - vars.Count1.value = 0 + Step0.value = int(float(StepTimeVar.get())*1000000) + Step1.value = 0 + Count0.value = int(JogStepsVar.get()) + Count1.value = 0 S.value += 1 # def Jog0CCW(): - vars.Step0.value = int(float(StepTimeVar.get())*1000000) - vars.Step1.value = 0 - vars.Count0.value = -int(JogStepsVar.get()) - vars.Count1.value = 0 + Step0.value = int(float(StepTimeVar.get())*1000000) + Step1.value = 0 + Count0.value = -int(JogStepsVar.get()) + Count1.value = 0 # def Jog1CW(): - vars.Step0.value = 0 - vars.Step1.value = int(float(StepTimeVar.get())*1000000) - vars.Count0.value = 0 - vars.Count1.value = int(JogStepsVar.get()) + Step0.value = 0 + Step1.value = int(float(StepTimeVar.get())*1000000) + Count0.value = 0 + Count1.value = int(JogStepsVar.get()) # def Jog1CCW(): - vars.Step0.value = 0 - vars.Step1.value = int(float(StepTimeVar.get())*1000000) - vars.Count0.value = 0 - vars.Count1.value = -int(JogStepsVar.get()) + Step0.value = 0 + Step1.value = int(float(StepTimeVar.get())*1000000) + Count0.value = 0 + Count1.value = -int(JogStepsVar.get()) # def PolarToCartesian(r0,r1): x0 = float(X0Var.get()) @@ -196,30 +181,30 @@ if __name__ == '__main__': MoveTo(xnew,ynew) # def UpdatePWM(): - vars.Servo.value = int(PWMVar.get()) + Servo.value = int(PWMVar.get()) # def PenUp(): - vars.Servo.value = int(PWMaxVar.get()) + Servo.value = int(PWMaxVar.get()) time.sleep(float(ServoDelayVar.get())) # def PenDown(): - vars.Servo.value = int(PWMinVar.get()) + Servo.value = int(PWMinVar.get()) time.sleep(float(ServoDelayVar.get())) # def LoadFile(): filename = tkinter.filedialog.askopenfile() file = open(filename.name,'r') - vars.xmin = vars.ymin = 1e10 - vars.xmax = vars.ymax = -1e10 + xmin = ymin = 1e10 + xmax = ymax = -1e10 digits = ['-','0','1','2','3','4','5','6','7','8','9'] arg = '' num = '' cmd = '' mode = '' point = 0 - vars.points = [] + points = [] x = y = 0 - Plot.create_rectangle(0,0,vars.plotsize,vars.plotsize,outline='',fill='white') + Plot.create_rectangle(0,0,plotsize,plotsize,outline='',fill='white') while 1: chr = file.read(1) if not chr: @@ -231,13 +216,13 @@ if __name__ == '__main__': if (mode == 'PR'): if (point == 0): x += float(num)/40.0 - vars.points[-1].append(x) + points[-1].append(x) else: y += float(num)/40.0 - vars.points[-1].append(y) + points[-1].append(y) point = 1-point else: - vars.points[-1].append(float(num)/40.0) + points[-1].append(float(num)/40.0) num = '' arg = '' cmd = '' @@ -246,13 +231,13 @@ if __name__ == '__main__': if (mode == 'PR'): if (point == 0): x += float(num)/40.0 - vars.points[-1].append(x) + points[-1].append(x) else: y += float(num)/40.0 - vars.points[-1].append(y) + points[-1].append(y) point = 1-point else: - vars.points[-1].append(float(num)/40.0) + points[-1].append(float(num)/40.0) num = '' else: arg += chr @@ -265,34 +250,34 @@ if __name__ == '__main__': elif (arg == 'PU'): cmd = 'PU' arg = '' - vars.points.append(['PU']) + points.append(['PU']) elif (arg == 'PD'): cmd = 'PD' arg = '' - vars.points.append(['PD']) + points.append(['PD']) elif ((chr in digits) and ((cmd == 'PU') or (cmd == 'PD'))): num += chr file.close() - for segment in range(len(vars.points)): - for point in range(1,len(vars.points[segment]),2): - x = vars.points[segment][point] - if (x > vars.xmax): vars.xmax = x - if (x < vars.xmin): vars.xmin = x - y = vars.points[segment][point+1] - if (y > vars.ymax): vars.ymax = y - if (y < vars.ymin): vars.ymin = y - FileLimits.config(text=f"{vars.xmax-vars.xmin:.3f} x {vars.ymax-vars.ymin:.3f}") - if ((vars.xmax-vars.xmin) > (vars.ymax-vars.ymin)): - vars.plotscale = (vars.plotsize-2)/(vars.xmax-vars.xmin) + for segment in range(len(points)): + for point in range(1,len(points[segment]),2): + x = points[segment][point] + if (x > xmax): xmax = x + if (x < xmin): xmin = x + y = points[segment][point+1] + if (y > ymax): ymax = y + if (y < ymin): ymin = y + FileLimits.config(text=f"{xmax-xmin:.3f} x {ymax-ymin:.3f}") + if ((xmax-xmin) > (ymax-ymin)): + plotscale = (plotsize-2)/(xmax-xmin) else: - vars.plotscale = (vars.plotsize-2)/(vars.ymax-vars.ymin) + plotscale = (plotsize-2)/(ymax-ymin) xold = 0 - yold = vars.plotsize-1 - for segment in range(len(vars.points)): - type = vars.points[segment][0] - for point in range(1,len(vars.points[segment]),2): - xnew = 1+vars.plotscale*(vars.points[segment][point]-vars.xmin) - ynew = (vars.plotsize-1)-vars.plotscale*(vars.points[segment][point+1]-vars.ymin) + yold = plotsize-1 + for segment in range(len(points)): + type = points[segment][0] + for point in range(1,len(points[segment]),2): + xnew = 1+plotscale*(points[segment][point]-xmin) + ynew = (plotsize-1)-plotscale*(points[segment][point+1]-ymin) if (type == 'PU'): Plot.create_line(xold,yold,xnew,ynew,width=1,fill="#FF0000") else: @@ -305,25 +290,25 @@ if __name__ == '__main__': r1 = float(R1Var.get()) x0,y0 = PolarToCartesian(r0,r1) xold = 0 - yold = vars.plotsize-1 - Plot.create_rectangle(0,0,vars.plotsize,vars.plotsize,outline='',fill='white') - for segment in range(len(vars.points)): - type = vars.points[segment][0] + yold = plotsize-1 + Plot.create_rectangle(0,0,plotsize,plotsize,outline='',fill='white') + for segment in range(len(points)): + type = points[segment][0] if (type == 'PU'): PenUp() else: PenDown() - for point in range(1,len(vars.points[segment]),2): - xnew = 1+vars.plotscale*(vars.points[segment][point]-vars.xmin) - ynew = (vars.plotsize-1)-vars.plotscale*(vars.points[segment][point+1]-vars.ymin) + for point in range(1,len(points[segment]),2): + xnew = 1+plotscale*(points[segment][point]-xmin) + ynew = (plotsize-1)-plotscale*(points[segment][point+1]-ymin) if (type == 'PU'): Plot.create_line(xold,yold,xnew,ynew,width=1,fill="#FF0000") else: Plot.create_line(xold,yold,xnew,ynew,width=1,fill="#000000") xold = xnew yold = ynew - x = x0-vars.points[segment][point]+vars.xmin - y = y0-vars.points[segment][point+1]+vars.ymin + x = x0-points[segment][point]+xmin + y = y0-points[segment][point+1]+ymin MoveTo(x,y) # def MoveTo(xnew,ynew): @@ -341,12 +326,12 @@ if __name__ == '__main__': Step0.value = fabs(t/count0) if (count1 != 0): Step1.value = fabs(t/count1) - vars.Count0.value,vars.Count1.value = count0,count1 + Count0.value,Count1.value = count0,count1 R0Var.set(f"{float(R0Var.get())+count0*float(StepSizeVar.get()):.3f}") R1Var.set(f"{float(R1Var.get())+count1*float(StepSizeVar.get()):.3f}") root.update() while (True): - if ((vars.Count0.value == 0) and (vars.Count1.value == 0)): + if ((Count0.value == 0) and (Count1.value == 0)): return # # set up UI @@ -442,7 +427,7 @@ if __name__ == '__main__': row += 1 Label(root,text='PWM (us):',bg='white').grid(column=0,row=row,padx=pad) PWMVar = StringVar(root,'1500') - vars.Servo.value = int(PWMVar.get()) + Servo.value = int(PWMVar.get()) Entry(root,textvariable=PWMVar,width=15).grid(column=1,row=row,padx=pad,pady=pad) Button(root,text='Update',command=UpdatePWM,bg='white').grid(column=2,columnspan=2,row=row,padx=pad,pady=pad) # @@ -463,7 +448,7 @@ if __name__ == '__main__': # row += 1 Label(root,text='origin',bg='white').grid(column=0,row=row,padx=pad,pady=pad,sticky='se') - Plot = Canvas(root,width=vars.plotsize,height=vars.plotsize,background='white') + Plot = Canvas(root,width=plotsize,height=plotsize,background='white') Plot.grid(column=1,row=row,columnspan=4,padx=pad,pady=pad,sticky='sw') # # start mainloop -- GitLab