-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Code.py
More file actions
254 lines (191 loc) · 7.37 KB
/
Python Code.py
File metadata and controls
254 lines (191 loc) · 7.37 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 22 15:59:54 2022
@author: Ali
"""
import time
import serial
import numpy as np
from vpython import *
import os
import random
import math
interval=10 #print Q every 10 seconds
arduino_data = serial.Serial('com6', 115200)
time.sleep(1)
def printit():
threading.Timer(interval, printit).start()
print ("Q(r,p,y) = ")
print(Q_rpy(roll , pitch , yaw))
print("***********")
print("Q(w,x,y,z) = ")
print(Q_quaternion(w,x,y,z))
print("**********************************************************")
toRad = np.pi/180.0
toDeg = 1/toRad
# Simulate pong game using VPython here
roomX=12
roomY=10
roomZ=16
wallT=.5
wallColor=vector(1,1,1)
wallOpacity=.8
frontOpacity=.1
marbleR=.5
ballColor=vector(0,0,1)
myFloor=box(size=vector(roomX,wallT,roomZ),pos=vector(0,-roomY/2,0),color=wallColor,opacity=wallOpacity)
myCeiling=box(size=vector(roomX,wallT,roomZ),pos=vector(0,roomY/2,0),color=wallColor,opacity=wallOpacity)
leftWall=box(size=vector(wallT,roomY,roomZ),pos=vector(-roomX/2,0,0),color=wallColor,opacity=wallOpacity)
rightWall=box(size=vector(wallT,roomY,roomZ),pos=vector(roomX/2,0,0),color=wallColor,opacity=wallOpacity)
backWall=box(size=vector(roomX,roomY,wallT),pos=vector(0,0,-roomZ/2),color=wallColor,opacity=wallOpacity)
frontWall=box(size=vector(roomX,roomY,wallT),pos=vector(0,0,roomZ/2),color=wallColor,opacity=frontOpacity)
marble=sphere(color=ballColor,radius=marbleR)
paddleX=2
paddleY=2
paddleZ=.2
paddleOpacity=.8
paddleColor=vector(0,.8,.6)
paddleHit=vector(1,.8,.6)
paddle=box(size=vector(paddleX,paddleY,paddleZ),pos=vector(0,0,roomZ/2),color=paddleColor,opacity=paddleOpacity)
marbleX=0
deltaX=.03
marbleY=0
deltaY=.03
marbleZ=0
deltaZ=.03
scale=120.0
# Def your function here
def Q_rpy(r , p , y): ## r,p,y should be in radian Q_xyz
qx=np.matrix([(1,0,0),(0,np.cos(r),-np.sin(r)),(0,np.sin(r),np.cos(r))] , dtype=np.float32)
qy=np.matrix([(np.cos(p),0,np.sin(p)),(0,1,0),(-np.sin(p),0,np.cos(p))] , dtype=np.float32)
qz=np.matrix([(np.cos(y),-np.sin(y),0),(np.sin(y),np.cos(y),0),(0,0,1)] , dtype=np.float32)
q=qx*qy*qz
return q
def Q_quaternion(q0,q1,q2,q3):
# First row of the rotation matrix
r00 = 2 * (q0 * q0 + q1 * q1) - 1
r01 = 2 * (q1 * q2 - q0 * q3)
r02 = 2 * (q1 * q3 + q0 * q2)
# Second row of the rotation matrix
r10 = 2 * (q1 * q2 + q0 * q3)
r11 = 2 * (q0 * q0 + q2 * q2) - 1
r12 = 2 * (q2 * q3 - q0 * q1)
# Third row of the rotation matrix
r20 = 2 * (q1 * q3 - q0 * q2)
r21 = 2 * (q2 * q3 + q0 * q1)
r22 = 2 * (q0 * q0 + q3 * q3) - 1
# 3x3 rotation matrix
q = np.array([[r00, r01, r02],
[r10, r11, r12],
[r20, r21, r22]],dtype=np.float32)
return q
## for convenience I keep the angles between +60 and -60 . anything more or less will be +60 or -60
# also I convert the floating point format into integer.
def saturation(a):
if a>=(scale/2):
a=scale/2
elif a<=-(scale/2):
a=-(scale/2)
else:
a=a
return int(a)
L = label()
## delete text from serial print or remove them
start=int(time.time())
while True:
while arduino_data.inWaiting() == 0:
pass
data_packet = arduino_data.readline()
try:
paddle.color=paddleColor
L.visible=False
data_packet = str(data_packet, 'utf-8')
split_packet = data_packet.split(",")
# quaternion
w= float(split_packet[0])
x= float(split_packet[1])
y= float(split_packet[2])
z= float(split_packet[3])
# kalman quaternion
KalmanW= float(split_packet[4])
KalmanX= float(split_packet[5])
KalmanY= float(split_packet[6])
KalmanZ= float(split_packet[7])
# roll , pitch , yaw
roll= float(split_packet[8])
pitch= float(split_packet[9])
yaw= float(split_packet[10])
#kalman roll , pitch , yaw
kalmanRoll= float(split_packet[11])
kalmanPitch= float(split_packet[12])
kalmanYaw= float(split_packet[13])
## comp.1 roll , pitch , yaw
comp1_roll= float(split_packet[14])
comp1_pitch= float(split_packet[15])
comp1_yaw= float(split_packet[16])
q_quat=Q_quaternion(KalmanW,KalmanX,KalmanY,KalmanZ)
q_rpy=Q_rpy(kalmanRoll*toRad , kalmanPitch*toRad , kalmanYaw*toRad)
if( int(time.time())-start % interval ==0):
print(' ')
print("Q(r,p,y) = ")
print(q_rpy)
print(' ')
print("Q(w,x,y,z) = ")
print(q_quat)
print(' ')
print("********************")
# Change the attributes of your object to syncronize it with real time motions
xx=saturation(kalmanRoll)+(scale/2)
yy=saturation(kalmanPitch)+(scale/2)
padX=(roomX/scale)*xx -roomX/2
padY=(roomY/scale)*yy+roomY/2
marbleX=marbleX+deltaX
marbleY=marbleY+deltaY
marbleZ=marbleZ+deltaZ
if marbleX+marbleR>(roomX/2-wallT/2) or marbleX-marbleR<(-roomX/2+wallT/2):
deltaX=deltaX*(-1)
marbleX=marbleX+deltaX
if marbleY+marbleR>(roomY/2-wallT/2) or marbleY-marbleR<(-roomY/2+wallT/2):
deltaY=deltaY*(-1)
marbleY=marbleY+deltaY
if marbleZ-marbleR<(-roomZ/2+wallT/2):
deltaZ=deltaZ*(-1)
marbleZ=marbleZ+deltaZ
if marbleZ+marbleR>(roomZ/2) :
if marbleX>padX-paddleX/2 and marbleX<padX+paddleX/2 and marbleY>padY-paddleY/2 and marbleY<padY+paddleY/2 :
deltaZ=deltaZ*(-1)
marbleZ=marbleZ+deltaZ
paddle.color=paddleHit
time.sleep(0.2)
else:
L.text= "GAME OVER! TRY AGAIN :)"
L.visible=True
time.sleep(1)
L.text= "3"
L.visible=True
time.sleep(.5)
L.text= "2"
L.visible=True
time.sleep(.5)
L.text= "1"
L.visible=True
time.sleep(.5)
L.text= "NOW!"
L.visible=True
time.sleep(.5)
L.visible=False
# back to initial location and choosing a random direction
n=[-1,1]
marbleX=0
deltaX=(random.choice(n))*0.03
marbleY=0
deltaY=(random.choice(n))*0.03
marbleZ=0
deltaZ=.03
padX=paddleX
padY=paddleY
padZ=paddleZ
marble.pos=vector(marbleX,marbleY,marbleZ)
paddle.pos=vector(padX,padY,roomZ/2)
except:
pass