-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (156 loc) · 6.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# ==========================================================
# File : main.py
# Author: Arkadiusz Wadowski
# Email: wadowski.arkadiusz@gmail.com
# Created: 17.05.2017
# ==========================================================
# L1 = 1.40; L2 = 0.85; M1 = 1.40; M2 = 3.20
# ==========================================================
import sys
from math import sqrt, sin, asin, cos, acos, degrees
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QMessageBox
from ui_code import Ui_MainWindow
import numpy as np
from PyQt4.QtCore import QThread
import time
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.L1 = 0
self.L2 = 1.4
self.L3 = 0.85
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.f_x.setValue(1.2)
self.ui.f_y.setValue(1.5)
self.update_forward_kin_plot()
self.ui.f_th1_1.setReadOnly(True)
self.ui.f_th1_2.setReadOnly(True)
self.ui.f_th2_1.setReadOnly(True)
self.ui.f_th2_2.setReadOnly(True)
self.ui.f_plot.axes.autoscale(False, 'both', False)
self.ui.f_plot.axes.set_xlim(-0.2, 2.5)
self.ui.f_plot.axes.set_ylim(-0.4, 2.5)
self.ui.f_plot.axes.set_aspect('equal', 'datalim')
self.ui.i_plot.axes.autoscale(False, 'both', False)
self.ui.i_plot.axes.set_xlim(-0.2, 2.5)
self.ui.i_plot.axes.set_ylim(-0.4, 2.5)
self.ui.i_plot.axes.set_aspect('equal', 'datalim')
self.ui.th11.axes.set_xlim(1, 3.5)
self.ui.th11.axes.set_ylim(-0.4, 5.5)
self.threadclass = MovePlotThread()
# send reference to ui for thread
self.threadclass.provide_ui(self.ui)
QtCore.QObject.connect(self.ui.f_x, QtCore.SIGNAL("clicked()"), self.update_forward_kin_plot)
self.connect(self.threadclass, QtCore.SIGNAL("plot"), self.plot_movement)
self.ui.f_x.valueChanged.connect(self.update_forward_kin_plot)
self.ui.f_y.valueChanged.connect(self.update_forward_kin_plot)
self.threadclass.start()
def plot_movement(self):
lista = np.arange(1, 1.51, 0.01)
ver = np.sin(50 * lista) / 10 + 1.5
self.ui.i_plot.draw()
tm = []
th11 = []
th21 = []
th12 = []
th22 = []
for i in xrange(0,lista.size):
theta11, theta21, theta12, theta22 = self.calculate_angles(lista[i],ver[i])
th11.append((theta11*180)/3.1415)
th21.append((theta21*180)/3.1415)
th12.append((theta12*180)/3.1415)
th22.append((theta22*180)/3.1415)
tm.append(lista[i])
# plot angles
self.ui.th11.axes.plot(tm, th11, 'blue')
self.ui.th11.draw()
self.ui.th21.axes.plot(tm, th21, 'blue')
self.ui.th21.draw()
self.ui.th12.axes.plot(tm, th12, 'red')
self.ui.th12.draw()
self.ui.th22.axes.plot(tm, th22, 'red')
self.ui.th22.draw()
coord1 = self.get_coordinates(theta11, theta21)
coord2 = self.get_coordinates(theta12, theta22)
self.ui.i_plot.axes.cla()
self.ui.i_plot.axes.plot(lista, ver, 'black')
self.ui.i_plot.axes.hold(True)
self.plot_links(coord1, coord2, 1)
def update_forward_kin_plot(self):
x = self.ui.f_x.value()
y = self.ui.f_y.value()
print self.ui.Theta1_1.currentIndex()
theta21, theta31, theta22, theta32 = self.calculate_angles(x, y)
self.ui.f_th1_1.setText( str( degrees(theta21) ))
self.ui.f_th2_1.setText( str( degrees(theta31) ) )
self.ui.f_th1_2.setText( str( degrees(theta22) ) )
self.ui.f_th2_2.setText( str( degrees(theta32) ) )
coord1 = self.get_coordinates(theta21, theta31)
coord2 = self.get_coordinates(theta22, theta32)
self.plot_links(coord1, coord2 , 0)
def plot_links(self, coord1, coord2, which):
if 0 == which:
self.ui.f_plot.axes.hold(True)
self.ui.f_plot.axes.set_xlabel("X")
self.ui.f_plot.axes.set_ylabel("Y")
self.ui.f_plot.axes.cla()
self.ui.f_plot.axes.plot(coord1[0], coord1[1], 'b')
self.ui.f_plot.axes.plot(coord2[0], coord2[1], 'r')
self.ui.f_plot.axes.plot(coord1[0], coord1[1], 'b' + 'o')
self.ui.f_plot.axes.plot(coord2[0], coord2[1], 'r' + 'o')
self.ui.f_plot.axes.set_xlim(-0.2, 2.5)
self.ui.f_plot.axes.set_ylim(-0.5, 2.5)
self.ui.f_plot.draw()
elif 1 == which:
if 1 == self.ui.Theta1_1.currentIndex():
self.ui.i_plot.axes.hold(True)
self.ui.i_plot.axes.set_xlabel("X")
self.ui.i_plot.axes.set_ylabel("Y")
self.ui.i_plot.axes.plot(coord1[0], coord1[1], 'b')
self.ui.i_plot.axes.plot(coord2[0], coord2[1], 'r')
self.ui.i_plot.axes.plot(coord1[0], coord1[1], 'b' + 'o')
self.ui.i_plot.axes.plot(coord2[0], coord2[1], 'r' + 'o')
self.ui.i_plot.axes.set_xlim(-0.2, 2.5)
self.ui.i_plot.axes.set_ylim(-0.5, 2.5)
self.ui.i_plot.draw()
def calculate_angles (self, x, y):
A = (x ** 2 + (y - self.L1) ** 2 + self.L2 ** 2 - self.L3 ** 2) / (2 * x * self.L2)
B = (y - self.L1) / x
delta = 4 * A ** 2 - 4 * (1 + B ** 2) * (A ** 2 - B ** 2)
theta21 = acos((2 * A - sqrt(delta)) / (2 + 2 * B ** 2))
theta31 = acos((x - self.L2 * cos(theta21)) / self.L3)
theta22 = acos((2 * A + sqrt(delta)) / (2 + 2 * B ** 2))
theta32 = acos((x - self.L2 * cos(theta22)) / self.L3)
return theta21, theta31, theta22, theta32
def get_coordinates(self, angle1, angle2):
x11 = 0
y11 = 0
x12 = 0
y12 = self.L1
x21 = x12
y21 = y12
x22 = self.L2 * cos(angle1)
y22 = y21 + self.L2 * sin(angle1)
x31 = x22
y31 = y22
x32 = x22 + self.L3 * cos(angle2)
y32 = y22 + self.L3 * sin(angle2)
return [[x11, x12, x21, x22, x31, x32], [y11, y12, y21, y22, y31, y32]]
class MovePlotThread(QThread):
def __init__(self, parent=None):
super(MovePlotThread, self).__init__(parent)
self.ui = None
def provide_ui(self, ui):
self.ui = ui
def run(self):
while True:
if 1 == self.ui.Theta1_1.currentIndex():
self.emit(QtCore.SIGNAL("plot"))
time.sleep(3.5)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = MyWindow()
myapp.show()
sys.exit(app.exec_())