рдПрдордХреЗ esp8266 рдкрд░ рд╕рд░рд▓ рд░реЛрдмреЛрдЯ micropython рдХреЗ рд╕рд╛рде

рд╣реЗрд▓реЛ, рд╣реЗрдмреНрд░!

рдЗрд╕ рд▓реЗрдЦ рдореЗрдВ рдПрдХ рд╕реНрдХреИрдирд┐рдВрдЧ рдЕрд▓реНрдЯреНрд░рд╛рд╕реЛрдирд┐рдХ рдмрд╛рдзрд╛ рд╕реЗрдВрд╕рд░, рдмреНрд▓рд┐рдВрдХрд┐рдВрдЧ рдПрд▓рдИрдбреА, рд╕реНрдЯрд╛рд░реНрдЯ / рд╕реНрдЯреЙрдк рдмрдЯрди рдХреЗ рд╕рд╛рде-рд╕рд╛рде рдПрдХ рдПрдХреАрдХреГрдд рд╡реЗрдм рд╕рд░реНрд╡рд░ рдХреЗ рд╕рд╛рде-рд╕рд╛рде рдПрдХ рдкреНрд░рд╢рд┐рдХреНрд╖рдг рдкрд░рд┐рдпреЛрдЬрдирд╛ рдХреЗ рд╣рд┐рд╕реНрд╕реЗ рдХреЗ рд░реВрдк рдореЗрдВ, рдорд╛рдЗрдХреНрд░реЛрдкреАрдереЙрди рдХреЗ рд╕рд╛рде esp8266 рдПрдордХреЗ рдкрд░ рдЖрдзрд╛рд░рд┐рдд рдПрдХ рд╕реНрд╡-рдЪрд╛рд▓рд┐рдд рдордВрдЪ рдХреЗ рдЙрдиреНрдирдпрди рдХреА рдкреНрд░рдХреНрд░рд┐рдпрд╛ рдХрд╛ рд╡рд░реНрдгрди рд╣реИ ред KDPV:





рддреЛ, рдкрд╣рд▓реЗ рджреЛ рднрд╛рдЧреЛрдВ рдиреЗ рдПрдХ рд╡рд╛рдИрдлрд╝рд╛рдИ рд╡реЗрдм рдЗрдВрдЯрд░рдлреЗрд╕ рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рдирд┐рдпрдВрддреНрд░рд┐рдд рдПрдХ рд╕реНрд╡-рдЪрд╛рд▓рд┐рдд рдордВрдЪ рдХреЗ рдирд┐рд░реНрдорд╛рдг рдХрд╛ рд╡рд░реНрдгрди рдХрд┐рдпрд╛ред

рд╡рд░реНрддрдорд╛рди рдЪрд░рдг рдХреЗ рд▓рд┐рдП рдХрд╛рд░реНрдп рдЗрд╕ рдЕрд▓реНрдЯреНрд░рд╛рд╕рд╛рдЙрдВрдб рдкреНрд▓реЗрдЯрдлреЙрд░реНрдо рдХреЛ HC-SR04 рд╕реЗрдВрд╕рд░ рд╕реЗ рд▓реИрд╕ рдХрд░рдирд╛ рд╣реИ, рдФрд░ рдСрдлрд╝рд▓рд╛рдЗрди рдХрд╛рдо рдХрд░рдиреЗ рдХреА рдХреНрд╖рдорддрд╛ рдХреЛ рдЬреЛрдбрд╝рдирд╛ рд╣реИред

рд╢реБрд░реВ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП - рдпрд╛рдВрддреНрд░рд┐рдХ рднрд╛рдЧ:
рдорд╛рдорд▓реЗ рдореЗрдВ рд╕реЗрдВрд╕рд░ рдФрд░ рд╕рд░реНрд╡реЛ рдХреЛ рдареАрдХ рдХрд░рдирд╛ рдЖрд╡рд╢реНрдпрдХ рд╣реИ, рдбрд┐рдЬрд╛рдЗрди (рдореИрдВрдиреЗ рдЗрд╕рдХреЗ рд▓рд┐рдП FreeCAD рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рд╣реИ ) рдФрд░ рд▓рд╛рдкрддрд╛ рднрд╛рдЧреЛрдВ рдХреЛ рдмрдирд╛рдПрдВ:





