kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

int Object has no attribute GPIO

Sat Sep 15, 2018 6:08 pm

Hi Everyone,

I am new on Python3. I tried the code below but I got an error message : int object has no attribute GPIO.
Could anyone help me out to take a look?

Thank you very much.




import RPi.GPIO as GPIO
from time import sleep
import threading
import sys
import sys,tty,termios

def getch():
fd=sys.stdin.fileno()
old=termios.tcgetattr(fd)
try:
tty.setraw(fd)
return sys.stdin.read(1)
finally:
termios.tcsetattr(fd,termios.TCSADRAIN,old)

class KeyEventThread(threading.Thread):
def run(self):
print("thread");
Fun()

def Fun():
Print("Fun")
while True:
key=getch()
if key=='q':
funExit()
exit()
return
elif key=='1':
print('speed 1')
funSpeed(100,100)
elif key=='2':
print('speed 2')
funSpeed(70,70)
elif key=='2':
print('speed 3')
funSpeed(20,20)
elif key=='w':
print('forward')
GPIO.output(MotorIN1,GPIO.HIGH)
GPIO.output(MotorIN2,GPIO.LOW)
GPIO.output(MotorIN3,GPIO.HIGH)
GPIO.output(MotorIN4,GPIO.LOW)
funSpeed(50,50)
elif key=='x':
print('backward')
GPIO.output(MotorIN1,GPIO.LOW)
GPIO.output(MotorIN2,GPIO.HIGH)
GPIO.output(MotorIN3,GPIO.LOW)
GPIO.output(MotorIN4,GPIO.HIGH)
funSpeed(50,50)
elif key=='a':
print('left')
GPIO.output(MotorIN1,GPIO.HIGH)
GPIO.output(MotorIN2,GPIO.LOW)
GPIO.output(MotorIN3,GPIO.LOW)
GPIO.output(MotorIN4,GPIO.HIGH)
funSpeed(50,50)
elif key=='d':
print('right')
GPIO.output(MotorIN1,GPIO.LOW)
GPIO.output(MotorIN2,GPIO.HIGH)
GPIO.output(MotorIN3,GPIO.HIGH)
GPIO.output(MotorIN4,GPIO.LOW)
funSpeed(50,50)
elif key=='s':
print('stop')
funSpeed(0,0)
else:
print("key="+key)
return


def funSpeed(i1,i2):
dc1=i1
dc2=i2
p1.ChangeDutyCycle(dc1)
p2.ChangeDutyCycle(dc2)


def funInit():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(MotorIN1,GPIO.OUT)
GPIO.setup(MotorIN2,GPIO.OUT)
GPIO.setup(MotorEN1,GPIO.OUT)
GPIO.setup(MotorIN3,GPIO.OUT)
GPIO.setup(MotorIN4.GPIO.OUT)
GPIO.setup(MotorEN2,GPIO.OUT)

def funExit():
print("Stopping motor")
GPIO.output(MotorEN1,GPIO.LOW)
GPIO.output(MotorEN2,GPIO.LOW)
GPIO.cleanup()


MotorIN1 = 11
MotorIN2 = 12
MotorEN1 = 13
MotorIN3 = 15
MotorIN4 = 16
MotorEN2 = 18

print("Press 'q' to exit")
print("'w'=forward, 'x'=backward,'a'=left,'d'=right,'s'=stop")
print("'1','2','3' motor speed")
funInit()
p1 = GPIO.PWM(MotorEN1,250)
p1.start(0)
p2 = GPIO.PWM(MotorEN2,250)
p2.start(0)

kethread = KeyEventThread()
kethread.start()

B.Goode
Posts: 6542
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: int Object has no attribute GPIO

Sat Sep 15, 2018 7:52 pm

Welcome to the Raspberry Pi forums.
I am new on Python3. I tried the code below but I got an error message : int object has no attribute GPIO.
Could anyone help me out to take a look?


Two tradeoffs for getting that help -

Retrospectively edit your post to use Code tags, by using the [ Code ] 'button' at the top of the message edit screen. Python relies on indentation for its syntax, but your post has stripped out all indentation, rendering it incomprehensible.

And please quote all of the error message, which normally contains a line number and other clues to the context of the problem.

User avatar
Paeryn
Posts: 2169
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: int Object has no attribute GPIO

Sat Sep 15, 2018 7:54 pm

