Compare commits

...

4 Commits

Author SHA1 Message Date
Vita Aeterna
80318e8576
Rename main.py to BrightnessRotation.py 2023-06-07 09:27:08 +02:00
Vita Aeterna
308197f510
Add files via upload 2023-06-07 09:26:32 +02:00
Vita Aeterna
eaea212404
Rename main.py to WASD.py 2023-06-07 08:33:29 +02:00
Vita Aeterna
5b79ebd196
Add files via upload 2023-06-07 08:23:13 +02:00
2 changed files with 218 additions and 0 deletions

101
BrightnessRotation.py Normal file
View File

@ -0,0 +1,101 @@
import requests
import signal
import webbrowser
import json
import time
from PIL import Image, ImageStat
from io import BytesIO
IP = "192.168.100.40"
PARAMS = {'sid' : 'Drive'}
run = True
def signal_handler(sig, frame):
global run
print('U pressed Ctrl-C')
run = False
# DEFINE ALL URL's
def set_vel(vel):
OMNIDRIVE_URL = "http://" + IP + "/data/omnidrive"
r = requests.post(url=OMNIDRIVE_URL, params=PARAMS, json=vel)
if r.status_code != requests.codes.ok:
raise RuntimeError("Error: post to %s with params %s failed", OMNIDRIVE_URL, PARAMS)
def bumper():
BUMPERURL = "http://" + IP + "/data/bumper"
r = requests.get(url=BUMPERURL)
if r.status_code == requests.codes.ok:
data = r.json()
return data["value"]
else:
raise RuntimeError("Error: post to %s with params %s failed", BUMPERURL, PARAMS)
def distances():
DISTANCE_URL = "http://" + IP + "/data/distancesensorarray"
r = requests.get(url=DISTANCE_URL, params=PARAMS)
if r.status_code == requests.codes.ok:
data = r.json()
return data
def Camera():
CAMURL = "http://" + IP + "/cam0"
r = requests.get(url=CAMURL)
while r.status_code != requests.codes.ok:
r = requests.get(url=CAMURL)
img = Image.open(BytesIO(r.content))
return img
def BrightnessCalc(img):
lum = img.convert('L')
stat = ImageStat.Stat(lum)
return stat.mean[0]
def GetImageAndBrightness():
vec_to_rotate_left = [0,0, 0.5]
vec_to_rotate_right = [0,0, -0.5]
image = Camera()
width, height = image.size
left_image = image.crop((0,0,width/2,height))
right_image = image.crop((width/2,0,width,height))
Left_Brightness = BrightnessCalc(left_image)
print("Left Image Brightness " + str(Left_Brightness))
Right_Brightness = BrightnessCalc(right_image)
print("Right Image Brighness " + str(Right_Brightness))
if Left_Brightness > Right_Brightness and Left_Brightness - Right_Brightness > 15:
print("Left is brighter then Right")
set_vel(vec_to_rotate_left) and print("Rotate Left")
if Right_Brightness > Left_Brightness and Right_Brightness - Left_Brightness > 15:
print("Right is brighter then Left")
set_vel(vec_to_rotate_right) and print("Rotate Right")
def main():
signal.signal(signal.SIGINT, signal_handler)
try:
while False == bumper() and True == run:
GetImageAndBrightness()
time.sleep(0.3)
except Exception as e:
print(e)
return 1
return 0
if __name__ == "__main__":
main()

117
WASD.py Normal file
View File

@ -0,0 +1,117 @@
import requests
import signal
import webbrowser
import json
import time
from PIL import Image, ImageStat
from io import BytesIO
import keyboard
IP = "192.168.100.40"
PARAMS = {'sid' : 'Drive'}
run = True
def signal_handler(sig, frame):
global run
print('U pressed Ctrl-C')
run = False
# DEFINE ALL URL's
def set_vel(vel):
OMNIDRIVE_URL = "http://" + IP + "/data/omnidrive"
r = requests.post(url=OMNIDRIVE_URL, params=PARAMS, json=vel)
if r.status_code != requests.codes.ok:
raise RuntimeError("Error: post to %s with params %s failed", OMNIDRIVE_URL, PARAMS)
def bumper():
BUMPERURL = "http://" + IP + "/data/bumper"
r = requests.get(url=BUMPERURL)
if r.status_code == requests.codes.ok:
data = r.json()
return data["value"]
else:
raise RuntimeError("Error: post to %s with params %s failed", BUMPERURL, PARAMS)
def distances():
DISTANCE_URL = "http://" + IP + "/data/distancesensorarray"
r = requests.get(url=DISTANCE_URL, params=PARAMS)
if r.status_code == requests.codes.ok:
data = r.json()
return data
def Camera():
CAMURL = "http://" + IP + "/cam0"
r = requests.get(url=CAMURL)
while r.status_code != requests.codes.ok:
r = requests.get(url=CAMURL)
img = Image.open(BytesIO(r.content))
return img
def OdometryGet():
ODOURL = "http://" + IP + "/data/odometry"
r = requests.get(url=ODOURL)
if r.status_code == requests.codes.ok:
data = r.json()
return [round(data[0], 1), round(data[1], 1)]
else:
raise RuntimeError("Error: post to %s with params %s failed", ODOURL, PARAMS)
def InputKeyboard():
while keyboard.is_pressed('w'):
set_vel([2, 0, 0])
while keyboard.is_pressed('a'):
set_vel([0, 2, 0])
while keyboard.is_pressed('s'):
set_vel([-2, 0, 0])
while keyboard.is_pressed('d'):
set_vel([0, -2, 0])
while keyboard.is_pressed('e'):
set_vel([0.2, 2, 0])
while keyboard.is_pressed('r'):
set_vel([0.2, -2, 0])
while keyboard.is_pressed('f'):
set_vel([-2, 2, 0])
while keyboard.is_pressed('g'):
set_vel([-2, -2, 0])
while keyboard.is_pressed('y'):
set_vel([0,0,3])
while keyboard.is_pressed('x'):
set_vel([0,0,-3])
set_vel([0, 0, 0])
def main():
signal.signal(signal.SIGINT, signal_handler)
try:
while False == bumper() and True == run:
InputKeyboard()
time.sleep(0.3)
except Exception as e:
print(e)
return 1
return 0
if __name__ == "__main__":
main()