victorH
Posts: 18
Joined: Fri Jun 10, 2016 5:12 pm

script value decimal to integer

Fri Jul 21, 2017 10:53 am

Hello,
To use the range () parameters, I must change something in the read_PWM.py script to change the output value from a decimal numeral to an interger (whole number). At the moment, I have an output value of 22.2 . I will need for example, 22 without decimal point. I imagine that the location is by def frequency(self) that has to be changed. I am also sending the code.

Thanks to anyone who can help me.

Victor

Code: Select all

#!/usr/bin/env python

# read_PWM.py
# 2015-12-08
# Public Domain

import pygame
pygame.mixer.init()
pygame.mixer.load("python/sound1.wav")
import time
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html

class reader:
   """
   A class to read PWM pulses and calculate their frequency
   and duty cycle.  The frequency is how often the pulse
   happens per second.  The duty cycle is the percentage of
   pulse high time per cycle.
   """
   def __init__(self, pi, gpio, weighting=0.0):
      """
      Instantiate with the Pi and gpio of the PWM signal
      to monitor.

      Optionally a weighting may be specified.  This is a number
      between 0 and 1 and indicates how much the old reading
      affects the new reading.  It defaults to 0 which means
      the old reading has no effect.  This may be used to
      smooth the data.
      """
      self.pi = pi
      self.gpio = gpio

      if weighting < 0.0:
         weighting = 0.0
      elif weighting > 0.99:
         weighting = 0.99

      self._new = 0.1 - weighting # Weighting for new reading.
      self._old = weighting       # Weighting for old reading.

      self._high_tick = None
      self._period = None
      self._high = None

      pi.set_mode(gpio, pigpio.INPUT)

      self._cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._cbf)

   def _cbf(self, gpio, level, tick):

      if level == 1:

         if self._high_tick is not None:
            t = pigpio.tickDiff(self._high_tick, tick)

            if self._period is not None:
               self._period = (self._old * self._period) + (self._new * t)
            else:
               self._period = t

         self._high_tick = tick

      elif level == 0:

         if self._high_tick is not None:
            t = pigpio.tickDiff(self._high_tick, tick)

            if self._high is not None:
               self._high = (self._old * self._high) + (self._new * t)
            else:
               self._high = t

   def frequency(self):
      """
      Returns the PWM frequency.
      """
      if self._period is not None:
         return 1000000.0 / self._period
      else:
         return 0.0

#  def pulse_width(self):
#     """
#     Returns the PWM pulse width in microseconds.
#     """
#     if self._high is not None:
#        return self._high
#     else:
#        return 0.0

#   def duty_cycle(self):
#      """
#      Returns the PWM duty cycle percentage.
#      """
#      if self._high is not None:
#         return 100.0 * self._high / self._period
#      else:
#         return 0.0

   def cancel(self):
      """
      Cancels the reader and releases resources.
      """
      self._cb.cancel()

if __name__ == "__main__":

   import time
   import pigpio
   import read_PWM1

   PWM_GPIO = 4
   RUN_TIME = 600.0
   SAMPLE_TIME = 0.1

   pi = pigpio.pi()

   p = read_PWM1.reader(pi, PWM_GPIO)

   start = time.time()

   while (time.time() - start) < RUN_TIME:

      time.sleep(SAMPLE_TIME)

      f = p.frequency()
#      pw = p.pulse_width()
#      dc = p.duty_cycle()
     
#      print("f={:.1f} pw={} dc={:.2f}".format(f, int(pw+0.5), dc))
      print("f={:.1f}".format(f))
    
#      if f < = 2.25:
#      print pygame play sound1.wav

   p.cancel()

   pi.stop()








User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: script value decimal to integer

Fri Jul 21, 2017 11:07 am

I have an output value of 22.2 . I will need for example, 22 without decimal point. I imagine that the location is by def frequency(self) that has to be changed
Maybe. That's a design decision for you.

But at some time in the future you might need the precision you are getting from frequency (), so you might regret changing that function itself.

My approach would simply be to change the way the value is printed here

Code: Select all

print("f={:.1f}".format(f))
or convert (round) the floating point value to an integer just before you print it.

User avatar
bensimmo
Posts: 4654
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: script value decimal to integer

Fri Jul 21, 2017 11:09 am

you need to change a 'float' to an 'int'
int(22.2) will give you 22
it always rounds down/truncates the number (so ignores anything after the decimal point)

victorH
Posts: 18
Joined: Fri Jun 10, 2016 5:12 pm

Re: script value decimal to integer

Fri Jul 21, 2017 12:50 pm

Thanks for answering so quickly. The output values vary every 250ms or less.
The idea to change the way the value is printed seems like the best idea, only how do I do that?
How can I incorperate the "int " via the print command?
Or does something else have to be done?

victorH

User avatar
bensimmo
Posts: 4654
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: script value decimal to integer

Fri Jul 21, 2017 1:21 pm

either the print code given above probably formats it like that.

or

print(int(22.2))
print(int(frequency))

If you're not understanding that, I suggest spending a good few our doing a basic Python course, worth it in the long run.
Something like codeacademy is free and easy to do or I'm sure there are plenty on the main site in learning.

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: script value decimal to integer

Fri Jul 21, 2017 1:24 pm

victorH wrote:Thanks for answering so quickly. The output values vary every 250ms or less.
The idea to change the way the value is printed seems like the best idea, only how do I do that?
How can I incorperate the "int " via the print command?
Or does something else have to be done?

victorH
If you just want to print a float value with no decimal places without actually converting it to an int,

Code: Select all

value = 2.2
print("value with no decimal places = {:.0f}".format(value))
She who travels light — forgot something.

victorH
Posts: 18
Joined: Fri Jun 10, 2016 5:12 pm

Re: script value decimal to integer

Fri Jul 21, 2017 1:53 pm

Thank-you. that is what I needed to get intergers from decimals.
victorH

Return to “Python”