add netowrking between pi and laptop, basically add all the components together
This commit is contained in:
parent
47eb71057f
commit
6524ba6cfe
@ -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
|
||||
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)
|
25
pi_client.py
Normal file
25
pi_client.py
Normal file
@ -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()
|
10
pi_receive.py
Normal file
10
pi_receive.py
Normal file
@ -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))
|
@ -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
|
Loading…
x
Reference in New Issue
Block a user