Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/MADS0LADEN/tema2-gruppe1 int…
Browse files Browse the repository at this point in the history
…o dev
  • Loading branch information
minityhyi committed Nov 9, 2023
2 parents 3cfd426 + 5fafe57 commit d20b161
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
8 changes: 4 additions & 4 deletions client.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import socket

debug = True
debug = False

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
enpoint = input("Hvilken endhed vil du snakke med?\nESP / LOCAL?\n")
if enpoint.lower() == "esp":
endpoint = input("Hvilken endhed vil du snakke med?\nESP / LOCAL?\n")
if endpoint.lower() == "esp":
server_addr = ("192.168.4.1", 7913)
else:
server_addr = ("127.0.0.1", 7913)

while True:
inp = input("Send besked: ").encode()
if not debug and len(inp) == 0:
inp = "NAN".encode()
inp = "0".encode()
try:
sock.sendto(inp, server_addr)
print(server_addr, len(inp), inp.decode())
Expand Down
8 changes: 8 additions & 0 deletions rover/boot.py
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
58 changes: 58 additions & 0 deletions rover/driver.py
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)
40 changes: 40 additions & 0 deletions rover/main.py
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}")

0 comments on commit d20b161

Please sign in to comment.