Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transferred to ev3dev2 #1

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.vscode/settings.json
/Chess Robot/source/__pycache__/
/Chess Robot/source/.vscode/settings.json
/Chess Robot/.vscode/settings.json
Binary file added Chess Robot/source/board.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 19 additions & 12 deletions Chess Robot/source/claw.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
from ev3dev.ev3 import *
from ev3dev2.motor import *
from time import sleep

class Claw:
class Claw(MediumMotor):
def __init__(self, port = OUTPUT_B):
self.motor = MediumMotor(port)
self.motor.reset()
MediumMotor.__init__(self,port)
self.reset()
#Close the gripper completely
convergence_counter = 0
time_delta = 0.01
tachometer_reading = self.motor.position
tachometer_reading = self.position
while convergence_counter < 3:
self.motor.run_forever(speed_sp=-360)
self.on(-24)
sleep(time_delta)
if tachometer_reading == self.motor.position:
if tachometer_reading == self.position:
convergence_counter = convergence_counter + 1
tachometer_reading = self.motor.position
self.motor.stop()
tachometer_reading = self.position
self.stop()
#Gripper is closed at this point
self.motor.run_to_rel_pos(position_sp=700, speed_sp = 360)

#self.motor.run_to_rel_pos(position_sp=700, speed_sp = 360)
self.on_for_degrees(24,700)
sleep(3)
self.isOpen = True

def open(self):
if not self.isOpen:
self.motor.run_to_rel_pos(position_sp=700, speed_sp = 360)
#self.motor.run_to_rel_pos(position_sp=700, speed_sp = 360)
self.on_for_degrees(24,700)
sleep(3)
self.isOpen = True

def close(self):
if self.isOpen:
self.motor.run_to_rel_pos(position_sp=-700, speed_sp = 360)
#self.motor.run_to_rel_pos(position_sp=-700, speed_sp = 360)
self.on_for_degrees(-24,700)
sleep(3)
self.isOpen = False

def stop(self):
self.reset()

#if __name__ == "__main__":
2 changes: 1 addition & 1 deletion Chess Robot/source/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self,port):

#We need to use the ipv4 address that shows up in ipconfig in the computer for the USB
#Ethernet adapter handling the connection to the EV3
host = "169.254.137.85"
host = "169.254.82.210"
print("setting up client, address =", host, "port =", port)
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((host, port))
Expand Down
125 changes: 125 additions & 0 deletions Chess Robot/source/coordinates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
from webcam import Webcam
import cv2
from datetime import datetime
from time import sleep
import numpy as np

from queue import Queue
from server import *

#Thread-safe queue to get data from threads
queue = Queue()
server = Server(9999)

webcam = Webcam()
webcam.start()

#image = cv2.imread("board.jpg")
#in milimeters
side_length = 307


