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

    pulses_get holding buffer values?

    Scheduled Pinned Locked Moved GPy
    3 Posts 3 Posters 113 Views 3 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.
    • P Offline
      pycom9
      last edited by

      I am reading a frequency input using pulses_get. When pulses_get is called the first time, it reads 128 values and calculates the frequency correctly. However, when I change the input frequency, the next time pulses_get is called it reads values that are from the previous frequency. I call pulses_get the 3rd time and it reads the NEW frequency correctly. Is there a buffer my program should be clearing? What am I missing? Any help would be much appreciated!

      Here is my code:

      #setup imports
      from machine import Pin
      from pycom import pulses_get
      import time
      
      p_in        = Pin('P13', mode=Pin.IN)
      p_out_pwr   = Pin('P4', mode=Pin.OUT)
      
      #turn on sensor
      p_out_pwr(True)
      time.sleep(2)
      
      pulses = pulses_get(p_in, 500)
      
      #turn off sensor
      p_out_pwr(False)
      
      #print length of pulses tuple
      print("Number of pulses: {}".format(len(pulses)))
      
      for i, (level, duration) in enumerate(pulses):
          #calculate pulse ON and OFF time
          if level == 1:
              pulse_on_time = duration
          elif level == 0:
              pulse_off_time = duration 
      
          if pulse_on_time > 0 and pulse_off_time > 0:
              period = pulse_on_time + pulse_off_time 
              frequency = 1000000.0/period #Hz
              print("i = {:2} f: {} Hz {} khz".format(i, frequency, frequency*.001))```
      robert-hhR 1 Reply Last reply Reply Quote 0
      • K Offline
        kjm
        last edited by

        Does pulses_get clear the pulses dict each time you call it? If not & you are appending into the dict then there is a chance that the pulses dict is still holding old values. Dicts written to in a function, unlike variables, are persistent between calls to the function.

        1 Reply Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh @pycom9
          last edited by

          @pycom9 pycom.pulses_get() has no means of managing what it is doing. It uses the RMT device. So you could try to use the RMT module API, allowing to deinit and init the channel again. That may reset the internal ringbuffer of the RMT peripheral.

          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