Rookie question about classes

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Dustfang
Posts: 4
Joined: Tue Jan 07, 2020 12:30 am

Rookie question about classes

Post by Dustfang » Tue Jan 14, 2020 10:42 am

I've got a question about classes. Learning micropython for work related stuff, and I need to use limit switches to stop motors. For ease of expansion, I thought I'd use a class and a function to setup the sensors. I followed the tutorial here https://randomnerdtutorials.com/micropy ... 2-esp8266/, and that worked. But when I try to use a pin callout instead, it doesn't work. Any thoughts?

Code: Select all

 from machine import Pin
from time import sleep

#class Opto setup to handle an expandable number of sensors
class Opto ():
  def __init__(self, name, sensor, motor):
    self.name = name
    self.sensor = sensor
    self.motor = motor
  def stop(self):
    if sensor.value() == 1:       <—line 11
      motor.value(0)
      print(name, "stopped")

#one sensor for test. Input on pin 4, output on pin 5.
opto1 = Opto("opto1", Pin(4, Pin.IN, Pin.PULL_DOWN), Pin(5, Pin.OUT))
#other variables
led = Pin(5, Pin.OUT)

#main loop
while True:
  led.value(1)
  opto1.stop()         <—line 23
  sleep(1)

///////////////////////////////////////////

Ready to download this file,please wait!
…..
download ok
exec(open(‘function interrupt test.py’).read(),globals())
Traceback (most recent call last):
     File”<stdin>”, line 1, in <module>
     File”<string>”, line 23, in <module>
     File”<string>”, line 11, in stop
NameError: name ‘sensor’ isn’t defined

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Rookie question about classes

Post by jimmo » Tue Jan 14, 2020 11:31 am

Inside `def stop()` you need to use self.sensor (and self.motor, self.name, etc).

Dustfang
Posts: 4
Joined: Tue Jan 07, 2020 12:30 am

Re: Rookie question about classes

Post by Dustfang » Wed Jan 15, 2020 9:38 am

That cleared it right up, thanks!

Additionally, I've tried to trigger the function with an interrupt

Code: Select all

opto1.sensor.irq(trigger=IRQ_RISING, handler=opto1.stop)
and ran into

Code: Select all

TypeError: function takes 1 argument, but 2 were given
I resolved it by changing
def stop(self)
To
def stop(self, waste)
and adding, within,
self.waste = waste

It works, but is there a cleaner solution, or a best practice to address this?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Rookie question about classes

Post by pythoncoder » Thu Jan 16, 2020 6:25 am

The docs tell you that a handler accepts a single argument which is the Pin instance. Where a function is a method there is an implied additional arg self. Your handler should look like this, accepting two args but discarding the second:

Code: Select all

def stop(self, _):
   # your code.
By convention _ is used to represent a required argument which is ignored. There is no reason to assign this arg to self: it can safely be ignored. You can use any name, but the _ tells those reading your code that it will be unused.
Peter Hinch
Index to my micropython libraries.

Dustfang
Posts: 4
Joined: Tue Jan 07, 2020 12:30 am

Re: Rookie question about classes

Post by Dustfang » Sat Jan 18, 2020 3:54 pm

Thank you much!

Post Reply