-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·176 lines (134 loc) · 5.64 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
from MomentManager import MomentManager
from SocketServer import SocketServer
from AngleManager import AngleManager
from MomentManager import MomentManager
from SafetyMomentManager import SafetyMomentManager
import numpy as np
import time
class MainServer:
def __init__(self, momentManagerPort, angleManagerPort, userName, safetyRate, sampleRate) -> None:
self.momentManager = MomentManager(momentManagerPort)
self.angleManager = AngleManager(angleManagerPort)
self.smm = SafetyMomentManager(
userName,
sampleRate=sampleRate,
safetyRate=safetyRate,
momentManager=self.momentManager,
angleManager=self.angleManager
)
self.userName = userName
self.SocketServer = None
self.InitSocketServer()
def InitSocketServer(self):
self.socketServer = SocketServer()
self.socketServer.create_socket()
def SendData(self, data):
self.socketServer.send_data(data)
def ReciveData(self):
data = self.socketServer.receive_data()
return data
def Run(self):
status = 0
while True:
data = self.ReciveData()
if data == 'preWalk':
self.smm.angleManager.preStartWalk()
self.SendData('1')
status = 1
elif data == 'walk':
if status == 1:
self.smm.angleManager.realStartWalk()
if self.smm.isSafetyMomentExisting():
self.SendData('2')
data = self.ReciveData()
if data == 'ReadOldData':
self.smm.LoadExistingData()
elif data[:11] == 'ReadNewData':
self.smm.RecordNewData(int(data[11:]))
self.SendData('3')
else:
self.smm.RecordNewData(20)
self.SendData('3')
status = 3
# data = self.ReciveData()
break
# Start Detecting
self.DetectMoment()
def DetectMoment(self):
angelList = []
momentList = None
idxList = []
indx = 0
flag = 0
keyAngel = 0.124
if self.smm.SafetyMoment is None:
self.SendEmergencyStop()
# Prevent the robot stopping before the stimulation
# while True:
# data = self.ReciveData()
# if data == 'Emergency Stop':
# break
self.angleManager.emergencyStopButton()
else:
try:
print('detecting...')
while True:
indx += 1
moment = self.momentManager.GetAllMoments()[0]
angel = self.angleManager.getInfo()
if angel[2] == 1:
checkResult, idx = self.smm.CheckMoment(np.hstack((moment, angel[1])))
idxList.append(idx)
tempdata = np.hstack((moment, self.smm.SafetyMoment[idx, 0:8], indx, time.time()))
if type(momentList) != type(np.array([[1,2,3,4]])):
momentList = tempdata.reshape(1, 14)
else:
momentList = np.vstack((momentList, tempdata))
angelList.append(angel[1])
# TODO: maybe need to change the 0.3
if abs(angel[1] - keyAngel) < 0.05:
flag += 1
print(flag)
if flag == 20:
print('Start Stimulation, indx: ', indx, 'time: ', time.time())
self.SendData('Start Stimulation')
if not checkResult:
self.SendEmergencyStop()
# while True:
# data = self.ReciveData()
# if data == 'Emergency Stop':
# break
self.angleManager.emergencyStopButton()
print('Emergency Stop! indx:', indx, 'time: ', time.time())
break
# return
# Send Data to plot process
# self.smm.Update(tempdata)
else:
print('angel paresed failed!')
except KeyboardInterrupt:
print("Program Terminated!")
finally:
angelList = np.array(angelList)
angelList = np.reshape(angelList, (len(angelList), 1))
print(momentList.shape, angelList.shape)
np.save('./data/detect_' + self.userName + time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) + '.npy', np.hstack((momentList, angelList)))
def SendEmergencyStop(self):
self.SendData('Emergency Stop')
def main():
angleManagerPort = input("Please input angel manager port (like \"com8\"): ")
if angleManagerPort == "":
angleManagerPort = "com3"
momentManagerPort = input("Please input moment manager port (like \"com3\"): ")
if momentManagerPort == "":
momentManagerPort = "com4"
mainServer = MainServer(
momentManagerPort=momentManagerPort,
angleManagerPort=angleManagerPort,
userName="test",
safetyRate=4,
sampleRate=35
)
mainServer.Run()
if __name__ == "__main__":
main()