# Ramp Test. Measre speed vs. voltage for a DC motor from machine import Pin, PWM, Timer from time import sleep from math import sin, pi, floor Out1 = Pin(18, Pin.OUT) Out2 = Pin(19, Pin.OUT) fwd = PWM(Pin(18)) rev = PWM(Pin(19)) fwd.freq(100) rev.freq(100) pin1 = Pin(26,Pin.IN) pin2 = Pin(27,Pin.IN,Pin.PULL_UP) N1 = N2 = N12 = 0 flag = 0 def ChanA(pin1): global pin2 global N1 if(pin2.value()): N1 -= 1 else: N1 += 1 pin1.irq(trigger=Pin.IRQ_RISING, handler=ChanA) Speed = E1 = E0 = Vin = flag = Ref = 0 def tick(timer): global N1, N2, N12 X = N1 N12 = N1 - N2 N2 = X tim = Timer() tim.init(freq=20, mode=Timer.PERIODIC, callback=tick) t = 0 dt = 1/20 # convert volts to PWM kv = 65535 / 13.4 # convert speed to rad/sec kw = 0.16*pi fwd.duty_u16(0) rev.duty_u16(0) V = 0 while(V < 13.4): fwd.duty_u16(int(V*kv)) rev.duty_u16(0) sleep(0.5) Speed = N12*kw print('{: 7.4f}'.format(V), '{: 7.4f}'.format(Speed), N12) V += 0.1 print('Stop') fwd.duty_u16(0) rev.duty_u16(0)