def mouse_drawing(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
print("Left click")
corners.append((x, y))

def add_target(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
print("Left click")
current_points.append((x, y))

#As explained in http://paulbourke.net/geometry/pointlineplane/
def closest_point(line_start, line_end, point):
#First element is delta x, second one is delta y
delta_vector = np.subtract(line_end,line_start)
u = ((point[0]-line_start[0])*(line_end[0]-line_start[0])+(point[1]-line_start[1])*(line_end[1]-line_start[1]))/np.dot(delta_vector, delta_vector)
x = line_start[0] + u*(line_end[0]-line_start[0])
y = line_start[1] + u*(line_end[1]-line_start[1])
return (x,y)


def calculate_coordinates(x,y):
print("Distance to left corner")
closest_left = closest_point(corners[0],corners[1], (x,y))
closest_bottom = closest_point(corners[0],corners[3], (x,y))
closest_right = closest_point(corners[3],corners[2], (x,y))
closest_top = closest_point(corners[1],corners[2], (x,y))
print(str(closest_point(corners[0],corners[1], (x,y))))
current_point = (x,y)
#Get Horizontal Length by Pythagoras Theorem
horizontal_length = ((closest_right[0] - closest_left[0])**2+(closest_right[1] - closest_left[1])**2)**(0.5)
#Get Vertical Length by Pythagoras Theorem
vertical_length = ((closest_top[0] - closest_bottom[0])**2+(closest_top[1] - closest_bottom[1])**2)**(0.5)

#Horizontal length until the closest point (from left to right)
horizontal_distance_to_point = ((current_point[0] - closest_left[0])**2+(current_point[1] - closest_left[1])**2)**(0.5)
#Vertical length until the closest point (from bottom to top)
vertical_distance_to_point = ((current_point[0] - closest_bottom[0])**2+(current_point[1] - closest_bottom[1])**2)**(0.5)

#In pixels
print("Current Point: " + str(current_point))
#Now we get the percentage eg (current position x)/total length of the x line )both in pixels. This will give us a percentage
print("Proportion in X: " + str(horizontal_distance_to_point/horizontal_length) + " Proportion in Y: " + str(vertical_distance_to_point/vertical_length))
#Then multiply that percentage by 307 to the the milimiter length
proportionX = horizontal_distance_to_point/horizontal_length
propotionY = vertical_distance_to_point/vertical_length
return (side_length*proportionX, side_length*propotionY)

#[lower_left, upper_left, upper_right, lower_right]
corners = []


cv2.namedWindow("Frame")
cv2.setMouseCallback("Frame", mouse_drawing)

while len(corners) < 4:

# get image from webcam
image = webcam.get_current_frame()
for center_position in corners:
cv2.circle(image, center_position, 5, (0, 0, 255), -1)
# display image
cv2.imshow('Frame', image)

key = cv2.waitKey(1)

cv2.setMouseCallback("Frame", add_target)

current_points = []

while True:
# get image from webcam
image = webcam.get_current_frame()
for center_position in corners:
cv2.circle(image, center_position, 5, (0, 0, 255), -1)
# display image
for current_point in current_points:
cv2.circle(image, current_point, 5, (0, 0, 0), -1)

cv2.imshow('Frame', image)

if (len(current_points) == 2):
#First, take the initial and position end effector over it
#Make sure to account for the 100 mm in offset for the y axis and the 19mm for x and y
(x1,y1) = calculate_coordinates(current_points[0][0], current_points[0][1])
server.sendDistances(-x1 + 19, -(100 + y1) + 19, queue)
server.sendLowerClaw()
server.sendCloseClaw()
server.sendRaiseClaw()
#After piece is grabbed, we move it to the desired position specified in the second point
(x2,y2) = calculate_coordinates(current_points[1][0], current_points[1][1])
delta_x = x2 - x1
delta_y = y2 - y1
server.sendDistances(-delta_x, -delta_y, queue)
server.sendLowerClaw()
server.sendOpenClaw()
server.sendRaiseClaw()
server.sendHome()
current_points.clear()
key = cv2.waitKey(1)
if 'q' == chr(key & 255):
break

server.sendTermination()
cv2.destroyAllWindows()
Empty file removed Chess Robot/source/distort.py
Empty file.
107 changes: 15 additions & 92 deletions Chess Robot/source/move.py
Original file line number Diff line number Diff line change
@@ -1,97 +1,20 @@
from queue import Queue
from server import *

from ev3dev.ev3 import *
import socket
from threading import Event, Thread
from time import sleep
from claw import *
#Thread-safe queue to get data from threads
queue = Queue()
server = Server(9999)

#yMotor = LargeMotor(OUTPUT_A)
#xMotor = LargeMotor(OUTPUT_C)
#zMotor = LargeMotor(OUTPUT_D)
#gripperMotor = MediumMotor(OUTPUT_B)
server.sendDistances(-76,-138, queue)
server.sendLowerClaw()
server.sendCloseClaw()
server.sendRaiseClaw()
server.sendDistances(0,-38,queue)
server.sendLowerClaw()
server.sendOpenClaw()
server.sendRaiseClaw()


#Y
#yMotor.run_to_rel_pos(position_sp=720, speed_sp = 230)
#sleep(3)
#yMotor.stop()
#X
#xMotor.run_to_rel_pos(position_sp=960, speed_sp = 230)
#sleep(3)
#xMotor.stop()
#Gripper
#gripperMotor.run_to_rel_pos(position_sp=500, speed_sp = 360)
#sleep(3)
#gripperMotor.stop()
# robot = Robot()

#Gripper
#gripperMotor.run_to_rel_pos(position_sp=-500, speed_sp = 360)
#sleep(3)
#gripperMotor.stop()

#Z
#Upwards
#zMotor.run_to_rel_pos(position_sp=-120, speed_sp = 100, stop_action="hold")
##sleep(3)
zMotor.stop()


#Downwards




#gripperMotor.run_to_rel_pos(position_sp=-2000, speed_sp = 360)
#sleep(6)
#gripperMotor.stop()


#zMotor.run_to_rel_pos(position_sp=-120, speed_sp = 100, stop_action="hold")
#sleep(3)
#zMotor.stop()

#xMotor.run_to_rel_pos(position_sp=-960, speed_sp = 230)
#sleep(3)
#xMotor.stop()

#yMotor.run_to_rel_pos(position_sp=-420, speed_sp = 230)
#sleep(3)
#yMotor.stop()

#zMotor.run_to_rel_pos(position_sp=120, speed_sp = 100, stop_action="coast")
#sleep(3)
#zMotor.stop()


#gripperMotor.run_to_rel_pos(position_sp=700, speed_sp = 360)
#sleep(4)
#gripperMotor.stop()

zMotor.run_to_rel_pos(position_sp=-120, speed_sp = 100, stop_action="hold")
sleep(3)
zMotor.stop()


claw = Claw(OUTPUT_B)

zMotor.run_to_rel_pos(position_sp=120, speed_sp = 100, stop_action="coast")
sleep(3)
zMotor.stop()

claw.close()


zMotor.run_to_rel_pos(position_sp=-120, speed_sp = 100, stop_action="hold")
sleep(3)
zMotor.stop()

yMotor.run_to_rel_pos(position_sp=-420, speed_sp = 230)
sleep(3)
yMotor.stop()

zMotor.run_to_rel_pos(position_sp=120, speed_sp = 100, stop_action="coast")
sleep(3)
zMotor.stop()

claw.open()
#zMotor.reset()
# robot.xMotor.run_to_rel_pos(position_sp=-360,speed_sp=230)
13 changes: 13 additions & 0 deletions Chess Robot/source/parallelogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Rectangle:
#Rectangle array is assumed to be in OpenCV standard representation of a rectange
# that is, an array of 4 elements: [x, y, width, height] where each quantity is given in pixels
# and x,y correspond to the coordinate of the top left corner of the rectangle.
def __init__(self,rectangle_array):
self.array_representation = rectangle_array
self.width = int(rectangle_array[2])
self.height = int(rectangle_array[3])
self.top_left = (int(rectangle_array[0]), int(rectangle_array[1]))
self.top_right = (int(rectangle_array[0]) + self.width , int(rectangle_array[1]))
self.bottom_left = (int(rectangle_array[0]) , int(rectangle_array[1]) + self.height)
self.bottom_right = (int(rectangle_array[0]) + self.width , int(rectangle_array[1]) + self.height)
self.centre = (self.top_left[0] + int(self.width/2), self.top_left[1] + int(self.height/2))
Loading