DeaD_EyE

motor controlled by pwm

Aug 7th, 2025
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. import time
  2. from _thread import start_new_thread
  3. from machine import Pin, PWM
  4.  
  5.  
  6. class Motor:
  7.     def __init__(self, direction, speed, position):
  8.         self.position = 0
  9.         self.thread = None
  10.        
  11.         self.direction_pin = Pin(direction, mode=Pin.OUT, value=1)
  12.         self.speed_pin = PWM(Pin(speed), freq=30_000, duty_u16=65535)
  13.         self.position_pin = Pin(position, mode=Pin.IN, pull=Pin.PULL_UP)
  14.         self.position_pin.irq(handler=self.edge, trigger=Pin.IRQ_FALLING)
  15.  
  16.     def edge(self, pin):
  17.         self.position += 1 if self.direction_pin() else -1
  18.  
  19.     @property
  20.     def speed(self):
  21.         return (65535 - self.speed_pin.duty_u16()) / 65535 * 100
  22.    
  23.     @speed.setter
  24.     def speed(self, speed):
  25.         duty_u16 = 65535 - int(speed / 100 * 65535)
  26.         self.speed_pin.duty_u16(duty_u16)
  27.  
  28.  
  29.     @property
  30.     def direction(self):
  31.         return 1 if self.direction_pin.value() else -1
  32.  
  33.     @direction.setter
  34.     def direction(self, value):
  35.         """
  36.        -1 := CCW
  37.         1 := CW
  38.        """
  39.         if value == -1:
  40.             self.direction_pin.value(0)
  41.         elif value == 1:
  42.             self.direction_pin.value(1)
  43.    
  44.     def stop(self):
  45.         self.speed = 0
  46.    
  47.     def stop_ramp(self):
  48.         self.thread = None
  49.    
  50.     def wait_ramp(self):
  51.         while self.thread is not None:
  52.             time.sleep_ms(1)
  53.  
  54.     def ramp(self, start, end, duration):
  55.         if duration == 0:
  56.             self.speed = end
  57.             return
  58.        
  59.         if self.thread is None:
  60.             self.thread = True
  61.             start_new_thread(self._ramp, (start, end, duration))
  62.        
  63.     def _ramp(self, start, end, duration):
  64.         steps = int(min(256, duration * 50))
  65.         delay = int(duration / steps * 1000)
  66.         increment = (end - start) / steps
  67.         current_speed = start
  68.         self.speed = int(current_speed)
  69.        
  70.         for _ in range(steps):
  71.             if self.thread is None:
  72.                 return
  73.            
  74.             current_speed += increment
  75.             self.speed = int(current_speed)
  76.             time.sleep_ms(delay)
  77.        
  78.         self.thread = None
  79.  
  80. DURATION = 0
  81. MAX_SPEED = 50
  82. MAX_POSITION = 500
  83.  
  84. motor = Motor(2, 3, 4)
  85. motor.ramp(0, MAX_SPEED, DURATION)
  86. while motor.thread is not None:
  87.     time.sleep_ms(1)
  88.  
  89. while True:
  90.     while motor.position < MAX_POSITION:
  91.         time.sleep_ms(1)
  92.        
  93.     print(motor.position)
  94.  
  95.     motor.ramp(MAX_SPEED, 0, DURATION)
  96.     motor.wait_ramp()
  97.     print(motor.position)
  98.    
  99.     motor.direction = -1
  100.     motor.ramp(0, MAX_SPEED, DURATION)
  101.     motor.wait_ramp()
  102.     print(motor.position)
  103.  
  104.     while motor.position > 0:
  105.         time.sleep_ms(1)
  106.  
  107.     print(motor.position)
  108.  
  109.     motor.ramp(MAX_SPEED, 0, DURATION)
  110.     motor.wait_ramp()
  111.     print(motor.position)
  112.     motor.direction = 1
  113.  
  114.     motor.ramp(0, MAX_SPEED, DURATION)
  115.     motor.wait_ramp()
  116.     print(motor.position)
  117.  
Advertisement