0

as a Micro python beginner, I combined few codes found on different forums in order to achieve a higher resolution for ESC signal control. The code will generate from a MIN 1000000 nanoseconds to a MAX 2000000 nanoseconds' pulse but I could only do 100 in increments. My code is kind of messy. Sorry if that hurts your eyes. My question is, does it represent an actual 100ns of resolution? and what's the trick to make it in increments of 1.(Not sure whether is even necessary, but I still hope someone can share some wisdom.)

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)

1 Answers1

0

try to change

esc.freq(500) => esc.freq(250)

x=3600
print (map(3600,256,65535,10000,20000)*100)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77