Page 1 of 1

Import not importing?

Posted: Tue Nov 30, 2021 3:12 pm
by Murray
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..

Re: Import not importing?

Posted: Tue Nov 30, 2021 9:51 pm
by Murray
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!')


Re: Import not importing?

Posted: Tue Nov 30, 2021 10:17 pm
by dhylands
Your import statement says "import irs" but the file is named ir.py not irs.py

Re: Import not importing?

Posted: Wed Dec 01, 2021 2:34 am
by Murray
Sorry, my description is different than the file naming.

The real version calls the correct file name

Re: Import not importing?

Posted: Wed Dec 01, 2021 5:24 am
by Murray
Figured it out.

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

Re: Import not importing?

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

Code: Select all

Interesting
:D