MicroPython Tutorial XVI

#!/usr/bin/env pybricks-micropython
from pybricks import ev3brick as brick
from pybricks.ev3devices import Motor, UltrasonicSensor, ColorSensor, GyroSensor, InfraredSensor
from pybricks.parameters import Port,Color
from pybricks.robotics import DriveBase
from pybricks.tools import print, wait, StopWatch
from time import sleep
import threading
import sys
import random
import socket
import os
# Section 01hostname = os.popen('hostname -I').read().strip().split(" ")
print("hostname address",hostname[0])
hostIPA = hostname[0]
port = random.randint(50000,50999)
# Section 02left = Motor(Port.C)
right = Motor(Port.B)
# 56 is the diameter of the wheels in mm
# 114 is the distance between them in mm
robot = DriveBase(left, right, 56, 114)
print("host ",hostIPA)
print("port",port)
# Section 03online = Trueai = socket.getaddrinfo(hostIPA,port)
addr = ai[0][-1]
backlog = 5
size = 1024
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(backlog)
# Section 0Etry:
res = s.accept()
while online:
client_s = res[0]
#client_addr = res[1]
req =client_s.recv(1024)
data=req.decode('utf-8')
print("data ",data)
except AssertionError as error:
print("Closing socket",error)
client_s.close()
s.close()
# Section 07def actionTrigger(data, client):
global transmit
if data[:5] == "#:end":
stopMotors()
brick.sound.beep()
peerMode = False
if data[:6] == "#:peer":
peerMode = True
if data[:7] == "#:begin":
pass
if data[:8] == "#:keypad":
brick.light(Color.YELLOW)
if data[:10] == "#:touchpad":
brick.light(Color.RED)
if data[:8] == "#:motion":
brick.light(Color.ORANGE)
if data[:5] == "#:con": # connected
brick.sound.beep()
if data[:5] == "#:dis": # disconnect
brick.light(Color.BLACK)
wait(2000)
brick.light(Color.GREEN)
client_s.close()
s.close()
if data[:8] == "#:short":
stopMotors()
brick.sound.beep()
if data[:6] == "#:long":
stopMotors()
brick.sound.beep()
# Section 08def stopMotors():
print("STOP STOP STOP ")
robot.stop()
lastPitch = 0
lastRoll = 0
# Section 09def joystick(pitch, roll):
global lastPitch
global lastRoll
calcPitch = int(round(pitch / 180 * 800,0)) # needs to be an integer
calcRoll = int(round(roll / 720 * 200,0)) # turns need to be slow and deliberate, this reduces roll by 4
if lastPitch != calcPitch or lastRoll != calcRoll:
print("calc",calcPitch,calcRoll)
robot.drive(calcPitch,calcRoll)
lastPitch = calcPitch
lastRoll = calcRoll
elif data[:2] == "@:":
mCommands = data.split("\n")
for mCommand in mCommands:
if len(mCommand) != 0:
cords = mCommand[2:]
cord = cords.split(":")
try:
roll = float(cord[0])
pitch = float(cord[1])
joystick(pitch,roll)
except:
pass

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mark Lucking

Mark Lucking

Coding for 35+ years, enjoying using and learning Swift/iOS development. Writer @ Better Programming, @The StartUp, @Mac O’Clock, Level Up Coding & More