rx: initial commit with udp
This commit is contained in:
commit
1df647198b
12 changed files with 414 additions and 0 deletions
1
src/boot.py
Normal file
1
src/boot.py
Normal file
|
@ -0,0 +1 @@
|
|||
# boot.py - - runs on boot-up
|
51
src/esc.py
Normal file
51
src/esc.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
from motor import Motor
|
||||
from time import sleep
|
||||
|
||||
delta = 10
|
||||
|
||||
class Esc:
|
||||
def __init__(self, pin_m1_ena, pin_m1_in1, pin_m1_in2,
|
||||
pin_m2_ena, pin_m2_in1, pin_m2_in2):
|
||||
self.m1 = Motor("m1", pin_m1_ena, pin_m1_in1, pin_m1_in2)
|
||||
self.m2 = Motor("m2", pin_m2_ena, pin_m2_in1, pin_m2_in2)
|
||||
|
||||
def fwd(self):
|
||||
speed = min((self.m1.speed + self.m2.speed)/2 + delta, 100)
|
||||
self.m1.set_speed(speed)
|
||||
self.m2.set_speed(speed)
|
||||
|
||||
def rev(self):
|
||||
speed = max((self.m1.speed + self.m2.speed)/2 - delta, -100)
|
||||
self.m1.set_speed(speed)
|
||||
self.m2.set_speed(speed)
|
||||
|
||||
def left(self):
|
||||
self.m1.dec(delta)
|
||||
self.m2.inc(delta)
|
||||
|
||||
def right(self):
|
||||
self.m1.inc(delta)
|
||||
self.m2.dec(delta)
|
||||
|
||||
# stop
|
||||
def b1(self):
|
||||
self.m1.set_speed(0)
|
||||
self.m2.set_speed(0)
|
||||
|
||||
def b2(self):
|
||||
if self.m1.speed >= 0:
|
||||
self.m1.set_speed(0)
|
||||
self.m2.set_speed(0)
|
||||
sleep(1)
|
||||
self.m1.set_speed(-50)
|
||||
self.m2.set_speed(50)
|
||||
else:
|
||||
self.m1.set_speed(0)
|
||||
self.m2.set_speed(0)
|
||||
sleep(1)
|
||||
self.m1.set_speed(50)
|
||||
self.m2.set_speed(-50)
|
||||
|
||||
def get_status(self):
|
||||
s = (self.m1.get_status(), self.m2.get_status())
|
||||
return s
|
69
src/main.py
Normal file
69
src/main.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
# main.py
|
||||
import machine
|
||||
import usocket as socket
|
||||
from time import sleep
|
||||
from esc import Esc
|
||||
from wifi import WifiAP
|
||||
from wifi import WifiSTA
|
||||
# import web
|
||||
|
||||
PIN_M1_ENA = 5 # D1
|
||||
PIN_M1_IN1 = 4 # D2
|
||||
PIN_M1_IN2 = 0 # D3
|
||||
PIN_M2_IN1 = 12 # D6
|
||||
PIN_M2_IN2 = 13 # D7
|
||||
PIN_M2_ENA = 15 # D8
|
||||
|
||||
esc = None
|
||||
|
||||
def main():
|
||||
print("main")
|
||||
global esc
|
||||
wifi_sta = WifiSTA()
|
||||
# wifi_ap = WifiAP()
|
||||
esc = Esc(PIN_M1_ENA, PIN_M1_IN1, PIN_M1_IN2,
|
||||
PIN_M2_ENA, PIN_M2_IN1, PIN_M2_IN2)
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
|
||||
s.bind(("", 80))
|
||||
|
||||
while True:
|
||||
request = ""
|
||||
try:
|
||||
request, addr = s.recvfrom(256)
|
||||
req = request.decode()
|
||||
|
||||
print("data: {}, from: {}".format(req, addr))
|
||||
if len(request) == 0:
|
||||
print("no data")
|
||||
continue
|
||||
except:
|
||||
print("error: no data")
|
||||
pass
|
||||
|
||||
if "fwd" in req:
|
||||
print("--> fwd")
|
||||
esc.fwd()
|
||||
|
||||
elif "left" in req:
|
||||
print("--> left")
|
||||
esc.left()
|
||||
|
||||
elif "rev" in req:
|
||||
print("--> rev")
|
||||
esc.rev()
|
||||
|
||||
elif "right" in req:
|
||||
print("--> right")
|
||||
esc.right()
|
||||
|
||||
elif "b1" in req:
|
||||
print("--> b1")
|
||||
esc.b1()
|
||||
|
||||
elif "b2" in req:
|
||||
print("--> b2")
|
||||
esc.b2()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
53
src/motor.py
Normal file
53
src/motor.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
import machine
|
||||
from time import sleep
|
||||
|
||||
|
||||
class Motor:
|
||||
PWM_MAX = 1023
|
||||
DIR_FWD = 1
|
||||
DIR_REV = 0
|
||||
|
||||
def __init__(self, name, ena, p1, p2):
|
||||
self.name = name
|
||||
self.pin1 = machine.Pin(p1, machine.Pin.OUT)
|
||||
self.pin2 = machine.Pin(p2, machine.Pin.OUT)
|
||||
self.pwm = machine.PWM(machine.Pin(ena, machine.Pin.OUT))
|
||||
self.pwm.freq(1000)
|
||||
# state
|
||||
self.dir = -1
|
||||
self.speed = -1
|
||||
sleep(1)
|
||||
# initialize motors
|
||||
self.set_speed(0)
|
||||
print(self.get_status())
|
||||
|
||||
def get_status(self):
|
||||
s = "[{}:status] pins {},{} duty {}".format(self.name, self.pin1.value(), self.pin2.value(), self.pwm.duty())
|
||||
return s
|
||||
|
||||
def set_dir(self, dir):
|
||||
if self.dir == dir:
|
||||
return
|
||||
self.dir = dir
|
||||
v1 = 1 if dir == self.DIR_FWD else 0
|
||||
v2 = 0 if dir == self.DIR_FWD else 1
|
||||
self.pin1.value(v1)
|
||||
self.pin2.value(v2)
|
||||
|
||||
def set_speed(self, speed=100):
|
||||
print("[{}:set_speed] {} [-100:100]".format(self.name, speed))
|
||||
if speed < 0:
|
||||
self.set_dir(self.DIR_REV)
|
||||
else:
|
||||
self.set_dir(self.DIR_FWD)
|
||||
self.speed = speed
|
||||
s = int(self.PWM_MAX * abs(speed)/100)
|
||||
self.pwm.duty(s)
|
||||
|
||||
def inc(self, delta):
|
||||
speed = min(self.speed + delta, 100)
|
||||
self.set_speed(speed)
|
||||
|
||||
def dec(self, delta):
|
||||
speed = max(self.speed - delta, -100)
|
||||
self.set_speed(speed)
|
31
src/wifi.py
Normal file
31
src/wifi.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
from credentials import *
|
||||
import network
|
||||
import time
|
||||
|
||||
address = "192.168.0.1"
|
||||
netmask = "255.255.255.0"
|
||||
gateway = "192.168.0.1"
|
||||
dns = "192.168.0.1"
|
||||
|
||||
if_ap = network.WLAN(network.AP_IF)
|
||||
if_sta = network.WLAN(network.STA_IF)
|
||||
|
||||
class WifiAP:
|
||||
def __init__(self):
|
||||
global if_ap, if_sta
|
||||
if_sta.active(False)
|
||||
if_ap.active(True)
|
||||
if_ap.ifconfig([address, netmask, gateway, dns])
|
||||
if_ap.config(essid=SSID, password=PASS)
|
||||
print("network config {}".format(if_ap.ifconfig()))
|
||||
|
||||
class WifiSTA:
|
||||
def __init__(self):
|
||||
global if_ap, if_sta
|
||||
if_ap.active(False)
|
||||
if_sta.active(True)
|
||||
if_sta.connect(SSID, PASS)
|
||||
while not if_sta.isconnected():
|
||||
pass
|
||||
time.sleep(0.1)
|
||||
print("network config {}".format(if_sta.ifconfig()))
|
Loading…
Add table
Add a link
Reference in a new issue