When posting code you need to enclose it in [ code ] ... [ /code ] tags (sans spaces, it's awkward to type invisible spaces on Andriod) to preserve indentation, without it we can't see what statements belong to which blocks. I've added them to the quote...
kentang1102 wrote:
Sat Sep 15, 2018 6:08 pm
Hi Everyone,

I am new on Python3. I tried the code below but I got an error message : int object has no attribute GPIO.
Could anyone help me out to take a look?

Thank you very much.

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
import threading
import sys
import sys,tty,termios

def getch():
    fd=sys.stdin.fileno()
    old=termios.tcgetattr(fd)
    try:
        tty.setraw(fd)
        return sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd,termios.TCSADRAIN,old)

class KeyEventThread(threading.Thread):
        def run(self):
            print("thread");
            Fun()

def Fun():
    Print("Fun")
    while True:
        key=getch()
        if key=='q':
            funExit()
            exit()
            return
        elif key=='1':
            print('speed 1')
            funSpeed(100,100)
        elif key=='2':
            print('speed 2')
            funSpeed(70,70)
        elif key=='2':
            print('speed 3')
            funSpeed(20,20)
        elif key=='w':
            print('forward')
            GPIO.output(MotorIN1,GPIO.HIGH)
            GPIO.output(MotorIN2,GPIO.LOW)
            GPIO.output(MotorIN3,GPIO.HIGH)
            GPIO.output(MotorIN4,GPIO.LOW)
            funSpeed(50,50)
        elif key=='x':
            print('backward')
            GPIO.output(MotorIN1,GPIO.LOW)
            GPIO.output(MotorIN2,GPIO.HIGH)
            GPIO.output(MotorIN3,GPIO.LOW)
            GPIO.output(MotorIN4,GPIO.HIGH)
            funSpeed(50,50)
        elif key=='a':
            print('left')
            GPIO.output(MotorIN1,GPIO.HIGH)
            GPIO.output(MotorIN2,GPIO.LOW)
            GPIO.output(MotorIN3,GPIO.LOW)
            GPIO.output(MotorIN4,GPIO.HIGH)
            funSpeed(50,50)
        elif key=='d':
            print('right')
            GPIO.output(MotorIN1,GPIO.LOW)
            GPIO.output(MotorIN2,GPIO.HIGH)
            GPIO.output(MotorIN3,GPIO.HIGH)
            GPIO.output(MotorIN4,GPIO.LOW)
            funSpeed(50,50)
        elif key=='s':
            print('stop')
            funSpeed(0,0)
        else:
            print("key="+key)
    return


def funSpeed(i1,i2):
    dc1=i1
    dc2=i2
    p1.ChangeDutyCycle(dc1)
    p2.ChangeDutyCycle(dc2)


def funInit():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(MotorIN1,GPIO.OUT)
    GPIO.setup(MotorIN2,GPIO.OUT)
    GPIO.setup(MotorEN1,GPIO.OUT)
    GPIO.setup(MotorIN3,GPIO.OUT)
    GPIO.setup(MotorIN4.GPIO.OUT)
    GPIO.setup(MotorEN2,GPIO.OUT)

def funExit():
    print("Stopping motor")
    GPIO.output(MotorEN1,GPIO.LOW)
    GPIO.output(MotorEN2,GPIO.LOW)
    GPIO.cleanup()


MotorIN1 = 11
MotorIN2 = 12
MotorEN1 = 13
MotorIN3 = 15
MotorIN4 = 16
MotorEN2 = 18

print("Press 'q' to exit")
print("'w'=forward, 'x'=backward,'a'=left,'d'=right,'s'=stop")
print("'1','2','3' motor speed")
funInit()
p1 = GPIO.PWM(MotorEN1,250)
p1.start(0)
p2 = GPIO.PWM(MotorEN2,250)
p2.start(0)

kethread = KeyEventThread()
kethread.start()
Also it helps to provide the full error message, Python will have told you which line it is on, makes life easier so we don't have to work through the whole code searching for it.

Located it, in funInit() you have a period instead of a comma between MotorIN4 and GPIO.OUT

Code: Select all

    GPIO.setup(MotorIN4.GPIO.OUT)
Should be

Code: Select all

    GPIO.setup(MotorIN4,GPIO.OUT)
She who travels light — forgot something.

B.Goode
Posts: 6542
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: int Object has no attribute GPIO

Sat Sep 15, 2018 7:57 pm

