Esempio n. 1
0
def pwmDisable(pwm_pin):
  """ Disables PWM output on given pin. """
  pin_config = PWM_PINS[pwm_pin]
  assert (pin_config), "*Invalid PWM pin: '%s'" % pwm_pin
  helper_path = pin_config[1]
  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN), '0')
  PWM_PINS_ENABLED[pwm_pin] = False
Esempio n. 2
0
File: pwm.py Progetto: Omtar/PyBBIO
def analogWrite(pwm_pin, value, resolution=RES_8BIT, polarity=0):
  """ Sets the duty cycle of the given PWM output using the
      given resolution. If polarity=0 this will set the width of
      the positive pulse, otherwise it will set the width of the
      negative pulse. """
  # Make sure the pin is configured:
  pwmEnable(pwm_pin)
  pin_config = PWM_PINS[pwm_pin]
  helper_path = pin_config[1]
  try:
    assert resolution > 0, "*PWM resolution must be greater than 0"
    if (value < 0): value = 0
    if (value >= resolution): value = resolution-1
    period_ns = int(sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD)))
    # Todo: round values properly!: 
    duty_ns = int(value * (period_ns/resolution))
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), str(duty_ns))
    if polarity == 0:
      sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_POLARITY), '0')
    else:
      sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_POLARITY), '1')
    # Enable output:
    if (sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN)) == '0\n'):
      sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN), '1') 
  except IOError:
    print "*PWM pin '%s' reserved by another process!" % pwm_pin
Esempio n. 3
0
def analogWrite(pwm_pin, value, resolution=RES_8BIT, polarity=0):
  """ Sets the duty cycle of the given PWM output using the
      given resolution. If polarity=0 this will set the width of
      the positive pulse, otherwise it will set the width of the
      negative pulse. """
  # Make sure the pin is configured:
  pwmEnable(pwm_pin)
  pin_config = PWM_PINS[pwm_pin]
  helper_path = pin_config[1]
  try:
    assert resolution > 0, "*PWM resolution must be greater than 0"
    if (value < 0): value = 0
    if (value >= resolution): value = resolution-1
    period_ns = int(sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD)))
    # Todo: round values properly!: 
    duty_ns = int(value * (period_ns/resolution))
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), str(duty_ns))
    if polarity == 0:
      sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_POLARITY), '0')
    else:
      sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_POLARITY), '1')
    # Enable output:
    if (sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN)) == '0\n'):
      sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN), '1') 
  except IOError:
    print "*PWM pin '%s' reserved by another process!" % pwm_pin
Esempio n. 4
0
File: pwm.py Progetto: Omtar/PyBBIO
def pwmDisable(pwm_pin):
  """ Disables PWM output on given pin. """
  pin_config = PWM_PINS[pwm_pin]
  assert (pin_config), "*Invalid PWM pin: '%s'" % pwm_pin
  helper_path = pin_config[1]
  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN), '0')
  PWM_PINS_ENABLED[pwm_pin] = False
Esempio n. 5
0
def pwmFrequency(pwm_pin, freq_hz):
  """ Sets the frequncy in Hertz of the given PWM output's module. """
  assert (pwm_pin in PWM_PINS), "*Invalid PWM pin: '%s'" % pwm_pin
  assert freq_hz > 0, "*PWM frequency must be greater than 0"
  # Make sure the pin is configured:
  pwmEnable(pwm_pin)
  # calculate the duty cycle in nanoseconds for the new period:
  pin_config = PWM_PINS[pwm_pin]
  helper_path = pin_config[1]

  old_duty_ns = int(sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY)))
  old_period_ns = int(sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD)))

  duty_percent = old_duty_ns / old_period_ns
  new_period_ns = int(1e9/freq_hz)
  # Todo: round values properly!:
  new_duty_ns = int(duty_percent * new_period_ns)

  try: 
    # Duty cyle must be set to 0 before changing frequency:
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), '0')
    # Set new frequency:
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD), str(new_period_ns))
    # Set the duty cycle:
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), str(new_duty_ns))
  except IOError:
    print "*PWM pin '%s' reserved by another process!" % pwm_pin
