Import not importing?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Murray
Posts: 4
Joined: Tue Nov 30, 2021 2:52 pm

Import not importing?

Post by Murray » Tue Nov 30, 2021 3:12 pm

Hi all

I am making a ESP8266 sumobot is sorts with a 4 channel IR sensor and a motor shield
Image
http://www.energiazero.org/arduino_sens ... sensor.pdf

Image



The plan is to create seperate files for the hbridge module (motors.py), the IR sensors (ir.py) and have a main file to create the drive program.

So I start with:

import motors
Import ir

The the body code which is a few if and elifs in a drive function.

In the webrepl, import the main file and call the drive() function.

Problem is, it says the ir module hasn't been imported. Only way to make it work is to manually import ir and motors before the main file in the webrepl.

Another way is to redownload all the files in uPyCraft which seems to so the same thing.

dir() shows all the files are in the same location?
I've tried importing main, ir and motors in boot.py

I will grab the code tomorrow and post it.

I've tried everything I can think of, it's going against everything read about how it should work?
It imports time or machine just fine..
Last edited by Murray on Tue Nov 30, 2021 9:59 pm, edited 1 time in total.

Murray
Posts: 4
Joined: Tue Nov 30, 2021 2:52 pm

Re: Import not importing?

Post by Murray » Tue Nov 30, 2021 9:51 pm

ir.py :

Code: Select all

from machine import Pin

rf = Pin(14, Pin.IN) 
lf = Pin(12, Pin.IN) 
rt = Pin(13, Pin.IN) 
lt = Pin(15, Pin.IN) 
motors.py :

Code: Select all

import time
from machine import Pin

pwm1 = Pin(5, Pin.OUT)  # D1
pwm2 = Pin(4, Pin.OUT)  # D2
dir1 = Pin(0, Pin.OUT)  # D3
dir2 = Pin(2, Pin.OUT)  # D4


def rev ():
  pwm1(1)
  pwm2(1)
  dir1(1)
  dir2(1)

def fwd ():
  pwm1(1)
  pwm2(1)
  dir1(0)
  dir2(0)

def stop ():
  pwm1(0)
  pwm2(0)
  dir1(0)
  dir2(0)

def left():
  pwm1(1)
  dir1(1)
  dir2(0)

def right():
  pwm2(1)
  dir1(0)
  dir2(1)
  

def turn():
  pwm2(1)
  dir1(0)
  dir2(1)
  time.sleep(.5)

main.py :

Code: Select all

import irs 
import motors 

def drive():
  try:
    while True:
      if   rf.value() == False and   lf.value() == False:
        motors.fwd()
      elif   rf.value() == True and   lf.value() == False:
        motors.turn()
      elif   rf.value() == False and   lf.value() == True:
        motors.turn()
  except KeyboardInterrupt:
    motors.stop()
    print('interrupted!')


User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Import not importing?

Post by dhylands » Tue Nov 30, 2021 10:17 pm

Your import statement says "import irs" but the file is named ir.py not irs.py

Murray
Posts: 4
Joined: Tue Nov 30, 2021 2:52 pm

Re: Import not importing?

Post by Murray » Wed Dec 01, 2021 2:34 am

Sorry, my description is different than the file naming.

The real version calls the correct file name

Murray
Posts: 4
Joined: Tue Nov 30, 2021 2:52 pm

Re: Import not importing?

Post by Murray » Wed Dec 01, 2021 5:24 am

Figured it out.

The function is called the same as the file and micropython got confused

User avatar
Wind-stormger
Posts: 17
Joined: Fri Nov 05, 2021 6:59 am

Re: Import not importing?

Post by Wind-stormger » Thu Dec 02, 2021 1:00 am

Code: Select all

Interesting
:D

Post Reply