Well spotted @Paeryn!

kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 3:58 am

Thanks for you guys replies.

Are you saying that I have to add "code:select all" on the top of the codes?

Thanks

User avatar
Paeryn
Posts: 2169
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 4:31 am

kentang1102 wrote:
Sun Sep 16, 2018 3:58 am
Thanks for you guys replies.

Are you saying that I have to add "code:select all" on the top of the codes?

Thanks
No, you put the tag

Code: Select all

[code]
before and the tag

Code: Select all

[/ code]
after (but without any space between the / and the word code, I can't write it out in full without it being interpreted). Alternatively use the code button above the input box to insert the tags, it's the 5th one along that looks like </>, it will put the cursor in-between the tags ready for you to put the code, or if you have the code already typed in then highlighting the code and pressing the button will put the tags around the highlighted code.
She who travels light — forgot something.

kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 5:41 am

Dear Paeryn,

O.. Sorry.. I am totally lost.

Any samples from youtube or websites?

Thank you very much. : )

User avatar
rpdom
Posts: 12866
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 5:58 am

kentang1102: Are you saying that I have to add "code:select all" on the top of the codes?

No, instead of typing your code like this:

import RPi.GPIO as GPIO
from time import sleep
import threading
import sys
import sys,tty,termios
(etc.)

type it like this:

[code]
import RPi.GPIO as GPIO
from time import sleep
import threading
import sys
import sys,tty,termios
(etc.)
[/code]

and it will show up properly.
(I've disabled it in this post so you can see what to type. I'm going to post a duplicate reply with the code enabled so you can see what happens)

User avatar
rpdom
Posts: 12866
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 5:59 am

kentang1102: Are you saying that I have to add "code:select all" on the top of the codes?

No, instead of typing your code like this:

import RPi.GPIO as GPIO
from time import sleep
import threading
import sys
import sys,tty,termios
(etc.)

type it like this:

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
import threading
import sys
import sys,tty,termios
(etc.)
and it will show up properly.
(I've disabled it in my previous reply so you can see what to type. This is a duplicate reply with the code enabled so you can see what happens)

kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 6:09 am

hi,

That means I need to put

Code: Select all

 at the first line, than 
at the last?

Thanks,

User avatar
rpdom
Posts: 12866
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 6:16 am

kentang1102 wrote:
Sun Sep 16, 2018 6:09 am
hi,

That means I need to put

Code: Select all

 at the first line, than 
at the last?

Thanks,
That works :-)

kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 6:19 am

but It shows SyntaxError: invalid syntax

:) :)

kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 6:24 am

Dear rpdom,

Would you mind you send me the py file to [email protected]?

Thank you very much,

B.Goode
Posts: 6542
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 6:47 am

kentang1102 wrote:
Sun Sep 16, 2018 6:19 am
but It shows SyntaxError: invalid syntax


The Code tags are only for use when sharing your script in these forums. They do not go into your python script itself.


This is all a distraction from your real problem - @Paeryn gave you the solution for that in his reply in the third post in this thread.

kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 7:14 am

Thank you so much , now i got it.

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
import threading
import sys
import sys, tty, termios

def getch():
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    try:
        tty.setraw(fd)
        return sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old)

class KeyEventThread(threading.Thread):
    def run(self):
        print("thread");
        Fun()

def Fun():
    print("Fun")
    while True:
        key=getch()
        if key=='q':
            funExit()
            exit()
            return
        elif key=='1':
            print('speed 1')
            funSpeed(100,100)
        elif key=='2':
            print('speed 2')
            funSpeed(70,70)
            funSpeed(20,20)
        elif key=='w':
            print('forward')
            GPIO.output(MotorIN1,GPIO.HIGH)
            GPIO.output(MotorIN2,GPIO.LOW)
            GPIO.output(MotorIN3,GPIO.HIGH)
            GPIO.output(MotorIN4,GPIO.LOW)
            funSpeed(50,50)
        elif key=='x':
            print('backward')
            GPIO.output(MotorIN1,GPIO.LOW)
            GPIO.output(MotorIN2,GPIO.HIGH)
            GPIO.output(MotorIN3,GPIO.LOW)
            GPIO.output(MotorIN4,GPIO.HIGH)
            funSpeed(50,50)
        elif key=='a':
            print('left')
            GPIO.output(MotorIN1,GPIO.HIGH)
            GPIO.output(MotorIN2,GPIO.LOW)
            GPIO.output(MotorIN3,GPIO.LOW)
            GPIO.output(MotorIN4,GPIO.HIGH)
            funSpeed(50,50)
        elif key=='d':
            print('right')
            GPIO.output(MotorIN1,GPIO.LOW)
            GPIO.output(MotorIN2,GPIO.HIGH)
            GPIO.output(MotorIN3,GPIO.HIGH)
            GPIO.output(MotorIN4,GPIO.LOW)
            funSpeed(50,50)
        elif key=='s':
            print('stop')
            funSpeed(0,0)
        else:
            print("key="+key)
    return


