Skip to content

Commit

Permalink
add rc controller
Browse files Browse the repository at this point in the history
  • Loading branch information
MADS0LADEN committed Dec 5, 2023
1 parent 4ff27c3 commit 4b05c03
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rc-controller/boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file is executed on every boot (including wake-boot from deepsleep)
import esp
esp.osdebug(None)

import wifi
import controller
29 changes: 29 additions & 0 deletions rc-controller/controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import socket
from time import sleep

from machine import ADC, Pin

SPEED = ADC(Pin(1, Pin.IN), atten=ADC.ATTN_11DB)
STEER = ADC(Pin(2, Pin.IN), atten=ADC.ATTN_11DB)

debug = True
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_addr = ("192.168.4.1", 7913)

def getProcent(pot):
return round(pot.read_uv()/1000/3027*100)

def getCmd(speed, steer):
if steer <= 50:
lspeed = speed-abs(50-steer)
rspeed = speed
else:
lspeed = speed
rspeed = speed-abs(50-steer)
return f"{lspeed} {rspeed}"

while True:
msg = getCmd(getProcent(SPEED), getProcent(STEER))
#print(msg)
sock.sendto(msg, server_addr)
sleep(0.05)
29 changes: 29 additions & 0 deletions rc-controller/wifi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import sys
import time
import network

wifi = network.WLAN(network.STA_IF)

if not wifi.isconnected():
wifi.active(True)
try:
wifi.config(dhcp_hostname="RC Controller")
wifi.connect("RoverNumber1", "qwerty123456")
except Exception as err:
wifi.active(False)
print("Error:", err)
sys.exit()
print("Connecting", end="")
n = 0
while not wifi.isconnected():
print(".", end="")
time.sleep(1)
n += 1
if n == 60:
break
if n == 60:
wifi.active(False)
print("\nGiving up! Not connected!")
else:
print("\nNow connected with IP: ", wifi.ifconfig()[0])

0 comments on commit 4b05c03

Please sign in to comment.