-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7061e08
commit 5fafe57
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# This file is executed on every boot (including wake-boot from deepsleep) | ||
import esp | ||
|
||
esp.osdebug(None) | ||
#import webrepl | ||
#webrepl.start() | ||
|
||
import main # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from machine import PWM, Pin | ||
|
||
FREQ = 200 | ||
DUTY = 0 | ||
|
||
DIR_M1 = Pin(16, Pin.OUT) | ||
DIR_M2 = Pin(13, Pin.OUT) | ||
PWM_M1 = PWM(Pin(15, Pin.OUT), freq=FREQ, duty_u16=DUTY) | ||
PWM_M2 = PWM(Pin(14, Pin.OUT), freq=FREQ, duty_u16=DUTY) | ||
|
||
def calcDuty(percentage): | ||
return int(65535/100 * percentage) | ||
|
||
def runM1(DIR, speed): | ||
DIR_M1.value(DIR) | ||
PWM_M1.duty_u16(calcDuty(speed)) | ||
|
||
def runM2(DIR, speed): | ||
DIR_M2.value(DIR) | ||
PWM_M2.duty_u16(calcDuty(speed)) | ||
|
||
def parseSpeed(speed) -> int: | ||
speed = str(speed) | ||
if speed.isdigit(): | ||
speed = int(speed) | ||
if speed <= 100: | ||
return speed | ||
elif speed > 100: | ||
return 100 | ||
else: | ||
return 0 | ||
|
||
def forward(speed): | ||
print("for", parseSpeed(speed)) | ||
def backward(speed): | ||
print("back", parseSpeed(speed)) | ||
def right(speed): | ||
print("r", parseSpeed(speed)) | ||
def left(speed): | ||
print("l", parseSpeed(speed)) | ||
def rMotor(speed): | ||
#print("rMotor", parseSpeed(speed)) | ||
runM1(0, parseSpeed(speed)) | ||
def lMotor(speed): | ||
#print("lMotor", parseSpeed(speed)) | ||
runM2(1, parseSpeed(speed)) | ||
|
||
def stop(): | ||
rMotor(0) | ||
lMotor(0) | ||
|
||
def execute(cmd, speed): | ||
if cmd == "forward": forward(speed) | ||
if cmd == "backward": backward(speed) | ||
if cmd == "right": right(speed) | ||
if cmd == "left": left(speed) | ||
if cmd == "rmotor": rMotor(speed) | ||
if cmd == "lmotor": lMotor(speed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import socket | ||
|
||
import driver | ||
|
||
debug = True | ||
platform = None | ||
|
||
UDP_IP = "0.0.0.0" | ||
UDP_PORT = 7913 | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP | ||
sock.bind((UDP_IP, UDP_PORT)) | ||
|
||
try: | ||
import network | ||
|
||
platform = "ESP" | ||
ap = network.WLAN(network.AP_IF) # create access-point interface | ||
ap.config(ssid="RoverNumber1", password="qwerty123456", authmode=3, max_clients=1) | ||
ap.active(True) | ||
print("Send packets to this IP:", ap.ifconfig()[2]) # Show IP | ||
except Exception as e: | ||
platform = "PC" | ||
hostname = socket.gethostname() | ||
ip = socket.gethostbyname(hostname) | ||
print(e, "= not on ESP, use this IP if external", ip, "otherwise use localhost") | ||
|
||
while True: | ||
data, addr = sock.recvfrom(32) | ||
size = len(data) | ||
data = data.decode().split() | ||
if data[0] == "0": | ||
driver.stop() | ||
if len(data) == 2: | ||
cmd, speed = data | ||
if platform == "ESP": | ||
driver.execute(cmd, speed) | ||
if debug: | ||
print(f"{addr} bytes={size}: {data}") | ||
|