рдлрд┐рд░ - рдЗрд▓реЗрдХреНрдЯреНрд░рд┐рдХ рдПрдХ:
рд╕рд░реНрдХрд┐рдЯ рдХреЛ рддреИрдпрд╛рд░ рдХрд░реЗрдВ (рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдлреНрд░рд┐рдЯрд┐рдВрдЧ рдореЗрдВ рдФрд░ рдЗрд╕рдХреЗ рдЕрдиреБрд╕рд╛рд░ рд╕реНрд╡рд┐рдЪрд┐рдВрдЧ рдкреНрд░рджрд░реНрд╢рди рдХрд░реЗрдВ:



рдЙрд╕рдХреЗ рдмрд╛рдж, рдпрд╣ рд╕рдм рдЙрдбрд╝рд╛рди рднрд░рдиреЗ рдХреА рдХреЛрд╢рд┐рд╢ рдХрд░реЛ ...

рдЪреВрдБрдХрд┐ рдореИрдВ рдЪрд╛рд╣рддрд╛ рдерд╛ рдХрд┐ рд░реЛрдмреЛрдЯ рдкреНрд░реЛрдЧреНрд░рд╛рдо рдХреЗ рдХреБрдЫ рдХрд╛рд░реНрдп рд╕рдорд╛рдирд╛рдВрддрд░ рдореЗрдВ рдХрд┐рдП рдЬрд╛рдПрдВ (рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдмрд╛рдзрд╛рдУрдВ рдФрд░ рдЧрддрд┐ рдХреЗ рдХрд╛рд░реНрдп рдХреЛ рджреВрд░реА рдХреЛ рд╕реНрдХреИрди рдХрд░рдиреЗ рдХреА рдкреНрд░рдХреНрд░рд┐рдпрд╛), рдореБрдЭреЗ рдПрд╕рд┐рдВрд╕рд┐рдпреЛ рдореЙрдбреНрдпреВрд▓ рдХреА рдХреНрд╖рдорддрд╛рдУрдВ рдореЗрдВ рдЙрддрд░рдирд╛ рдкрдбрд╝рд╛ ред рдЗрд╕ рдФрд░ рдЗрд╕ рд▓реЗрдЦ рдореЗрдВ asyncio рдХреЗ рд╕рд╛рде рдЕрдзрд┐рдХ рд╡рд┐рд╕реНрддреГрдд рдХрд╛рдо рдХрд╛ рд╡рд░реНрдгрди рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИ ред

рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдПрдХ рдПрд▓рдИрдбреА рдХреЛ рдмреНрд▓рд┐рдВрдХ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП, рдЖрдк рдРрд╕реЗ рдХреЛрд░рдЯрд╛рдЗрди рдХреЛ рд▓рд╛рдЧреВ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ, рдЬреЛ рд╡реНрдпрд╛рд╡рд╣рд╛рд░рд┐рдХ рд░реВрдк рд╕реЗ рд╕рд┐рдВрдХреНрд░реЛрдирд╕ рд╕реЗ рдЕрд▓рдЧ рдирд╣реАрдВ рд╣реИ:

import uasyncio as asyncio
from machine import Pin

# onboard LED is connected to D0(GPIO16)
syst_led =  Pin(16, Pin.OUT)

async def blink_led(led, interval_ms):
    led_val = True
    while True:
        led_val = not(led_val)
        led_state = led.value(int(led_val))
        await asyncio.sleep_ms(interval_ms)

# define loop
loop = asyncio.get_event_loop()

#create looped tasks
loop.create_task(blink_led(syst_led, interval_ms=250))

# loop run forever
loop.run_forever()

рдЕрдВрддрд░ рдпрд╣ рд╣реИ рдХрд┐ рдРрд╕реЗ рдХреЛрд░рд╛рдЙрдЯрд╛рдЗрди рдЬреЛ рдЕрд▓рдЧ-рдЕрд▓рдЧ рдХрд╛рд░реНрдп рдХрд░рддреЗ рд╣реИрдВ, рдПрдХ рд╣реА рд╕рдордп рдореЗрдВ рдХрдИ рд▓реЙрдиреНрдЪ рдХрд┐рдП рдЬрд╛ рд╕рдХрддреЗ рд╣реИрдВ (рд╕рдВрд╕рд╛рдзрдиреЛрдВ рдХреЛ рдЕрдиреБрд╕реВрдЪрдХ рджреНрд╡рд╛рд░рд╛ рдЖрд╡рдВрдЯрд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рдПрдЧрд╛)ред

рдЗрд╕ рдкреНрд░рдХрд╛рд░, рд╣рдо рдХреНрд╖реЗрддреНрд░ рдХреЛ рдорд╛рдкрдиреЗ рдФрд░ рд╕реНрдХреИрди рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП, рд╕рд╛рде рд╣реА рд╕рд╛рде рдПрдХ рд╣рд╛рд░реНрдбрд╡реЗрдпрд░ рдЗрдВрдЯрд░рдкреНрдЯ (рдмрдЯрди) рдХреЗ рд▓рд┐рдП рдХреЙрд▓рдмреИрдХ рд▓рд┐рдЦреЗрдВрдЧреЗ рдЬреЛ рд╕реНрдХреИрдирд┐рдВрдЧ рд╢реБрд░реВ рдпрд╛ рдмрдВрдж рдХрд░рддрд╛ рд╣реИред рд╕рдмрд╕реЗ рд╕рд░рд▓ рдорд╛рдорд▓реЗ рдореЗрдВ рдХреЛрд░рдЯрд╛рдЗрди рдХреЗ рдмреАрдЪ рд░рд╛рдЬреНрдп рдХрд╛ рд╕реНрдерд╛рдирд╛рдВрддрд░рдг рд╡реИрд╢реНрд╡рд┐рдХ рдЪрд░ рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ:

рдмрдЯрди рдХреЗ рд▓рд┐рдП рдХреЙрд▓рдмреИрдХ:

from machine import Pin

run_flag = False

# on/off button
button =  Pin(15, Pin.IN, Pin.PULL_UP) # connected to D8 (GPIO15)

# callback function for start/stop button
def callback(p):
    global run_flag
    run_flag = not(run_flag)
    print('set run_flag', run_flag, p)

# create callback for button:
button.irq(trigger=Pin.IRQ_FALLING, handler=callback)

рджреВрд░реА рдорд╛рдк:

import uasyncio as asyncio
from utime import sleep, sleep_us
from machine import Pin, time_pulse_us

# HC-SR04 ultrasonic sensor connected to GPIO12(D6)-trigger and GPIO13(D7)-echo
trig=Pin(12, Pin.OUT)
echo=Pin(13, Pin.IN)

async def async_measure_range():
    echo_timeout_us=500*2*30 # Timeout in microseconds to listen to echo pin.
    trig.off() # Stabilize the sensor
    sleep_us(5)
    trig.on()
    sleep_us(10) # Send a 10us pulse.
    trig.off()
    try:
        pulse_time = time_pulse_us(echo, 1, echo_timeout_us)
    except:
        pass
    dist = (pulse_time / 2) / 29.1
    return dist

рд╕реЗрдХреНрдЯрд░ рд╕реНрдХреИрдирд┐рдВрдЧ (рджреВрд░реА рдорд╛рдкрдХ рдХреЙрд▓рдЖрдЙрдЯ рдХреЗ рд╕рд╛рде):

import uasyncio as asyncio
from machine import Pin, PWM

pos_actual = 75
dist_cm = 50

# servo SG90 connected to GPIO14(D5)
p14 =  Pin(14, Pin.OUT)
servo = PWM(p14, freq=50)

async def radar_scan(interval_ms):
    pos_list = [45,75,105,75]
    global pos_actual
    global dist_cm
    while True:
        if run_flag:
            for pos in pos_list:
                servo.duty(pos)
                await asyncio.sleep_ms(interval_ms)
                dist_cm = await async_measure_range()
                pos_actual = pos
                print('pos_actual = %s, dist_cm = %s' % (pos_actual, dist_cm)
        elif not run_flag:
            await asyncio.sleep(0) # do nothing

# define loop
loop = asyncio.get_event_loop(

#create looped tasks
loop.create_task(radar_scan(interval_ms=250))

# loop run forever
loop.run_forever()

рд╕реЗрдВрд╕рд░ рдХреЛ рдбрд┐рдмрдЧ рдХрд░рдиреЗ рдХреА рдкреНрд░рдХреНрд░рд┐рдпрд╛ рдореЗрдВ, рд╕рдордп-рд╕рдордп рдкрд░, рдПрдХ рдирдХрд╛рд░рд╛рддреНрдордХ рджреВрд░реА рдореВрд▓реНрдп рджрд┐рдпрд╛ред рдпрд╣ рдкрддрд╛ рдЪрд▓рд╛ - "рдЗрд▓реЗрдХреНрдЯреНрд░реЙрдирд┐рдХреНрд╕ рдмреБрд░реЗ рд╕рдВрдкрд░реНрдХреЛрдВ рдХрд╛ рд╡рд┐рдЬреНрдЮрд╛рди рд╣реИ" , рдЬрдм рд╕реЗрдВрд╕рд░ рдЪрд╛рд▓реВ рдХрд┐рдпрд╛ рдЧрдпрд╛ рдерд╛, рддреЛ рдХреЗрдмрд▓ рдЦреАрдВрдЪрд╛ рдЧрдпрд╛ рдерд╛ рдФрд░ рд╕рдВрдкрд░реНрдХ рдЦреЛ рдЧрдпрд╛ рдерд╛ред

рдпрд╣ рд╕реНрдХреИрди рдХреЗ рдкрд░рд┐рдгрд╛рдореЛрдВ рдХреЗ рдЖрдзрд╛рд░ рдкрд░ рдХрд╛рд░реНрд░рд╡рд╛рдИ рдХреА рдкрд╕рдВрдж рдХреЗ рддрд░реНрдХ рдХреЛ рддреЗрдЬ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдмрдирд╛ рд╣реБрдЖ рд╣реИ:

avoid_left = False
avoid_right = False
avoid_backward = False

async def make_decision(interval_ms, avoid_limit_cm):
    global avoid_left
    global avoid_right
    global avoid_backward
    while True:
        if run_flag:
            # make decision what to do
            if pos_actual == 45 and dist_cm < avoid_limit_cm :
                avoid_left = True
                if debug : print('avoid_left = %s' % avoid_left)
            elif pos_actual == 45 and dist_cm >= avoid_limit_cm :
                avoid_left = False
                if debug : print('avoid_left = %s' % avoid_left)
            elif pos_actual == 75 and dist_cm < avoid_limit_cm*1.25 :
                avoid_backward = True
                if debug : print('avoid_backward = %s' % avoid_backward)
            elif pos_actual == 75 and dist_cm >= avoid_limit_cm*1.25 :
                avoid_backward = False
                if debug : print('avoid_backward = %s' % avoid_backward)
            elif pos_actual == 105 and dist_cm < avoid_limit_cm :
                avoid_right = True
                if debug : print('avoid_right = %s' % avoid_right)
            elif pos_actual == 105 and dist_cm >= avoid_limit_cm :
                avoid_right = False
                if debug : print('avoid_right = %s' % avoid_right)
            # for debuging
            if debug : print('pos = %s, dist_cm = %s' % (pos_actual,dist_cm))  
            await asyncio.sleep_ms(interval_ms)
        elif not run_flag:
            await asyncio.sleep(0) # do nothing

#create looped tasks
loop.create_task(make_decision(interval_ms=250, avoid_limit_cm=15))

рдореЛрдЯрд░ рдХрд╛рд░реНрдп:

from random import getrandbits

async def moving(interval_ms):
    while True:
        if run_flag:
            # moving functions
            if avoid_backward :
                print('avoid_backward = %s' % avoid_backward)
                await backward(interval_ms*2)
                if bool(getrandbits(1)) :
                    await right_rotate(interval_ms+getrandbits(3)*100)
                    await stop_all()
                else:
                    await left_rotate(interval_ms+getrandbits(3)*100)
                    await stop_all()
            elif avoid_left :
                print('avoid_left = %s' % avoid_left)
                await left_turn(interval_ms)
            elif avoid_right :
                print('avoid_right = %s' % avoid_right)
                await right_turn(interval_ms)
            else:
                print('move_forward')
                await forward(interval_ms)
                
            await asyncio.sleep_ms(interval_ms)
        elif not run_flag:
            #stop all motors first
            await stop_all()
            await asyncio.sleep(0) # do nothing

#create looped tasks
loop.create_task(moving(interval_ms=1000))

рдФрд░ рдореЛрдЯрд░ рдирд┐рдпрдВрддреНрд░рдг
# nodemcu pins from the motor shield
p5 = Pin(5, Pin.OUT)  # connected to GPIO4(D1)
p4 = Pin(4, Pin.OUT)  # connected to GPIO4(D2)
revrs_L = Pin(0, Pin.OUT, value=0)  # connected to GPIO0(D3)
revrs_R = Pin(2, Pin.OUT, value=0)  # connected to GPIO2(D4) , also connected to onboard wifi LED
motor_L = PWM(p5, freq=1000, duty=0)
motor_R = PWM(p4, freq=1000, duty=0)
speed = 1023  #TODO: variable speed

async def stop_all():
    revrs_L.value(0)
    motor_L.duty(0)
    revrs_R.value(0)
    motor_R.duty(0)

async def forward(interval_ms):
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def backward(interval_ms):
    revrs_L.value(1)
    motor_L.duty(speed)
    revrs_R.value(1)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def right_rotate(interval_ms):
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(1)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def left_rotate(interval_ms):
    revrs_L.value(1)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def right_turn(interval_ms):
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(0)
    await asyncio.sleep_ms(interval_ms)

async def left_turn(interval_ms):
    revrs_L.value(0)
    motor_L.duty(0)
    revrs_R.value(0)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)


рд╕рд╛рде рд╣реА рдПрдХ рдирд┐рдорд┐рд╖ рдПрд▓рдИрдбреА рдХреЛ рдирд┐рдпрдВрддреНрд░рд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐ рдХрд╛рд░реНрдпрдХреНрд░рдо рдХрд╛рдо рдХрд░ рд░рд╣рд╛ рд╣реИ:

async def blink_led(led, interval_ms):
    led_val = True
    while True:
        if run_flag:
            led_val = not(led_val)
            led_state = led.value(int(led_val))
            await asyncio.sleep_ms(interval_ms)
        elif not run_flag:
            await asyncio.sleep(0) # do nothing

#create looped tasks
loop.create_task(blink_led(syst_led, interval_ms=250))

рдЬрд┐рд╕рдХреЗ рдмрд╛рдж, рдпрд╣ рдХреЗрд╡рд▓ рдпрд╣ рд╕рдм рдЗрдХрдЯреНрдард╛ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдмрдиреА рд╣реБрдИ рд╣реИ

рд╕рд╛рдмреБрдд
import gc
import uasyncio as asyncio
from utime import sleep, sleep_us
from machine import Pin, PWM, time_pulse_us
from random import getrandbits

# nodemcu pins from the motor shield
p5 = Pin(5, Pin.OUT)  # connected to GPIO4(D1)
p4 = Pin(4, Pin.OUT)  # connected to GPIO4(D2)
revrs_L = Pin(0, Pin.OUT, value=0)  # connected to GPIO0(D3)
revrs_R = Pin(2, Pin.OUT, value=0)  # connected to GPIO2(D4) , also connected to onboard wifi LED
motor_L = PWM(p5, freq=1000, duty=0)
motor_R = PWM(p4, freq=1000, duty=0)
speed = 1023  #TODO: variable speed

# servo SG90 connected to GPIO14(D5)
p14 =  Pin(14, Pin.OUT)
servo = PWM(p14, freq=50)
# on/off button
button =  Pin(15, Pin.IN, Pin.PULL_UP) # connected to D8 (GPIO15)
# onboard LED is connected to D0(GPIO16)
syst_led =  Pin(16, Pin.OUT)
# HC-SR04 ultrasonic sensor connected to GPIO12(D6)-trigger and GPIO13(D7)-echo
trig=Pin(12, Pin.OUT)
echo=Pin(13, Pin.IN)

#global flags and variables
run_flag = False
avoid_left = False
avoid_right = False
avoid_backward = False
pos_actual = 75
dist_cm = 50
debug = False


# callback function for start/stop button
def callback(p):
    global run_flag
    run_flag = not(run_flag)
    print('set run_flag', run_flag, p)

# sync fuctions
def stop_all_sync():
    revrs_L.value(0)
    motor_L.duty(0)
    revrs_R.value(0)
    motor_R.duty(0)
    
# async fuctions
async def stop_all():
    revrs_L.value(0)
    motor_L.duty(0)
    revrs_R.value(0)
    motor_R.duty(0)

async def forward(interval_ms):
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def backward(interval_ms):
    revrs_L.value(1)
    motor_L.duty(speed)
    revrs_R.value(1)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def right_rotate(interval_ms):
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(1)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def left_rotate(interval_ms):
    revrs_L.value(1)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def right_turn(interval_ms):
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(0)
    await asyncio.sleep_ms(interval_ms)

async def left_turn(interval_ms):
    revrs_L.value(0)
    motor_L.duty(0)
    revrs_R.value(0)
    motor_R.duty(speed)
    await asyncio.sleep_ms(interval_ms)

async def moving(interval_ms):
    while True:
        if run_flag:
            # moving functions
            if avoid_backward :
                print('avoid_backward = %s' % avoid_backward)
                await backward(interval_ms*2)
                if bool(getrandbits(1)) :
                    await right_rotate(interval_ms+getrandbits(3)*100)
                    await stop_all()
                else:
                    await left_rotate(interval_ms+getrandbits(3)*100)
                    await stop_all()
            elif avoid_left :
                print('avoid_left = %s' % avoid_left)
                await left_turn(interval_ms)
            elif avoid_right :
                print('avoid_right = %s' % avoid_right)
                await right_turn(interval_ms)
            else:
                print('move_forward')
                await forward(interval_ms)
                
            await asyncio.sleep_ms(interval_ms)
        elif not run_flag:
            #stop all motors first
            await stop_all()
            await asyncio.sleep(0) # do nothing


async def blink_led(led, interval_ms):
    led_val = True
    while True:
        if run_flag:
            led_val = not(led_val)
            led_state = led.value(int(led_val))
            await asyncio.sleep_ms(interval_ms)
        elif not run_flag:
            await asyncio.sleep(0) # do nothing
            
async def async_measure_range():
    echo_timeout_us=500*2*30 # Timeout in microseconds to listen to echo pin.
    trig.off() # Stabilize the sensor
    sleep_us(5)
    trig.on()
    sleep_us(10) # Send a 10us pulse.
    trig.off()
    try:
        pulse_time = time_pulse_us(echo, 1, echo_timeout_us)
    except:
        pass
    dist = (pulse_time / 2) / 29.1
    return dist

async def make_decision(interval_ms, avoid_limit_cm):
    global avoid_left
    global avoid_right
    global avoid_backward
    while True:
        if run_flag:
            # make decision what to do
            if pos_actual == 45 and dist_cm < avoid_limit_cm :
                avoid_left = True
                if debug : print('avoid_left = %s' % avoid_left)
            elif pos_actual == 45 and dist_cm >= avoid_limit_cm :
                avoid_left = False
                if debug : print('avoid_left = %s' % avoid_left)
            elif pos_actual == 75 and dist_cm < avoid_limit_cm*1.25 :
                avoid_backward = True
                if debug : print('avoid_backward = %s' % avoid_backward)
            elif pos_actual == 75 and dist_cm >= avoid_limit_cm*1.25 :
                avoid_backward = False
                if debug : print('avoid_backward = %s' % avoid_backward)
            elif pos_actual == 105 and dist_cm < avoid_limit_cm :
                avoid_right = True
                if debug : print('avoid_right = %s' % avoid_right)
            elif pos_actual == 105 and dist_cm >= avoid_limit_cm :
                avoid_right = False
                if debug : print('avoid_right = %s' % avoid_right)
            # for debuging
            if debug : print('pos = %s, dist_cm = %s' % (pos_actual,dist_cm))  
            await asyncio.sleep_ms(interval_ms)
        elif not run_flag:
            await asyncio.sleep(0) # do nothing

async def radar_scan(interval_ms):
    pos_list = [45,75,105,75]
    global pos_actual
    global dist_cm
    while True:
        if run_flag:
            for pos in pos_list:
                servo.duty(pos)
                await asyncio.sleep_ms(interval_ms)
                dist_cm = await async_measure_range()
                pos_actual = pos
        elif not run_flag:
            await asyncio.sleep(0) # do nothing
    
#stop all motors first
stop_all_sync()

# move servo to initial position
print('Move sensor to initial position...')
servo.duty(75)
sleep(1) #wait 1s for servo reaching initial position
print('Waiting for start button...')

#enable gc
gc.enable()

# create callback fo button:
button.irq(trigger=Pin.IRQ_FALLING, handler=callback)

# define loop
loop = asyncio.get_event_loop()

#create looped tasks
loop.create_task(blink_led(syst_led, interval_ms=250))
loop.create_task(radar_scan(interval_ms=250))
loop.create_task(make_decision(interval_ms=250, avoid_limit_cm=15))
loop.create_task(moving(interval_ms=1000))

# loop run forever
loop.run_forever()

рдФрд░ рдХрд╛рдо рдореЗрдВ рдЬрд╛рдВрдЪ:

рд╣рд╛рд▓рд╛рдБрдХрд┐, рдореИрдВ рд╡реЗрдм рдкреЗрдЬ рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рдореИрдиреНрдпреБрдЕрд▓ рдирд┐рдпрдВрддреНрд░рдг рдХреА рд╕рдВрднрд╛рд╡рдирд╛ рд░рдЦрдирд╛ рдЪрд╛рд╣реВрдВрдЧрд╛ ...

рдЗрд╕рдХреЗ рд▓рд┐рдП, рдПрдХ рдЕрд▓рдЧ coroutine рдореЗрдВ, рдПрдХ рд╕рд╛рдзрд╛рд░рдг рд╡реЗрдм рд╕рд░реНрд╡рд░ рдЬреЛрдбрд╝реЗрдВ:

async def web_page(request):
    global auto_run_flag
    motor_state="Stopped"
    if request.find('GET /?forward') > 0:
        motor_state="Going Forward"
        auto_run_flag = False
        forward_sync()
    elif request.find('GET /?left_rotate') > 0:
        motor_state="Rotate Left"
        auto_run_flag = False
        left_rotate_sync()
    elif request.find('GET /?right_rotate') > 0:
        motor_state="Rotate Right"
        auto_run_flag = False
        right_rotate_sync()
    elif request.find('GET /?left_turn') > 0:
        motor_state="Turn Left"
        auto_run_flag = False
        left_turn_sync()
    elif request.find('GET /?right_turn') > 0:
        motor_state="Turn Right"
        auto_run_flag = False
        right_turn_sync()
    elif request.find('GET /?backward') > 0:
        motor_state="Going Backward"
        auto_run_flag = False
        backward_sync()
    elif request.find('GET /?stop') > 0:
        motor_state="Stopped"
        auto_run_flag = False
        stop_all_sync()
    elif request.find('GET /?auto') > 0:
        auto_run_flag = not auto_run_flag
        if  auto_run_flag :
            motor_state="Autopilot"
        elif not auto_run_flag :
             motor_state="Stopped"
             stop_all_sync()

    html = """<html><head><title>RoboTank WEB</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="data:,"> <style>
    html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
    h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}
    .button{display: inline-block; background-color: #33c080; border: none; 
    border-radius: 4px; color: white; text-decoration: none; font-size: 30px; width:100%}
    .button2{background-color: #4286f4; width:30%}
    .button3{background-color: #eb2b10; width:35%}
    .button4{background-color: #8386f4; width:44%}
    </style></head>
    <body> <h1>RoboTank WEB</h1> 
    <p>Status : <strong>""" + motor_state + """</strong></p>
    <p><a href='/?forward'><button class="button">Forward</button></a></p>
    <p><a href='/?left_turn'><button class="button button2">LEFT</button></a>
    <a href='/?stop'><button class="button button3">STOP</button></a>
    <a href='/?right_turn'><button class="button button2">RIGHT</button></a>
    <p><a href='/?backward'><button class="button">Backward</button></a></p>
    <p><a href='/?left_rotate'><button class="button button4">L-rotate</button></a>
    <a href='/?right_rotate'><button class="button button4">R-rotate</button></a></p>
    <p><a href='/?auto'><button class="button button3">AUTO</button></a></p>
    </body></html>"""
    return html

async def web_handler(reader, writer):
    try:
        request = str(await reader.read(1024))
        #print('request = %s' % request)
        header = """HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close\n\n"""
        response = await web_page(request)
        await writer.awrite(header)
        await writer.awrite(response)
        await writer.aclose()
        print("Finished processing request")
    except Exception as e:
        print(e)
    
async def tcp_server(host, port):
    server = await asyncio.start_server(web_handler, host, port)

#create looped tasks
loop.create_task(tcp_server('0.0.0.0', 80))

рдФрд░ рдореИрдиреБрдЕрд▓ рдирд┐рдпрдВрддреНрд░рдг рдХреЗ рд▓рд┐рдП рддреБрд▓реНрдпрдХрд╛рд▓рд┐рдХ рдЧрддрд┐ рдХрд╛рд░реНрдп рдХрд░рддрд╛ рд╣реИред
def stop_all_sync():
    revrs_L.value(0)
    motor_L.duty(0)
    revrs_R.value(0)
    motor_R.duty(0)

def backward_sync():
    revrs_L.value(1)
    motor_L.duty(speed)
    revrs_R.value(1)
    motor_R.duty(speed)

def forward_sync():
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(speed)

def right_rotate_sync():
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(1)
    motor_R.duty(speed)

def left_rotate_sync():
    revrs_L.value(1)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(speed)
    
def right_turn_sync():
    revrs_L.value(0)
    motor_L.duty(speed)
    revrs_R.value(0)
    motor_R.duty(0)

def left_turn_sync():
    revrs_L.value(0)
    motor_L.duty(0)
    revrs_R.value(0)
    motor_R.duty(speed)


рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдЙрдкрд╕реНрдерд┐рддрд┐:



рдЕрдВрддрд┐рдо рд╕рдВрд╕реНрдХрд░рдг рдХреЗ рдкрд░реАрдХреНрд╖рдг:


рдпрд╣рд╛рдВ рд╕реНрд░реЛрдд рдЙрдкрд▓рдмреНрдз рд╣реИрдВред

рдкреНрд░реЗрд░рдгрд╛ рдХреЗ рд╕реНрд░реЛрдд:

docs.micropython.org/en/latest/library/uasyncio.html
habr.com/en/post/484446
habr.com/en/post/337420
habr.com/en/post/484472
github.com/peterhinch /micropython-async/blob/master/TUTORIAL.md
github.com/rsc1975/micropython-hcsr04
medium.com/@pgjones/an-asyncio-socket-tutorial-5e6f3308b8b0b0

All Articles