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

Assignment2_20110029 #40

Open
wants to merge 4 commits into
base: Ashutosh
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
Binary file added 20110029_ME639_Mini_Project.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from render.render import *
Binary file added circle2.xlsx
Binary file not shown.
72 changes: 72 additions & 0 deletions render.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@


import cv2
import numpy
import matplotlib.pyplot as plt
import os
def scaleAndShow(im, name = 'window', height = None, waitKey = 1):
def callback(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
print(x, y, im[y, x])

cv2.namedWindow(name)
cv2.setMouseCallback(name,callback)
if height is not None:
width = int(im.shape[1]*height/im.shape[0])
im = cv2.resize(im, (width, height), interpolation= cv2.INTER_NEAREST)
cv2.imshow(name, im)
if cv2.waitKey(waitKey) == ord('q'):
exit()


class Renderer():


def __init__(self, height = 600, width = 600, recordLocation = None ):
shape = (height, width, 3)
self.height = height
self.width = width
self.writer = None
if recordLocation is not None:
self.writer = cv2.VideoWriter(recordLocation, cv2.VideoWriter_fourcc(*'XVID'), 25, (width, height))


self.origImage = numpy.ones(shape, dtype=numpy.uint8) * 255


def putText(self, image, info = {}):
for i, (k, v) in enumerate(info.items()):
cv2.putText(image, k + ' : ' + str(v), (10, 20 + i*20), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1, cv2.LINE_AA)
return image

def draw(self, image):
raise NotImplementedError

def getInfo(self):
raise NotImplementedError

def render(self, height = 600, pause = 10):
image = self.origImage.copy()
image = self.draw(image)

image = self.putText(image, self.getInfo())

if self.writer is not None:
self.writer.write(image)
scaleAndShow(image, height= height, waitKey = pause)
return image

if __name__ == "__main__":


def render(image):
cv2.line(image, (0, 0), (100, 100), (0, 128, 0), 1)
return image

r = Renderer(600, 600, render)

for i in range(100):
r.render(info = {'hello' : '00'})



108 changes: 108 additions & 0 deletions task1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import math
import numpy as np
import matplotlib.pyplot as plt
import cv2
import scipy
from render import Renderer
import pandas as pd
import xlsxwriter

# The co-ordinate system provided in the render system is slightly different.
# The window size is approx 600*600
# The bootom co-ordiante is considered as (50,500)



user=input('Enter the excel file name: ') # Taking the excel file as a input which has x,y co-ordinates of end effector as a columns.
pf=pd.read_excel(user)
data=pf.to_numpy() # Converting the excel file in list of arrays.



class object(Renderer):
def __init__(self, traject, recordLocation=None):
super().__init__(recordLocation=recordLocation) # Accesing the parent class Renderer
# Initializing all the required variables.
self.its=0
self.l1=225 # Length of link 1
self.l2=226 # Length of link 2
self.theta=0
self.q1=0 # Angle made by link 1 with horizontal

self.q2=0 # Angle made by link 2 with horizontal
self.traject=traject
self.points=[]





def getInfo(self):
# Printing the required variables during simulation.
info = {
'q1' : round(self.q1, 4),
'q2' : round(self.q2, 4),
'x' : round(self.x, 4),
'y' : round(self.y, 4),
}
return info

def plot(self,data):
# Plotting the desired end-effector points.
plt.scatter(data[:,0],data[:,1])

plt.show()






def step(self):
# Incrementing the variables according to provided excel file.

self.x = self.traject[self.its,0]
self.y = self.traject[self.its,1]
self.theta=math.acos(((self.x-50)**2+(self.y-500)**2-(self.l1)**2-(self.l2)**2)/(2*self.l1*self.l2))

self.q1=math.atan((self.y-500)/(self.x-50))-math.atan((self.l2*math.sin(self.theta))/(self.l1+(self.l2*math.cos(self.theta))))
self.q2=(self.q1+self.theta)

self.a=50 # Starting point of the link 1, x-coordinate
self.b=500 # Starting point of the link 1, y-coordinate
self.c=(self.a)+(self.l1*math.cos(self.q1)) # Starting point of the link 2, x-coordinate
self.d=(self.b)+(self.l1*math.sin(self.q1)) # Starting point of the link 2, y-coordinate
self.x1=(self.c)+(self.l2*math.cos(self.q2)) # ending point of the link 2, x-coordinate
self.y1=(self.d)+(self.l2*math.sin(self.q2)) # ending point of the link 2, y-coordinate


self.its += 1

self.points.append((self.c,self.d,self.x1,self.y1))


def draw(self,image):
# Showing the animation.
for c,d,x1,y1 in self.points:


cv2.line(image,(50,500),(int(self.c),int(self.d)),(0,255,0),1)
cv2.line(image,(int(self.c),int(self.d)),(int(self.x1),int(self.y1)),(0,0,255),1)
cv2.circle(image,(int(x1),int(y1)),3,(255,0,),-1)




return image

speed=int(input('Enter the speed of end-effector: '))
anim=object(data, recordLocation='anim.mp4')

for i in range(len(data[:,1])):
anim.step()
if int(100/speed)==0:
anim.render(height=600,pause=1)
else:
anim.render(height=600,pause=int(100/speed))
anim.plot(data)

112 changes: 112 additions & 0 deletions task2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import math
import numpy as np
import matplotlib.pyplot as plt
import cv2
import scipy
from render import Renderer
import pandas as pd
import xlsxwriter


class object(Renderer):
# Initializing the variables.
def __init__(self, recordLocation=None):
super().__init__(recordLocation=recordLocation)
self.its=0
self.l1=225 # Length of link 1
self.l2=226 # Length of link 2
self.theta=0
self.q1=0 # Angle made by link 1 with horizontal

self.q2=0 # Angle made by link 2 with horizontal
self.fx=5 # Force applied on the wall in x-direction
self.fy=5 # Force applied on the wall in y-direction
self.tau1=0 # Iniliazing the torques
self.tau2=0



self.points=set()

def getInfo(self):
# Printing the required variables during simulation.
info = {
'q1' : round(self.q1, 4),
'q2' : round(self.q2, 4),
'x' : round(self.x, 4),
'y' : round(self.y, 4),
'tau1': round(self.tau1, 4),
'tau2' : round(self.tau2, 4),
'fx' : round(self.fx, 4),
'fy' : round(self.fy, 4)
}
return info

def plot(self):
plt.plot(1,1)
plt.show()
# Just using the plot function because due to some internal issue animation window was turning off after sucessful
# exhibition of the code.
# Although this plot is of no use.


def step(self,u,v):
# Incrementing the variables according to provided excel file.
self.j=0
while self.j<len(u):
self.x = u[self.j]
self.y= v[self.j]
self.theta=math.acos(((self.x-50)**2+(self.y-500)**2-(self.l1)**2-(self.l2)**2)/(2*self.l1*self.l2))

self.q1=math.atan((self.y-500)/(self.x-50))-math.atan((self.l2*math.sin(self.theta))/(self.l1+(self.l2*math.cos(self.theta))))
self.q2=(self.q1+self.theta)


self.a=50 # Starting point of the link 1, x-coordinate
self.b=500 # Starting point of the link 1, y-coordinate
self.c=(self.a)+(self.l1*math.cos(self.q1)) # Starting point of the link 2, x-coordinate
self.d=(self.b)+(self.l1*math.sin(self.q1)) # Starting point of the link 2, y-coordinate
self.x1=(self.c)+(self.l2*math.cos(self.q2)) # ending point of the link 2, x-coordinate
self.y1=(self.d)+(self.l2*math.sin(self.q2)) # ending point of the link 2, y-coordinate
self.tau1=((self.l1)*(math.cos(self.q1))*(self.fy))/1000-((self.l1)*(math.sin(self.q1))*(self.fx))/1000
self.tau2=((self.l2)*(math.cos(self.q2))*(self.fy))/1000-((self.l2)*(math.sin(self.q2))*(self.fx))/1000


#self.its += 1

self.points.add((self.c,self.d,int(self.x),int(self.y),int(self.x1),(self.y1),(self.tau1),(self.tau2)))

self.j=self.j+1
anim.render(height=600,pause=100)


def draw(self,image):
# Showing the animation.
cv2.line(image,(50,500),(int(self.c),int(self.d)),(0,255,0),1)
cv2.line(image,(int(self.c),int(self.d)),(int(self.x1),int(self.y1)),(0,0,255),1)
cv2.circle(image,(int(self.x1),int(self.y1)),3,(255,0,),-1)




return image


anim=object(recordLocation='anim.mp4')
# Reaching the location of wall with initial co-ordinates as (200,200)
u=[]
v=[]
n=100
wall=list(map(int,input('Enter the x and y co-ordinate of wall where you want to apply force: ').split(' ')))
dtx=(wall[0]-200)/n
dty=(wall[1]-200)/n
for i in range(n+1):
u.append(200+(i*dtx))
v.append(200+(i*dty))

anim.step(u,v)
anim.plot()




Loading