Esempio n. 6
0
File: pwm.py Progetto: Omtar/PyBBIO
def pwmFrequency(pwm_pin, freq_hz):
  """ Sets the frequncy in Hertz of the given PWM output's module. """
  assert (pwm_pin in PWM_PINS), "*Invalid PWM pin: '%s'" % pwm_pin
  assert freq_hz > 0, "*PWM frequency must be greater than 0"
  # Make sure the pin is configured:
  pwmEnable(pwm_pin)
  # calculate the duty cycle in nanoseconds for the new period:
  pin_config = PWM_PINS[pwm_pin]
  helper_path = pin_config[1]

  old_duty_ns = int(sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY)))
  old_period_ns = int(sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD)))

  duty_percent = old_duty_ns / old_period_ns
  new_period_ns = int(1e9/freq_hz)
  # Todo: round values properly!:
  new_duty_ns = int(duty_percent * new_period_ns)

  try: 
    # Duty cyle must be set to 0 before changing frequency:
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), '0')
    # Set new frequency:
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD), str(new_period_ns))
    # Set the duty cycle:
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), str(new_duty_ns))
  except IOError:
    print "*PWM pin '%s' reserved by another process!" % pwm_pin
Esempio n. 7
0
 def setFrequency(self, freq):
     '''
 setFrequency(freq)
 Set the frequency in Hz at which the driver reports new positions.
 '''
     period_file = "%s/period" % self.base_dir
     return sysfs.kernelFileIO(period_file, str(1000000000 / freq))
Esempio n. 8
0
 def setPosition(self, val):
     ''' 
 setPosition(value)
 Give a new value to the current position
 '''
     position_file = "%s/position" % self.base_dir
     return sysfs.kernelFileIO(position_file, str(val))
Esempio n. 9
0
 def disable(self):
     '''
 disable()
 Turns the eQEP hardware OFF
 '''
     enable_file = "%s/enabled" % self.base_dir
     return sysfs.kernelFileIO(enable_file, '0')
Esempio n. 10
0
 def getMode(self):
   '''
   getMode()
   Returns the mode the eQEP hardware is in.
   '''
   mode_file = "%s/mode" % self.base_dir
   return sysfs.kernelFileIO(mode_file)
Esempio n. 11
0
 def getMode(self):
     '''
 getMode()
 Returns the mode the eQEP hardware is in.
 '''
     mode_file = "%s/mode" % self.base_dir
     return sysfs.kernelFileIO(mode_file)
Esempio n. 12
0
 def disable(self):
   '''
   disable()
   Turns the eQEP hardware OFF
   '''
   enable_file = "%s/enabled" % self.base_dir
   return sysfs.kernelFileIO(enable_file, 0)
Esempio n. 13
0
 def setPosition(self,val):
   ''' 
   setPosition(value)
   Give a new value to the current position
   '''
   position_file = "%s/position" % self.base_dir
   return sysfs.kernelFileIO(position_file,val)
Esempio n. 14
0
 def setFrequency(self,freq):
   '''
   setFrequency(freq)
   Set the frequency in Hz at which the driver reports new positions.
   '''
   period_file = "%s/period" % self.base_dir
   return sysfs.kernelFileIO(period_file,1000000000/freq)
Esempio n. 15
0
 def setRelative(self):
   '''
   setRelative()
   Set mode as Relative
   The position is reset when the unit timer overflows.
   '''
   mode_file = "%s/mode" % self.base_dir
   return sysfs.kernelFileIO(mode_file, 1)
Esempio n. 16
0
 def init_encoder(self):
     enc = sysfs.kernelFileIO(self.encoder_file).split()
     d = int(enc[0]) * self.coef_d
     g = int(enc[1]) * self.coef_g * 1.5
     angle = (d - g) * self.teta_to_radian
     with self.lock:
         self.previous = (d, g)
         self.A = angle
Esempio n. 17
0
 def setRelative(self):
     '''
 setRelative()
 Set mode as Relative
 The position is reset when the unit timer overflows.
 '''
     mode_file = "%s/mode" % self.base_dir
     return sysfs.kernelFileIO(mode_file, '1')
Esempio n. 18
0
 def init_encoder(self):
     enc = sysfs.kernelFileIO(self.encoder_file).split()
     d = int(enc[0]) * self.coef_d
     g = int(enc[1]) * self.coef_g * 1.5
     angle = (d - g) * self.teta_to_radian
     with self.lock:
         self.previous = (d, g)
         self.A = angle
