From 6524ba6cfe9dd8cf2fd47b2ef3082405376f77d0 Mon Sep 17 00:00:00 2001 From: Cole Deck Date: Fri, 21 Feb 2020 13:17:50 -0600 Subject: [PATCH] add netowrking between pi and laptop, basically add all the components together --- control_motor.py | 29 ++++++++++-- pixel_control.py => control_pixel.py | 0 pi_client.py | 25 ++++++++++ pi_receive.py | 10 ++++ run_detect.py | 70 +++++++++++++++++++++++++--- 5 files changed, 123 insertions(+), 11 deletions(-) rename pixel_control.py => control_pixel.py (100%) create mode 100644 pi_client.py create mode 100644 pi_receive.py diff --git a/control_motor.py b/control_motor.py index 2d00415..d0a253c 100644 --- a/control_motor.py +++ b/control_motor.py @@ -1,10 +1,29 @@ import math +import serial +import RPi.GPIO as gpio +import time + +ser = serial.Serial('/dev/ttyUSB0', 115200) +gpio.setmode(gpio.BCM) +gpio.setup(13, gpio.OUT) +pwm = gpio.PWM(13, 100) +pwm.start(5) def goToBin(bin): - if bin % 2 == 0: - direction = "right" - else: - direction = "left" adjustedBin = math.floor(bin / 2) distance = adjustedBin * 18 - time = 0.5 + 0.93 * adjustedBin \ No newline at end of file + delay = 0.5 + 0.93 * adjustedBin + command = 'G0 X' + command += distance + ser.write(command) + time.sleep(delay) + if bin % 2 == 0: # tilt to right + pwm.ChangeDutyCycle(0) + time.sleep(1) + pwm.ChangeDutyCycle(20) + else: + pwm.ChangeDutyCycle(40) + time.sleep(1) + pwm.ChangeDutyCycle(20) + ser.write(b'G0 X0') + time.sleep(delay) \ No newline at end of file diff --git a/pixel_control.py b/control_pixel.py similarity index 100% rename from pixel_control.py rename to control_pixel.py diff --git a/pi_client.py b/pi_client.py new file mode 100644 index 0000000..a7efac3 --- /dev/null +++ b/pi_client.py @@ -0,0 +1,25 @@ +import socket +import sort +import control_motor +import control_pixel + +host = socket.gethostname() +port = 9001 # The same port as used by the server +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.connect((host, port)) +s.sendall(b'Connected to server!') +control_pixel.ledOn() +while True: + try: + data = s.recv(1024) + if not data: break + print("Found "+data) + selectedBin = sort.sort(data) + control_pixel.ledOff() + control_motor.goToBin(selectedBin) + control_pixel.ledOn() + s.sendall(b'Continue') + except socket.error: + print("Error Occured.") + break +s.close() \ No newline at end of file diff --git a/pi_receive.py b/pi_receive.py new file mode 100644 index 0000000..2214fa7 --- /dev/null +++ b/pi_receive.py @@ -0,0 +1,10 @@ +import socket + +host = socket.gethostname() +port = 12345 # The same port as used by the server +s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +s.connect((host, port)) +s.sendall(b'Hello, world') +data = s.recv(1024) +s.close() +print('Received', repr(data)) \ No newline at end of file diff --git a/run_detect.py b/run_detect.py index efd92fd..255cd4f 100755 --- a/run_detect.py +++ b/run_detect.py @@ -3,6 +3,10 @@ import detect import sort import timeit import cv2 +import control_motor +import socket +import time + from imutils.video import FPS calibration_width = 0.75 image = "img7.jpg" @@ -24,7 +28,8 @@ def go(): break if valid: print("Found " + itema) - bin = sort.sort(itema) + selectedBin = sort.sort(itema) + control_motor.goToBin(selectedBin) if not video: elapsed_time = timeit.timeit(go, number=1)/1 @@ -33,13 +38,29 @@ if not video: else : #capture = cv2.VideoCapture('tcpclientsrc port=5001 host=192.168.1.129 ! gdpdepay ! rtph264depay ! avdec_h264 ! videoconvert ! videorate ! video/x-raw,framerate=5/1 ! appsink', cv2.CAP_GSTREAMER) capture = cv2.VideoCapture('udpsrc port=9000 caps="application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264" ! rtph264depay ! avdec_h264 ! videoconvert ! appsink sync=false', cv2.CAP_GSTREAMER) - + host = '' # Symbolic name meaning all available interfaces + port = 9001 # Arbitrary non-privileged port + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind((host, port)) + s.listen(1) + conn, addr = s.accept() + print('Connected to', addr) + time.sleep(1) + waitForPi = True + while waitForPi is True: + try: + data = conn.recv(1024) + if not data: break + print "Client: "+data + waitForPi = False + except socket.error: + print("Error Occured.") + break # server command for imx135 camera ./video2stdout | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=192.168.43.152 port=5001 # server command for udp: ./video2stdout | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=10 pt=96 ! udpsink host=192.168.43.40 port=9000 - #ret,frame = capture.read() - #detect.detect(calibration_width, "img7.jpg", True, False) - #detect.detect(calibration_width, frame, True, True) x = 0 + currentItem = "" + samples = 0 while True: ret = capture.grab() x+=1 @@ -49,8 +70,45 @@ else : #print('frame') if x > 1: ret,frame = capture.retrieve() - list,output = detect.detect(calibration_width, frame, True, True) + items,output = detect.detect(calibration_width, frame, True, True) cv2.imshow('Item Sorter', output) x = 0 + if "Penny" in items: + items.remove("Penny") + itema = items[0] + valid = True + for item in items: + if item != itema: + print("Too many items!") + valid = False + break + if valid: + if currentItem == itema: + samples += 1 + else: + currentItem = itema + samples = 0 + if samples == 5: + samples = 0 + sendString(currentItem) + waitForPi = True + while waitForPi is True: + try: + data = conn.recv(1024) + if not data: break + if data == "Continue": + waitForPi = False + except socket.error: + print("Error Occured.") + break + if cv2.waitKey(1)&0xFF == ord('q'): break + +def sendString(string): + print("Found " + string) + try: + conn.sendall(string) + except socket.error: + print("Error Occured.") + break \ No newline at end of file