0

program runs in MU but not from shell
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

program runs in MU but not from shell

by ataragan on Wed Nov 06, 2019 11:45 am

I have 2 programs that I am trying to combine.
If I run the code below from the shell it works fine; however if I run it using MU I get a "termios.error: (25, inappropriate ioctl for device')".
Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/python3
# adapted from https://github.com/recantha/EduKit3-RC-Keyboard/blob/master/rc_keyboard.py

import sys, termios, tty, os, time
"""
from adafruit_motorkit import MotorKit
"""

def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(sys.stdin.fileno())
        ch = sys.stdin.read(1)

    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

"""
def run_motor():
    global kit
    kit.motor1.throttle = 1.0
    time.sleep(0.5)
    kit.motor1.throttle = 0

kit = MotorKit()
"""

button_delay = 0.2

while True:
    char = getch()

    if (char == "p"):
        print("Stop!")
        exit(0)

    if (char == "a"):
        """run_motor()"""
        print("Left pressed")
        time.sleep(button_delay)

    elif (char == "d"):
        print("Right pressed")
        time.sleep(button_delay)

    elif (char == "w"):
        print("Up pressed")
        time.sleep(button_delay)

    elif (char == "s"):
        print("Down pressed")
        time.sleep(button_delay)

    elif (char == "1"):
        print("Number 1 pressed")
        time.sleep(button_delay)


On the other hand the code below runs using MU, but I get an Import Error "No module named adafruit_motorkit" when running it from the shell.

Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/python3
import time
from adafruit_motorkit import MotorKit

kit = MotorKit()

while True:
    x = input("Press key:")
    print(x)
    if x == "t":
        kit.motor1.throttle = 1.0
        time.sleep(0.5)
        kit.motor1.throttle = 0
    else:
        print("False")


I ran the blinka test which worked fine. Output:
Hello blinka!
Digital IO ok!
I2C ok!
SPI ok!
done!

Any suggestions?
Thank you

ataragan
 
Posts: 3
Joined: Tue Nov 05, 2019 2:56 pm

Re: program runs in MU but not from shell

by adafruit_support_mike on Fri Nov 08, 2019 3:20 am

For the first piece of code, you're probably running into an issue with different runtime contexts.

The function getch() opens the filehandle for data stream STDIN, modifies its settings, then reads data from it. The streams STDIN, STDOUT, and STDERR exist to support communication between programs and the world outside them. The Mu editor has a different IO mechanism, and doesn't need to communicate with other programs to get input from the keyboard. The human-language translation of the error message is something like, "Mu doesn't handle input this way."

If you want the code to work in both contexts, you'll probably have to find versions that work in each context, then glue them together with a conditional or a try: block.


For the second item, try executing the following commands in both contexts and compare the results:

Code: Select all | TOGGLE FULL SIZE
import sys
print sys.path
'sys.path' is the list of places the Python interpreter will look for loadable modules like Adafruit_motorkit.

adafruit_support_mike
 
Posts: 59421
Joined: Thu Feb 11, 2010 2:51 pm

Re: program runs in MU but not from shell

by ataragan on Fri Nov 08, 2019 10:18 am

adafruit_support_mike

Thank you so much for your help. Running the system path code revealed that the shell was using version 2.7
A quick update of the version resolved the problem immediately. I appreciate your help.

ataragan
 
Posts: 3
Joined: Tue Nov 05, 2019 2:56 pm

Re: program runs in MU but not from shell

by adafruit_support_mike on Sat Nov 09, 2019 1:17 am

Glad to hear you got things working. Happy hacking!

adafruit_support_mike
 
Posts: 59421
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.