Esempio n. 19
0
def pwmEnable(pwm_pin):
  """ Ensures PWM module for the given pin is enabled and its ocp helper
      is loaded. """
  global PWM_PINS_ENABLED
  if PWM_PINS_ENABLED.get(pwm_pin): return
  pin_config = PWM_PINS[pwm_pin]
  assert (pin_config), "*Invalid PWM pin: '%s'" % pwm_pin

  for overlay in pin_config[2]:
    cape_manager.load(overlay, auto_unload=False)
    delay(250) # Give it some time to take effect
  cape_manager.load(pin_config[0], auto_unload=False)
  delay(250)

  helper_path = pin_config[1]
  # Make sure output is disabled, so it won't start outputing a 
  # signal until analogWrite() is called: 
  if (sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN)) == '1\n'):
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN), '0')

  # Duty cyle must be set to 0 before changing frequency:
  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), '0')
  # Is this still true??

  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD), 
                      str(PWM_DEFAULT_PERIOD))
  addToCleanup(lambda : pwmDisable(pwm_pin))
  PWM_PINS_ENABLED[pwm_pin] = True
Esempio n. 20
0
File: pwm.py Progetto: Omtar/PyBBIO
def pwmEnable(pwm_pin):
  """ Ensures PWM module for the given pin is enabled and its ocp helper
      is loaded. """
  global PWM_PINS_ENABLED
  if PWM_PINS_ENABLED.get(pwm_pin): return
  pin_config = PWM_PINS[pwm_pin]
  assert (pin_config), "*Invalid PWM pin: '%s'" % pwm_pin

  for overlay in pin_config[2]:
    cape_manager.load(overlay, auto_unload=False)
    delay(250) # Give it some time to take effect
  cape_manager.load(pin_config[0], auto_unload=False)
  delay(250)

  helper_path = pin_config[1]
  # Make sure output is disabled, so it won't start outputing a 
  # signal until analogWrite() is called: 
  if (sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN)) == '1\n'):
    sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_RUN), '0')

  # Duty cyle must be set to 0 before changing frequency:
  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_DUTY), '0')
  # Is this still true??

  sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD), 
                      str(PWM_DEFAULT_PERIOD))
  addToCleanup(lambda : pwmDisable(pwm_pin))
  PWM_PINS_ENABLED[pwm_pin] = True
Esempio n. 21
0
 def setAbsolute(self):
   '''
   setAbsolute()
   Set mode as Absolute
   The position starts at zero and is incremented or 
   decremented by the encoder's movement
   '''
   mode_file = "%s/mode" % self.base_dir
   return sysfs.kernelFileIO(mode_file, 0)
Esempio n. 22
0
 def setAbsolute(self):
     '''
 setAbsolute()
 Set mode as Absolute
 The position starts at zero and is incremented or 
 decremented by the encoder's movement
 '''
     mode_file = "%s/mode" % self.base_dir
     return sysfs.kernelFileIO(mode_file, '0')
Esempio n. 23
0
    def _get_encoder(self):
        enc = sysfs.kernelFileIO(self.encoder_file).split()
        d = int(enc[0]) * self.coef_d
        g = int(enc[1]) * self.coef_g * 1.5
        angle = (d - g)

        ret = d - self.previous[0], g - self.previous[1], angle
        with self.lock:
            self.previous = (d, g)
        return ret
Esempio n. 24
0
    def _get_encoder(self):
        enc = sysfs.kernelFileIO(self.encoder_file).split()
        d = int(enc[0]) * self.coef_d
        g = int(enc[1]) * self.coef_g * 1.5
        angle = (d - g)

        ret = d - self.previous[0], g - self.previous[1], angle
        with self.lock:
            self.previous = (d, g)
        return ret
Esempio n. 25
0
 def getPosition(self):
     '''
 getPosition()
 Get the current position of the encoder.
 In absolute mode, this attribute represents the current position 
 of the encoder. 
 In relative mode, this attribute represents the position of the 
 encoder at the last unit timer overflow.
 '''
     position_file = "%s/position" % self.base_dir
     return sysfs.kernelFileIO(position_file)
Esempio n. 26
0
 def getPosition(self):
   '''
   getPosition()
   Get the current position of the encoder.
   In absolute mode, this attribute represents the current position 
   of the encoder. 
   In relative mode, this attribute represents the position of the 
   encoder at the last unit timer overflow.
   '''
   position_file = "%s/position" % self.base_dir
   return sysfs.kernelFileIO(position_file)