Advertisement
silver2row

Updated_PWM

Sep 4th, 2022
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import pathlib
  4. from time import sleep
  5.  
  6. class PWM:
  7.  
  8.     def start(duty, freq=2000, polarity=0):
  9.         """ Set up and start the PWM channel. """
  10.  
  11.         path = pathlib.Path("/dev/bone/pwm/1/b")
  12.         if path == None:
  13.             return None
  14.  
  15.         period_ns = 1e9 / freq
  16.         duty_ns = (period_ns * (duty / 100.0))
  17.  
  18.         # export
  19.         try:
  20.             fd = open(path + "/export", 'w')
  21.             fd.write(str(path))
  22.             fd.close()
  23.         except:
  24.             pass
  25.         time.sleep(0.05)    # Give export a chance
  26.  
  27.         # Duty Cycle - Set to 0 so period can be changed
  28.         try:
  29.             fd = open(pathpwm + "/duty_cycle", 'w')
  30.             fd.write('0')
  31.             fd.close()
  32.         except:
  33.             pass
  34.  
  35.         # Period
  36.         fd = open(pathpwm + "/period", 'w')
  37.         fd.write(str(int(period_ns)))
  38.         fd.close()
  39.  
  40.         # Duty Cycle
  41.         fd = open(pathpwm + "/duty_cycle", 'w')
  42.         fd.write(str(int(duty_ns)))
  43.         fd.close()
  44.  
  45.         # Enable
  46.         fd = open(pathpwm + "/enable", 'w')
  47.         fd.write('1')
  48.         fd.close()
  49.  
  50.         return 'Started'
  51.  
  52.     def set_frequency(freq):
  53.         """ Change the frequency
  54.  
  55.        frequency - frequency in Hz (freq > 0.0) """
  56.         path = Path("/dev/bone/pwm/1")
  57.         pathpwm = path + "/b"
  58.  
  59.         # compute current duty cycle
  60.         fd = open(pathpwm + "/duty_cycle", 'r')
  61.         duty_cycle_ns = int(fd.read())
  62.         fd.close()
  63.  
  64.         fd = open(pathpwm + "/period", 'r')
  65.         period_ns = int(fd.read())
  66.         fd.close()
  67.  
  68.         duty_cycle = duty_cycle_ns/period_ns          # compute current duty cycle as fraction
  69.         period_ns = 1e9 / freq                  # compute new period
  70.         duty_cycle_ns = period_ns*duty_cycle # compute new duty cycle as fraction of period
  71.  
  72.     # print('duty_cycle: ' + str(duty_cycle))
  73.     # print('period_ns: ' + str(period_ns))
  74.     # print('duty_cycle_ns: ' + str(duty_cycle_ns))
  75.  
  76.         # Duty Cycle - Set to 0
  77.         fd = open(pathpwm + "/duty_cycle", 'w')
  78.         fd.write('0')
  79.         fd.close()
  80.  
  81.         # Period
  82.         fd = open(pathpwm + "/period", 'w')
  83.         fd.write(str(int(period_ns)))
  84.         fd.close()
  85.  
  86.         # Duty Cycle
  87.         fd = open(pathpwm + "/duty_cycle", 'w')
  88.         fd.write(str(int(duty_cycle_ns)))
  89.         fd.close()
  90.  
  91.     def set_duty_cycle(duty):
  92.         """ Change the duty cycle.
  93.  
  94.        dutycycle - between 0.0 and 100.0 """
  95.         path = get_pwm_path()
  96.         pathpwm = path + "/b"
  97.  
  98.         fd = open(pathpwm + "/period", 'r')
  99.         period_ns = int(fd.read())
  100.         fd.close()
  101.  
  102.         duty_cycle_ns = duty/100 * period_ns
  103.         # Duty Cycle
  104.         fd = open(pathpwm + "/duty_cycle", 'w')
  105.         fd.write(str(int(duty_cycle_ns)))
  106.         fd.close()
  107.  
  108.     def stop():
  109.         """ Stop the PWM channel. """
  110.  
  111.         path = pwm_path()
  112.         pathpwm = path + "/b"
  113.  
  114.         # Disable
  115.         fd = open(pathpwm + "/enable", 'w')
  116.         fd.write('0')
  117.         fd.close()
  118.  
  119.         # unexport
  120.         fd = open(path + "/unexport", 'w')
  121.         fd.write(str(path[1]))
  122.         fd.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement