How to use multiprocess with Tkinter in Raspberry Pi Python

542 views Asked by At

I am trying to move two servos at the same time. But I cant move it. Because 2nd motor moving after finish the first motor movement which I command over tkinter scale. I have imported Process from multiprocess and tried. When i change scale command from command=self.update to command=Process(target=update, args()).start it gives me NameError: name 'update' is not defined.

I have no idea what should i du with this so any help would be appreciated.

My working (Non-simultaneous) code is below.

#!/usr/bin/env python
#-*- coding: utf-8 -*-

from Tkinter import *
import pigpio
import time

servos = 4-5 #GPIO number
pi = pigpio.pi()
horz = int(500)
vert = int(650)
pi.set_servo_pulsewidth(5, 500)
pi.set_servo_pulsewidth(4, 650)

try:
    class App:

        def __init__(self, master):
            frame = Frame(master)
            frame.pack()

            scale = Scale(frame, from_=2500, to=500,
            orient=HORIZONTAL, command=self.update)  
            scale.grid(row=0)

            scale = Scale(frame, from_=650, to=2000,
            orient=VERTICAL, command=self.update2)
            scale.grid(row=10)

        def update(self, angle_yat):
            global horz
            a = int(angle_yat)
            if horz <= a:
                for i in range(horz, a, 20):
                    pi.set_servo_pulsewidth(5, i)
                    print "yatay ai: ", i
                    time.sleep(0.001)
                    horz = a
            else:
                for i in range(horz, a, -20):
                    pi.set_servo_pulsewidth(5, i)
                    print "yatay ai: ", i
                    time.sleep(0.001)
                    horz = a


        def update2(self, angle):
            global vert
            b = int(angle)
            if vert <= b:
                for i in range(vert, b, 20):
                    pi.set_servo_pulsewidth(4, i)
                    print "yatay ai: ", i
                    time.sleep(0.001)
                    vert = b
            else:
                for i in range(vert, b, -20):
                    pi.set_servo_pulsewidth(4, i)
                    print "dikey ai: ", i
                    time.sleep(0.001)
                    vert = b


    root = Tk()
    root.wm_title('Servo Control')
    app = App(root)
    root.geometry("200x150+0+0")    
    root.mainloop()
except KeyboardInterrupt:
    pi.set_servo_pulsewidth(servos, 0)

finally:
    pi.stop()
0

There are 0 answers