tx-udp: change control scheme, try to optimize
This commit is contained in:
parent
8075579496
commit
83474b34c7
2 changed files with 42 additions and 27 deletions
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"address": "/dev/ttyUSB1",
|
"address": "/dev/esp-tx",
|
||||||
"username": "micro",
|
"username": "micro",
|
||||||
"password": "python",
|
"password": "python",
|
||||||
"sync_folder": "src",
|
"sync_folder": "src",
|
||||||
|
|
65
src/main.py
65
src/main.py
|
@ -1,7 +1,7 @@
|
||||||
# main.py
|
# main.py
|
||||||
import machine
|
import machine
|
||||||
import socket
|
import socket
|
||||||
from time import sleep
|
import time
|
||||||
from wifi import WifiSTA
|
from wifi import WifiSTA
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,36 +15,38 @@ PIN_B1 = 0 # D2
|
||||||
PIN_B2 = 4 # D3
|
PIN_B2 = 4 # D3
|
||||||
## Delay
|
## Delay
|
||||||
DELAY_CMD = 0.1
|
DELAY_CMD = 0.1
|
||||||
|
DELAY_IRQ = 0.1
|
||||||
## HOST
|
## HOST
|
||||||
RX_ADDR = ("192.168.11.220", 80)
|
RX_ADDR = ("192.168.11.220", 80)
|
||||||
# RX_ADDR = ("192.168.0.1", 80)
|
# RX_ADDR = ("192.168.0.1", 80)
|
||||||
|
|
||||||
ts_cmd = 0
|
ts_irq = 0
|
||||||
s = None
|
s = None
|
||||||
|
|
||||||
def handle_btn_up(pin):
|
def handle_btn_up(pin):
|
||||||
send_cmd("fwd")
|
global ts_irq
|
||||||
|
t = time.time()
|
||||||
|
if t > (ts_irq + DELAY_IRQ):
|
||||||
|
ts_irq = t
|
||||||
|
send_cmd("rotate")
|
||||||
|
|
||||||
def handle_btn_dn(pin):
|
# def handle_btn_b1(pin):
|
||||||
send_cmd("rev")
|
# global ts_irq
|
||||||
|
# t = time.time()
|
||||||
|
# if t > (ts_irq + DELAY_IRQ):
|
||||||
|
# ts_irq = t
|
||||||
|
# send_cmd("b1")
|
||||||
|
|
||||||
def handle_btn_le(pin):
|
# def handle_btn_b2(pin):
|
||||||
send_cmd("left")
|
# global ts_irq
|
||||||
|
# t = time.time()
|
||||||
def handle_btn_ri(pin):
|
# if t > (ts_irq + DELAY_IRQ):
|
||||||
send_cmd("right")
|
# ts_irq = t
|
||||||
|
# send_cmd("b2")
|
||||||
def handle_btn_b1(pin):
|
|
||||||
send_cmd("b1")
|
|
||||||
|
|
||||||
def handle_btn_b2(pin):
|
|
||||||
send_cmd("b2")
|
|
||||||
|
|
||||||
def send_cmd(cmd):
|
def send_cmd(cmd):
|
||||||
|
global s
|
||||||
print("cmd: {}".format(cmd))
|
print("cmd: {}".format(cmd))
|
||||||
global s, ts_cmd
|
|
||||||
# if time.time() > (ts_cmd + DELAY_CMD):
|
|
||||||
# ts_cmd = time.time()
|
|
||||||
try:
|
try:
|
||||||
s.sendto(cmd.encode(), RX_ADDR)
|
s.sendto(cmd.encode(), RX_ADDR)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
|
@ -57,19 +59,32 @@ def main():
|
||||||
btn_up = machine.Pin(PIN_UP, machine.Pin.IN, machine.Pin.PULL_UP)
|
btn_up = machine.Pin(PIN_UP, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||||
btn_up.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_up)
|
btn_up.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_up)
|
||||||
btn_dn = machine.Pin(PIN_DN, machine.Pin.IN, machine.Pin.PULL_UP)
|
btn_dn = machine.Pin(PIN_DN, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||||
btn_dn.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_dn)
|
# btn_dn.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_dn)
|
||||||
btn_le = machine.Pin(PIN_LE, machine.Pin.IN, machine.Pin.PULL_UP)
|
btn_le = machine.Pin(PIN_LE, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||||
btn_le.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_le)
|
# btn_le.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_le)
|
||||||
btn_ri = machine.Pin(PIN_RI, machine.Pin.IN, machine.Pin.PULL_UP)
|
btn_ri = machine.Pin(PIN_RI, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||||
btn_ri.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_ri)
|
# btn_ri.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_ri)
|
||||||
btn_b1 = machine.Pin(PIN_B1, machine.Pin.IN, machine.Pin.PULL_UP)
|
btn_b1 = machine.Pin(PIN_B1, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||||
btn_b1.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_b1)
|
# btn_b1.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_b1)
|
||||||
btn_b2 = machine.Pin(PIN_B2, machine.Pin.IN, machine.Pin.PULL_UP)
|
btn_b2 = machine.Pin(PIN_B2, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||||
btn_b2.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_b2)
|
# btn_b2.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_b2)
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
sleep(0.01)
|
if not btn_le.value():
|
||||||
|
send_cmd("left")
|
||||||
|
elif not btn_ri.value():
|
||||||
|
send_cmd("right")
|
||||||
|
elif not btn_b1.value():
|
||||||
|
send_cmd("accel")
|
||||||
|
elif not btn_b2.value():
|
||||||
|
send_cmd("decel")
|
||||||
|
elif not btn_up.value():
|
||||||
|
send_cmd("rotate")
|
||||||
|
elif not btn_dn.value():
|
||||||
|
send_cmd("stop")
|
||||||
|
|
||||||
|
time.sleep(DELAY_CMD)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
Loading…
Reference in a new issue