关于在micropython中结合def function()和PWM duty_ns()的问题 &



作为一个微python初学者,我结合了在不同论坛上发现的一些代码,以实现ESC信号控制的更高分辨率。代码将生成从最小1000000纳秒到最大2000000纳秒的脉冲,但我只能做100的增量。我的代码有点乱。对不起,如果这伤害了你的眼睛。我的问题是,它代表100ns的分辨率吗?让它以1为增量的技巧是什么呢?(不确定是否有必要,但我仍然希望有人能分享一些智慧。)

from machine import Pin, PWM, ADC
from time import sleep
MIN=10000
MAX=20000
class setPin(PWM):
def __init__(self, pin: Pin):
super().__init__(pin)
def duty(self,d):
super().duty_ns(d*100)
print(d*100)
pot = ADC(0)
esc = setPin(Pin(7))
esc.freq(500)
esc.duty(MIN)    # arming ESC at 1000 us.
sleep(1)
def map(x, in_min, in_max, out_min, out_max):  
return int((x - in_min)*(out_max - out_min)/(in_max - in_min) + out_min)

while True:
pot_val = pot.read_u16()
pulse_ns = map(pot_val, 256, 65535, 10000, 20000)
if pot_val<300:    # makes ESC more stable at startup.
esc.duty(MIN)
sleep(0.1)
if pot_val>65300:    # gives less tolerance when reaching MAX.
esc.duty(MAX)
sleep(0.1)
else:
esc.duty(pulse_ns)    # generates 1000000ns to 2000000ns of pulse.
sleep(0.1)

尝试更改

esc.freq(500) => esc.freq(250)
x=3600
print (map(3600,256,65535,10000,20000)*100)

最新更新