def funSpeed(i1,i2):
    dc1=i1
    dc2=i2
    p1.ChangeDutyCycle(dc1)
    p2.ChangeDutyCycle(dc2)


def funInit():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(MotorIN1,GPIO.OUT)
    GPIO.setup(MotorIN2,GPIO.OUT)
    GPIO.setup(MotorEN1,GPIO.OUT)
    GPIO.setup(MotorIN3,GPIO.OUT)
    GPIO.setup(MotorIN4,GPIO.OUT)
    GPIO.setup(MotorEN2,GPIO.OUT)

def funExit():
    print("Stopping motor")
    GPIO.output(MotorEN1,GPIO.LOW)
    GPIO.output(MotorEN2,GPIO.LOW)
    GPIO.cleanup()


MotorIN1 = 11 # 17
MotorIN2 = 12 # 18
MotorEN1 = 13 # 21
MotorIN3 = 15 # 22
MotorIN4 = 16 # 23
MotorEN2 = 18 # 24

print("Press 'q' to exit")
print("'w'=forward,'x'=backward,'a'=left,'d'=right,'s'=stop")
print("'1','2','3' motor speed")
funInit()
p1 = GPIO.PWM(MotorEN1,250)
p1.start(0)
p2 = GPIO.PWM(MotorEN2,250)
p2.start(0)

kethread = KeyEventThread()
kethread.start()

kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 7:25 am

Error messages:



Exception in thread Thread-1:
Traceback (most recent call last):
Files "/usr/lib/python3.5/threading.py", line 914, in_bootstrap_inner
self.run()
File "home/pi/Dpwnloads/L293D_remote.py", line 21, in run
Fun()
File "home/pi/Dpwnloads/L293D_remote.py", line 26, in Fun
key=getch()
File "home/pi/Dpwnloads/L293D_remote.py", line 11, in getch

old = termios.tcgetattr(fd)
termios.error: (25, 'inappropriate ioctl for device')

B.Goode
Posts: 6542
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 8:14 am

I don't have a solution.

But I have some other suggestions.

The documentation for the termios module that you are using is here: https://docs.python.org/2/library/termios.html

I assume this is not your own code, but something you have copied from elsewhere? (Because if you had written it you would surely have checked that getting a character from the console worked at an early stage of coding... ) Maybe this technique is applicable to some other Operating System but not to Raspbian?

The documentation for the gpiozero Python library module installed by default in recent versions of the Raspbian Operating System has some simple examples of implementing a keyboard-controlled robot. Perhaps the keyboard handling from those examples could be used as a basis for your own version?

See https://gpiozero.readthedocs.io/en/stab ... lled-robot and find section 2.23 if the link does not take you straight to it.

markkuk
Posts: 58
Joined: Thu Mar 22, 2018 1:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 8:44 am

How are you starting your code? Are you using IDLE, Thonny or some other IDE? Try running your code from a normal terminal window instead.

kentang1102
Posts: 9
Joined: Sat Sep 15, 2018 6:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 10:06 am

Thanks all~~

Error codes came out from the IDLE, but it works fine finally on Raspberry pi Python3. Don't know why..

Thanks you all again.

Do you guys have any suggestions on how to learn the Python3 programming?

Thanks,

markkuk
Posts: 58
Joined: Thu Mar 22, 2018 1:02 pm

Re: int Object has no attribute GPIO

Sun Sep 16, 2018 12:22 pm

kentang1102 wrote:
Sun Sep 16, 2018 10:06 am
Thanks all~~

Error codes came out from the IDLE, but it works fine finally on Raspberry pi Python3. Don't know why..
When you are running under IDLE the standard output isn't connected to a terminal. That's why you received the "inappropriate ioctl" error.

Return to “Python”