From ab12fc60ed76a8db240e11c6cb19344f906c1928 Mon Sep 17 00:00:00 2001 From: Edvin Date: Fri, 4 Apr 2025 09:03:58 +0200 Subject: Added commands from translation protocol --- main.py | 107 +++++++++++++++++++++++++++++----------------------------------- 1 file changed, 49 insertions(+), 58 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 27e5e52..8c0e777 100644 --- a/main.py +++ b/main.py @@ -7,53 +7,18 @@ import bluetooth import re -GPIO.setmode(GPIO.BCM) -GPIO.setup(17, GPIO.OUT) -GPIO.setup(27, GPIO.OUT) -GPIO.output(17, False) -GPIO.output(27, False) - -target = "D4:D2:52:ED:14:89" - -server_sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) -server_sock.bind(("", bluetooth.PORT_ANY)) -server_sock.listen(1) - -port = server_sock.getsockname()[1] - -print("Waiting for connection on RFCOMM channel %d..." % port) - -client_sock, client_info = server_sock.accept() - -while client_sock: - read = client_sock.recv(1024) - read = read.decode("utf-8").strip('\n') - print(read) - - if read == "11": - GPIO.output(17, True) - GPIO.output(27, False) - elif read == "22": - GPIO.output(17, False) - GPIO.output(27, True) - else: - GPIO.output(17, False) - GPIO.output(27, False) -pi@pigroup3:~/TNE107-RPI$ cat main.py -import RPi.GPIO as GPIO -from time import sleep - -import os -import glob -import bluetooth - -import re +# 3 Pins to communicate with Arduino +P1 = 17 +P2 = 27 +P3 = 22 GPIO.setmode(GPIO.BCM) -GPIO.setup(17, GPIO.OUT) -GPIO.setup(27, GPIO.OUT) -GPIO.output(17, False) -GPIO.output(27, False) +GPIO.setup(P1, GPIO.OUT) +GPIO.setup(P2, GPIO.OUT) +GPIO.setup(P3, GPIO.OUT) +GPIO.output(P1, False) +GPIO.output(P2, False) +GPIO.output(P3, False) target = "D4:D2:52:ED:14:89" @@ -67,24 +32,50 @@ print("Waiting for connection on RFCOMM channel %d..." % port) client_sock, client_info = server_sock.accept() +print("Connected with %d" % client_info) + while client_sock: read = client_sock.recv(1024) read = read.decode("utf-8").strip('\n') print(read) - if read == "11": - GPIO.output(17, True) - GPIO.output(27, False) - elif read == "22": - GPIO.output(17, False) - GPIO.output(27, True) - else: - GPIO.output(17, False) - GPIO.output(27, False) - -GPIO.output(17, False) -GPIO.output(27, False) + if read == "0": # Do nothing + if read == "11": # Drive forward + GPIO.output(P1, True) + GPIO.output(P2, False) + GPIO.output(P3, False) + elif read == "22": # Drive backward + GPIO.output(P1, False) + GPIO.output(P2, True) + GPIO.output(P3, False) + elif read == "33": # Rotate right, 90 degrees + GPIO.output(P1, True) + GPIO.output(P2, True) + GPIO.output(P3, False) + elif read == "44": # Rotate left, 90 degrees + GPIO.output(P1, False) + GPIO.output(P2, False) + GPIO.output(P3, True) + elif read == "55": # Rotate right, 45 degrees + GPIO.output(P1, True) + GPIO.output(P2, False) + GPIO.output(P3, True) + elif read == "66": # Rotate left, 45 degrees + GPIO.output(P1, False) + GPIO.output(P2, True) + GPIO.output(P3, True) + elif read == "98": # Turn on light at landmark + GPIO.output(P1, True) + GPIO.output(P2, True) + GPIO.output(P3, True) + else: # Do nothing + GPIO.output(P1, False) + GPIO.output(P2, False) + GPIO.output(P3, False) + +GPIO.output(P1, False) +GPIO.output(P2, False) +GPIO.output(P3, False) print("Shutting down...") server_sock.close() -print("Done.") -- cgit v1.2.3