diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..28cf95e Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba28150 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +images/ \ No newline at end of file diff --git a/README.md b/README.md index da34c65..5baaef4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # RaspberryDrive Use a Raspberry Pi 3 to drive an RC Car + +## Libraries +Python 2 +SimpleCV +PiCamera \ No newline at end of file diff --git a/models/decide.py b/__init__.py similarity index 100% rename from models/decide.py rename to __init__.py diff --git a/analyzeLine.py b/analyzeLine.py new file mode 100644 index 0000000..2995e6b --- /dev/null +++ b/analyzeLine.py @@ -0,0 +1,24 @@ +import SimpleCV +import cv2 +import time + + +def findBlockingBlobs(img,blobs): + for blob in blobs: + if blockingBlob(img,blob): + return True + return False + +def blockingBlob(img,blob): + if heightBelowThreshold(img,blob): + if lengthSufficientToBlock(img,blob): + return True + +def heightBelowThreshold(img,blob): + if (blob.maxY() > img.size()[1] * 0.4): + return True + +def lengthSufficientToBlock(img,blob): + if (blob.width() > img.size()[0] * 0.55): + return True + diff --git a/analyzeLine.pyc b/analyzeLine.pyc new file mode 100644 index 0000000..a1849f4 Binary files /dev/null and b/analyzeLine.pyc differ diff --git a/connect.py b/connect.py new file mode 100644 index 0000000..2b34031 --- /dev/null +++ b/connect.py @@ -0,0 +1,23 @@ +import socket + +def new_connection(): + host = '192.168.2.4' + port = 9000 + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((host, port)) + return s; + +def send_command(connection,command,time): + connection.send('' + command + ' ; ' + time) + +def receive_confirmation(connection): + received = connection.recv(20) + return received; + +def send_end_connection(connection): + connection.send('quit') + return + +# example: +# con = new_conntection +# send_command(con,"forward",0.5) diff --git a/connect.pyc b/connect.pyc new file mode 100644 index 0000000..1e7ac6c Binary files /dev/null and b/connect.pyc differ diff --git a/controllers/.DS_Store b/controllers/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/controllers/.DS_Store differ diff --git a/controllers/controller.py b/controllers/controller.py index e69de29..a2b334d 100644 --- a/controllers/controller.py +++ b/controllers/controller.py @@ -0,0 +1,56 @@ +import os +import sys +from time import sleep +sys.path.insert(0, os.getcwd()) +from models import analyzeLine +from models import runAnalyzeLine +from models.runAnalyzeLine import runAnalyzeLines +# from models.analyze import findBlockingBlobs +from models.analyzeLine import findBlockingBlobs +from get_images_from_pi import get_image, valid_image +from connect import new_connection, send_command, receive_confirmation,send_end_connection +from start_camera import start_camera +from start_server import start_server +import threading + +#Start-all +threads = [] +#start the server listening on contr pi +server_thread = threading.Thread(target=start_server) +threads.append(server_thread) +server_thread.start() +#Sleep for testing +sleep(2) +#start the camera taking pictures on camera pi +camera_thread = threading.Thread(target=start_camera) +threads.append(camera_thread) +camera_thread.start() +# Sleep to make sure server connects +sleep(5) +#start the server connection on this current server +s = new_connection() + +#Run-All. X times do + #retrieve image. #Blog Detection + #send instruction over server +count = 0 +while (count < 20): + instruction = "forward" + get_image() + if valid_image(os.getcwd() + "/gregTest.jpg"): + in_way = runAnalyzeLines(os.getcwd() + "/gregTest.jpg") + if (in_way == True): + instruction = "stop" + send_command(s, instruction, "0.5") + receive_confirmation(s) + # sleep(0.25) + print instruction + count += 1 + +# #End-all +# #Stop the camera taking pictures on the camera pi -- still needs to be implemented +# #close the server connection +send_end_connection(s) +# #Kill the server listening process +s.close() +exit() diff --git a/distance_to_camera.py b/distance_to_camera.py deleted file mode 100644 index ade6403..0000000 --- a/distance_to_camera.py +++ /dev/null @@ -1,48 +0,0 @@ -# Import image library necessary for file -import SimpleCV -import sys -from SimpleCV import Image - -def check_image(image_path): - #Find file by path and import. File currently resides in same directory. - image = Image(image_path) - # grey = image.grayscale() - - instruction = "go" - array_bounds_possible_widths = [image.width / 4, image.width / 4 * 3] - shapes_pic_attributes = image.size() - - shapes_pic_size = shapes_pic_attributes[0] * shapes_pic_attributes[1] - # dist = img.colorDistance(SimpleCV.Color.Black).dilate(2) - blobs = image.findBlobs() - - #check if the blob is in the middle 1/2 of screen and is too high - for blob in blobs[:-1]: - # print blob.contains() - - if (blob.coordinates()[0] > array_bounds_possible_widths[0] and blob.coordinates()[0] < array_bounds_possible_widths[1]) and (blob.height() > image.height / 5 and blob.coordinates()[1] > image.height / 5 * 2): - # print grey.height - print blob.coordinates()[1] - print "Blob is in the way!!" - blob.draw(color=(255,0,0)) - instruction = "stop" - - # Display the blob until you click on the left side of the picture. - display = SimpleCV.Display() - while display.isNotDone(): - image.show() - if display.mouseLeft: - break - - return instruction - -print sys.argv[1] -instruction = check_image(sys.argv[1]) -print instruction - - -# def check_width(image, blob): -# print check_width -# def check_height(image, blob): -# return (blob.height() > image.height / 1.5) and blob - diff --git a/driving_server.py b/driving_server.py new file mode 100644 index 0000000..5ff4c5a --- /dev/null +++ b/driving_server.py @@ -0,0 +1,45 @@ +from socket import * +import run_rc_car as picar + +def createPiReceiver(): + serversocket = socket(AF_INET, SOCK_STREAM) + port = 9000 + new_picar = picar.PiCar() + serversocket.bind(('',port)) + serversocket.listen(5) + print "New Server Created, listening on port " + str(port) + while(1): + (clientsocket, address) = serversocket.accept() + print "accepted connection from " + str(address) + while(1): + transfer = clientsocket.recv(20).split(';') + if not transfer: break + if transfer == ['']: break + print "received: " + transfer + try: + inputParser(transfer[0].strip(),float(transfer[1]),new_picar) + clientsocket.send("true") + except Exception: + clientsocket.send("false") + continue + + +def inputParser(command,number,car): + print 'executing ' + command + ' for ' + str(number) +' seconds' + if command == "forward": + car.go_forward(number) + elif command == "backward": + car.go_backward(number) + elif command == "right": + car.go_forward_right(number) + elif command == "left": + car.go_forward_left(number) + elif command == "backward right": + car.go_backward_right(number) + elif command == "backward left": + car.go_backward_left(number) + else: + car.stop() + + +createPiReceiver() diff --git a/get_images_from_pi.py b/get_images_from_pi.py new file mode 100644 index 0000000..33aa19c --- /dev/null +++ b/get_images_from_pi.py @@ -0,0 +1,15 @@ +import os +from PIL import Image + +def get_image(): + os.system('scp pi@192.168.2.5:Desktop/gregTest.jpg gregTest.jpg') + return + #if on a new computer and the pi address isn't connecting right away, need to create a key. Use the following website to do so + #https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md + +def valid_image(path): + try: + Image.open(path) + except IOError: + return False + return True diff --git a/get_images_from_pi.pyc b/get_images_from_pi.pyc new file mode 100644 index 0000000..271fb69 Binary files /dev/null and b/get_images_from_pi.pyc differ diff --git a/gregTest.jpg b/gregTest.jpg new file mode 100644 index 0000000..d83ca77 Binary files /dev/null and b/gregTest.jpg differ diff --git a/images/five.jpg b/images/five.jpg new file mode 100644 index 0000000..73f96ad Binary files /dev/null and b/images/five.jpg differ diff --git a/images/four.jpg b/images/four.jpg new file mode 100644 index 0000000..f64b20d Binary files /dev/null and b/images/four.jpg differ diff --git a/images/greg.jpg b/images/greg.jpg new file mode 100644 index 0000000..2ca6251 Binary files /dev/null and b/images/greg.jpg differ diff --git a/images/gregTest.jpg b/images/gregTest.jpg new file mode 100644 index 0000000..37a38b9 Binary files /dev/null and b/images/gregTest.jpg differ diff --git a/images/one.jpg b/images/one.jpg new file mode 100644 index 0000000..0b4bf99 Binary files /dev/null and b/images/one.jpg differ diff --git a/images/seven.jpg b/images/seven.jpg new file mode 100644 index 0000000..1c8ce12 Binary files /dev/null and b/images/seven.jpg differ diff --git a/images/six.jpg b/images/six.jpg new file mode 100644 index 0000000..f790546 Binary files /dev/null and b/images/six.jpg differ diff --git a/images/two.jpg b/images/two.jpg new file mode 100644 index 0000000..348a3c0 Binary files /dev/null and b/images/two.jpg differ diff --git a/models/ident.py b/models/ident.py deleted file mode 100644 index e69de29..0000000 diff --git a/models/send.py b/models/send.py deleted file mode 100644 index 53a4d59..0000000 --- a/models/send.py +++ /dev/null @@ -1,13 +0,0 @@ -import socket - -UDP_IP = "10.50.111.176" -UDP_PORT = 5005 -MESSAGE = "Hello, World!" - -print "UDP target IP:", UDP_IP -print "UDP target port:", UDP_PORT -print "message:", MESSAGE - -sock = socket.socket(socket.AF_INET, # Internet - socket.SOCK_DGRAM) # UDP -sock.sendto(MESSAGE, (UDP_IP, UDP_PORT)) \ No newline at end of file diff --git a/pseudocode.py b/pseudocode.py index f09bd69..3c1fc2f 100644 --- a/pseudocode.py +++ b/pseudocode.py @@ -1,6 +1,3 @@ - - - def driveforward(time): FOR each second in time goforward(1) diff --git a/runAnalyzeLine.py b/runAnalyzeLine.py new file mode 100644 index 0000000..7834daf --- /dev/null +++ b/runAnalyzeLine.py @@ -0,0 +1,14 @@ +import SimpleCV +import cv2 +import time +from analyzeLine import * + +def runAnalyzeLines(image): + scvImg = SimpleCV.Image(image) + segmented = scvImg.stretch(160,161) + blobs = segmented.findBlobs(minsize=100) + if (blobs and (len(blobs) > 0)): + return findBlockingBlobs(scvImg,blobs) + return False + # print x + # print blob diff --git a/runAnalyzeLine.pyc b/runAnalyzeLine.pyc new file mode 100644 index 0000000..ee59bb9 Binary files /dev/null and b/runAnalyzeLine.pyc differ diff --git a/run_rc_car.py b/run_rc_car.py new file mode 100644 index 0000000..46fc896 --- /dev/null +++ b/run_rc_car.py @@ -0,0 +1,63 @@ +import RPi.GPIO as GPIO +from time import sleep + +class PiCar(object): + + def __init__(self): + GPIO.setmode(GPIO.BOARD) + self.pins = {'left' : 11, + 'right' : 18, + 'forward' : 16, + 'backward' : 12 } + for pin_number in self.pins.itervalues(): + GPIO.setup(pin_number,GPIO.OUT) + self.stop() + + + def stop(self): + for pin_number in self.pins.itervalues(): + GPIO.output(pin_number,0) + + def go_forward(self,time): + self.stop() + GPIO.output(self.pins['forward'],1) + sleep(int(time)) + self.stop() + + def go_backward(self,time): + self.stop() + GPIO.output(self.pins['backward'],1) + sleep(int(time)) + self.stop() + + def go_forward_right(self,time): + self.stop() + GPIO.output(self.pins['right'],1) + GPIO.output(self.pins['forward'],1) + sleep(int(time)) + self.stop() + + def go_forward_left(self,time): + self.stop() + GPIO.output(self.pins['left'],1) + GPIO.output(self.pins['forward'],1) + sleep(int(time)) + self.stop() + + def go_backward_right(self,time): + self.stop() + GPIO.output(self.pins['right'],1) + GPIO.output(self.pins['backward'],1) + sleep(int(time)) + self.stop() + + def go_backward_left(self,time): + self.stop() + GPIO.output(self.pins['left'],1) + GPIO.output(self.pins['backward'],1) + sleep(int(time)) + self.stop() + + +picar = PiCar() + diff --git a/run_rc_car.pyc b/run_rc_car.pyc new file mode 100644 index 0000000..aa686fe Binary files /dev/null and b/run_rc_car.pyc differ diff --git a/start_camera.py b/start_camera.py new file mode 100644 index 0000000..674daf8 --- /dev/null +++ b/start_camera.py @@ -0,0 +1,6 @@ +import os + +def start_camera(): + os.system('ssh pi@192.168.2.5 python RaspberryDrive/views/takePicture.py &') + return + diff --git a/start_camera.pyc b/start_camera.pyc new file mode 100644 index 0000000..d270016 Binary files /dev/null and b/start_camera.pyc differ diff --git a/start_server.py b/start_server.py new file mode 100644 index 0000000..4020e03 --- /dev/null +++ b/start_server.py @@ -0,0 +1,6 @@ +import os + +def start_server(): + os.system('ssh pi@192.168.2.4 python python-libs/RaspberryDrive/driving_server.py &') + return + diff --git a/start_server.pyc b/start_server.pyc new file mode 100644 index 0000000..fdec23b Binary files /dev/null and b/start_server.pyc differ diff --git a/takePicture.py b/takePicture.py new file mode 100644 index 0000000..4f658a2 --- /dev/null +++ b/takePicture.py @@ -0,0 +1,20 @@ +import picamera as p +import os +import time + +os.chdir('/home/pi/Desktop') + +cam = p.PiCamera() +cam.resolution = (320,240) +cam.hflip = True +cam.vflip = True + +x = 0 + +while x < 50: + #os.unlink('greg.jpg') + img = cam.capture('gregTest.jpg') + time.sleep(.25) + #oc.rename('gregTemp.jpg', 'greg.jpg') + x +=1 +exit() diff --git a/views/input.py b/views/input.py deleted file mode 100644 index e69de29..0000000 diff --git a/views/output.py b/views/output.py deleted file mode 100644 index e69de29..0000000