Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    threading error (NameError: name 'xxx' is not defined)

    Scheduled Pinned Locked Moved MicroPython
    threadingthread
    4 Posts 2 Posters 2.5k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G Offline
      gregcope
      last edited by gregcope

      hi all,

      I am trying to make a Class that merges L76GNSS and micropyGPS. I would like to create a thread to feed the micropyGPS object with NMEA data in the background.

      My code is here; https://github.com/gregcope/L76micropyGPS.

      Basically;

      class L76micropyGPS:
      
          def __init__(self, my_gps, pytrack=None, sda='P22', scl='P21', timeout=None):
          <snippage>
          # start thread feeding microGPS
              _thread.start_new_thread(feedMicroGPS, '')
      
          def feedMicroGPS(self):
             # <snippage>
              while True:
                 
                  # get some NMEA data
                  someNmeaData = self.i2c.readfrom(GPS_I2CADDR, 128)
                  print(" feedGps_thread - gpsChars recieved : {}".format(len(someNmeaData)))
      
                  # Pass NMEA data to micropyGPS object
                  for x in someNmeaData:
                      self.my_gps.update(x)
      

      However I get this error;

      Traceback (most recent call last):
      File "main.py", line 21, in <module>
      File "/flash/lib/L76micropyGPS.py", line 41, in init
      NameError: name 'feedMicroGPS' is not defined

      What I am doing wrong?

      T 1 Reply Last reply Reply Quote 0
      • T Offline
        timh @gregcope
        last edited by

        @gregcope you call _thread.start_new_thread(feedMicroGPS, '') can't find feedMicroGPS which is to be expected, it's definition is not in the scope of the init method.

        You should either be using self.feedMicroGPS or cls.feedMicroGPS (if it where a class method).

        The examples use a function not a method.

        1 Reply Last reply Reply Quote 0
        • G Offline
          gregcope
          last edited by

          Brilliant. Thanks. Now fails much further on!

          I had assumed that as methods in python are declared as functions that this should just work(tm).

          Thanks again.

          1 Reply Last reply Reply Quote 0
          • T Offline
            timh
            last edited by

            Functions and methods are actually different things and exist in different scopes.
            You can't look up a method by name without the self. (or similiar) prefix. Unless you bind it to another name in the current scope.

            The same applies to class methods.

            1 Reply Last reply Reply Quote 0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • 1 / 1
            • First post
              Last post

            Pycom on Twitter

            Pycom