Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 8th, 2012, 08:52 PM
klein5366 klein5366 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Location: east coast
Posts: 20 klein5366 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 28 m 21 sec
Reputation Power: 0
Global in python not working as expected

good day
i'm new to python and trying to learn how to control an arduino via serial communication, i have everything sorted out but!!!!!! i have three buttons that send commands to my arduino that all works fine, i need a way to lock cretin buttons out after others have been pushed.

bup = boom up > boom motor ccw
stop= stop bup and bdown
bdown= boom down > boom motor cw

so if bup has been pushed then bdown doesn't do anything untill stop has been pushed if bdown has been pushed then bup doesn't do anything till stop has been pushed.

i can Wright the code in c on the arduino but i was hoping to be able to do it in python. i tried setting global variables but it doesn't seem to work correctly

#!/usr/bin/env python
from Tkinter import *
from arduinoController import *
the_port = '/dev/ttyACM0'
close(the_port)
init(the_port)
bup = '0'
bdown = '0'


class MyApp(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
self.grid(sticky=N+S+E+W)
self.createWidgets()
def createWidgets(self):
top=self.winfo_toplevel()
top.rowconfigure(0,weight=1)
top.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.columnconfigure(0,weight=1)

self.up = Button(self,text='B Up',activebackground='green',cursor='gumby',command=self.bup)
self.up.grid(row=1,column=1,sticky=N+S+E+W)

self.ccw = Button(self,text='C.C.W',activebackground='green',cursor='gumby',command=self.ccw)
self.ccw.grid(row=2,column=0,sticky=N+S+E+W)

self.stop = Button(self,text='Stop',activebackground='red',cursor='gumby',command=self.stop)
self.stop.grid(row=2,column=1,sticky=N+S+E+W)

self.cw = Button(self,text='C.W',activebackground='green',cursor='gumby',command=self.cw)
self.cw.grid(row=2,column=2,sticky=N+S+E+W)

self.down = Button(self,text='B Down',activebackground='green',cursor='gumby',command=self.bdown)
self.down.grid(row=3,column=1,sticky=N+S+E+W)

self.wup = Button(self,text='W Up',activebackground='green',cursor='gumby',command=self.wup)
self.wup.grid(row=1,column=4,sticky=N+S+E+W)

self.wstop = Button(self,text='W Stop',activebackground='red',cursor='gumby',command=self.wstop)
self.wstop.grid(row=2,column=4,sticky=N+S+E+W)

self.wdown = Button(self,text='W Down',activebackground='green',cursor='gumby',command=self.wdown)
self.wdown.grid(row=3,column=4,sticky=N+S+E+W)


def bup(self):
global bdown
global bup
if bdown==0:
bup = 1
init(the_port)
output(2, True)
close(the_port)

def ccw(self):
init(the_port)
output(3, True)
close(the_port)

def stop(self):
global bdown
global bup
bdown = 0
bup = 0
init(the_port)
output(2, False)
output(3, False)
close(the_port)

def cw(self):
init(the_port)
output(3, False)
close(the_port)

def bdown(self):
global bdown
global bup
if bup==0:
bdown = 1
init(the_port)
output(3, True)
close(the_port)

def wup(self):
init(the_port)
output(2, True)
time.sleep(1)
output(3, True)
close(the_port)

def wstop(self):
for x in range(1, 16):
ser.write("7")

def wdown(self):
for x in range(1, 16):
ser.write("8")



app = MyApp()
app.master.title("Serial Adruino Uno ")
app.mainloop()



what i tried to do :
bup =0
bdown = 0
stop = bdown and bup set to 0

is there a easy way to do this in python ?

Reply With Quote
  #2  
Old July 9th, 2012, 09:47 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
you changed the datatype of your global variables from string to integer. Whatever. This solution doesn't depend on that. Look at the bup method for an idea about solving your problem. self.down.config(state=DISABLED)
Code:
#!/usr/bin/env python
from Tkinter import *
print('from arduinoController import *')
the_port = '/dev/ttyACM0'
print('close(the_port)')
print('init(the_port)')
bup = '0'
bdown = '0'


class MyApp(Frame):

    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.grid(sticky=N+S+E+W)
        self.createWidgets()

    def createWidgets(self):
        top=self.winfo_toplevel()
        top.rowconfigure(0,weight=1)
        top.columnconfigure(0,weight=1)
        self.rowconfigure(0,weight=1)
        self.columnconfigure(0,weight=1)

        self.up = Button(self,text='B Up',activebackground='green',cursor='gumby',command=self.bup)
        self.up.grid(row=1,column=1,sticky=N+S+E+W)

        self.ccw = Button(self,text='C.C.W',activebackground='green',cursor='gumby',command=self.ccw)
        self.ccw.grid(row=2,column=0,sticky=N+S+E+W)

        self.stop = Button(self,text='Stop',activebackground='red',cursor='gumby',command=self.stop)
        self.stop.grid(row=2,column=1,sticky=N+S+E+W)

        self.cw = Button(self,text='C.W',activebackground='green',cursor='gumby',command=self.cw)
        self.cw.grid(row=2,column=2,sticky=N+S+E+W)

        self.down = Button(self,text='B Down',activebackground='green',cursor='gumby',command=self.bdown)
        self.down.grid(row=3,column=1,sticky=N+S+E+W)

        self.wup = Button(self,text='W Up',activebackground='green',cursor='gumby',command=self.wup)
        self.wup.grid(row=1,column=4,sticky=N+S+E+W)

        self.wstop = Button(self,text='W Stop',activebackground='red',cursor='gumby',command=self.wstop)
        self.wstop.grid(row=2,column=4,sticky=N+S+E+W)

        self.wdown = Button(self,text='W Down',activebackground='green',cursor='gumby',command=self.wdown)
        self.wdown.grid(row=3,column=4,sticky=N+S+E+W)


    def bup(self):
        self.down.config(state=DISABLED)
        print('init(the_port)')
        print('output(2, True)')
        print('close(the_port)')

    def ccw(self):
        print('init(the_port)')
        print('output(3, True)')
        print('close(the_port)')

    def stop(self):
        global bdown
        global bup
        bdown = 0
        bup = 0
        print('init(the_port)')
        print('output(2, False)')
        print('output(3, False)')
        print('close(the_port)')

    def cw(self):
        print('init(the_port)')
        print('output(3, False)')
        print('close(the_port)')

    def bdown(self):
        global bdown
        global bup
        if bup==0:
            bdown = 1
        print('init(the_port)')
        print('output(3, True)')
        print('close(the_port)')

    def wup(self):
        print('init(the_port)')
        print('output(2, True)')
        print('time.sleep(1)')
        print('output(3, True)')
        print('close(the_port)')

    def wstop(self):
        for x in range(1, 16):
            print('ser.write("7")')

    def wdown(self):
        for x in range(1, 16):
            print('ser.write("8")')

app = MyApp()
app.master.title("Serial Adruino Uno ")
app.mainloop()
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old July 9th, 2012, 05:44 PM
klein5366 klein5366 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Location: east coast
Posts: 20 klein5366 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 28 m 21 sec
Reputation Power: 0
good day
thank you b49P23TIvg

after butting my head with global i final found that i could make another file and put my variables in there and import the file then Wright back and forth to them.

but i am going to try this, when importing the variables from another file every time i open the gui and it connects to my arduino i have to click on each stop buttons before it lets me turn anything on.

Reply With Quote
  #4  
Old July 9th, 2012, 09:37 PM
klein5366 klein5366 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Location: east coast
Posts: 20 klein5366 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 28 m 21 sec
Reputation Power: 0
Global in python not working as expected

good day
thank you b49P23TIvg

got disabling buttons working and it works much better that using variables i'm going to post my working code if others need an example.

python Code:
Original - python Code
  1.  
  2. #!/usr/bin/env python
  3. from Tkinter import *
  4. from arduinoController import *
  5. the_port = '/dev/ttyACM0'
  6. close(the_port)
  7. init(the_port)
  8.  
  9.  
  10.  
  11. class MyApp(Frame):
  12.         def __init__(self,master=None):
  13.                 Frame.__init__(self,master)
  14.                 self.grid(sticky=N+S+E+W)
  15.                 self.createWidgets()
  16.         def createWidgets(self):
  17.                 top=self.winfo_toplevel()
  18.                 top.rowconfigure(0,weight=1)
  19.                 top.columnconfigure(0,weight=1)
  20.                 self.rowconfigure(0,weight=1)
  21.                 self.columnconfigure(0,weight=1)
  22.                
  23.                 self.up = Button(self,text='B Up',activebackground='green',cursor='gumby',command=self.bup)
  24.                 self.up.configure(state='normal')
  25.                 self.up.grid(row=1,column=1,sticky=N+S+E+W)
  26.                                                
  27.                 self.ccw = Button(self,text='C.C.W',activebackground='green',cursor='gumby',command=self.ccw)
  28.                 self.ccw.configure(state='normal')
  29.                 self.ccw.grid(row=2,column=0,sticky=N+S+E+W)
  30.                
  31.                 self.stop = Button(self,text='Stop',activebackground='red',cursor='gumby',command=self.stop)
  32.                 self.stop.grid(row=2,column=1,sticky=N+S+E+W)
  33.                
  34.                 self.cw = Button(self,text='C.W',activebackground='green',cursor='gumby',command=self.cw)
  35.                 self.cw.configure(state='normal')
  36.                 self.cw.grid(row=2,column=2,sticky=N+S+E+W)
  37.                
  38.                 self.down = Button(self,text='B Down',activebackground='green',cursor='gumby',command=self.bdown)
  39.                 self.down.configure(state='normal')
  40.                 self.down.grid(row=3,column=1,sticky=N+S+E+W)
  41.                
  42.                 self.wup = Button(self,text='W Up',activebackground='green',cursor='gumby',command=self.wup)
  43.                 self.wup.configure(state='normal')
  44.                 self.wup.grid(row=1,column=4,sticky=N+S+E+W)
  45.                
  46.                 self.wstop = Button(self,text='W Stop',activebackground='red',cursor='gumby',command=self.wstop)
  47.                 self.wstop.grid(row=2,column=4,sticky=N+S+E+W)
  48.  
  49.                 self.wdown = Button(self,text='W Down',activebackground='green',cursor='gumby',command=self.wdown)
  50.                 self.wdown.configure(state='normal')
  51.                 self.wdown.grid(row=3,column=4,sticky=N+S+E+W)
  52.                                                                    
  53.                                                                    
  54.         def bup(self):
  55.                 self.down.configure(state='disabled')               
  56.                 init(the_port)
  57.                 output(2, True)
  58.                 close(the_port)                 
  59.                                                                
  60.         def ccw(self):
  61.                 self.cw.configure(state='disabled')
  62.                 init(the_port) 
  63.                 output(4, True) 
  64.                 close(the_port)
  65.                                
  66.         def stop(self):
  67.                 self.up.configure(state='normal')
  68.                 self.down.configure(state='normal')
  69.                 self.cw.configure(state='normal')
  70.                 self.ccw.configure(state='normal')
  71.                 init(the_port)
  72.                 output(2, False)
  73.                 output(3, False)
  74.                 output(4, False)
  75.                 output(5, False)
  76.                 close(the_port)         
  77.                                
  78.         def cw(self):
  79.                 self.ccw.configure(state='disabled')                       
  80.                 init(the_port) 
  81.                 output(5, True) 
  82.                 close(the_port)
  83.                                
  84.         def bdown(self):
  85.                 self.up.configure(state='disabled')
  86.                 init(the_port)
  87.                 output(3, True)
  88.                 close(the_port)
  89.                                
  90.         def wup(self):
  91.                 self.wdown.configure(state='disabled')
  92.                 init(the_port)   
  93.                 output(6, True)
  94.                 close(the_port)
  95.                                
  96.         def wstop(self):
  97.                 self.wup.configure(state='normal')
  98.                 self.wdown.configure(state='normal')
  99.                 init(the_port)
  100.                 output(6, False) 
  101.                 output(7, False)
  102.                 close(the_port)
  103.                                
  104.         def wdown(self):
  105.                 self.wup.configure(state='disabled')
  106.                 init(the_port)
  107.                 output(7, True)
  108.                 close(the_port)
  109.  
  110.  
  111.                                                                                                                                                
  112. app = MyApp()
  113. app.master.title("Serial Adruino Uno ")
  114. app.mainloop()




hay b49P23TIvg i learned how to use code tags too lol

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Global in python not working as expected

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap