0

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()
finefoot
  • 9,914
  • 7
  • 59
  • 102
blocer
  • 1
  • 2
  • to use `multiprocessing` you have to put code in functions/classes and use `if __name__ == '__main__'` because new process run new python process which run the same file and `if __name__ == '__main__'` decides which part is executed only in main process and which is executed in new process. If you don't have `if __name__ == '__main__'` then both process start the same functions and you have mess. – furas Jan 02 '17 at 13:30
  • BTW: `tkinter` has function `after(milisecond, function_name)` which can be used to executed some functions periodically and many times it can works instead of multiprocess, subprocess or thread. – furas Jan 02 '17 at 13:33
  • you don't have `update` and `update2` - you have `app.update` and `app.update` – furas Jan 02 '17 at 13:37
  • @furas, thank you. I am trying to move my servos over `tkinter` `scale` exactly when I move the GUI Tkinter scale bar. There is two update function in my code. `update` is for horizontal movement and `update2` for vertical movement. For example, when I make first horizontal movement command and immediately vertical movement; my horizontal servo doing own movement. After finish horizontal movement, vertical servo starting its own movement. Thats my problem. – blocer Jan 02 '17 at 15:14
  • `update` and `update2` have to do only small moves so you could execute them many times to get full move. And then you can use `after()` to execute both functions periodically, and it will look like both work at the same time. – furas Jan 02 '17 at 15:29

0 Answers0