Skip to content

Commit

Permalink
Added files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Ridhwanluthra committed Feb 22, 2016
1 parent 18eb041 commit 6d8d3f1
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 0 deletions.
130 changes: 130 additions & 0 deletions bot_movement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from gpiozero import Motor
#from gpiozero.Pin import
#import RPi.GPIO as GPIO
from encoders import d_move, refresh

ml = Motor(2, 3)
mr = Motor(14, 15)
ml.stop()
mr.stop()
"""
I am assuming that the initial location of the bot
is facing upwards at 0,0
the bot can make only 90 degree turns
"""



direction = 'n' # n,e,w,s for different locations that it is facing
usfront = 20

try:
# ADD SOMETHING TO DO WHEN US IS LESS THAN 5;
def forward():
if usfront <= 5:
print "error"
else:
ml.forward()
mr.forward()

def sstop():
ml.stop()
mr.stop()
def turn_left():
while d_move()[0] <= 20 and d_move()[1] <= 20:
ml.backward()
mr.forward()
sstop()
refresh()

def turn_right():
while d_move()[0] <= 20 and d_move()[1] <= 20:
ml.forward()
mr.backward()
sstop()
refresh()

def up():
look_up()
while d_move()[0] <= 20 and d_move()[1] <= 20:
forward()
sstop()
refresh()

def left():
look_left()
while d_move()[0] <= 20 and d_move()[1] <= 20:
forward()
sstop()
refresh()

def right():
look_right()
while d_move()[0] <= 20 and d_move()[1] <= 20:
forward()
sstop()
refresh()

def down():
look_down()
while d_move()[0] <= 20 and d_move()[1] <= 20:
forward()
sstop()
refresh()

def look_up():
global direction
if (direction == 'n'):
pass
elif (direction == 'e'):
turn_left()
elif (direction == 'w'):
turn_right()
elif (drection == 's'):
turn_left()
turn_left()
direction = 'n'

def look_down():
global direction
if (direction == 'n'):
turn_left()
turn_left()
elif (direction == 'e'):
turn_right()
elif (direction == 'w'):
turn_left()
elif (drection == 's'):
pass
direction = 's'

def look_left():
global direction
if (direction == 'n'):
turn_left()
elif (direction == 'e'):
turn_left()
turn_left()
elif (direction == 'w'):
pass
elif (drection == 's'):
turn_right()
direction = 'w'

def look_right():
global direction
if (direction == 'n'):
turn_right()
elif (direction == 'e'):
pass
elif (direction == 'w'):
turn_left()
turn_left()
elif (drection == 's'):
turn_left()
direction = 'e'
except KeyboardInterrupt:
print "cleaning"
finally:
#GPIO.cleanup()
print"check it"
Binary file added bot_movement.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions click_picture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import picamera

camera = picamera.PiCamera()

"""
i need ultrasonic reading
usfront, usleft, usright, usback
if any are less than lets say 10cm click
"""

def click_picture(int k,int l):
camera.capture('img_%d_%d.jpg'%k %l)

#analysis of image
51 changes: 51 additions & 0 deletions edging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import cv2
import numpy as np

def countArray(i, j):
counter=0
for row in range(i, i+m):
for column in range(j, j+n):
if(inputArray[i][j]==255):
counter+=1
return counter

def edging(dir):
img = cv2.imread(dir, cv2.IMREAD_COLOR)
edges=cv2.Canny(img,100,200)
oppp=[]
for x in xrange(edges.shape[0]):
opp=[]
for y in xrange(edges.shape[1]):
opp.append(edges[x][y])
pass
oppp.append(opp)
pass
#Save image to storage with a pseudo-unique name
cv2.imwrite('img.png', edges)

#if the number of cells in inputArray having value 255 are greater than "K",then value is 1 in output
#here I am setting k=50
k=20
s01_=edges.shape[0]
s02_=edges.shape[1]
m=edges.shape[0]/10
n=edges.shape[1]/10

c=[]
inputArray=[]
#populating input array with random numbers 0 and 255 for testing purposes
for i in range (s01_):
for j in range (s02_):
c.append(edges[i][j])
inputArray.append(c)
c=[]
outputArrayRows=s01_/m
outputArrayColumns=s02_/n
outputArray=[[] for i in range (outputArrayColumns)]
for i in range (outputArrayRows):
for j in range (outputArrayColumns):
if countArray(i*m,j*n)>=k:
outputArray[i].append(1)
else:
outputArray[i].append(0)
return outputArray
40 changes: 40 additions & 0 deletions encoders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)

leften = 18
righten = 17
dps = 30/24 #distance per spoke
gpio.setup(leften,gpio.IN)
gpio.setup(righten,gpio.IN)
counter_left = 0
counter_right = 0
c = 0
a = 0
def d_move():
while True:
global a
global c
global counter_left
global counter_right
b = a
d = c
a = gpio.input(leften)
c = gpio.input(righten)
if (a == 1 and b == 0):
counter_left += 1
#print "left counter "
#print counter_left
if (c == 1 and d == 0):
counter_right += 1
#print "right coutner "
#print counter_right
return [counter_left*dps, counter_right*dps]
def refresh():
global a
global c
global counter_left
global counter_right
counter_left = 0
counter_right = 0
c = 0
a = 0
Binary file added encoders.pyc
Binary file not shown.
15 changes: 15 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# RUN THIS SCRIPT AND THE REST WILL RUN THEMSELVES

from sys import argv
from traversal import mapping
from edging import edging

image_matrix = edging(argv[1]) # convert the picture into edges and return the sub_matrix
x=0
y=0
# now i have the completed map of the room ready

# add the ultrasonic sensors to find and save from obstacles.

while True:
mapping(x,y,image_matrix)
12 changes: 12 additions & 0 deletions ptest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import bot_movement as bm

try:
bm.up()
print "turning left"
bm.turn_left()
print "turning right"
bm.turn_right()
except KeyboardInterrupt:
print "cleaning ptest.py"
finally:
print "done execution"
Loading

0 comments on commit 6d8d3f1

Please sign in to comment.