【问题标题】:question about combining def function() and PWM duty_ns() in micropython关于在 micropython 中结合 def function() 和 PWM duty_ns() 的问题
【发布时间】:2022-08-18 09:03:24
【问题描述】:

作为一个 Micro python 初学者,我结合了在不同论坛上找到的一些代码,以实现更高分辨率的 ESC 信号控制。该代码将生成从 MIN 1000000 纳秒到 MAX 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)

    标签: resolution micropython pwm rp2040


    【解决方案1】:

    尝试改变

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多