-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstApproach.py
190 lines (153 loc) · 5.61 KB
/
FirstApproach.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
from syntacts import *
from time import sleep, time
from math import sin
from math import pi
import Adafruit_ADS1x15
import csv
import tensorflow as tf
from decimal import *
getcontext().prec = 2
adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)
GAIN = 1
offset = [0]*4
offset[0] = adc.read_adc(0, gain=GAIN)
offset[1] = adc.read_adc(1, gain=GAIN)
offset[2] = adc.read_adc(2, gain=GAIN)
offset[3] = adc.read_adc(3, gain=GAIN)
import numpy as np
s = Session()
s.open()
# measure wall time
t0 = time()
flagT=True
# with myFile:
# writer = csv.writer(myFile, dialect='myDialect')
# writer.writerows(myData)
i=0
#NmeanNew=np.array([408.942958,138.473131,394.008856,308.599539,0.548101,-0.518616])
#NstadNew=np.array([106.510489,204.268260,128.503499,149.708490,0.429971,0.476176])
NmeanRest = np.array([326.27320,12.01916,310.34624,222.99996])
NmeanNew = np.array([82.669758,126.453971,83.662616,85.599579,311.351552,-0.157280,0.038515])
#NmeanNew = np.array([408.942958,138.473131,394.008856,308.599539,0.548101,-0.518616])
NstadNew = np.array([106.510489,204.268260,128.503499,149.708490,179.464143,45.116143,9.908787])
#NstadNew = np.array([106.510489,204.268260,128.503499,149.708490,0.429971,0.476176])
print("please don't touch the board!..")
loaded_model = tf.keras.models.load_model('xyForcePlateN5/')
totalTime = 10
brazil = Sine(446) * Envelope(totalTime,1)
class Country:
def __init__(self, name,x,y,radius,fsom,tsom):
self.name = name
self.Pos = np.array([x,y])
self.radius = radius
self.som=Sine(fsom) * Envelope(tsom,1)
class Continent:
# creating list
def __init__(self):
self.t0=time()-totalTime
self.timeSom=totalTime
self.list = []
self.objAntname="Init"
# appending instances to list
self.list.append( Country('Brasil', 10.5,7.5,2.8,446,self.timeSom) )
self.list.append( Country('Colombia', 3.0,3.0,2.0,84,self.timeSom) )
self.list.append( Country('Peru', 2.25,6.75,1.7,67,self.timeSom) )
self.list.append( Country('Bolivia', 6.0,9.0,1.5,13,self.timeSom) )
self.list.append( Country('paraguai', 7.5,11.25,1.5,8,self.timeSom) )
self.list.append( Country('Argentina', 6.0,15,2.2,74,self.timeSom) )
self.list.append( Country('Argentina', 6.0,12.0,1.5,74,self.timeSom) )
self.list.append( Country('guiana', 7.5,2.25,1.0,74,self.timeSom) )
def CollisionDetect(self,xp,yp):
for obj in self.list:
dist=obj.Pos-np.array([xp,yp])
if(np.dot(dist,dist)<=(obj.radius*obj.radius)):
print( obj.name, sep =' ' )
#s.play_all(obj.som)
if(self.objAntname!=obj.name):
#print("diff")
self.objAntname=obj.name
s.stop(0)
s.stop(1)
s.stop(2)
s.stop(3)
s.stop(4)
s.stop(5)
s.stop(6)
s.stop(7)
self.t0 = time()- self.timeSom
if ((time() - self.t0) >= obj.som.length):
s.play(0, obj.som)
s.play(1, obj.som)
s.play(2, obj.som)
s.play(3, obj.som)
s.play(4, obj.som)
s.play(5, obj.som)
s.play(6, obj.som)
s.play(7, obj.som)
#print('Signal 2')
self.t0=time()
#sleep(obj.som.length)
def normNew(x):
return ((x - NmeanNew) / NstadNew)
def ratioSxSy(x):
try:
Sxnum=(-1/x[0])+(1/x[1])+(1/x[2])-(1/x[3])
Synum=(-1/x[0])-(1/x[1])+(1/x[2])+(1/x[3])
Sden=(1/x[0])+(1/x[1])+(1/x[2])+(1/x[3])
Sx = Sxnum/Sden
Sy = Synum/Sden
return np.array([Sx,Sy])
except:
return np.array([-1,-1])
def resultCon(x):
SxSy = ratioSxSy(x)
val=np.concatenate((x,SxSy))
return normNew(val)
def lengthVal(x):
return np.sqrt(np.sum(np.power(x,2)))
Nmean=10
values = [0]*4
mean10=np.empty((Nmean,4))
for j in range(Nmean):
for i in range(4):
#Read the specified ADC channel using the previously set gain value.
values[i] = adc.read_adc(i, gain=GAIN)
mean10[j,:]=values
valInit=np.mean(mean10, axis=0)
continent = Continent()
valMax = 450.0
print("Start")
while True:
#mean5=np.empty((Nmean,4))
#for j in range(Nmean):
for i in range(4):
#Read the specified ADC channel using the previously set gain value.
values[i] = adc.read_adc(i, gain=GAIN)
#mean5[j,:]=values
#valCur=np.mean(mean5, axis=0)
ActVal= values-valInit
LenVal=lengthVal(ActVal)
if (LenVal>60.0 and LenVal<valMax):
res=np.append(ActVal,LenVal)
#print("{}".format(res))
resultC=resultCon(np.array(res))
Pos=loaded_model.predict(resultC.reshape((1,7)))
continent.CollisionDetect(float(Pos[0]),float(Pos[1]))
print("x: {}, y: {}, f:{}".format(float(Pos[0]),float(Pos[1]), LenVal))
# print("touch")
# elif(LenVal>valMax):
# print("forca excessiva")
elif(LenVal<40.0):
s.stop(0)
s.stop(1)
s.stop(2)
s.stop(3)
s.stop(4)
s.stop(5)
s.stop(6)
s.stop(7)
continent.t0=time()-continent.timeSom
#Unpos=unorm(Pos)
#print(Pos)
# print(Unpos.shape)
#print("x: {}, y: {}".format(float(Pos[0]),float(Pos[1])))